mLog.Log4CPP.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #pragma once
  2. #include <assert.h>
  3. #include "String.DString.hpp"
  4. #define __M_LOG_LOG4CPP__
  5. //-----------------------------------------------------------------------------
  6. // mLog 是对 gLogger 的简单封装
  7. // mLog 可以理解为 myLog, 或者 mainLog 的短名称
  8. //-----------------------------------------------------------------------------
  9. namespace mLog
  10. {
  11. extern Log4CPP::Logger * gLogger;
  12. inline void NewLine ()
  13. {
  14. if (! gLogger) return;
  15. gLogger->NewLine ();
  16. }
  17. #if 1
  18. //< const char *
  19. template <typename... Args>
  20. inline void Force (const char * fmt, Args && ... args)
  21. {
  22. if (! gLogger) return;
  23. gLogger->Force (fmt, std::forward <Args> (args)...);
  24. }
  25. template <typename... Args>
  26. inline void Log (const char * fmt, Args && ... args)
  27. {
  28. if (! gLogger) return;
  29. gLogger->Info (fmt, std::forward <Args> (args)...);
  30. }
  31. template <typename... Args>
  32. inline void Verbose (const char * fmt, Args && ... args)
  33. {
  34. if (! gLogger) return;
  35. gLogger->Verbose (fmt, std::forward <Args> (args)...);
  36. }
  37. template <typename... Args>
  38. inline void Trace (const char * fmt, Args && ... args)
  39. {
  40. if (! gLogger) return;
  41. gLogger->Trace (fmt, std::forward <Args> (args)...);
  42. }
  43. template <typename... Args>
  44. inline void Debug (const char * fmt, Args && ... args)
  45. {
  46. if (! gLogger) return;
  47. gLogger->Debug (fmt, std::forward <Args> (args)...);
  48. }
  49. template <typename... Args>
  50. inline void Info (const char * fmt, Args && ... args)
  51. {
  52. if (! gLogger) return;
  53. gLogger->Info (fmt, std::forward <Args> (args)...);
  54. }
  55. template <typename... Args>
  56. inline void Notice (const char * fmt, Args && ... args)
  57. {
  58. if (! gLogger) return;
  59. gLogger->Notice (fmt, std::forward <Args> (args)...);
  60. }
  61. template <typename... Args>
  62. inline void Warn (const char * fmt, Args && ... args)
  63. {
  64. if (! gLogger) return;
  65. gLogger->Warn (fmt, std::forward <Args> (args)...);
  66. }
  67. template <typename... Args>
  68. inline void Warning (const char * fmt, Args && ... args)
  69. {
  70. if (! gLogger) return;
  71. gLogger->Warn (fmt, std::forward <Args> (args)...);
  72. }
  73. template <typename... Args>
  74. inline void Error (const char * fmt, Args && ... args)
  75. {
  76. if (! gLogger) return;
  77. gLogger->Error (fmt, std::forward <Args> (args)...);
  78. }
  79. template <typename... Args>
  80. inline void Fatal (const char * fmt, Args && ... args)
  81. {
  82. if (! gLogger) return;
  83. gLogger->Fatal (fmt, std::forward <Args> (args)...);
  84. }
  85. //>
  86. #endif
  87. //< StringView
  88. template <typename... Args>
  89. inline void Force (const eSTR::StringView & fmt, Args && ... args)
  90. {
  91. if (! gLogger) return;
  92. gLogger->Force (fmt, std::forward <Args> (args)...);
  93. }
  94. template <typename... Args>
  95. inline void Log (const eSTR::StringView & fmt, Args && ... args)
  96. {
  97. if (! gLogger) return;
  98. gLogger->Info (fmt, std::forward <Args> (args)...);
  99. }
  100. template <typename... Args>
  101. inline void Trace (const eSTR::StringView & fmt, Args && ... args)
  102. {
  103. if (! gLogger) return;
  104. gLogger->Trace (fmt, std::forward <Args> (args)...);
  105. }
  106. template <typename... Args>
  107. inline void Debug (const eSTR::StringView & fmt, Args && ... args)
  108. {
  109. if (! gLogger) return;
  110. gLogger->Debug (fmt, std::forward <Args> (args)...);
  111. }
  112. template <typename... Args>
  113. inline void Info (const eSTR::StringView & fmt, Args && ... args)
  114. {
  115. if (! gLogger) return;
  116. gLogger->Info (fmt, std::forward <Args> (args)...);
  117. }
  118. template <typename... Args>
  119. inline void Notice (const eSTR::StringView & fmt, Args && ... args)
  120. {
  121. if (! gLogger) return;
  122. gLogger->Notice (fmt, std::forward <Args> (args)...);
  123. }
  124. template <typename... Args>
  125. inline void Warn (const eSTR::StringView & fmt, Args && ... args)
  126. {
  127. if (! gLogger) return;
  128. gLogger->Warn (fmt, std::forward <Args> (args)...);
  129. }
  130. template <typename... Args>
  131. inline void Warning (const eSTR::StringView & fmt, Args && ... args)
  132. {
  133. if (! gLogger) return;
  134. gLogger->Warn (fmt, std::forward <Args> (args)...);
  135. }
  136. template <typename... Args>
  137. inline void Error (const eSTR::StringView & fmt, Args && ... args)
  138. {
  139. if (! gLogger) return;
  140. gLogger->Error (fmt, std::forward <Args> (args)...);
  141. }
  142. template <typename... Args>
  143. inline void Fatal (const eSTR::StringView & fmt, Args && ... args)
  144. {
  145. if (! gLogger) return;
  146. gLogger->Fatal (fmt, std::forward <Args> (args)...);
  147. }
  148. //>
  149. //< DString
  150. template <typename... Args>
  151. inline void Force (const eSTR::DString & fmt, Args && ... args)
  152. {
  153. if (! gLogger) return;
  154. gLogger->Force (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  155. }
  156. template <typename... Args>
  157. inline void Log (const eSTR::DString & fmt, Args && ... args)
  158. {
  159. if (! gLogger) return;
  160. gLogger->Info (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  161. }
  162. template <typename... Args>
  163. inline void Trace (const eSTR::DString & fmt, Args && ... args)
  164. {
  165. if (! gLogger) return;
  166. gLogger->Trace (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  167. }
  168. template <typename... Args>
  169. inline void Debug (const eSTR::DString & fmt, Args && ... args)
  170. {
  171. if (! gLogger) return;
  172. gLogger->Debug (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  173. }
  174. template <typename... Args>
  175. inline void Info (const eSTR::DString & fmt, Args && ... args)
  176. {
  177. if (! gLogger) return;
  178. gLogger->Info (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  179. }
  180. template <typename... Args>
  181. inline void Notice (const eSTR::DString & fmt, Args && ... args)
  182. {
  183. if (! gLogger) return;
  184. gLogger->Notice (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  185. }
  186. template <typename... Args>
  187. inline void Warn (const eSTR::DString & fmt, Args && ... args)
  188. {
  189. if (! gLogger) return;
  190. gLogger->Warn (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  191. }
  192. template <typename... Args>
  193. inline void Warning (const eSTR::DString & fmt, Args && ... args)
  194. {
  195. if (! gLogger) return;
  196. gLogger->Warn (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  197. }
  198. template <typename... Args>
  199. inline void Error (const eSTR::DString & fmt, Args && ... args)
  200. {
  201. if (! gLogger) return;
  202. gLogger->Error (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  203. }
  204. template <typename... Args>
  205. inline void Fatal (const eSTR::DString & fmt, Args && ... args)
  206. {
  207. if (! gLogger) return;
  208. gLogger->Fatal (fmt.To <eSTR::StringView> (), std::forward <Args> (args)...);
  209. }
  210. //>
  211. #define FLOG(tmpl,...) Log("{$}@{$}\t"#tmpl, __FUNCTION__,__LINE__,__VA_ARGS__)
  212. #define FTRACE(tmpl,...) Trace("{$}@{$}\t"#tmpl, __FUNCTION__,__LINE__,__VA_ARGS__)
  213. #define FDEBUG(tmpl,...) Debug("{$}@{$}\t"#tmpl, __FUNCTION__,__LINE__,__VA_ARGS__)
  214. #define FINFO(tmpl,...) Info("{$}@{$}\t "#tmpl, __FUNCTION__,__LINE__,__VA_ARGS__)
  215. #define FNOTICE(tmpl,...) Notice("{$}@{$}\t"#tmpl, __FUNCTION__,__LINE__,__VA_ARGS__)
  216. #define FWARN(tmpl,...) Warn("{$}@{$}\t"#tmpl, __FUNCTION__,__LINE__,__VA_ARGS__)
  217. #define FERROR(tmpl,...) Error("{$}@{$}\t"#tmpl, __FUNCTION__,__LINE__,__VA_ARGS__)
  218. inline void Flush (const eSTR::StringView & str)
  219. {
  220. if (! gLogger) return;
  221. gLogger->LogNoFormat (Log4CPP::enInfo, str, str.GetLength (), false);
  222. }
  223. inline void Flush (const eSTR::DString & str)
  224. {
  225. if (! gLogger) return;
  226. gLogger->LogNoFormat (Log4CPP::enInfo, str, str.GetLength (), false);
  227. }
  228. inline void LogNoFormat (int Level, const eSTR::DString & str, bool bWithLayout = false)
  229. {
  230. if (! gLogger) return;
  231. gLogger->LogNoFormat (Level, str, str.GetLength (), bWithLayout);
  232. }
  233. inline void LogNoFormat (int Level, const eSTR::StringView & str, bool bWithLayout = false)
  234. {
  235. if (! gLogger) return;
  236. gLogger->LogNoFormat (Level, str, str.GetLength (), bWithLayout);
  237. }
  238. inline void LogNoFormat (int Level, const char * buf, int len, bool bWithLayout = false)
  239. {
  240. if (! gLogger) return;
  241. gLogger->LogNoFormat (Level, buf, len, bWithLayout);
  242. }
  243. inline bool Decide (int Level)
  244. {
  245. if (! gLogger) return false;
  246. return (gLogger->Decide (Level));
  247. }
  248. inline void Close ()
  249. {
  250. if (! gLogger) return;
  251. gLogger->Close ();
  252. }
  253. inline eSTR::DString ErrorCodeToString (DWORD errorCode)
  254. {
  255. if (! gLogger) return eSTR::DString ();
  256. return gLogger->ErrorCodeToString (errorCode);
  257. }
  258. inline int CurrentLevel ()
  259. {
  260. if (! gLogger) return 0;
  261. return gLogger->GetLevel ();
  262. }
  263. inline eSTR::DString CurrentLevelName ()
  264. {
  265. if (! gLogger) return eSTR::DString ();
  266. return gLogger->LogLevelToString (gLogger->GetLevel ());
  267. }
  268. };