Logger.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #pragma once
  2. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  3. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 LOGGER_EXPORTS
  4. // 符号编译的。在使用此 DLL 的
  5. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  6. // LOGGER_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  7. // 符号视为是被导出的。
  8. #ifndef LOGGER_EXPORTS
  9. #ifdef _WIN64
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "CcosLoggerX64D.lib")
  12. #else
  13. #pragma comment(lib, "CcosLoggerX64.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment(lib, "CcosLoggerD.lib")
  18. #else
  19. #pragma comment(lib, "CcosLogger.lib")
  20. #endif
  21. #endif
  22. #endif
  23. #include <string>
  24. #include <sstream>
  25. #include "CcosLock.h"
  26. #include "ResDataObject.h"
  27. using namespace std;
  28. #ifdef LOGGER_EXPORTS
  29. #define LOGGER_API __declspec(dllexport)
  30. #define LOGGER_C_API extern "C" __declspec(dllexport)
  31. #else
  32. #define LOGGER_API __declspec(dllimport)
  33. #define LOGGER_C_API extern "C" __declspec(dllimport)
  34. #endif
  35. typedef enum _Log_Level {
  36. LOG_LEVEL_INIT,
  37. LOG_LEVEL_TRACE,
  38. LOG_LEVEL_DEBUG,
  39. LOG_LEVEL_INFO,
  40. LOG_LEVEL_WARNING,
  41. LOG_LEVEL_ERROR,
  42. LOG_LEVEL_FATAL,
  43. LOG_LEVEL_MAX
  44. }LOG_LEVEL;
  45. class LOGGER_API Logger_Pattern {
  46. public:
  47. Logger_Pattern(void);
  48. virtual ~Logger_Pattern(void);
  49. bool SetLevel(LONG Level);
  50. LOG_LEVEL m_Level;//log level,def:WARNING
  51. size_t MaxFileSize;//log file size limit.def:10M
  52. size_t MaxBackupCount;//log file count limit.def:
  53. size_t MaxTimePeriod;//by day
  54. //debug info
  55. bool PrintFileName;
  56. bool PrintFunctionName;
  57. bool PrintLine;
  58. //date
  59. bool PrintDate;
  60. //time
  61. //bool PrintTime;
  62. //Proc
  63. bool PrintProcId;
  64. //Thread
  65. bool PrintThreadId;
  66. //cachable
  67. bool Cachable;
  68. //the whole pattern like below line
  69. /*
  70. //full log line
  71. [ date ] [ time ] [LEVEL] [ PID ][ TID ] [ FileName Line Func ]
  72. [2016:11:07] [14:03:35:0231] [DEBUG] [PID:0023][TID:00137] [FileName (Line) (Function)] : context...........
  73. //minimum log line
  74. [14:03:35:0231] : context......
  75. */
  76. };
  77. class LOGGER_API Logger : public CcosLock
  78. {
  79. DWORD m_FileId;
  80. /*file name using NAME_YYYYMMDDHHMMSS.LOG*/
  81. string *m_pFullLogFileName;
  82. string *m_pErrFullLogFileName;
  83. Logger_Pattern *m_pPattern;
  84. public:
  85. Logger(Logger &obj);
  86. Logger(void);
  87. virtual ~Logger(void);
  88. Logger& operator = (const Logger &tValue);
  89. void SetLogPattern(Logger_Pattern& Pattern);
  90. void GetLogPattern(Logger_Pattern& Pattern);
  91. void SetLogFilepath(const char *pPath);
  92. bool ChangeLoggerFileTitle(Logger* p, const char *pTitle);
  93. bool IsLogFilePathExist();
  94. const char* GetErrLogFilePath();
  95. DWORD GetLogFileId();
  96. int GetLogLevel();
  97. };
  98. LOGGER_C_API Logger* GetGlobalLogger();
  99. LOGGER_C_API Logger* GetBusLogger();
  100. LOGGER_C_API Logger* GetThreadLogger();
  101. LOGGER_C_API void ExitLoggerModule();
  102. LOGGER_C_API Logger* CreateLogger();
  103. LOGGER_C_API void ReleseLogger(Logger* p);//非自己创建(CreateLogger获取)的Logger,不要Relase!!!!
  104. LOGGER_C_API void PrintA_IOLOG(Logger*& plog, int Level, const char* pszContext, size_t ContextSize);
  105. LOGGER_C_API void __DebugPrintA(Logger* &plog,
  106. int Level,
  107. const char* file,
  108. int line,
  109. const char* function,
  110. const char* fmt,
  111. ...);
  112. LOGGER_C_API void __G_DebugPrintA(
  113. int Level,
  114. const char* file,
  115. int line,
  116. const char* function,
  117. const char* fmt,
  118. ...);
  119. LOGGER_C_API void __T_DebugPrintA(
  120. int Level,
  121. const char* file,
  122. int line,
  123. const char* function,
  124. const char* fmt,
  125. ...);
  126. LOGGER_C_API void __Res_DebugPrintA(
  127. Logger* &plog,
  128. ResDataObject &obj,
  129. int Level,
  130. const char* file,
  131. int line,
  132. const char* function,
  133. const char* fmt,
  134. ...);
  135. LOGGER_C_API void __Res_T_DebugPrintA(
  136. ResDataObject &obj,
  137. int Level,
  138. const char* file,
  139. int line,
  140. const char* function,
  141. const char* fmt,
  142. ...);
  143. #define PRINTA_LOG(log,Level,format,...) \
  144. {\
  145. if (log)\
  146. {\
  147. int level = log->GetLogLevel(); \
  148. if (level <= Level)\
  149. {\
  150. __DebugPrintA(log, LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, format, __VA_ARGS__); \
  151. }\
  152. }\
  153. }
  154. //#define PRINTA_TRACE(log,format,...) PRINTA_LOG(log,LOG_LEVEL_TRACE,format,__VA_ARGS__)
  155. //#define PRINTA_DEBUG(log,format,...) PRINTA_LOG(log,LOG_LEVEL_DEBUG,format,__VA_ARGS__)
  156. //#define PRINTA_INFO(log,format,...) PRINTA_LOG(log,LOG_LEVEL_INFO,format,__VA_ARGS__)
  157. //#define PRINTA_WARN(log,format,...) PRINTA_LOG(log,LOG_LEVEL_WARNING,format,__VA_ARGS__)
  158. //#define PRINTA_ERROR(log,format,...) PRINTA_LOG(log,LOG_LEVEL_ERROR,format,__VA_ARGS__)
  159. //#define PRINTA_FATAL(log,format,...) PRINTA_LOG(log,LOG_LEVEL_FATAL,format,__VA_ARGS__)
  160. #define PRINTA_TRACE(log,format,...) __DebugPrintA(log,LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  161. #define PRINTA_DEBUG(log,format,...) __DebugPrintA(log,LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  162. #define PRINTA_INFO(log,format,...) __DebugPrintA(log,LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  163. #define PRINTA_WARN(log,format,...) __DebugPrintA(log,LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  164. #define PRINTA_ERROR(log,format,...) __DebugPrintA(log,LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  165. #define PRINTA_FATAL(log,format,...) __DebugPrintA(log,LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  166. //#define GPRINTA_TRACE(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_TRACE,format,__VA_ARGS__)
  167. //#define GPRINTA_DEBUG(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_DEBUG,format,__VA_ARGS__)
  168. //#define GPRINTA_INFO(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_INFO,format,__VA_ARGS__)
  169. //#define GPRINTA_WARN(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_WARNING,format,__VA_ARGS__)
  170. //#define GPRINTA_ERROR(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_ERROR,format,__VA_ARGS__)
  171. //#define GPRINTA_FATAL(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_FATAL,format,__VA_ARGS__)
  172. #define GPRINTA_TRACE(format,...) __G_DebugPrintA(LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  173. #define GPRINTA_DEBUG(format,...) __G_DebugPrintA(LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  174. #define GPRINTA_INFO(format,...) __G_DebugPrintA(LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  175. #define GPRINTA_WARN(format,...) __G_DebugPrintA(LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  176. #define GPRINTA_ERROR(format,...) __G_DebugPrintA(LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  177. #define GPRINTA_FATAL(format,...) __G_DebugPrintA(LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  178. //#define TPRINTA_TRACE(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_TRACE,format,__VA_ARGS__)
  179. //#define TPRINTA_DEBUG(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_DEBUG,format,__VA_ARGS__)
  180. //#define TPRINTA_INFO(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_INFO,format,__VA_ARGS__)
  181. //#define TPRINTA_WARN(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_WARNING,format,__VA_ARGS__)
  182. //#define TPRINTA_ERROR(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_ERROR,format,__VA_ARGS__)
  183. //#define TPRINTA_FATAL(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_FATAL,format,__VA_ARGS__)
  184. #define TPRINTA_TRACE(format,...) __T_DebugPrintA(LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  185. #define TPRINTA_DEBUG(format,...) __T_DebugPrintA(LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  186. #define TPRINTA_INFO(format,...) __T_DebugPrintA(LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  187. #define TPRINTA_WARN(format,...) __T_DebugPrintA(LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  188. #define TPRINTA_ERROR(format,...) __T_DebugPrintA(LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  189. #define TPRINTA_FATAL(format,...) __T_DebugPrintA(LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  190. #define RES_TPRINTA_TRACE(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  191. #define RES_TPRINTA_DEBUG(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  192. #define RES_TPRINTA_INFO(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  193. #define RES_TPRINTA_WARN(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  194. #define RES_TPRINTA_ERROR(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  195. #define RES_TPRINTA_FATAL(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  196. #define RES_PRINTA_TRACE(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  197. #define RES_PRINTA_DEBUG(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  198. #define RES_PRINTA_INFO(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  199. #define RES_PRINTA_WARN(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  200. #define RES_PRINTA_ERROR(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  201. #define RES_PRINTA_FATAL(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)