Log.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Object.h"
  25. /// Debug message level. By default only shown in debug mode.
  26. static const int LOG_DEBUG = 0;
  27. /// Informative message level.
  28. static const int LOG_INFO = 1;
  29. /// Warning message level.
  30. static const int LOG_WARNING = 2;
  31. /// Error message level.
  32. static const int LOG_ERROR = 3;
  33. /// Disable all log messages.
  34. static const int LOG_NONE = 4;
  35. class File;
  36. /// Logging subsystem.
  37. class Log : public Object
  38. {
  39. OBJECT(Log);
  40. public:
  41. /// Construct.
  42. Log(Context* context);
  43. /// Destruct. Close the log file if open.
  44. virtual ~Log();
  45. /// Open the log file.
  46. void Open(const String& fileName);
  47. /// Write to the log. If logging level is higher than the level of the message, the message is ignored.
  48. void Write(int level, const String& message);
  49. /// Write raw output to the log.
  50. void WriteRaw(const String& message);
  51. /// %Set logging level.
  52. void SetLevel(int level);
  53. /// Return logging level.
  54. int GetLevel() const { return level_; }
  55. /// Return last log message.
  56. const String& GetLastMessage() const { return lastMessage_; }
  57. private:
  58. /// Log file.
  59. SharedPtr<File> logFile_;
  60. /// Last log message.
  61. String lastMessage_;
  62. /// Logging level.
  63. int level_;
  64. /// In write flag to prevent recursion.
  65. bool inWrite_;
  66. };
  67. /// Write to the log (static.)
  68. void WriteToLog(Context* context, int level, const String& message);
  69. /// Write raw output to the log (static.)
  70. void WriteToLogRaw(Context* context, const String& message);
  71. #define LOGDEBUG(message) WriteToLog(context_, LOG_DEBUG, message)
  72. #define LOGINFO(message) WriteToLog(context_, LOG_INFO, message)
  73. #define LOGWARNING(message) WriteToLog(context_, LOG_WARNING, message)
  74. #define LOGERROR(message) WriteToLog(context_, LOG_ERROR, message)
  75. #define LOGRAW(message) WriteToLogRaw(context_, message)