HAL_Project/Project -APP-V1.0/Hardware/LOG/log.c

90 lines
2.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "log.h"
static LogLevel current_level = LOG_DEBUG;
void log_set_level(LogLevel level){
current_level = level;
}
// 内部公共函数:处理日志级别检查和实际打印
static void _log_print(LogLevel level, const char *tag, const char *fmt, va_list args) {
if (current_level <= level) {
u1_printf("[%s] ", tag); // 打印日志标签(如 DEBUG, INFO
u1_vprintf(fmt, args); // 打印用户传入的格式化内容
u1_printf("\r\n"); // 换行
}
}
// 各日志级别的函数(只需调用 _log_print
void log_debug(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
_log_print(LOG_DEBUG, "DEBUG", fmt, args);
va_end(args);
}
void log_info(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
_log_print(LOG_INFO, "INFO", fmt, args);
va_end(args);
}
void log_warn(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
_log_print(LOG_WARN, "WARN", fmt, args);
va_end(args);
}
void log_error(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
_log_print(LOG_ERROR, "ERROR", fmt, args);
va_end(args);
}
//void log_debug(const char *fmt, ...){
// if(current_level <= LOG_DEBUG) {
// va_list args;
// va_start(args, fmt);
// u1_printf("[DEBUG]");
// u1_vprintf(fmt, args); // 添加一个支持可变参数的打印函数
// u1_printf("\r\n");
// va_end(args);
// }
//}
//void log_info(const char *fmt, ...){
// if(current_level <= LOG_INFO) {
// va_list args;
// va_start(args, fmt);
// u1_printf("[INFO]");
// u1_vprintf(fmt, args); // 添加一个支持可变参数的打印函数
// u1_printf("\r\n");
// va_end(args);
// }
//}
//void log_warn(const char *fmt, ...) {
// if(current_level <= LOG_WARN) {
// va_list args;
// va_start(args, fmt);
// u1_printf("[WARN]");
// u1_vprintf(fmt, args); // 添加一个支持可变参数的打印函数
// u1_printf("\r\n");
// va_end(args);
// }
//}
//void log_error(const char *fmt, ...) {
// if(current_level <= LOG_ERROR) {
// va_list args;
// va_start(args, fmt);
// u1_printf("[ERROR]");
// u1_vprintf(fmt, args);
// u1_printf("\r\n");
// va_end(args);
// }
//}