在原有的基础上增加了module_name,表示模块名,当一个系统由多模块组成时,输出的日志将更加清晰,使用方法,以MOOON-agent中的为例:
-
-
-
- enum
- {
- LOG_LINE_SIZE_MIN = 256,
- LOG_LINE_SIZE_MAX = 32768,
- DEFAULT_LOG_FILE_SIZE = 104857600,
- DEFAULT_LOG_FILE_BACKUP_NUMBER = 10
- };
-
-
- typedef enum
- {
- LOG_LEVEL_DETAIL = 0,
- LOG_LEVEL_DEBUG = 1,
- LOG_LEVEL_INFO = 2,
- LOG_LEVEL_WARN = 3,
- LOG_LEVEL_ERROR = 4,
- LOG_LEVEL_FATAL = 5,
- LOG_LEVEL_STATE = 6,
- LOG_LEVEL_TRACE = 7
- }log_level_t;
-
-
- extern log_level_t get_log_level(const char* level_name);
-
- extern const char* get_log_level_name(log_level_t log_level);
-
-
-
-
- class ILogger
- {
- public:
-
- virtual ~ILogger() {}
-
-
- virtual void enable_screen(bool enabled) {}
-
- virtual void enable_bin_log(bool enabled) {}
-
- virtual void enable_trace_log(bool enabled) {}
-
- virtual void enable_auto_adddot(bool enabled) {}
-
- virtual void enable_auto_newline(bool enabled) {}
-
- virtual void set_log_level(log_level_t log_level) {}
-
- virtual void set_single_filesize(uint32_t filesize) {}
-
- virtual void set_backup_number(uint16_t backup_number) {}
-
-
- virtual bool enabled_bin() { return false; }
-
- virtual bool enabled_detail() { return false; }
-
- virtual bool enabled_debug() { return false; }
-
- virtual bool enabled_info() { return false; }
-
- virtual bool enabled_warn() { return false; }
-
- virtual bool enabled_error() { return false; }
-
- virtual bool enabled_fatal() { return false; }
-
- virtual bool enabled_state() { return false; }
-
- virtual bool enabled_trace() { return false; }
-
- virtual void log_detail(const char* filename, int lineno, const char* module_name, const char* format, ...) {}
- virtual void log_debug(const char* filename, int lineno, const char* module_name, const char* format, ...) {}
- virtual void log_info(const char* filename, int lineno, const char* module_name, const char* format, ...) {}
- virtual void log_warn(const char* filename, int lineno, const char* module_name, const char* format, ...) {}
- virtual void log_error(const char* filename, int lineno, const char* module_name, const char* format, ...) {}
- virtual void log_fatal(const char* filename, int lineno, const char* module_name, const char* format, ...) {}
- virtual void log_state(const char* filename, int lineno, const char* module_name, const char* format, ...) {}
- virtual void log_trace(const char* filename, int lineno, const char* module_name, const char* format, ...) {}
-
-
- virtual void bin_log(const char* filename, int lineno, const char* module_name, const char* log, uint16_t size) {}
- };
-
-
-
- extern ILogger* g_logger;
-
- #define __MYLOG_DETAIL(logger, module_name, format, ...) \
- do { \
- if (NULL == logger) { \
- printf("[DETAIL][%s:%d]", __FILE__, __LINE__); \
- printf(format, ##__VA_ARGS__); \
- } \
- else if (logger->enabled_detail()) { \
- logger->log_detail(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \
- } \
- } while(false)
-
- #define __MYLOG_DEBUG(logger, module_name, format, ...) \
- do { \
- if (NULL == logger) { \
- printf(PRINT_COLOR_DARY_GRAY"[DEBUG][%s:%d]"PRINT_COLOR_NONE, __FILE__, __LINE__); \
- printf(format, ##__VA_ARGS__); \
- } \
- else if (logger->enabled_debug()) { \
- logger->log_debug(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \
- } \
- } while(false)
-
- #define __MYLOG_INFO(logger, module_name, format, ...) \
- do { \
- if (NULL == logger) { \
- printf("[INFO][%s:%d]", __FILE__, __LINE__); \
- printf(format, ##__VA_ARGS__); \
- } \
- else if (logger->enabled_info()) { \
- logger->log_info(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \
- } \
- } while(false)
-
- #define __MYLOG_WARN(logger, module_name, format, ...) \
- do { \
- if (NULL == logger) { \
- printf(PRINT_COLOR_YELLOW"[WARN][%s:%d]"PRINT_COLOR_NONE, __FILE__, __LINE__); \
- printf(format, ##__VA_ARGS__); \
- } \
- else if (logger->enabled_warn()) { \
- logger->log_warn(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \
- } \
- } while(false)
-
- #define __MYLOG_ERROR(logger, module_name, format, ...) \
- do { \
- if (NULL == logger) { \
- printf(PRINT_COLOR_RED"[ERROR][%s:%d]"PRINT_COLOR_NONE, __FILE__, __LINE__); \
- printf(format, ##__VA_ARGS__); \
- } \
- else if (logger->enabled_error()) { \
- logger->log_error(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \
- } \
- } while(false)
-
- #define __MYLOG_FATAL(logger, module_name, format, ...) \
- do { \
- if (NULL == logger) { \
- printf(PRINT_COLOR_BROWN"[FATAL][%s:%d]"PRINT_COLOR_NONE, __FILE__, __LINE__); \
- printf(format, ##__VA_ARGS__); \
- } \
- else if (logger->enabled_fatal()) { \
- logger->log_fatal(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \
- } \
- } while(false)
-
- #define __MYLOG_STATE(logger, module_name, format, ...) \
- do { \
- if (NULL == logger) { \
- printf("[STATE][%s:%d]", __FILE__, __LINE__); \
- printf(format, ##__VA_ARGS__); \
- } \
- else if (logger->enabled_state()) { \
- logger->log_state(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \
- } \
- } while(false)
-
- #define __MYLOG_TRACE(logger, module_name, format, ...) \
- do { \
- if (NULL == logger) { \
- printf("[TRACE][%s:%d]", __FILE__, __LINE__); \
- printf(format, ##__VA_ARGS__); \
- } \
- else if (logger->enabled_trace()) { \
- logger->log_trace(__FILE__, __LINE__, module_name, format, ##__VA_ARGS__); \
- } \
- } while(false)
-
- #define __MYLOG_BIN(logger, module_name, log, size) \
- do { \
- if ((logger != NULL) && logger->enabled_bin()) \
- logger->bin_log(__FILE__, __LINE__, module_name, log, size); \
- } while(false)
-
- #define MYLOG_BIN(log, size) __MYLOG_BIN(sys::g_logger, log, size)
- #define MYLOG_TRACE(format, ...) __MYLOG_TRACE(sys::g_logger, NULL, format, ##__VA_ARGS__)
- #define MYLOG_STATE(format, ...) __MYLOG_STATE(sys::g_logger, NULL, format, ##__VA_ARGS__)
- #define MYLOG_FATAL(format, ...) __MYLOG_FATAL(sys::g_logger, NULL, format, ##__VA_ARGS__)
- #define MYLOG_ERROR(format, ...) __MYLOG_ERROR(sys::g_logger, NULL, format, ##__VA_ARGS__)
- #define MYLOG_WARN(format, ...) __MYLOG_WARN(sys::g_logger, NULL, format, ##__VA_ARGS__)
- #define MYLOG_INFO(format, ...) __MYLOG_INFO(sys::g_logger, NULL, format, ##__VA_ARGS__)
- #define MYLOG_DEBUG(format, ...) __MYLOG_DEBUG(sys::g_logger, NULL, format, ##__VA_ARGS__)
- #define MYLOG_DETAIL(format, ...) __MYLOG_DETAIL(sys::g_logger, NULL, format, ##__VA_ARGS__)
转载于:https://blog.51cto.com/mooon/941082