Log.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/List.h"
  24. #include "../Core/Mutex.h"
  25. #include "../Core/Object.h"
  26. #include "../Core/StringUtils.h"
  27. namespace Urho3D
  28. {
  29. /// Fictional message level to indicate a stored raw message.
  30. static const int LOG_RAW = -1;
  31. /// Trace message level.
  32. static const int LOG_TRACE = 0;
  33. /// Debug message level. By default only shown in debug mode.
  34. static const int LOG_DEBUG = 1;
  35. /// Informative message level.
  36. static const int LOG_INFO = 2;
  37. /// Warning message level.
  38. static const int LOG_WARNING = 3;
  39. /// Error message level.
  40. static const int LOG_ERROR = 4;
  41. /// Disable all log messages.
  42. static const int LOG_NONE = 5;
  43. class File;
  44. /// Stored log message from another thread.
  45. struct StoredLogMessage
  46. {
  47. /// Construct undefined.
  48. StoredLogMessage() = default;
  49. /// Construct with parameters.
  50. StoredLogMessage(const String& message, int level, bool error) :
  51. message_(message),
  52. level_(level),
  53. error_(error)
  54. {
  55. }
  56. /// Message text.
  57. String message_;
  58. /// Message level. -1 for raw messages.
  59. int level_{};
  60. /// Error flag for raw messages.
  61. bool error_{};
  62. };
  63. /// Logging subsystem.
  64. class URHO3D_API Log : public Object
  65. {
  66. URHO3D_OBJECT(Log, Object);
  67. public:
  68. /// Construct.
  69. explicit Log(Context* context);
  70. /// Destruct. Close the log file if open.
  71. ~Log() override;
  72. /// Open the log file.
  73. void Open(const String& fileName);
  74. /// Close the log file.
  75. void Close();
  76. /// Set logging level.
  77. /// @property
  78. void SetLevel(int level);
  79. /// Set whether to timestamp log messages.
  80. /// @property
  81. void SetTimeStamp(bool enable);
  82. /// Set quiet mode ie. only print error entries to standard error stream (which is normally redirected to console also). Output to log file is not affected by this mode.
  83. /// @property
  84. void SetQuiet(bool quiet);
  85. /// Return logging level.
  86. /// @property
  87. int GetLevel() const { return level_; }
  88. /// Return whether log messages are timestamped.
  89. /// @property
  90. bool GetTimeStamp() const { return timeStamp_; }
  91. /// Return last log message.
  92. /// @property
  93. String GetLastMessage() const { return lastMessage_; }
  94. /// Return whether log is in quiet mode (only errors printed to standard error stream).
  95. /// @property
  96. bool IsQuiet() const { return quiet_; }
  97. /// Write to the log. If logging level is higher than the level of the message, the message is ignored.
  98. /// @nobind
  99. static void Write(int level, const String& message);
  100. /// Write raw output to the log.
  101. static void WriteRaw(const String& message, bool error = false);
  102. private:
  103. /// Handle end of frame. Process the threaded log messages.
  104. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  105. /// Mutex for threaded operation.
  106. Mutex logMutex_;
  107. /// Log messages from other threads.
  108. List<StoredLogMessage> threadMessages_;
  109. /// Log file.
  110. SharedPtr<File> logFile_;
  111. /// Last log message.
  112. String lastMessage_;
  113. /// Logging level.
  114. int level_;
  115. /// Timestamp log messages flag.
  116. bool timeStamp_;
  117. /// In write flag to prevent recursion.
  118. bool inWrite_;
  119. /// Quiet mode flag.
  120. bool quiet_;
  121. };
  122. #ifdef URHO3D_LOGGING
  123. #define URHO3D_LOGTRACE(message) Urho3D::Log::Write(Urho3D::LOG_TRACE, message)
  124. #define URHO3D_LOGDEBUG(message) Urho3D::Log::Write(Urho3D::LOG_DEBUG, message)
  125. #define URHO3D_LOGINFO(message) Urho3D::Log::Write(Urho3D::LOG_INFO, message)
  126. #define URHO3D_LOGWARNING(message) Urho3D::Log::Write(Urho3D::LOG_WARNING, message)
  127. #define URHO3D_LOGERROR(message) Urho3D::Log::Write(Urho3D::LOG_ERROR, message)
  128. #define URHO3D_LOGRAW(message) Urho3D::Log::WriteRaw(message)
  129. #define URHO3D_LOGTRACEF(format, ...) Urho3D::Log::Write(Urho3D::LOG_TRACE, Urho3D::ToString(format, ##__VA_ARGS__))
  130. #define URHO3D_LOGDEBUGF(format, ...) Urho3D::Log::Write(Urho3D::LOG_DEBUG, Urho3D::ToString(format, ##__VA_ARGS__))
  131. #define URHO3D_LOGINFOF(format, ...) Urho3D::Log::Write(Urho3D::LOG_INFO, Urho3D::ToString(format, ##__VA_ARGS__))
  132. #define URHO3D_LOGWARNINGF(format, ...) Urho3D::Log::Write(Urho3D::LOG_WARNING, Urho3D::ToString(format, ##__VA_ARGS__))
  133. #define URHO3D_LOGERRORF(format, ...) Urho3D::Log::Write(Urho3D::LOG_ERROR, Urho3D::ToString(format, ##__VA_ARGS__))
  134. #define URHO3D_LOGRAWF(format, ...) Urho3D::Log::WriteRaw(Urho3D::ToString(format, ##__VA_ARGS__))
  135. #else
  136. #define URHO3D_LOGTRACE(message) ((void)0)
  137. #define URHO3D_LOGDEBUG(message) ((void)0)
  138. #define URHO3D_LOGINFO(message) ((void)0)
  139. #define URHO3D_LOGWARNING(message) ((void)0)
  140. #define URHO3D_LOGERROR(message) ((void)0)
  141. #define URHO3D_LOGRAW(message) ((void)0)
  142. #define URHO3D_LOGTRACEF(...) ((void)0)
  143. #define URHO3D_LOGDEBUGF(...) ((void)0)
  144. #define URHO3D_LOGINFOF(...) ((void)0)
  145. #define URHO3D_LOGWARNINGF(...) ((void)0)
  146. #define URHO3D_LOGERRORF(...) ((void)0)
  147. #define URHO3D_LOGRAWF(...) ((void)0)
  148. #endif
  149. }