#include static int user_log_handler(logger_t *module, log_level_t level, const char *file, int line, const char *func, const char *fmt, ...) { /* clang-format off */ (void) module, (void)file, (void)line, (void)func; // 不再使用 const char *level_str = null; switch (level) { case LOG_LEVEL_DEBUG: level_str = "DEBUG"; break; case LOG_LEVEL_INFO: level_str = "INFO "; break; case LOG_LEVEL_WARN: level_str = "WARN "; break; case LOG_LEVEL_ERROR: level_str = "ERROR"; break; case LOG_LEVEL_FATAL: level_str = "FATAL"; break; case LOG_LEVEL_TRACE: level_str = "TRACE"; break; default: level_str = "NOTSET"; break; } /// @note: 定义 __LOG_NO_COLOR__ 会取消颜色输出 #ifndef __LOG_NO_COLOR__ const char *color_code; switch (level) { case LOG_LEVEL_DEBUG: color_code = ANSI_FG_CYAN; break; case LOG_LEVEL_INFO: color_code = ANSI_FG_GREEN; break; case LOG_LEVEL_TRACE: color_code = ANSI_FG_BLUE; break; case LOG_LEVEL_WARN: color_code = ANSI_FG_YELLOW; break; case LOG_LEVEL_ERROR: color_code = ANSI_FG_RED; break; case LOG_LEVEL_FATAL: color_code = ANSI_FG_RED ANSI_UNDERLINED; break; default: color_code = ANSI_NONE; } #endif /* clang-format on */ char buf[LOGGER_MAX_BUF_SIZE]; int off = snprintf_(buf, sizeof(buf), "%s[%s]%s ", color_code ? color_code : "", level_str, color_code ? ANSI_NONE : ""); va_list args; va_start(args, fmt); vsnprintf_(buf + off, sizeof(buf) - off, fmt, args); va_end(args); log_puts(buf); log_puts("\n"); if (level == LOG_LEVEL_FATAL) log_exit(1); return 0; } logger_t __scc_usr_log = { .name = "user", .handler = user_log_handler, .level = LOG_LEVEL_ALL, };