Log4CPP.Logger.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #pragma once
  2. #define __USING_LOG4CPP__
  3. #include <memory>
  4. #include <list>
  5. #include <functional>
  6. #include "String.DString.hpp"
  7. #include "String.Format.tlh"
  8. #ifdef Log4CPP_EXPORTS
  9. #define Log4CPP_API _declspec(dllexport)
  10. #else
  11. #define Log4CPP_API _declspec(dllimport)
  12. #endif
  13. #include "Log4CPP.Define.hpp"
  14. #include "Log4CPP.LogLevel.hpp"
  15. #include "Log4CPP.Appender.hpp"
  16. #include "Log4CPP.TLSLog.hpp"
  17. #ifndef Log4CPP_EXPORTS
  18. #ifdef _WIN64
  19. #ifdef _DEBUG
  20. #pragma comment (lib, "Log4CPP64D.lib")
  21. #pragma comment (lib, "Log4CPP.TLSLog64D.lib")
  22. #pragma comment (lib, "ECOM.Utility.DString64D.lib")
  23. #else
  24. #pragma comment (lib, "801.Log4CPP64.lib")
  25. #pragma comment (lib, "801.Log4CPP.TLSLog64.lib")
  26. #pragma comment (lib, "801.ECOM.Utility.DString64.lib")
  27. #endif
  28. #else // X86
  29. #ifdef _DEBUG
  30. #pragma comment (lib, "Log4CPPD.lib")
  31. #pragma comment (lib, "Log4CPP.TLSLogD.lib")
  32. #else
  33. #pragma comment (lib, "Log4CPP.lib")
  34. #pragma comment (lib, "Log4CPP.TLSLog.lib")
  35. #endif
  36. #endif
  37. #endif // Log4CPP_EXPORTS
  38. //-----------------------------------------------------------------------------
  39. // Logger
  40. //-----------------------------------------------------------------------------
  41. namespace Log4CPP
  42. {
  43. namespace Detail
  44. {
  45. class Logger_Detail;
  46. }
  47. };
  48. namespace Log4CPP
  49. {
  50. class Log4CPP_API Logger
  51. {
  52. public:
  53. Logger ();
  54. Logger (const char * Name);
  55. Logger (const char * Name, const char * DisplayName);
  56. Logger (const Logger & from);
  57. Logger (Logger && from);
  58. ~Logger ();
  59. public:
  60. Logger & operator = (const Logger & from);
  61. Logger & operator = (Logger && from);
  62. public:
  63. void Close ();
  64. public:
  65. //< const char *
  66. // 忽略级别, 无条件输出日志. 常用于组件启动时的版本/构建等信息
  67. template <typename... Args>
  68. inline void Force (const char * fmt, Args && ... args)
  69. {
  70. DoLog ((int) enLogLevel::enTrace, fmt, std::forward <Args> (args)...);
  71. }
  72. template <typename... Args>
  73. inline void Verbose (const char * fmt, Args && ... args)
  74. {
  75. if (GetLevel () > enLogLevel::enVerbose) return;
  76. DoLog ((int) enLogLevel::enVerbose, fmt, std::forward <Args> (args)...);
  77. }
  78. template <typename... Args>
  79. inline void Trace (const char * fmt, Args && ... args)
  80. {
  81. if (GetLevel () > enLogLevel::enTrace) return;
  82. DoLog ((int)enLogLevel::enTrace, fmt, std::forward <Args> (args)...);
  83. }
  84. template <typename... Args>
  85. inline void Debug (const char * fmt, Args && ... args)
  86. {
  87. if (GetLevel () > enLogLevel::enDebug) return;
  88. DoLog ((int) enLogLevel::enDebug, fmt, std::forward <Args> (args)...);
  89. }
  90. template <typename... Args>
  91. inline void Info (const char* fmt, Args && ... args)
  92. {
  93. if (GetLevel () > enLogLevel::enInfo) return;
  94. DoLog ((int) enLogLevel::enInfo, fmt, std::forward <Args> (args)...);
  95. }
  96. template <typename... Args>
  97. inline void Notice (const char* fmt, Args && ... args)
  98. {
  99. if (GetLevel () > enLogLevel::enNotice) return;
  100. DoLog ((int) enLogLevel::enNotice, fmt, std::forward <Args> (args)...);
  101. }
  102. template <typename... Args>
  103. inline void Warn (const char * fmt, Args && ... args)
  104. {
  105. if (GetLevel () > enLogLevel::enWarn) return;
  106. DoLog ((int) enLogLevel::enWarn, fmt, std::forward <Args> (args)...);
  107. }
  108. template <typename... Args>
  109. inline void Error (const char * fmt, Args && ... args)
  110. {
  111. if (GetLevel () > enLogLevel::enError) return;
  112. DoLog ((int) enLogLevel::enError, fmt, std::forward <Args> (args)...);
  113. }
  114. template <typename... Args>
  115. inline void Fatal (const char * fmt, Args && ... args)
  116. {
  117. if (GetLevel () > enLogLevel::enFatal) return;
  118. DoLog ((int) enLogLevel::enFatal, fmt, std::forward <Args> (args)...);
  119. }
  120. //>
  121. //< StringView
  122. template <typename... Args>
  123. inline void Force (const eSTR::StringView & fmt, Args && ... args)
  124. {
  125. DoLog ((int) enLogLevel::enTrace, fmt, std::forward <Args> (args)...);
  126. }
  127. template <typename... Args>
  128. inline void Trace (const eSTR::StringView & fmt, Args && ... args)
  129. {
  130. if (GetLevel () > enLogLevel::enTrace) return;
  131. DoLog ((int) enLogLevel::enTrace, fmt, std::forward <Args> (args)...);
  132. }
  133. template <typename... Args>
  134. inline void Debug (const eSTR::StringView & fmt, Args && ... args)
  135. {
  136. if (GetLevel () > enLogLevel::enDebug) return;
  137. DoLog ((int) enLogLevel::enDebug, fmt, std::forward <Args> (args)...);
  138. }
  139. template <typename... Args>
  140. inline void Info (const eSTR::StringView & fmt, Args && ... args)
  141. {
  142. if (GetLevel () > enLogLevel::enInfo) return;
  143. DoLog ((int) enLogLevel::enInfo, fmt, std::forward <Args> (args)...);
  144. }
  145. template <typename... Args>
  146. inline void Notice (const eSTR::StringView & fmt, Args && ... args)
  147. {
  148. if (GetLevel () > enLogLevel::enNotice) return;
  149. DoLog ((int) enLogLevel::enNotice, fmt, std::forward <Args> (args)...);
  150. }
  151. template <typename... Args>
  152. inline void Warn (const eSTR::StringView & fmt, Args && ... args)
  153. {
  154. if (GetLevel () > enLogLevel::enWarn) return;
  155. DoLog ((int) enLogLevel::enWarn, fmt, std::forward <Args> (args)...);
  156. }
  157. template <typename... Args>
  158. inline void Error (const eSTR::StringView & fmt, Args && ... args)
  159. {
  160. if (GetLevel () > enLogLevel::enError) return;
  161. DoLog ((int) enLogLevel::enError, fmt, std::forward <Args> (args)...);
  162. }
  163. template <typename... Args>
  164. inline void Fatal (const eSTR::StringView & fmt, Args && ... args)
  165. {
  166. if (GetLevel () > enLogLevel::enFatal) return;
  167. DoLog ((int) enLogLevel::enFatal, fmt, std::forward <Args> (args)...);
  168. }
  169. //>
  170. template <typename... Args>
  171. inline void Log (int Level, const char * fmt, Args && ... args)
  172. {
  173. if (GetLevel () > Level) return;
  174. DoLog (Level, fmt, std::forward <Args> (args)...);
  175. }
  176. bool Decide (int Level) const
  177. {
  178. return (GetLevel () <= Level);
  179. }
  180. //< 以下 3 个函数中, if (tbuf [0]) 及最后的 tbuf [0] = 0, 是为了避免重入
  181. // 在 Appender.BUS 及 Appender.UDP 中, 执行到最后, MyWriter::WriteLine () 函数体的最后一行
  182. // 有可能再次调用 mLog::XX (), 这有可能再次到这里, tbuf 就会被破坏
  183. public:
  184. // 直接把字符串刷到 Appender 中, 不要做格式化
  185. inline void LogNoFormat (int Level, const char * buf, int len, bool bWithLayout = false)
  186. {
  187. if (GetLevel () > Level) return;
  188. auto tbuf = ThreadContext::TLSMessage::Get ();
  189. auto size = ThreadContext::TLSMessage::Size () - 4;
  190. if (tbuf [0])
  191. return;
  192. auto cch = (std::min) (size, len);
  193. memcpy (tbuf, buf, cch);
  194. tbuf [cch] = 0;
  195. Flush (Level, cch, tbuf, bWithLayout);
  196. tbuf [0] = 0;
  197. }
  198. private:
  199. template <typename... Args>
  200. inline void DoLog (int Level, const char * fmt, Args && ... args)
  201. {
  202. auto tbuf = ThreadContext::TLSMessage::Get ();
  203. auto size = ThreadContext::TLSMessage::Size () - 4;
  204. if (tbuf [0])
  205. return;
  206. auto cch = 0;
  207. if (sizeof...(args) == 0)
  208. {
  209. cch = (int) strlen (fmt);
  210. cch = (std::min) (cch, size);
  211. memcpy (tbuf, fmt, cch);
  212. tbuf [cch] = 0;
  213. }
  214. else
  215. {
  216. cch = ECOM::Utility::String::StringFormat (tbuf, size).Format (fmt, std::forward <Args> (args)...);
  217. tbuf [cch] = 0;
  218. }
  219. Flush (Level, cch, tbuf, true);
  220. tbuf [0] = 0;
  221. }
  222. template <typename... Args>
  223. inline void DoLog (int Level, const eSTR::StringView & fmt, Args && ... args)
  224. {
  225. auto tbuf = ThreadContext::TLSMessage::Get ();
  226. auto size = ThreadContext::TLSMessage::Size () - 4;
  227. if (tbuf [0])
  228. return;
  229. auto cch = 0;
  230. if (sizeof...(args) == 0)
  231. {
  232. cch = fmt.GetLength ();
  233. cch = (std::min) (cch, size);
  234. memcpy (tbuf, fmt, cch);
  235. tbuf [cch] = 0;
  236. }
  237. else
  238. {
  239. cch = ECOM::Utility::String::StringFormat (tbuf, size).Format (fmt, std::forward <Args> (args)...);
  240. tbuf [cch] = 0;
  241. }
  242. Flush (Level, cch, tbuf, true);
  243. tbuf [0] = 0;
  244. }
  245. //>
  246. public:
  247. void NewLine (int Level = enLogLevel::enInfo);
  248. public:
  249. TLogString GetName () const; // { return m_Name; }
  250. TLogString GetDisplayName () const; // { return m_DisplayName; }
  251. enLogLevel GetLevel () const; // { return m_Level; }
  252. void SetLevel (int value); // { m_Level = value; }
  253. void Set (const TLogString & key, const TLogString & value); // for arAppender
  254. public: // 对 Appender 进行迭代
  255. int GetNbOfAppender () const;
  256. int ForEachAppender (std::function <void (Appender *)> fun);
  257. int OnEachAppender (std::function <bool (Appender *)> fun);
  258. Appender * FindAppender (std::function <bool (Appender *)> Pred);
  259. public:
  260. // 添加一个没有任何特殊配置项的 Appender
  261. Appender * AddAppender (const char * Type, const char * Name);
  262. bool RemoveAppender (const char * Name);
  263. public:
  264. // 把 from 中已有的 Appender 复制给我自己. 目前主要是 ConfigManager 在使用
  265. void CopyAppenderFrom (const Logger & from);
  266. // 把 from 中已有的 Appender 添加到我自己. 目前主要是 ConfigManager 在使用
  267. void AddAppenderFrom (const Logger & from);
  268. // 把已有的 Appender 添加到 Logger 中, 目前主要是 ConfigManager 在使用
  269. void AddAppender (std::shared_ptr <Appender> a);
  270. public:
  271. void Flush (int Level, int cbBuf, const char * buf, bool bWithLayout);
  272. private:
  273. static tTraceID GetTraceID ();
  274. public:
  275. static TLogString ErrorCodeToString (DWORD errorCode);
  276. static TLogString LogLevelToString (int level);
  277. private:
  278. enLogLevel m_Level = enLogLevel::enUndefined;
  279. Detail::Logger_Detail * m_pDetail;
  280. };
  281. };
  282. //-----------------------------------------------------------------------------
  283. //
  284. //-----------------------------------------------------------------------------
  285. namespace Log4CPP
  286. {
  287. class Log4CPP_API LogManager
  288. {
  289. private:
  290. LogManager () = delete;
  291. public:
  292. static bool LoadConfigFile (const char * CfgFileName);
  293. static bool LoadConfigString (const char * strXML);
  294. static Logger * GetLogger (const char * LoggerName);
  295. static bool IsExist (const char * LoggerName);
  296. static auto NewAppender (const char * Type, const char * Name) -> std::unique_ptr <Appender>;
  297. static int ForEachLogger (std::function <void (Logger *)> fun);
  298. static void CloseAll ();
  299. };
  300. };