diff --git a/server/config.conf_example.ini b/server/config.conf_example.ini index 39f5a7bffb53b4047c39e2f2480ed64a1712cecf..bcab2e4e55d8a1b9acb496734f1be40ab1fea787 100644 --- a/server/config.conf_example.ini +++ b/server/config.conf_example.ini @@ -18,6 +18,10 @@ mysql_charset=utf8 # 级别 debug < info < warning < error # log_output_level 控制日志输出的最小级别,只有大于等于该级别的日志才会输出 log_output_level=debug +# 控制日志输出文件是否隔离,ture为隔离,false为不隔离 +log_output_isolate=true +# 控制日志输出是否携带操作线程信息,true为携带,false为不携带 +log_output_thread=false [jwt_token] # 不设置token_key时,会自动生成一个随机的token_key diff --git a/server/log.c b/server/log.c index 0c7c8cec686bd7d1f714d42d3340513f2955d956..ec82437fb924fb2b17f681892e9e4565c70f0e58 100644 --- a/server/log.c +++ b/server/log.c @@ -1,29 +1,66 @@ #include "header.h" -int log_fd = -1; +int log_fd[5] = {-1, -1, -1, -1, -1}; +int log_isolate = -1; +int log_thread = -1; char curr_date[11]; int output_level = -1; -void update_log_info(const char *now_date); +int update_log_info(const char *now_date, int level); void print_log(int level, const char *format, ...); void get_now_date(char *date); -int get_log_fd(); +int get_log_fd(int level); int str_to_lever(char *set_lever); -int get_log_fd() +int get_log_isolate() +{ + if (log_isolate != -1) + { + return log_isolate; + } + printf("init log isolate\n"); + // LOG(LOG_LEVEL_DEBUG, "init log isolate"); + char *isloate = read_config("log_output_isolate"); + if (isloate == NULL) + { + printf("log_output_isolate is not set, use default value: 0\n"); + // LOG(LOG_LEVEL_WARNING, "log_output_isolate is not set, use default value: 0"); + log_isolate = 0; + } + else + { + printf("log_output_isolate: %s\n", isloate); + log_isolate = strcmp(isloate, "true") == 0 ? 1 : 0; + printf("log_isolate: %d\n", log_isolate); + // LOG(LOG_LEVEL_DEBUG, "log_output_isolate: %s", isloate); + free(isloate); + } + return log_isolate; +} + +int get_curr_fd(int level) +{ + int is_isloate = get_log_isolate(); + int log_idx = 0; + if (is_isloate == 1) + log_idx = level + 1; + return log_fd[log_idx]; +} + +int get_log_fd(int level) { // 获取当前日期 char now_date[11]; get_now_date(now_date); - + int curr_fd = get_curr_fd(level); // 初始化文件描述符 - if (log_fd == -1 || (strncmp(curr_date, now_date, 10) != 0)) + if (curr_fd == -1 || (strncmp(curr_date, now_date, 10) != 0)) { // 更新 log_fd 和 curr_date - update_log_info(now_date); - return log_fd; + curr_fd = update_log_info(now_date, level); + return curr_fd; } - return log_fd; + return curr_fd; } void get_now_date(char *date) @@ -42,17 +79,62 @@ void get_now_date(char *date) sprintf(date, "%d-%02d-%02d", year, month, day); // 格式化日期 } -void update_log_info(const char *now_date) +int update_log_info(const char *now_date, int level) { - if (log_fd != -1) + int curr_log_fd = get_curr_fd(level); + if (curr_log_fd != -1) { - close(log_fd); + close(curr_log_fd); } char log_path[128] = {0}; + + // 获取是否分离日志 + int is_isloate = get_log_isolate(); + // 获取日志文件路径 - sprintf(log_path, "%s/%s.log", LOG_PATH, now_date); - log_fd = open(log_path, O_CREAT | O_RDWR | O_APPEND, 0666); + if (is_isloate == 1) + { + char level_str[20] = {0}; + level_to_str(level, level_str); + sprintf(log_path, "%s/%s_%s.log", LOG_PATH, now_date, level_str); + } + else + sprintf(log_path, "%s/%s.log", LOG_PATH, now_date); + curr_log_fd = open(log_path, O_CREAT | O_RDWR | O_APPEND, 0666); + + // 更新本地缓存的文件描述符和日期 + if (is_isloate == 1) + { + log_fd[level + 1] = curr_log_fd; + } + else + { + log_fd[0] = curr_log_fd; + } + + // 更新日期 strcpy(curr_date, now_date); + return curr_log_fd; +} + +int get_thread_conf() +{ + if (log_thread != -1) + { + return log_thread; + } + char *thread = read_config("log_output_thread"); + if (thread == NULL) + { + printf("log_output_thread is not set, use default value: 0\n"); + log_thread = 0; + } + else + { + log_thread = strcmp(thread, "true") == 0 ? 1 : 0; + free(thread); + } + return log_thread; } void print_log(int level, const char *format, ...) @@ -62,13 +144,23 @@ void print_log(int level, const char *format, ...) char buf[1024] = {0}; vsprintf(buf, format, args); va_end(args); // 结束参数列表 + + if (get_thread_conf() == 1) + { + char buf_thread[20] = {0}; + sprintf(buf_thread, " [tid: %ld] ", pthread_self() % 10000); + strcat(buf, buf_thread); + } + if (level == LOG_LEVEL_ERROR) { - strcat(buf, ":"); + strcat(buf, "[errmsg:"); strcat(buf, strerror(errno)); + strcat(buf, "]"); } + strcat(buf, "\n"); - int fd = get_log_fd(); + int fd = get_log_fd(level); write(fd, buf, strlen(buf)); }