改進後的日誌類CLogger

  在原有的基礎上增加了module_name,表示模塊名,當一個系統由多模塊組成時,輸出的日誌將更加清晰,使用方法,以MOOON-agent中的爲例:

 

  1. /** 不要修改下面的常量值,而應當通過對應的方法去修改 
  2.   * 這些常量值主要是方便多模塊共享,故放在這個公有頭文件當中 
  3.   */ 
  4. enum 
  5.     LOG_LINE_SIZE_MIN              = 256,        /** 日誌行最小長度 */ 
  6.     LOG_LINE_SIZE_MAX              = 32768,      /** 日誌行最大長度(32K) ,最大不能超過64K,因爲使用2字節無符號整數存儲的 */ 
  7.     DEFAULT_LOG_FILE_SIZE          = 104857600,  /** 默認的單個日誌文件大小(100MB) */ 
  8.     DEFAULT_LOG_FILE_BACKUP_NUMBER = 10          /** 默認的日誌文件備份個數 */ 
  9. }; 
  10.  
  11. /** 定義日誌級別 */ 
  12. typedef enum 
  13.     LOG_LEVEL_DETAIL = 0, 
  14.     LOG_LEVEL_DEBUG  = 1, 
  15.     LOG_LEVEL_INFO   = 2, 
  16.     LOG_LEVEL_WARN   = 3, 
  17.     LOG_LEVEL_ERROR  = 4, 
  18.     LOG_LEVEL_FATAL  = 5,     
  19.     LOG_LEVEL_STATE  = 6,  /** 僅輸出狀態數據 */ 
  20.     LOG_LEVEL_TRACE  = 7 
  21. }log_level_t; 
  22.  
  23. /** 通過日誌級別名得到日誌級別 */ 
  24. extern log_level_t get_log_level(const char* level_name); 
  25. /** 通過日誌級別得到日誌級別名,如果傳入錯誤的日誌級別,則返回NULL */ 
  26. extern const char* get_log_level_name(log_level_t log_level); 
  27.  
  28. /** 
  29.   * 日誌器接口,提供常見的寫日誌功能 
  30.   */ 
  31. class ILogger 
  32. public
  33.     /** 空虛擬析構函數,以屏蔽編譯器告警 */ 
  34.     virtual ~ILogger() {} 
  35.          
  36.     /** 是否允許同時在標準輸出上打印日誌 */ 
  37.     virtual void enable_screen(bool enabled) {} 
  38.     /** 是否允許二進制日誌,二進制日誌必須通過它來打開 */ 
  39.     virtual void enable_bin_log(bool enabled) {} 
  40.     /** 是否允許跟蹤日誌,跟蹤日誌必須通過它來打開 */ 
  41.     virtual void enable_trace_log(bool enabled) {} 
  42.     /** 是否自動在一行後添加結尾的點號,如果最後已經有點號或換符符,則不會再添加 */ 
  43.     virtual void enable_auto_adddot(bool enabled) {} 
  44.     /** 是否自動添加換行符,如果已經有換行符,則不會再自動添加換行符 */ 
  45.     virtual void enable_auto_newline(bool enabled) {}    
  46.     /** 設置日誌級別,跟蹤日誌級別不能通過它來設置 */ 
  47.     virtual void set_log_level(log_level_t log_level) {} 
  48.     /** 設置單個文件的最大建議大小 */ 
  49.     virtual void set_single_filesize(uint32_t filesize) {} 
  50.     /** 設置日誌文件備份個數,不包正在寫的日誌文件 */ 
  51.     virtual void set_backup_number(uint16_t backup_number) {} 
  52.  
  53.     /** 是否允許二進制日誌 */ 
  54.     virtual bool enabled_bin() { return false; } 
  55.     /** 是否允許Detail級別日誌 */ 
  56.     virtual bool enabled_detail() { return false; } 
  57.     /** 是否允許Debug級別日誌 */ 
  58.     virtual bool enabled_debug() { return false; } 
  59.     /** 是否允許Info級別日誌 */ 
  60.     virtual bool enabled_info() { return false; } 
  61.     /** 是否允許Warn級別日誌 */ 
  62.     virtual bool enabled_warn() { return false; } 
  63.     /** 是否允許Error級別日誌 */ 
  64.     virtual bool enabled_error() { return false; } 
  65.     /** 是否允許Fatal級別日誌 */ 
  66.     virtual bool enabled_fatal() { return false; } 
  67.     /** 是否允許輸出狀態日誌 */ 
  68.     virtual bool enabled_state() { return false; } 
  69.     /** 是否允許Trace級別日誌 */ 
  70.     virtual bool enabled_trace() { return false; } 
  71.  
  72.     virtual void log_detail(const char* filename, int lineno, const char* module_name, const char* format, ...) {} 
  73.     virtual void log_debug(const char* filename, int lineno, const char* module_name, const char* format, ...)  {} 
  74.     virtual void log_info(const char* filename, int lineno, const char* module_name, const char* format, ...)   {} 
  75.     virtual void log_warn(const char* filename, int lineno, const char* module_name, const char* format, ...)   {} 
  76.     virtual void log_error(const char* filename, int lineno, const char* module_name, const char* format, ...)  {} 
  77.     virtual void log_fatal(const char* filename, int lineno, const char* module_name, const char* format, ...)  {} 
  78.     virtual void log_state(const char* filename, int lineno, const char* module_name, const char* format, ...)  {} 
  79.     virtual void log_trace(const char* filename, int lineno, const char* module_name, const char* format, ...)  {} 
  80.  
  81.     /** 寫二進制日誌 */ 
  82.     virtual void bin_log(const char* filename, int lineno, const char* module_name, const char* log, uint16_t size) {} 
  83. }; 
  84.  
  85. ////////////////////////////////////////////////////////////////////////// 
  86. // 日誌宏,方便記錄日誌 
  87. extern ILogger* g_logger; // 只是聲明,不是定義,不能賦值哦! 
  88.  
  89. #define __MYLOG_DETAIL(logger, module_name, format, ...) \ 
  90. do { \ 
  91.     if (NULL == logger) { \ 
  92.         printf("[DETAIL][%s:%d]", __FILE__, __LINE__); \ 
  93.         printf(format, ##__VA_ARGS__); \ 
  94.     } \ 
  95.     else if (logger->enabled_detail()) { \ 
  96.         logger->log_detail(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \ 
  97.     } \ 
  98. while(false
  99.  
  100. #define __MYLOG_DEBUG(logger, module_name, format, ...) \ 
  101. do { \ 
  102.     if (NULL == logger) { \ 
  103.         printf(PRINT_COLOR_DARY_GRAY"[DEBUG][%s:%d]"PRINT_COLOR_NONE, __FILE__, __LINE__); \ 
  104.         printf(format, ##__VA_ARGS__); \ 
  105.     } \ 
  106.     else if (logger->enabled_debug()) { \ 
  107.         logger->log_debug(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \ 
  108.     } \ 
  109. while(false
  110.  
  111. #define __MYLOG_INFO(logger, module_name, format, ...) \ 
  112. do { \ 
  113.     if (NULL == logger) { \ 
  114.         printf("[INFO][%s:%d]", __FILE__, __LINE__); \ 
  115.         printf(format, ##__VA_ARGS__); \ 
  116.     } \ 
  117.     else if (logger->enabled_info()) { \ 
  118.         logger->log_info(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \ 
  119.     } \ 
  120. while(false
  121.  
  122. #define __MYLOG_WARN(logger, module_name, format, ...) \ 
  123. do { \ 
  124.     if (NULL == logger) { \ 
  125.         printf(PRINT_COLOR_YELLOW"[WARN][%s:%d]"PRINT_COLOR_NONE, __FILE__, __LINE__); \ 
  126.         printf(format, ##__VA_ARGS__); \ 
  127.     } \ 
  128.     else if (logger->enabled_warn()) { \ 
  129.         logger->log_warn(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \ 
  130.     } \ 
  131. while(false
  132.  
  133. #define __MYLOG_ERROR(logger, module_name, format, ...) \ 
  134. do { \ 
  135.     if (NULL == logger) { \ 
  136.         printf(PRINT_COLOR_RED"[ERROR][%s:%d]"PRINT_COLOR_NONE, __FILE__, __LINE__); \ 
  137.         printf(format, ##__VA_ARGS__); \ 
  138.     } \ 
  139.     else if (logger->enabled_error()) { \ 
  140.         logger->log_error(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \ 
  141.     } \ 
  142. while(false
  143.  
  144. #define __MYLOG_FATAL(logger, module_name, format, ...) \ 
  145. do { \ 
  146.     if (NULL == logger) { \ 
  147.         printf(PRINT_COLOR_BROWN"[FATAL][%s:%d]"PRINT_COLOR_NONE, __FILE__, __LINE__); \ 
  148.         printf(format, ##__VA_ARGS__); \ 
  149.     } \ 
  150.     else if (logger->enabled_fatal()) { \ 
  151.         logger->log_fatal(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \ 
  152.     } \ 
  153. while(false
  154.  
  155. #define __MYLOG_STATE(logger, module_name, format, ...) \ 
  156. do { \ 
  157.     if (NULL == logger) { \ 
  158.         printf("[STATE][%s:%d]", __FILE__, __LINE__); \ 
  159.         printf(format, ##__VA_ARGS__); \ 
  160.     } \ 
  161.     else if (logger->enabled_state()) { \ 
  162.         logger->log_state(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \ 
  163.     } \ 
  164. while(false
  165.  
  166. #define __MYLOG_TRACE(logger, module_name, format, ...) \ 
  167. do { \ 
  168.     if (NULL == logger) { \ 
  169.         printf("[TRACE][%s:%d]", __FILE__, __LINE__); \ 
  170.         printf(format, ##__VA_ARGS__); \ 
  171.     } \ 
  172.     else if (logger->enabled_trace()) { \ 
  173.         logger->log_trace(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \ 
  174.     } \ 
  175. while(false
  176.  
  177. #define __MYLOG_BIN(logger, module_name, log, size) \ 
  178. do { \ 
  179.     if ((logger != NULL) && logger->enabled_bin()) \ 
  180.         logger->bin_log(__FILE__, __LINE__, module_name, log, size); \ 
  181. while(false
  182.  
  183. #define MYLOG_BIN(log, size)         __MYLOG_BIN(sys::g_logger, log, size) 
  184. #define MYLOG_TRACE(format, ...)     __MYLOG_TRACE(sys::g_logger, NULL, format, ##__VA_ARGS__) 
  185. #define MYLOG_STATE(format, ...)     __MYLOG_STATE(sys::g_logger, NULL, format, ##__VA_ARGS__) 
  186. #define MYLOG_FATAL(format, ...)     __MYLOG_FATAL(sys::g_logger, NULL, format, ##__VA_ARGS__) 
  187. #define MYLOG_ERROR(format, ...)     __MYLOG_ERROR(sys::g_logger, NULL, format, ##__VA_ARGS__) 
  188. #define MYLOG_WARN(format, ...)      __MYLOG_WARN(sys::g_logger, NULL, format, ##__VA_ARGS__) 
  189. #define MYLOG_INFO(format, ...)      __MYLOG_INFO(sys::g_logger, NULL, format, ##__VA_ARGS__) 
  190. #define MYLOG_DEBUG(format, ...)     __MYLOG_DEBUG(sys::g_logger, NULL, format, ##__VA_ARGS__) 
  191. #define MYLOG_DETAIL(format, ...)    __MYLOG_DETAIL(sys::g_logger, NULL, format, ##__VA_ARGS__) 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章