Logging.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_LOGGING_HPP
  26. #define CAT_LOGGING_HPP
  27. #include <cat/threads/RegionAllocator.hpp>
  28. #if defined(CAT_OS_WINDOWS)
  29. #include <cat/port/WindowsInclude.hpp>
  30. #endif
  31. namespace cat {
  32. //// Enumerations
  33. enum EventSeverity
  34. {
  35. LVL_INANE,
  36. LVL_INFO,
  37. LVL_WARN,
  38. LVL_OOPS,
  39. LVL_FATAL,
  40. LVL_SILENT, // invalid for an actual event's level, valid value for a threshold
  41. };
  42. //// Utility
  43. region_string HexDumpString(const void *vdata, u32 bytes);
  44. // Write to console (and debug log in windows) then trigger a breakpoint and exit
  45. void FatalStop(const char *message);
  46. void DefaultLogCallback(EventSeverity severity, const char *source, region_ostringstream &msg);
  47. //// Logging
  48. typedef void (*LogCallback)(EventSeverity severity, const char *source, region_ostringstream &msg);
  49. class Logging : public Singleton<Logging>
  50. {
  51. CAT_SINGLETON(Logging);
  52. LogCallback _callback;
  53. friend class Recorder;
  54. void LogEvent(Recorder *recorder);
  55. public:
  56. int _log_threshold;
  57. public:
  58. void Initialize(EventSeverity min_severity = LVL_INANE);
  59. CAT_INLINE void SetThreshold(EventSeverity min_severity) { _log_threshold = min_severity; }
  60. void ReadSettings();
  61. void Shutdown();
  62. protected:
  63. bool _service;
  64. #if defined(CAT_OS_WINDOWS)
  65. HANDLE _event_source;
  66. #endif
  67. public:
  68. CAT_INLINE bool IsService() { return _service; }
  69. void EnableServiceMode(const char *service_name);
  70. void WriteServiceLog(EventSeverity severity, const char *line);
  71. public:
  72. // Not thread-safe
  73. CAT_INLINE void SetLogCallback(LogCallback cb) { _callback = cb; }
  74. };
  75. //// Recorder
  76. class Recorder
  77. {
  78. friend class Logging;
  79. EventSeverity _severity;
  80. const char *_subsystem;
  81. region_ostringstream _msg;
  82. public:
  83. Recorder(const char *subsystem, EventSeverity severity);
  84. ~Recorder();
  85. public:
  86. template<class T> inline Recorder &operator<<(const T &t)
  87. {
  88. _msg << t;
  89. return *this;
  90. }
  91. };
  92. // Because there is an IF statement in the macro, you cannot use the
  93. // braceless if-else construction:
  94. // if (XYZ) WARN("SS") << "ERROR!"; else INFO("SS") << "OK!"; <-- bad
  95. // Instead use:
  96. // if (XYZ) { WARN("SS") << "ERROR!"; } else INFO("SS") << "OK!"; <-- good
  97. #define RECORD(subsystem, severity) \
  98. if (severity >= Logging::ii->_log_threshold) Recorder(subsystem, severity)
  99. #define INANE(subsystem) RECORD(subsystem, LVL_INANE)
  100. #define INFO(subsystem) RECORD(subsystem, LVL_INFO)
  101. #define WARN(subsystem) RECORD(subsystem, LVL_WARN)
  102. #define OOPS(subsystem) RECORD(subsystem, LVL_OOPS)
  103. #define FATAL(subsystem) RECORD(subsystem, LVL_FATAL)
  104. //// Enforcer
  105. class Enforcer
  106. {
  107. protected:
  108. std::ostringstream oss;
  109. public:
  110. Enforcer(const char *locus);
  111. ~Enforcer();
  112. public:
  113. template<class T> inline Enforcer &operator<<(const T &t)
  114. {
  115. oss << t;
  116. return *this;
  117. }
  118. };
  119. #define USE_ENFORCE_EXPRESSION_STRING
  120. #define USE_ENFORCE_FILE_LINE_STRING
  121. #if defined(USE_ENFORCE_EXPRESSION_STRING)
  122. # define ENFORCE_EXPRESSION_STRING(exp) "Failed assertion (" #exp ")"
  123. #else
  124. # define ENFORCE_EXPRESSION_STRING(exp) "Failed assertion"
  125. #endif
  126. #if defined(USE_ENFORCE_FILE_LINE_STRING)
  127. # define ENFORCE_FILE_LINE_STRING " at " __FILE__ ":" CAT_STRINGIZE(__LINE__)
  128. #else
  129. # define ENFORCE_FILE_LINE_STRING ""
  130. #endif
  131. // Because there is an IF statement in the macro, you cannot use the
  132. // braceless if-else construction:
  133. // if (XYZ) ENFORCE(A == B) << "ERROR"; else INFO("SS") << "OK"; <-- bad!
  134. // Instead use:
  135. // if (XYZ) { ENFORCE(A == B) << "ERROR"; } else INFO("SS") << "OK"; <-- good!
  136. #define ENFORCE(exp) if ( (exp) == 0 ) Enforcer(ENFORCE_EXPRESSION_STRING(exp) ENFORCE_FILE_LINE_STRING "\n")
  137. #define EXCEPTION() Enforcer("Exception" ENFORCE_FILE_LINE_STRING "\n")
  138. #if defined(CAT_DEBUG)
  139. # define DEBUG_ENFORCE(exp) ENFORCE(exp)
  140. #else
  141. # define DEBUG_ENFORCE(exp) if (0) ENFORCE(exp) /* hopefully will be optimized out of existence */
  142. #endif
  143. } // namespace cat
  144. #endif // CAT_LOGGING_HPP
粤ICP备19079148号