feat(lexer): 添加SCC语言扩展关键字支持并重构标准定义
- 将SCC_CEXT_ASM重命名为SCC_CEXT_SCC以更好地反映扩展类型 - 为asm关键字添加SCC_CEXT_SCC标准标识 - 新增atomic、auto、bool、complex四个关键字及其对应的token类型 - 所有新关键字都标记为SCC_CEXT_SCC扩展标准 fix(lexer): 修复数字字面量解析中的类型不匹配问题 - 将token->value.n更正为token->value.u以正确存储解析结果 - 统一了词法分析器中数值解析的字段访问 refactor(log): 重构日志系统API设计并增强功能 - 将日志处理器接口从void返回改为int返回类型 - 新增函数名记录功能,通过__func__宏获取当前函数名 - 引入可变参数支持,允许格式化字符串传递 - 重构内部宏实现,使用SCC_LOG_IMPL统一处理逻辑 - 改进缓冲区管理,优化日志消息格式化流程 - 重命名头文件保护宏从__SCC_LOG_H__到__SCC_LOG_IMPL_H__ - 替换snprintf为vsnprintf以支持可变参数处理 - 更新断言宏实现,提供更清晰的错误信息格式
This commit is contained in:
@@ -1,63 +1,60 @@
|
||||
#include <log.h>
|
||||
|
||||
void log_default_handler(log_level_t level, const char *module,
|
||||
const char *file, int line, const char *message) {
|
||||
const char *level_str;
|
||||
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;
|
||||
}
|
||||
static inline int log_snprintf(char *s, size_t n, const char *format, ...) {
|
||||
int ret;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
ret = log_vsnprintf(s, n, format, args);
|
||||
va_end(args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// @note: 定义 __LOG_NO_COLOR__ 会取消颜色输出
|
||||
#ifndef __LOG_NO_COLOR__
|
||||
int log_default_handler(logger_t *module, log_level_t level, const char *file,
|
||||
int line, const char *func, const char *fmt, ...) {
|
||||
const char *level_str;
|
||||
int offset = 0;
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
/* clang-format off */
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
log_printf(ANSI_BOLD "%s[%s] - %s - %s:%d | %s" ANSI_NONE "\n", color_code,
|
||||
level_str, module, file, line, message);
|
||||
/* clang-format on */
|
||||
offset =
|
||||
log_snprintf(module->buf, sizeof(module->buf),
|
||||
ANSI_BOLD "%s[%s] %s - %s:%d in %s()" ANSI_NONE " ",
|
||||
color_code, level_str, module->name, file, line, func);
|
||||
#else
|
||||
log_printf("[%s] %s:%d | %s: %s\n", level_str, file, line, module, message);
|
||||
offset = log_snprintf(module->buf, sizeof(module->buf),
|
||||
"[%s] %s - %s:%d in %s() ", level_str, module->name,
|
||||
file, line, func);
|
||||
#endif
|
||||
/* 然后写入用户消息(如果有) */
|
||||
if (fmt && fmt[0]) {
|
||||
log_vsnprintf(module->buf + offset, sizeof(module->buf) - offset, fmt,
|
||||
args);
|
||||
}
|
||||
va_end(args);
|
||||
log_puts(module->buf);
|
||||
log_puts("\n");
|
||||
// for clangd warning
|
||||
// clang-analyzer-deadcode.DeadStores
|
||||
(void)color_code;
|
||||
@@ -65,6 +62,7 @@ void log_default_handler(log_level_t level, const char *module,
|
||||
if (level & LOG_LEVEL_FATAL) {
|
||||
log_exit(-LOG_LEVEL_FATAL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
logger_t __default_logger_root = {
|
||||
|
||||
Reference in New Issue
Block a user