ShareMemory_Logger.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #pragma once
  2. // 下列 ifdef 块是创建使从 DLL 导出更简单的
  3. // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 LOGGER_EXPORTS
  4. // 符号编译的。在使用此 DLL 的
  5. // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
  6. // LOGGER_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
  7. // 符号视为是被导出的。
  8. #ifndef SHAREMEMORY_LOGGER_EXPORTS
  9. #ifdef _WIN64
  10. #ifdef _DEBUG
  11. #pragma comment(lib, "ShareMemory_LoggerX64D.lib")
  12. #else
  13. #pragma comment(lib, "ShareMemory_LoggerX64.lib")
  14. #endif
  15. #else
  16. #ifdef _DEBUG
  17. #pragma comment(lib, "ShareMemory_LoggerD.lib")
  18. #else
  19. #pragma comment(lib, "ShareMemory_Logger.lib")
  20. #endif
  21. #endif
  22. #endif
  23. #include <string>
  24. //#include <sstream>
  25. #include "ShareMemory_Lock.h"
  26. using namespace std;
  27. #ifdef SHAREMEMORY_LOGGER_EXPORTS
  28. #define SHAREMEMORY_LOGGER_API __declspec(dllexport)
  29. #define SHAREMEMORY_LOGGER_C_API extern "C" __declspec(dllexport)
  30. #else
  31. #define SHAREMEMORY_LOGGER_API __declspec(dllimport)
  32. #define SHAREMEMORY_LOGGER_C_API extern "C" __declspec(dllimport)
  33. #endif
  34. #define lOG_LEVEL 1 //1:TRACE,2:DEBUG,3:INFO,4:WARN,5:ERROR,6:FATAL
  35. #define MAXFILESIZE 12 //Mbs
  36. #define MAXBACKUPCOUNT 15 //backup count
  37. #define MAXTIMEPERIOD 1 //time period:day
  38. #define FILENAME 0 //print filename. 0:No,1:Yes
  39. #define FUNCTIONNAME 1 //print functionname. 0:No,1:Yes
  40. #define LINE 1 //print line number. 0:No,1:Yes
  41. #define DATE 1 //print date. 0:No,1:Yes
  42. #define PROC 0 //print process Id. 0:No,1:Yes
  43. #define THREAD 1 //print Thread Id. 0:No,1:Yes
  44. #define CACHE 1 //cache. 0:No,1:Yes
  45. typedef enum _SHAREMEMORY_Log_Level {
  46. SM_LOG_LEVEL_INIT,
  47. SM_LOG_LEVEL_TRACE,
  48. SM_LOG_LEVEL_DEBUG,
  49. SM_LOG_LEVEL_INFO,
  50. SM_LOG_LEVEL_WARNING,
  51. SM_LOG_LEVEL_ERROR,
  52. SM_LOG_LEVEL_FATAL,
  53. SM_LOG_LEVEL_MAX
  54. }SHAREMEMORY_LOG_LEVEL;
  55. class SHAREMEMORY_LOGGER_API ShareMemory_Logger_Pattern {
  56. public:
  57. ShareMemory_Logger_Pattern(void);
  58. virtual ~ShareMemory_Logger_Pattern(void);
  59. bool SetLevel(LONG Level);
  60. SHAREMEMORY_LOG_LEVEL m_Level;//log level,def:WARNING
  61. size_t MaxFileSize;//log file size limit.def:10M
  62. size_t MaxBackupCount;//log file count limit.def:
  63. size_t MaxTimePeriod;//by day
  64. //debug info
  65. bool PrintFileName;
  66. bool PrintFunctionName;
  67. bool PrintLine;
  68. //date
  69. bool PrintDate;
  70. //time
  71. //bool PrintTime;
  72. //Proc
  73. bool PrintProcId;
  74. //Thread
  75. bool PrintThreadId;
  76. //cachable
  77. bool Cachable;
  78. //the whole pattern like below line
  79. /*
  80. //full log line
  81. [ date ] [ time ] [LEVEL] [ PID ][ TID ] [ FileName Line Func ]
  82. [2016:11:07] [14:03:35:0231] [DEBUG] [PID:0023][TID:00137] [FileName (Line) (Function)] : context...........
  83. //minimum log line
  84. [14:03:35:0231] : context......
  85. */
  86. };
  87. class SHAREMEMORY_LOGGER_API ShareMemory_Logger : public CShareMemory_Lock
  88. {
  89. DWORD m_FileId;
  90. /*file name using NAME_YYYYMMDDHHMMSS.LOG*/
  91. string *m_pFullLogFileName;
  92. ShareMemory_Logger_Pattern *m_pPattern;
  93. public:
  94. ShareMemory_Logger(ShareMemory_Logger &obj);
  95. ShareMemory_Logger(void);
  96. virtual ~ShareMemory_Logger(void);
  97. ShareMemory_Logger& operator = (const ShareMemory_Logger &tValue);
  98. void SetLogPattern(ShareMemory_Logger_Pattern& Pattern);
  99. void GetLogPattern(ShareMemory_Logger_Pattern& Pattern);
  100. void SetLogFilepath(const char *pPath);
  101. bool ChangeLoggerFileTitle(ShareMemory_Logger* p, const char *pTitle);
  102. bool IsLogFilePathExist();
  103. DWORD GetLogFileId();
  104. int GetLogLevel();
  105. };
  106. SHAREMEMORY_LOGGER_C_API void ExitLoggerModule();
  107. SHAREMEMORY_LOGGER_C_API ShareMemory_Logger* ShareMemCreateLogger();
  108. SHAREMEMORY_LOGGER_C_API void ShareMemReleseLogger(ShareMemory_Logger* p);//非自己创建(CreateLogger获取)的Logger,不要Relase!!!!
  109. SHAREMEMORY_LOGGER_C_API void __ShareMemory_DebugPrintA(ShareMemory_Logger* &plog,
  110. int Level,
  111. const char* file,
  112. int line,
  113. const char* function,
  114. const char* fmt,
  115. ...);
  116. #define SM_PRINTA_LOG_INFO(log,format,...) \
  117. {\
  118. if (log)\
  119. {\
  120. __ShareMemory_DebugPrintA(log, SM_LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, format, __VA_ARGS__); \
  121. }\
  122. }
  123. #define SM_PRINTA_LOG_WARN(log,format,...) \
  124. {\
  125. if (log)\
  126. {\
  127. __ShareMemory_DebugPrintA(log, SM_LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__, format, __VA_ARGS__); \
  128. }\
  129. }
  130. #define SM_PRINTA_LOG_ERR(log,format,...) \
  131. {\
  132. if (log)\
  133. {\
  134. __ShareMemory_DebugPrintA(log, SM_LOG_LEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__, format, __VA_ARGS__); \
  135. }\
  136. }
  137. //#define mLog::FINFO(log,format,...) __DebugPrintA(log,LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  138. //#define PRINTA_DEBUG(log,format,...) __DebugPrintA(log,LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  139. //#define PRINTA_INFO(log,format,...) __DebugPrintA(log,LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  140. //#define PRINTA_WARN(log,format,...) __DebugPrintA(log,LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  141. //#define PRINTA_ERROR(log,format,...) __DebugPrintA(log,LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)
  142. //#define PRINTA_FATAL(log,format,...) __DebugPrintA(log,LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)