AppLog.Interface.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #pragma once
  2. #include "AppTLSLog.hpp"
  3. namespace ECOM
  4. {
  5. namespace Log
  6. {
  7. class AppLog_API ShareLog
  8. {
  9. public:
  10. ShareLog ();
  11. ShareLog (ShareLog && from);
  12. ShareLog (const ShareLog & from);
  13. private:
  14. CSystemLog * m_FileLog;
  15. CSystemLog * m_ErrorLog;
  16. int m_LogLevel;
  17. bool m_bErrorOn;
  18. friend class AppLog;
  19. };
  20. class AppLog_API AppLog
  21. {
  22. private:
  23. static CSystemLog * FileLog;
  24. static CSystemLog * ErrorLog;
  25. public:
  26. static void EnableErrorLog (bool bSet);
  27. static void PrepareLog (const char * logFilename);
  28. static void CloseLog ();
  29. static int LogLevel (); // 此日志模块是否启用? 默认 = On
  30. static bool IsErrorLogOn (); // 此日志模块对应的错误日志是否启用? 默认 = Off
  31. static void SetMQServerLogPort (const char * szPort);
  32. static void Flush (const char * str);
  33. static void TLSFlush (CSystemLog::Severity s);
  34. static void NewLine ();
  35. public:
  36. static void Share (ShareLog & with);
  37. public:
  38. template <typename... Args>
  39. static void Log (const char * format, Args... args)
  40. {
  41. if (LogLevel () > 1) return;
  42. if (sizeof...(args) == 0)
  43. Flush (format);
  44. else
  45. {
  46. TLSLog::Format (format, args...);
  47. TLSFlush (CSystemLog::STY_DEBUG);
  48. }
  49. }
  50. template <typename... Args>
  51. static void Info (const char * format, Args... args)
  52. {
  53. if (LogLevel () > 2) return;
  54. if (sizeof...(args) == 0)
  55. Flush (format);
  56. else
  57. {
  58. TLSLog::Format (format, args...);
  59. TLSFlush (CSystemLog::STY_INFO);
  60. }
  61. }
  62. template <typename... Args>
  63. static void Warning (const char * format, Args... args)
  64. {
  65. if (LogLevel () > 3) return;
  66. TLSLog::Format (format, args...);
  67. TLSFlush (CSystemLog::STY_WARNING);
  68. }
  69. template <typename... Args>
  70. static void Error (const char * format, Args... args)
  71. {
  72. if (LogLevel () > 4) return;
  73. TLSLog::Format (format, args...);
  74. TLSFlush (CSystemLog::STY_ERROR);
  75. }
  76. template <typename... Args>
  77. static void Fatal (const char * format, Args... args)
  78. {
  79. if (LogLevel () > 5) return;
  80. TLSLog::Format (format, args...);
  81. TLSFlush (CSystemLog::STY_CRITICAL);
  82. }
  83. // 忽略级别, 无条件输出日志. 常用于组件启动时的版本/构建等信息
  84. template <typename... Args>
  85. static void Force (const char * format, Args... args)
  86. {
  87. if (sizeof...(args) == 0)
  88. Flush (format);
  89. else
  90. {
  91. TLSLog::Format (format, args...);
  92. TLSFlush (CSystemLog::STY_DEBUG);
  93. }
  94. }
  95. template <typename... Args>
  96. static void Log (const std::string & format, Args... args)
  97. {
  98. if (LogLevel () > 1) return;
  99. if (sizeof...(args) == 0)
  100. Flush (format.c_str ());
  101. else
  102. {
  103. TLSLog::Format (format.c_str (), args...);
  104. TLSFlush (CSystemLog::STY_DEBUG);
  105. }
  106. }
  107. template <typename... Args>
  108. static void Info (const std::string & format, Args... args)
  109. {
  110. if (LogLevel () > 2) return;
  111. if (sizeof...(args) == 0)
  112. Flush (format);
  113. else
  114. {
  115. TLSLog::Format (format.c_str (), args...);
  116. TLSFlush (CSystemLog::STY_INFO);
  117. }
  118. }
  119. template <typename... Args>
  120. static void Warning (const std::string & format, Args... args)
  121. {
  122. if (LogLevel () > 3) return;
  123. TLSLog::Format (format.c_str (), args...);
  124. TLSFlush (CSystemLog::STY_WARNING);
  125. }
  126. template <typename... Args>
  127. static void Error (const std::string & format, Args... args)
  128. {
  129. if (LogLevel () > 4) return;
  130. TLSLog::Format (format.c_str (), args...);
  131. TLSFlush (CSystemLog::STY_ERROR);
  132. }
  133. template <typename... Args>
  134. static void Fatal (const std::string & format, Args... args)
  135. {
  136. if (LogLevel () > 5) return;
  137. TLSLog::Format (format.c_str (), args...);
  138. TLSFlush (CSystemLog::STY_CRITICAL);
  139. }
  140. // 忽略级别, 无条件输出日志. 常用于组件启动时的版本/构建等信息
  141. template <typename... Args>
  142. static void Force (const std::string & format, Args... args)
  143. {
  144. if (sizeof...(args) == 0)
  145. Flush (format);
  146. else
  147. {
  148. TLSLog::Format (format.c_str (), args...);
  149. TLSFlush (CSystemLog::STY_DEBUG);
  150. }
  151. }
  152. static DString ErrorCodeToString (DWORD errorCode)
  153. {
  154. DString rc;
  155. const int nPreAlloc = 2048;
  156. rc.GetBuffer (nPreAlloc);
  157. if (errorCode == 0)
  158. errorCode = GetLastError ();
  159. rc._XAppendFormat ("<Error Code : {$} (0x{$:x})> = ", errorCode, errorCode);
  160. int nCurrentLength = rc.GetLength ();
  161. char * pszBuffer = rc.GetBuffer (1);
  162. pszBuffer += nCurrentLength;
  163. int nAppendLength = nPreAlloc - nCurrentLength;
  164. ::FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
  165. NULL,
  166. errorCode, // GetLastError (),
  167. MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  168. (LPTSTR) pszBuffer,
  169. nAppendLength,
  170. NULL);
  171. rc.TrimRight (); // 删除尾部的回车
  172. rc.ReleaseBuffer ();
  173. return std::move (rc);
  174. }
  175. };
  176. }
  177. }