Log.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  28. {
  29. /// Fictional message level to indicate a stored raw message.
  30. static const int LOG_RAW = -1;
  31. /// Debug message level. By default only shown in debug mode.
  32. static const int LOG_DEBUG = 0;
  33. /// Informative message level.
  34. static const int LOG_INFO = 1;
  35. /// Warning message level.
  36. static const int LOG_WARNING = 2;
  37. /// Error message level.
  38. static const int LOG_ERROR = 3;
  39. /// Disable all log messages.
  40. static const int LOG_NONE = 4;
  41. class File;
  42. /// Stored log message from another thread.
  43. struct StoredLogMessage
  44. {
  45. /// Construct undefined.
  46. StoredLogMessage()
  47. {
  48. }
  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 ATOMIC_API Log : public Object
  65. {
  66. ATOMIC_OBJECT(Log, Object);
  67. public:
  68. /// Construct.
  69. Log(Context* context);
  70. /// Destruct. Close the log file if open.
  71. virtual ~Log();
  72. /// Open the log file.
  73. void Open(const String& fileName);
  74. /// Close the log file.
  75. void Close();
  76. /// Set logging level.
  77. void SetLevel(int level);
  78. /// Set whether to timestamp log messages.
  79. void SetTimeStamp(bool enable);
  80. /// 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.
  81. void SetQuiet(bool quiet);
  82. /// Return logging level.
  83. int GetLevel() const { return level_; }
  84. /// Return whether log messages are timestamped.
  85. bool GetTimeStamp() const { return timeStamp_; }
  86. /// Return last log message.
  87. String GetLastMessage() const { return lastMessage_; }
  88. /// Return whether log is in quiet mode (only errors printed to standard error stream).
  89. bool IsQuiet() const { return quiet_; }
  90. /// Write to the log. If logging level is higher than the level of the message, the message is ignored.
  91. static void Write(int level, const String& message);
  92. /// Write raw output to the log.
  93. static void WriteRaw(const String& message, bool error = false);
  94. // ATOMIC BEGIN
  95. const File* GetLogFile() const { return logFile_; }
  96. // ATOMIC END
  97. private:
  98. /// Handle end of frame. Process the threaded log messages.
  99. void HandleEndFrame(StringHash eventType, VariantMap& eventData);
  100. /// Mutex for threaded operation.
  101. Mutex logMutex_;
  102. /// Log messages from other threads.
  103. List<StoredLogMessage> threadMessages_;
  104. /// Log file.
  105. SharedPtr<File> logFile_;
  106. /// Last log message.
  107. String lastMessage_;
  108. /// Logging level.
  109. int level_;
  110. /// Timestamp log messages flag.
  111. bool timeStamp_;
  112. /// In write flag to prevent recursion.
  113. bool inWrite_;
  114. /// Quiet mode flag.
  115. bool quiet_;
  116. };
  117. #ifdef ATOMIC_LOGGING
  118. #define ATOMIC_LOGDEBUG(message) Atomic::Log::Write(Atomic::LOG_DEBUG, message)
  119. #define ATOMIC_LOGINFO(message) Atomic::Log::Write(Atomic::LOG_INFO, message)
  120. #define ATOMIC_LOGWARNING(message) Atomic::Log::Write(Atomic::LOG_WARNING, message)
  121. #define ATOMIC_LOGERROR(message) Atomic::Log::Write(Atomic::LOG_ERROR, message)
  122. #define ATOMIC_LOGRAW(message) Atomic::Log::WriteRaw(message)
  123. #define ATOMIC_LOGDEBUGF(format, ...) Atomic::Log::Write(Atomic::LOG_DEBUG, Atomic::ToString(format, ##__VA_ARGS__))
  124. #define ATOMIC_LOGINFOF(format, ...) Atomic::Log::Write(Atomic::LOG_INFO, Atomic::ToString(format, ##__VA_ARGS__))
  125. #define ATOMIC_LOGWARNINGF(format, ...) Atomic::Log::Write(Atomic::LOG_WARNING, Atomic::ToString(format, ##__VA_ARGS__))
  126. #define ATOMIC_LOGERRORF(format, ...) Atomic::Log::Write(Atomic::LOG_ERROR, Atomic::ToString(format, ##__VA_ARGS__))
  127. #define ATOMIC_LOGRAWF(format, ...) Atomic::Log::WriteRaw(Atomic::ToString(format, ##__VA_ARGS__))
  128. #else
  129. #define ATOMIC_LOGDEBUG(message) ((void)0)
  130. #define ATOMIC_LOGINFO(message) ((void)0)
  131. #define ATOMIC_LOGWARNING(message) ((void)0)
  132. #define ATOMIC_LOGERROR(message) ((void)0)
  133. #define ATOMIC_LOGRAW(message) ((void)0)
  134. #define ATOMIC_LOGDEBUGF(...) ((void)0)
  135. #define ATOMIC_LOGINFOF(...) ((void)0)
  136. #define ATOMIC_LOGWARNINGF(...) ((void)0)
  137. #define ATOMIC_LOGERRORF(...) ((void)0)
  138. #define ATOMIC_LOGRAWF(...) ((void)0)
  139. #endif
  140. }