添加 格式化输出 颜色输出 日志等级输出 使用说明

This commit is contained in:
2025-12-17 02:33:27 +08:00
commit eea80be1c7
4 changed files with 459 additions and 0 deletions

24
log.c Normal file
View File

@@ -0,0 +1,24 @@
#include "log.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
// 获取颜色代码
const char* get_color_code(const char* color_name) {
if (strcmp(color_name, "red") == 0) return COLOR_RED;
if (strcmp(color_name, "green") == 0) return COLOR_GREEN;
if (strcmp(color_name, "yellow") == 0) return COLOR_YELLOW;
if (strcmp(color_name, "blue") == 0) return COLOR_BLUE;
if (strcmp(color_name, "magenta") == 0) return COLOR_MAGENTA;
if (strcmp(color_name, "cyan") == 0) return COLOR_CYAN;
if (strcmp(color_name, "white") == 0) return COLOR_WHITE;
if (strcmp(color_name, "gray") == 0) return COLOR_GRAY;
return COLOR_RESET;
}
// 获取当前时间字符串
void get_time_string(char* buffer, size_t size) {
time_t now = time(NULL);
struct tm* t = localtime(&now);
strftime(buffer, size, "%Y-%m-%d %H:%M:%S", t);
}