Update all position tracking calls in lex_parser.c to use the scc_pos_* function family (scc_pos_next, scc_pos_next_line) instead of the deprecated core_pos_* variants. This ensures consistency with the scc naming convention and prepares for future removal of the old core functions.
199 lines
8.2 KiB
C
199 lines
8.2 KiB
C
/**
|
||
* @file log.h
|
||
* @brief 日志系统核心模块(支持多级日志、断言和异常处理)
|
||
*/
|
||
|
||
#ifndef __SCC_LOG_H__
|
||
#define __SCC_LOG_H__
|
||
|
||
#include "color.h"
|
||
|
||
#ifdef __SCC_LOG_USE_STD_IMPL__
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#define log_snprintf snprintf
|
||
#define log_printf printf
|
||
#define log_exit exit
|
||
#endif
|
||
|
||
#ifdef __GNUC__ // GCC, Clang, ICC
|
||
#define __smcc_log_unreachable() (__builtin_unreachable())
|
||
#elif defined _MSC_VER // MSVC
|
||
#define __smcc_log_unreachable() (__assume(false))
|
||
#elif defined __SMCC_BUILT_IN__ // The SMCC (my compiler)
|
||
#define __smcc_log_unreachable() (__smcc_builtin_unreachable())
|
||
#else
|
||
#define __smcc_log_unreachable()
|
||
#endif
|
||
|
||
#ifndef log_snprintf
|
||
#define log_snprintf(...)
|
||
#warning "log_snprintf not defined"
|
||
#endif
|
||
|
||
#ifndef log_printf
|
||
#define log_printf(...)
|
||
#warning "log_printf not defined"
|
||
#endif
|
||
|
||
#ifndef log_exit
|
||
#define log_exit(...)
|
||
#warning "log_exit not defined"
|
||
#endif
|
||
|
||
/**
|
||
* @brief 日志级别枚举
|
||
*
|
||
* 定义日志系统的输出级别和组合标志位
|
||
*/
|
||
typedef enum log_level {
|
||
LOG_LEVEL_NOTSET = 0, ///< 未设置级别(继承默认配置)
|
||
LOG_LEVEL_DEBUG = 1 << 0, ///< 调试信息(开发阶段详细信息)
|
||
LOG_LEVEL_INFO = 1 << 1, ///< 常规信息(系统运行状态)
|
||
LOG_LEVEL_WARN = 1 << 2, ///< 警告信息(潜在问题提示)
|
||
LOG_LEVEL_ERROR = 1 << 3, ///< 错误信息(可恢复的错误)
|
||
LOG_LEVEL_FATAL = 1 << 4, ///< 致命错误(导致程序终止的严重错误)
|
||
LOG_LEVEL_TRACE = 1 << 5, ///< 追踪(性能追踪或者栈帧追踪)
|
||
LOG_LEVEL_ALL = 0xFF, ///< 全级别标志(组合所有日志级别)
|
||
} log_level_t;
|
||
|
||
/**
|
||
* @brief 日志处理回调函数类型
|
||
* @param level 日志级别
|
||
* @param module 模块名称(可为NULL)
|
||
* @param file 源文件名
|
||
* @param line 代码行号
|
||
* @param message 格式化后的日志消息
|
||
* @todo 待实现模块名称,输入的模块名称,都将被忽略
|
||
*/
|
||
typedef void (*log_handler)(log_level_t level, const char *module,
|
||
const char *file, int line, const char *message);
|
||
|
||
#ifndef LOGGER_MAX_BUF_SIZE
|
||
#define LOGGER_MAX_BUF_SIZE 512 ///< 单条日志最大缓冲区尺寸
|
||
#endif
|
||
|
||
/**
|
||
* @brief 日志器实例结构体
|
||
*
|
||
* 每个日志器实例维护独立的配置和缓冲区
|
||
*/
|
||
typedef struct logger {
|
||
const char *name; ///< 日志器名称(用于模块区分)
|
||
log_level_t level; ///< 当前设置的日志级别
|
||
log_handler handler; ///< 日志处理回调函数
|
||
char buf[LOGGER_MAX_BUF_SIZE]; ///< 格式化缓冲区
|
||
} logger_t;
|
||
|
||
void log_default_handler(log_level_t level, const char *module,
|
||
const char *file, int line, const char *message);
|
||
extern logger_t __default_logger_root;
|
||
|
||
/**
|
||
* @brief 初始化日志实例 其余参数设置为默认值
|
||
* @param[in] logger 日志器实例指针
|
||
* @param[in] name 日志器名称(NULL表示获取默认日志器名称)
|
||
*/
|
||
void init_logger(logger_t *logger, const char *name);
|
||
|
||
/**
|
||
* @brief 设置日志级别
|
||
* @param[in] logger 目标日志器实例
|
||
* @param[in] level 要设置的日志级别(可组合多个级别)
|
||
*/
|
||
void log_set_level(logger_t *logger, int level);
|
||
|
||
/**
|
||
* @brief 设置自定义日志处理器
|
||
* @param[in] logger 目标日志器实例
|
||
* @param[in] handler 自定义处理函数(NULL恢复默认处理)
|
||
*/
|
||
void log_set_handler(logger_t *logger, log_handler handler);
|
||
|
||
#ifndef LOG_MAX_MAROC_BUF_SIZE
|
||
#define LOG_MAX_MAROC_BUF_SIZE LOGGER_MAX_BUF_SIZE ///< 宏展开缓冲区尺寸
|
||
#endif
|
||
|
||
/**
|
||
* @def _LOG
|
||
* @brief 内部日志宏(供其他日志宏调用)
|
||
* @param _module_ 模块实例(NULL表示使用默认日志器)
|
||
* @param _level_ 日志级别
|
||
* @param _msg_ 格式字符串
|
||
* @param ... 可变参数列表
|
||
*/
|
||
#define _LOG(_module_, _level_, _msg_, ...) \
|
||
do { \
|
||
logger_t *_logger = _module_; \
|
||
if (_logger && _logger->handler && (_logger->level & (_level_))) { \
|
||
log_snprintf(_logger->buf, sizeof(_logger->buf), (_msg_), \
|
||
##__VA_ARGS__); \
|
||
_logger->handler((_level_), _logger->name, __FILE__, __LINE__, \
|
||
_logger->buf); \
|
||
} \
|
||
} while (0)
|
||
|
||
/* clang-format off */
|
||
/// @name 模块日志宏
|
||
/// @{
|
||
#define MLOG_NOTSET(module, ...)_LOG(module, LOG_LEVEL_NOTSET, __VA_ARGS__) ///< 未分类日志
|
||
#define MLOG_DEBUG(module, ...) _LOG(module, LOG_LEVEL_DEBUG, __VA_ARGS__) ///< 调试日志(需启用DEBUG级别)
|
||
#define MLOG_INFO(module, ...) _LOG(module, LOG_LEVEL_INFO, __VA_ARGS__) ///< 信息日志(常规运行日志)
|
||
#define MLOG_WARN(module, ...) _LOG(module, LOG_LEVEL_WARN, __VA_ARGS__) ///< 警告日志(潜在问题)
|
||
#define MLOG_ERROR(module, ...) _LOG(module, LOG_LEVEL_ERROR, __VA_ARGS__) ///< 错误日志(可恢复错误)
|
||
#define MLOG_FATAL(module, ...) _LOG(module, LOG_LEVEL_FATAL, __VA_ARGS__) ///< 致命错误日志(程序终止前)
|
||
#define MLOG_TRACE(module, ...) _LOG(module, LOG_LEVEL_TRACE, __VA_ARGS__) ///< 追踪日志(调用栈跟踪)
|
||
/// @}
|
||
|
||
/// @name 快捷日志宏
|
||
/// @{
|
||
#define LOG_NOTSET(...) _LOG(&__default_logger_root, LOG_LEVEL_NOTSET, __VA_ARGS__) ///< 未分类日志
|
||
#define LOG_DEBUG(...) _LOG(&__default_logger_root, LOG_LEVEL_DEBUG, __VA_ARGS__) ///< 调试日志(需启用DEBUG级别)
|
||
#define LOG_INFO(...) _LOG(&__default_logger_root, LOG_LEVEL_INFO, __VA_ARGS__) ///< 信息日志(常规运行日志)
|
||
#define LOG_WARN(...) _LOG(&__default_logger_root, LOG_LEVEL_WARN, __VA_ARGS__) ///< 警告日志(潜在问题)
|
||
#define LOG_ERROR(...) _LOG(&__default_logger_root, LOG_LEVEL_ERROR, __VA_ARGS__) ///< 错误日志(可恢复错误)
|
||
#define LOG_FATAL(...) _LOG(&__default_logger_root, LOG_LEVEL_FATAL, __VA_ARGS__) ///< 致命错误日志(程序终止前)
|
||
#define LOG_TRACE(...) _LOG(&__default_logger_root, LOG_LEVEL_TRACE, __VA_ARGS__) ///< 追踪日志(调用栈跟踪)
|
||
/// @}
|
||
/* clang-format on */
|
||
|
||
/**
|
||
* @def _Assert
|
||
* @brief 断言检查内部宏
|
||
* @param cond 检查条件表达式
|
||
* @param ... 错误信息参数(格式字符串+参数)
|
||
*/
|
||
#define _Assert(cond, ...) \
|
||
do { \
|
||
if (!(cond)) { \
|
||
LOG_FATAL(__VA_ARGS__); \
|
||
log_exit(1); \
|
||
__smcc_log_unreachable(); \
|
||
} \
|
||
} while (0)
|
||
|
||
/// @name 断言工具宏
|
||
/// @{
|
||
#define __INNER_LOG_STR(str) #str
|
||
#define __LOG_STR(str) __INNER_LOG_STR(str)
|
||
#define AssertFmt(cond, format, ...) \
|
||
_Assert(cond, "Assertion Failure: " format, \
|
||
##__VA_ARGS__) ///< 带格式的断言检查
|
||
#define PanicFmt(format, ...) \
|
||
_Assert(0, "Panic: " format, ##__VA_ARGS__) ///< 立即触发致命错误
|
||
#define Assert(cond) \
|
||
AssertFmt(cond, "cond is `" __LOG_STR(cond) "`") ///< 基础断言检查
|
||
#define Panic(...) PanicFmt(__VA_ARGS__) ///< 触发致命错误(带自定义消息)
|
||
#define TODO() \
|
||
PanicFmt("TODO please implement me") ///< 标记未实现代码(触发致命错误)
|
||
#define UNREACHABLE() PanicFmt("UNREACHABLE") ///< 触发致命错误(代码不可达)
|
||
#define FIXME(str) \
|
||
PanicFmt("FIXME " __LOG_STR(str)) ///< 提醒开发者修改代码(触发致命错误)
|
||
/// @}
|
||
|
||
#ifdef __SCC_LOG_IMPORT_SRC__
|
||
#include "log.c"
|
||
#endif
|
||
|
||
#endif /* __SCC_LOG_H__ */
|