include/Log.c

filter: Statementstmt 39.2% 176 / 449 Branchbran 28.2% 66 / 234 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 35.4% 242 / 683 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/XS/Log.pm ↑ Summary Log.xs »
1 /***************************************************************************************
2 * Build MD5 : 8f6jqw6yrm66ePomTN0BQg
3 * Build Time : 2026-09-14 02:16:45
4 * Version : 6.090001
5 * Author : H.Q.Wang
6 ****************************************************************************************/
7 #include "Log.h"
8 #include <stdarg.h>
9 #include <time.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <stdlib.h> // getenv()
13  
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <sys/stat.h>
17 #include <sys/time.h> // 提供 gettimeofday 用于获取毫秒级时间
18 #include <sys/file.h>
19 #include <pthread.h>
20 /***************************************************************************************
21 颜色 前景色 背景色
22 黑色 \033[30m \033[40m
23 红色 \033[31m \033[41m
24 绿色 \033[32m \033[42m
25 黄色 \033[33m \033[43m
26 蓝色 \033[34m \033[44m
27 紫色 \033[35m \033[45m
28 青色 \033[36m \033[46m
29 浅灰 \033[37m \033[47m
30 回退到发行版默认值 \033[39m \033[49m
31 -------------------------------------------------------------------------------
32 颜色 背景色
33 深灰 \033[100m
34 浅红 \033[101m
35 浅绿 \033[102m
36 黄色 \033[103m
37 浅蓝 \033[104m
38 浅紫 \033[105m
39 蓝绿 \033[106m
40 白色 \033[107m
41 ****************************************************************************************/
42  
43 //echo -e "\e[1;43;32m背景色,前景色\e[0m"
44  
45  
46 // 日志模块全局状态
47 static pthread_mutex_t g_log_mutex;
48 static pthread_once_t g_log_mutex_once = PTHREAD_ONCE_INIT;
49 2 static void init_log_mutex(void) {
50 pthread_mutexattr_t attr;
51 2 pthread_mutexattr_init(&attr);
52 2 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
53 2 pthread_mutex_init(&g_log_mutex, &attr);
54 2 pthread_mutexattr_destroy(&attr);
55 2 }
56 22 static void lock_log(void) { pthread_once(&g_log_mutex_once, init_log_mutex); pthread_mutex_lock(&g_log_mutex); }
57 22 static void unlock_log(void) { pthread_mutex_unlock(&g_log_mutex); }
58 static struct {
59 LogOptions options;
60 FILE *log_file;
61 int rep_file;
62
63 char cur_rep_path[256];
64 char cur_rep_dir[256];
65 char cur_rep_day[9];
66 char cur_rep_hour[3];
67 char rep_dir[150];
68 char rep_name[64];
69
70 char cur_log_path[256];
71 char cur_log_dir[256];
72 char cur_log_day[9];
73 char cur_log_hour[3];
74 char log_path[256];
75 char log_name[64];
76 char log_dir[150];
77 char log_ext[16];
78
79 // 控制台颜色定义
80 const char *color_trace;
81 const char *color_debug;
82 const char *color_info;
83 const char *color_warn;
84 const char *color_error;
85 const char *color_fatal;
86 const char *color_reset;
87 const char *color_unkown;
88 } g_config = {
89 .options = {
90 .level = LOG_LEVEL_INFO,
91 .mode = LOG_MODE_CYCLE,
92 .targets = LOG_TARGET_CONSOLE,
93 .use_color = true,
94 .with_rep = false,
95 .show_timestamp = true,
96 .show_log_level = true,
97 .show_file_info = false,
98 .max_file_size = 100*1024*1024, // 100MB
99 .max_files = 10,
100 .flush_immediately = false
101 },
102
103 .cur_rep_path= {0},
104 .cur_rep_day = {0},
105 .cur_rep_dir = {0},
106 .cur_rep_hour= {0},
107
108 .cur_log_path= {0},
109 .cur_log_day = {0},
110 .cur_log_dir = {0},
111 .cur_log_hour= {0},
112
113 .log_name = {0},
114 .log_dir = {0},
115 .log_ext = {0},
116 .log_file = NULL,
117 .rep_file = -1,
118 .color_trace = "\033[36m", // 青色
119 .color_info = "\033[32m", // 绿色
120 .color_warn = "\033[33m", // 黄色
121 .color_error = "\033[31m", // 红色
122 .color_fatal = "\033[35m", // 紫色
123 .color_unkown= "\033[37m", // 灰色
124 .color_debug = "\033[44;37m", // 蓝色,青色
125 .color_reset = "\033[0m" // 重置
126 };
127  
128 // 内部函数声明
129 static void rep_rotate_file(void);
130 static bool rep_open_file(void);
131 static void rep_close_file(void);
132  
133 static void log_rotate_file(void);
134 static bool log_open_file(void);
135 static void log_close_file(void);
136  
137 static void get_current_log_path();
138 static bool parse_filepath(const char* filepath);
139 static const char* log_level_to_string(LogLevel level);
140  
141 // 初始化日志系统
142 1 bool openLog(const char *log_filepath,const LogOptions* config) {
143 1 lock_log();
144 1 ▶ if (log_filepath == NULL || strlen(log_filepath) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (log_filepath == NULL || strlen(log_filepath) == 0) { 1 0 1
if (log_filepath == NULL || strlen(log_filepath) == 0) { 0 1 1
145 0 unlock_log(); return false;
146 }
147
148 1 ▶ if (config == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (config == NULL) { 0 1 1
149 0 unlock_log(); return false;
150 }
151 1 memcpy(&g_config, config, sizeof(LogOptions));
152
153 // 解析文件路径,分离基础路径、程序名和扩展名
154 1 ▶ if (!parse_filepath(log_filepath)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_filepath(log_filepath)) { 0 1 1
155 0 unlock_log(); return false;
156 }
157
158 1 bool l = log_open_file();
159 1 bool r = true;
160 1 ▶ if(g_config.options.with_rep)
Branch: 1 branch
BranchTrueFalseTotal
if(g_config.options.with_rep) 0 1 1
161 {
162 0 r = rep_open_file();
163 }
164 1 unlock_log();
165 1 ▶ return l && r;
Branch: 2 branches
BranchTrueFalseTotal
return l && r; 1 0 1
return l && r; 1 0 1
166 }
167 1 void setLogLevel(int level)
168 {
169 1 lock_log();
170 1 g_config.options.level = level;
171 1 unlock_log();
172 1 }
173 1 void setLogMode(int flag)
174 {
175 1 lock_log();
176 1 g_config.options.mode = flag;
177 1 unlock_log();
178 1 }
179 1 void setLogColor(int flag)
180 {
181 1 lock_log();
182 1 g_config.options.use_color = flag;
183 1 unlock_log();
184 1 }
185 1 void setLogTargets(int flag)
186 {
187 1 lock_log();
188 1 g_config.options.targets = flag;
189 1 unlock_log();
190 1 }
191 // 设置配置项的函数
192 2 bool setLogOptions(const char *key, long val) {
193 2 lock_log();
194 2 ▶ if (!key) { unlock_log(); return false; }
Branch: 1 branch
BranchTrueFalseTotal
if (!key) { unlock_log(); return false; } 0 2 2
195  
196 // 比较键名并设置对应的值
197 2 ▶ if (strcmp(key, "level") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "level") == 0) { 0 2 2
198 0 g_config.options.level = (LogLevel)val;
199 2 ▶ } else if (strcmp(key, "mode") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "mode") == 0) { 0 2 2
200 0 g_config.options.mode = (LogMode)val;
201 2 ▶ } else if (strcmp(key, "targets") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "targets") == 0) { 0 2 2
202 0 g_config.options.targets = (LogTarget)val;
203 2 ▶ } else if (strcmp(key, "use_color") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "use_color") == 0) { 0 2 2
204 0 g_config.options.use_color = (bool)val;
205 2 ▶ } else if (strcmp(key, "show_timestamp") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "show_timestamp") == 0) { 0 2 2
206 0 g_config.options.show_timestamp = (bool)val;
207 2 ▶ } else if (strcmp(key, "show_log_level") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "show_log_level") == 0) { 0 2 2
208 0 g_config.options.show_log_level = (bool)val;
209 2 ▶ } else if (strcmp(key, "show_file_info") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "show_file_info") == 0) { 1 1 2
210 1 g_config.options.show_file_info = (bool)val;
211 1 ▶ } else if (strcmp(key, "max_file_size") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "max_file_size") == 0) { 0 1 1
212 0 g_config.options.max_file_size = val;
213 1 ▶ } else if (strcmp(key, "max_files") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "max_files") == 0) { 0 1 1
214 0 g_config.options.max_files = (int)val;
215 1 ▶ } else if (strcmp(key, "flush_immediately") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strcmp(key, "flush_immediately") == 0) { 0 1 1
216 0 g_config.options.flush_immediately = (bool)val;
217 } else {
218 // 未知的配置项
219 1 unlock_log(); return false;
220 }
221  
222 1 unlock_log(); return true;
223 }
224 1 void closeLog() {
225 1 lock_log();
226 1 flushLog();
227 1 log_close_file();
228 1 rep_close_file();
229 1 unlock_log();
230 1 }
231 8 void flushLog() {
232 8 lock_log();
233 8 ▶ if (g_config.log_file != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.log_file != NULL) { 8 0 8
234 8 fflush(g_config.log_file);
235 }
236 8 fflush(stdout);
237 8 fflush(stderr);
238 8 unlock_log();
239 8 }
240  
241 // 函数定义
242 0 int confirmAction() {
243 char input[10]; // 缓冲区较大,以容纳潜在的额外输入
244
245 0 printf("确认操作请按 'y' 或 'Y',否则直接按回车键以取消: ");
246 0 ▶ if (fgets(input, sizeof(input), stdin) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (fgets(input, sizeof(input), stdin) != NULL) { 0 0 0
247 // 若仅输入回车键,fgets 会捕捉到一个换行符
248 0 ▶ if (input[0] == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (input[0] == '\n') { 0 0 0
249 0 return 0; // 空输入(仅回车)表示取消
250 }
251 // 检查用户输入的第一个字符是否是 'y' 或 'Y'
252 0 ▶ if (input[0] == 'y' || input[0] == 'Y') {
Branch: 2 branches
BranchTrueFalseTotal
if (input[0] == 'y' || input[0] == 'Y') { 0 0 0
if (input[0] == 'y' || input[0] == 'Y') { 0 0 0
253 0 return 1; // 确认操作
254 }
255 }
256 0 return 0; // 任何其他情况都表示取消
257 }
258  
259 7 bool makeDir(const char* dir) {
260 7 ▶ if (dir == NULL || strlen(dir) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (dir == NULL || strlen(dir) == 0) { 7 0 7
if (dir == NULL || strlen(dir) == 0) { 0 7 7
261 0 return false;
262 }
263 char tmp[256];
264 7 char* p = NULL;
265 size_t len;
266
267 // 复制路径到临时变量
268 7 snprintf(tmp, sizeof(tmp), "%s", dir);
269 7 len = strlen(tmp);
270
271 // 去除末尾的斜杠
272 7 ▶ if (tmp[len - 1] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (tmp[len - 1] == '/') { 0 7 7
273 0 tmp[len - 1] = 0;
274 }
275
276 // 逐级创建目录
277 105 ▶ for (p = tmp + 1; *p; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = tmp + 1; *p; p++) { 98 7 105
278 98 ▶ if (*p == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '/') { 7 91 98
279 7 *p = 0; // 临时截断路径
280
281 // 创建当前级别的目录
282 7 ▶ if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
Branch: 2 branches
BranchTrueFalseTotal
if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { 7 0 7
if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { 0 7 7
283 0 return false;
284 }
285
286 7 *p = '/'; // 恢复斜杠
287 }
288 }
289
290 // 创建最终目录
291 7 ▶ if (mkdir(tmp, 0755) != 0 && errno != EEXIST) {
Branch: 2 branches
BranchTrueFalseTotal
if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { 7 0 7
if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { 0 7 7
292 0 return false;
293 }
294
295 7 return true;
296 }
297  
298 0 static bool rep_open_file() {
299  
300 0 ▶ if(!(g_config.options.targets & LOG_TARGET_FILE)) return true;
Branch: 1 branch
BranchTrueFalseTotal
if(!(g_config.options.targets & LOG_TARGET_FILE)) return true; 0 0 0
301
302 // 获取当前日志文件路径并打开
303
304 0 get_current_log_path();
305  
306 0 ▶ if (strlen(g_config.cur_rep_path) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(g_config.cur_rep_path) == 0) { 0 0 0
307 0 return false;
308 }
309 0 ▶ if (!makeDir(g_config.cur_rep_dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!makeDir(g_config.cur_rep_dir)) { 0 0 0
310 0 return false;
311 }
312 0 rep_close_file();
313 //O_APPEND 存在追加 O_TRUNC 存在覆盖
314 0 g_config.rep_file = open(g_config.cur_rep_path, O_WRONLY | O_CREAT | O_APPEND, 0644);
315 0 ▶ if (g_config.rep_file < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.rep_file < 0) { 0 0 0
316 0 printf("Failed to open rep file %s\n",g_config.cur_rep_path);
317 0 return false;
318 }
319
320 0 return true;
321 }
322  
323 1 static void rep_close_file() {
324 1 ▶ if (g_config.rep_file >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.rep_file >= 0) { 0 1 1
325 0 close(g_config.rep_file);
326 0 g_config.rep_file = -1;
327 }
328 1 }
329  
330 7 static bool log_open_file() {
331  
332 7 ▶ if(!(g_config.options.targets & LOG_TARGET_FILE)) return true;
Branch: 1 branch
BranchTrueFalseTotal
if(!(g_config.options.targets & LOG_TARGET_FILE)) return true; 0 7 7
333
334 // 获取当前日志文件路径并打开
335
336 7 get_current_log_path();
337  
338 7 ▶ if (strlen(g_config.cur_log_path) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(g_config.cur_log_path) == 0) { 0 7 7
339 0 return false;
340 }
341 7 ▶ if (!makeDir(g_config.cur_log_dir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!makeDir(g_config.cur_log_dir)) { 0 7 7
342 0 return false;
343 }
344 7 log_close_file();
345 7 g_config.log_file = fopen(g_config.cur_log_path, "a+");
346 7 ▶ if (g_config.log_file == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.log_file == NULL) { 0 7 7
347 0 printf("Failed to open log file %s\n",g_config.cur_log_path);
348 0 return false;
349 }
350
351 7 return true;
352 }
353  
354 14 static void log_close_file() {
355 14 ▶ if (g_config.log_file != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.log_file != NULL) { 7 7 14
356 7 fputs("*************************************\n"
357 "* 安全关闭日志文件\n"
358 "*************************************\n", g_config.log_file);
359 7 fflush(g_config.log_file);
360 7 fclose(g_config.log_file);
361 7 g_config.log_file = NULL;
362 }
363 14 }
364  
365 // 获取当前日志文件路径
366 7 static void get_current_log_path() {
367 7 time_t now = time(NULL);
368 7 struct tm* tm_info = localtime(&now);
369 7 g_config.cur_log_path[0] = '\0';
370  
371 7 ▶ if(g_config.options.with_rep)
Branch: 1 branch
BranchTrueFalseTotal
if(g_config.options.with_rep) 0 7 7
372 {
373 0 g_config.cur_rep_path[0] = '\0';
374 }
375  
376 7 switch (g_config.options.mode) {
377 7 case LOG_MODE_CYCLE:{
378 7 snprintf(g_config.cur_log_path, sizeof(g_config.cur_log_path), "%s/%s.%s",
379 g_config.log_dir, g_config.log_name, g_config.log_ext);
380
381 //snprintf(g_config.cur_log_dir, sizeof(g_config.cur_log_dir), "%s",g_config.log_dir);
382 // 等价以上,推荐用:
383 7 strncpy(g_config.cur_log_dir, g_config.log_dir, sizeof(g_config.cur_log_dir) - 1);
384 7 g_config.cur_log_dir[sizeof(g_config.cur_log_dir) - 1] = '\0'; // 确保字符串以 null 结尾
385
386
387 7 ▶ if(g_config.options.with_rep)
Branch: 1 branch
BranchTrueFalseTotal
if(g_config.options.with_rep) 0 7 7
388 {
389 0 snprintf(g_config.cur_rep_path, sizeof(g_config.cur_rep_path), "%s/%s.rep",
390 g_config.rep_dir, g_config.rep_name);
391 0 snprintf(g_config.cur_rep_dir, sizeof(g_config.cur_rep_dir), "%s",g_config.rep_dir);
392 }
393
394
395 7 break;
396 }
397 0 case LOG_MODE_DAILY: {
398 char date_str[9];
399 0 strftime(date_str, sizeof(date_str), "%Y%m%d", tm_info);
400 0 snprintf(g_config.cur_log_path, sizeof(g_config.cur_log_path), "%s/%s/%s.%s",
401 g_config.log_dir, date_str, g_config.log_name, g_config.log_ext);
402
403 0 snprintf(g_config.cur_log_dir, sizeof(g_config.cur_log_dir), "%s/%s",
404 g_config.log_dir, date_str);
405 //snprintf(g_config.cur_log_day, sizeof(g_config.cur_log_day), date_str);
406 // 等价以上,推荐用:
407 0 strncpy(g_config.cur_log_day, date_str, sizeof(g_config.cur_log_day) - 1);
408 0 g_config.cur_log_day[sizeof(g_config.cur_log_day) - 1] = '\0'; // 确保字符串以 null 结尾
409
410 0 ▶ if(g_config.options.with_rep)
Branch: 1 branch
BranchTrueFalseTotal
if(g_config.options.with_rep) 0 0 0
411 {
412 0 snprintf(g_config.cur_rep_path, sizeof(g_config.cur_rep_path), "%s/%s/%s.rep",
413 g_config.rep_dir, date_str, g_config.rep_name);
414 0 snprintf(g_config.cur_rep_dir, sizeof(g_config.cur_rep_dir), "%s/%s",
415 g_config.rep_dir, date_str);
416
417 //有warning用下面方式赋值
418 //snprintf(g_config.cur_rep_day, sizeof(g_config.cur_rep_day), date_str);
419 0 strncpy(g_config.cur_rep_day, date_str, sizeof(g_config.cur_rep_day) - 1);
420 0 g_config.cur_rep_day[sizeof(g_config.cur_rep_day) - 1] = '\0'; // 确保字符串以 null 结尾
421
422 }
423
424 0 break;
425 }
426
427 0 case LOG_MODE_HOURLY: {
428 char date_str[9];
429 char hour_str[3];
430 0 strftime(date_str, sizeof(date_str), "%Y%m%d", tm_info);
431 0 strftime(hour_str, sizeof(hour_str), "%H", tm_info);
432 0 snprintf(g_config.cur_log_path, sizeof(g_config.cur_log_path), "%s/%s/%s.%s.%s",
433 g_config.log_dir, date_str, g_config.log_name, hour_str, g_config.log_ext);
434
435 0 snprintf(g_config.cur_log_dir, sizeof(g_config.cur_log_dir), "%s/%s",
436 g_config.log_dir, date_str);
437 //snprintf(g_config.cur_log_day, sizeof(g_config.cur_log_day), date_str);
438 //snprintf(g_config.cur_log_hour, sizeof(g_config.cur_log_hour), hour_str);
439
440 // 等价以上,推荐用:
441 0 strncpy(g_config.cur_log_day, date_str, sizeof(g_config.cur_log_day) - 1);
442 0 g_config.cur_log_day[sizeof(g_config.cur_log_day) - 1] = '\0'; // 确保字符串以 null 结尾
443
444 0 strncpy(g_config.cur_log_hour, hour_str, sizeof(g_config.cur_log_hour) - 1);
445 0 g_config.cur_log_hour[sizeof(g_config.cur_log_hour) - 1] = '\0'; // 确保字符串以 null 结尾
446
447
448 0 ▶ if(g_config.options.with_rep)
Branch: 1 branch
BranchTrueFalseTotal
if(g_config.options.with_rep) 0 0 0
449 {
450 0 snprintf(g_config.cur_rep_path, sizeof(g_config.cur_rep_path), "%s/%s/%s.%s.rep",
451 g_config.rep_dir, date_str, g_config.rep_name, hour_str);
452 0 snprintf(g_config.cur_rep_dir, sizeof(g_config.cur_rep_dir), "%s/%s",
453 g_config.rep_dir, date_str);
454
455 //snprintf(g_config.cur_rep_day, sizeof(g_config.cur_rep_day), date_str);
456 //snprintf(g_config.cur_rep_hour, sizeof(g_config.cur_rep_hour), hour_str);
457
458 0 strncpy(g_config.cur_rep_day, date_str, sizeof(g_config.cur_rep_day) - 1);
459 0 g_config.cur_rep_day[sizeof(g_config.cur_rep_day) - 1] = '\0'; // 确保字符串以 null 结尾
460
461 0 strncpy(g_config.cur_rep_hour, hour_str, sizeof(g_config.cur_rep_hour) - 1);
462 0 g_config.cur_rep_hour[sizeof(g_config.cur_rep_hour) - 1] = '\0'; // 确保字符串以 null 结尾
463
464 }
465
466
467 0 break;
468 }
469 }
470 7 }
471  
472 // 解析文件路径,分离基础路径、程序名和扩展名
473 1 static bool parse_filepath(const char* filepath) {
474 1 ▶ if (filepath == NULL || strlen(filepath) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (filepath == NULL || strlen(filepath) == 0) { 1 0 1
if (filepath == NULL || strlen(filepath) == 0) { 0 1 1
475 0 return false;
476 }
477 // 复制文件路径用于处理
478 char temp_path[128];
479 1 strncpy(temp_path, filepath, sizeof(temp_path) - 1);
480 1 temp_path[sizeof(temp_path) - 1] = '\0';
481
482 1 strncpy(g_config.log_path, temp_path, sizeof(g_config.log_path) - 1);
483 1 g_config.log_path[sizeof(g_config.log_path) - 1] = '\0';
484
485 // 获取文件名(包含扩展名)
486 1 char* filename = strrchr(temp_path, '/');
487 1 ▶ if (filename == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (filename == NULL) { 0 1 1
488 0 filename = temp_path;
489 0 ▶ if(g_config.options.with_rep)
Branch: 1 branch
BranchTrueFalseTotal
if(g_config.options.with_rep) 0 0 0
490 {
491 0 strcpy(g_config.rep_dir, ".");
492 0 strcpy(g_config.log_dir, "./LOG");
493 }
494 else
495 {
496 0 strcpy(g_config.log_dir, ".");
497 }
498 } else {
499 1 *filename = '\0';
500 1 filename++;
501 1 ▶ if(g_config.options.with_rep)
Branch: 1 branch
BranchTrueFalseTotal
if(g_config.options.with_rep) 0 1 1
502 {
503 0 strncpy(g_config.rep_dir, temp_path, sizeof(g_config.rep_dir) - 1);
504 0 g_config.rep_dir[sizeof(g_config.rep_dir) - 1] = '\0';
505 0 snprintf(g_config.log_dir, sizeof(g_config.log_dir), "%s/LOG",
506 temp_path);
507 }
508 else
509 {
510 1 strncpy(g_config.log_dir, temp_path, sizeof(g_config.log_dir) - 1);
511 1 g_config.log_dir[sizeof(g_config.log_dir) - 1] = '\0';
512 }
513  
514 }
515 // 分离程序名和扩展名:
516 1 char* dot = strrchr(filename, '.');
517 //带扩展名的情况
518 1 ▶ if (dot != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (dot != NULL) { 1 0 1
519 1 *dot = '\0';
520 1 strncpy(g_config.log_name, filename, sizeof(g_config.log_name) - 1);
521 1 g_config.log_name[sizeof(g_config.log_name) - 1] = '\0';
522 1 strncpy(g_config.log_ext, dot + 1, sizeof(g_config.log_ext) - 1);
523 1 g_config.log_ext[sizeof(g_config.log_ext) - 1] = '\0';
524 //rep日志名称
525 1 ▶ if(g_config.options.with_rep)
Branch: 1 branch
BranchTrueFalseTotal
if(g_config.options.with_rep) 0 1 1
526 {
527 //可能TongSMT.P02.log,需要再次分离文件名称
528 0 dot = strrchr(filename, '.');
529 0 ▶ if (dot != NULL) {*dot = '\0';}
Branch: 1 branch
BranchTrueFalseTotal
if (dot != NULL) {*dot = '\0';} 0 0 0
530 0 strncpy(g_config.rep_name, filename, sizeof(g_config.rep_name) - 1);
531 0 g_config.rep_name[sizeof(g_config.rep_name) - 1] = '\0';
532 }
533 }
534 //不带扩展名的情况
535 else
536 {
537 0 strncpy(g_config.log_name, filename, sizeof(g_config.log_name) - 1);
538 0 g_config.log_name[sizeof(g_config.log_name) - 1] = '\0';
539 0 strcpy(g_config.log_ext, "log"); // 默认扩展名
540 //rep日志名称
541 0 ▶ if(g_config.options.with_rep)
Branch: 1 branch
BranchTrueFalseTotal
if(g_config.options.with_rep) 0 0 0
542 {
543 0 strncpy(g_config.rep_name, filename, sizeof(g_config.rep_name) - 1);
544 0 g_config.rep_name[sizeof(g_config.rep_name) - 1] = '\0';
545 }
546 }
547 1 return true;
548 }
549  
550 0 static const char* log_level_to_string(LogLevel level) {
551 0 switch (level) {
552 0 case LOG_LEVEL_TRACE: return "TRC";
553 0 case LOG_LEVEL_DEBUG: return "BUG";
554 0 case LOG_LEVEL_INFO: return "INF";
555 0 case LOG_LEVEL_WARN: return "WRN";
556 0 case LOG_LEVEL_ERROR: return "ERR";
557 0 case LOG_LEVEL_FATAL: return "FAL";
558 0 default: return "UNK";
559 }
560 }
561 // 判断是否需要备份日志日志文件
562 0 static void rep_rotate_file() {
563 0 ▶ if (g_config.options.max_files <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.options.max_files <= 0) { 0 0 0
564 0 remove(g_config.cur_rep_path);
565 0 return;
566 }
567  
568 // 删除最旧的日志文件
569 char old_name[512];
570 char new_name[512];
571
572 0 snprintf(old_name, sizeof(old_name), "%s/%s.%d.rep",
573 g_config.cur_rep_dir, g_config.rep_name, g_config.options.max_files);
574 0 remove(old_name);
575  
576 // 重命名其他日志文件
577 0 ▶ for (int i = g_config.options.max_files - 1; i >= 1; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = g_config.options.max_files - 1; i >= 1; i--) { 0 0 0
578 0 snprintf(new_name, sizeof(old_name), "%s/%s.%d.rep",
579 g_config.cur_rep_dir, g_config.rep_name,i);
580 0 snprintf(old_name, sizeof(new_name), "%s/%s.%d.rep",
581 g_config.cur_rep_dir, g_config.rep_name,i - 1);
582 0 rename(old_name, new_name);
583 }
584  
585 // 重命名当前日志文件
586 0 snprintf(new_name, sizeof(new_name), "%s/%s.%d.rep",
587 g_config.cur_rep_dir, g_config.rep_name,1);
588 0 rename(g_config.cur_rep_path, new_name);
589 }
590  
591 // 判断是否需要备份日志日志文件
592 6 static void log_rotate_file() {
593 6 ▶ if (g_config.options.max_files <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.options.max_files <= 0) { 6 0 6
594 6 remove(g_config.cur_log_path);
595 6 return;
596 }
597  
598 // 删除最旧的日志文件
599 char old_name[512];
600 char new_name[512];
601
602 0 snprintf(old_name, sizeof(old_name), "%s/%s.%d.%s",
603 g_config.cur_log_dir, g_config.log_name, g_config.options.max_files,g_config.log_ext);
604 0 remove(old_name);
605  
606 // 重命名其他日志文件
607 0 ▶ for (int i = g_config.options.max_files - 1; i >= 1; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = g_config.options.max_files - 1; i >= 1; i--) { 0 0 0
608 0 snprintf(new_name, sizeof(old_name), "%s/%s.%d.%s",
609 g_config.cur_log_dir, g_config.log_name,i,g_config.log_ext);
610 0 snprintf(old_name, sizeof(new_name), "%s/%s.%d.%s",
611 g_config.cur_log_dir, g_config.log_name,i - 1,g_config.log_ext);
612 0 rename(old_name, new_name);
613 }
614  
615 // 重命名当前日志文件
616 0 snprintf(new_name, sizeof(new_name), "%s/%s.%d.%s",
617 g_config.cur_log_dir, g_config.log_name,1,g_config.log_ext);
618 0 rename(g_config.cur_log_path, new_name);
619 }
620  
621 0 void log_print(LogLevel level, const char *file, int line, const char *format, ...) {
622 0 lock_log();
623 0 ▶ if(level == LOG_LEVEL_OFF) { unlock_log(); return;}
Branch: 1 branch
BranchTrueFalseTotal
if(level == LOG_LEVEL_OFF) { unlock_log(); return;} 0 0 0
624 0 ▶ if(level != LOG_LEVEL_TEXT)
Branch: 1 branch
BranchTrueFalseTotal
if(level != LOG_LEVEL_TEXT) 0 0 0
625 {
626 0 ▶ if (level > g_config.options.level) {
Branch: 1 branch
BranchTrueFalseTotal
if (level > g_config.options.level) { 0 0 0
627 0 unlock_log(); return;
628 }
629 }
630 va_list args;
631 char buf[1024];
632
633
634 // 格式化日志头
635 0 int pos = 0;
636
637 time_t now;
638 struct tm *tm_now;
639
640 // 获取当前时间
641 0 time(&now);
642 0 tm_now = localtime(&now);
643
644 0 ▶ if (g_config.options.show_timestamp && level != LOG_LEVEL_TEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_config.options.show_timestamp && level != LOG_LEVEL_TEXT) { 0 0 0
if (g_config.options.show_timestamp && level != LOG_LEVEL_TEXT) { 0 0 0
645  
646 struct timeval tv;
647 0 gettimeofday(&tv, NULL);
648 0 pos += strftime(buf + pos, sizeof(buf) - pos, "[%Y-%m-%d %H:%M:%S", tm_now);
649
650 0 pos += snprintf(buf + pos,sizeof(buf) - pos,".%03ld]", tv.tv_usec / 1000);
651 }
652
653 0 ▶ if (g_config.options.show_log_level && level != LOG_LEVEL_TEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_config.options.show_log_level && level != LOG_LEVEL_TEXT) { 0 0 0
if (g_config.options.show_log_level && level != LOG_LEVEL_TEXT) { 0 0 0
654 0 const char *color = "";
655 0 const char *color_reset = "";
656
657 0 ▶ if (g_config.options.use_color && g_config.options.targets & LOG_TARGET_CONSOLE) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_config.options.use_color && g_config.options.targets & LOG_TARGET_CONSOLE) { 0 0 0
if (g_config.options.use_color && g_config.options.targets & LOG_TARGET_CONSOLE) { 0 0 0
658 0 switch (level) {
659 0 case LOG_LEVEL_TRACE: color = g_config.color_trace; break;
660 0 case LOG_LEVEL_DEBUG: color = g_config.color_debug; break;
661 0 case LOG_LEVEL_INFO: color = g_config.color_info; break;
662 0 case LOG_LEVEL_WARN: color = g_config.color_warn; break;
663 0 case LOG_LEVEL_ERROR: color = g_config.color_error; break;
664 0 case LOG_LEVEL_FATAL: color = g_config.color_fatal; break;
665 0 default: color = ""; break;
666 }
667 0 color_reset = g_config.color_reset;
668 }
669
670 0 pos += snprintf(buf + pos, sizeof(buf) - pos, "[%s%s%s] ",
671 color, log_level_to_string(level), color_reset);
672 }
673
674 0 ▶ if (g_config.options.show_file_info && level != LOG_LEVEL_TEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_config.options.show_file_info && level != LOG_LEVEL_TEXT) { 0 0 0
if (g_config.options.show_file_info && level != LOG_LEVEL_TEXT) { 0 0 0
675 0 pos += snprintf(buf + pos, sizeof(buf) - pos, "(%s:%d) ", file, line);
676 }
677
678 // 格式化用户提供的日志内容
679 0 va_start(args, format);
680 0 pos += vsnprintf(buf + pos, sizeof(buf) - pos, format, args);
681 0 va_end(args);
682
683 // 确保以换行符结尾
684 0 ▶ if (pos >= sizeof(buf) - 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= sizeof(buf) - 2) { 0 0 0
685 0 pos = sizeof(buf) - 2;
686 }
687 0 ▶ if (buf[pos-1] != '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[pos-1] != '\n') { 0 0 0
688 0 buf[pos++] = '\n';
689 0 buf[pos] = '\0';
690 }
691
692 // 输出到控制台
693 0 ▶ if (g_config.options.targets & LOG_TARGET_CONSOLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.options.targets & LOG_TARGET_CONSOLE) { 0 0 0
694 0 ▶ if (level >= LOG_LEVEL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (level >= LOG_LEVEL_ERROR) { 0 0 0
695 0 fputs(buf, stderr);
696 } else {
697 0 fputs(buf, stdout);
698 }
699 }
700 // 输出到文件
701 0 ▶ if ((g_config.options.targets & LOG_TARGET_FILE) && g_config.log_file != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if ((g_config.options.targets & LOG_TARGET_FILE) && g_config.log_file != NULL) { 0 0 0
if ((g_config.options.targets & LOG_TARGET_FILE) && g_config.log_file != NULL) { 0 0 0
702 0 fputs(buf, g_config.log_file);
703
704 0 switch (g_config.options.mode) {
705 0 case LOG_MODE_CYCLE:{
706 0 long size = ftell(g_config.log_file);
707
708 0 ▶ if (size > g_config.options.max_file_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > g_config.options.max_file_size) { 0 0 0
709 0 log_close_file();
710 0 log_rotate_file();
711 0 log_open_file();
712 }
713 0 break;
714 }
715 0 case LOG_MODE_DAILY: {
716 char date_str[9];
717 0 strftime(date_str, sizeof(date_str), "%Y%m%d", tm_now);
718 0 ▶ if (strcmp(date_str, g_config.cur_log_day) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(date_str, g_config.cur_log_day) != 0) { 0 0 0
719 0 log_close_file();
720 0 log_open_file();
721 }
722 0 break;
723 }
724 0 case LOG_MODE_HOURLY: {
725 char hour_str[3];
726 0 strftime(hour_str, sizeof(hour_str), "%H", tm_now);
727 0 ▶ if (strcmp(hour_str, g_config.cur_log_hour) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(hour_str, g_config.cur_log_hour) != 0) { 0 0 0
728 0 log_close_file();
729 0 log_open_file();
730 }
731 0 break;
732 }
733 }
734 }
735
736 // 立即刷新缓冲
737 0 ▶ if (g_config.options.flush_immediately) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.options.flush_immediately) { 0 0 0
738 0 flushLog();
739 }
740
741 // 如果是FATAL级别,终止程序
742 0 ▶ if (level == LOG_LEVEL_FATAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (level == LOG_LEVEL_FATAL) { 0 0 0
743 0 closeLog();
744 0 unlock_log();
745 0 exit(EXIT_FAILURE);
746 }
747 0 unlock_log();
748 }
749  
750 6 void log_write(LogLevel level, const char *file, int line, const char *message) {
751 6 lock_log();
752 6 ▶ if(message == NULL) { unlock_log(); return; }
Branch: 1 branch
BranchTrueFalseTotal
if(message == NULL) { unlock_log(); return; } 0 6 6
753 6 size_t len = strlen(message);
754 6 ▶ if(message == NULL || len == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if(message == NULL || len == 0) return; 6 0 6
if(message == NULL || len == 0) return; 0 6 6
755 6 ▶ if(level == LOG_LEVEL_OFF) { unlock_log(); return;}
Branch: 1 branch
BranchTrueFalseTotal
if(level == LOG_LEVEL_OFF) { unlock_log(); return;} 0 6 6
756 6 ▶ if(level != LOG_LEVEL_TEXT)
Branch: 1 branch
BranchTrueFalseTotal
if(level != LOG_LEVEL_TEXT) 5 1 6
757 {
758 5 ▶ if (level > g_config.options.level) {
Branch: 1 branch
BranchTrueFalseTotal
if (level > g_config.options.level) { 0 5 5
759 0 unlock_log(); return;
760 }
761 }
762 char buf[1024];
763
764 // 格式化日志头
765 6 int pos = 0;
766
767 time_t now;
768 struct tm *tm_now;
769
770 // 获取当前时间
771 6 time(&now);
772 6 tm_now = localtime(&now);
773
774 6 ▶ if (g_config.options.show_timestamp && level != LOG_LEVEL_TEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_config.options.show_timestamp && level != LOG_LEVEL_TEXT) { 0 6 6
if (g_config.options.show_timestamp && level != LOG_LEVEL_TEXT) { 0 0 0
775  
776 struct timeval tv;
777 0 gettimeofday(&tv, NULL);
778 0 pos += strftime(buf + pos, sizeof(buf) - pos, "[%Y-%m-%d %H:%M:%S", tm_now);
779
780 0 pos += snprintf(buf + pos,sizeof(buf) - pos,".%03ld]", tv.tv_usec / 1000);
781 }
782
783 6 ▶ if (g_config.options.show_log_level && level != LOG_LEVEL_TEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_config.options.show_log_level && level != LOG_LEVEL_TEXT) { 0 6 6
if (g_config.options.show_log_level && level != LOG_LEVEL_TEXT) { 0 0 0
784 0 const char *color = "";
785 0 const char *color_reset = "";
786
787 0 ▶ if (g_config.options.use_color && g_config.options.targets & LOG_TARGET_CONSOLE) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_config.options.use_color && g_config.options.targets & LOG_TARGET_CONSOLE) { 0 0 0
if (g_config.options.use_color && g_config.options.targets & LOG_TARGET_CONSOLE) { 0 0 0
788 0 switch (level) {
789 0 case LOG_LEVEL_TRACE: color = g_config.color_trace; break;
790 0 case LOG_LEVEL_DEBUG: color = g_config.color_debug; break;
791 0 case LOG_LEVEL_INFO: color = g_config.color_info; break;
792 0 case LOG_LEVEL_WARN: color = g_config.color_warn; break;
793 0 case LOG_LEVEL_ERROR: color = g_config.color_error; break;
794 0 case LOG_LEVEL_FATAL: color = g_config.color_fatal; break;
795 0 default: color = ""; break;
796 }
797 0 color_reset = g_config.color_reset;
798 }
799
800 0 pos += snprintf(buf + pos, sizeof(buf) - pos, "[%s%s%s] ",
801 color, log_level_to_string(level), color_reset);
802 }
803
804 6 ▶ if (g_config.options.show_file_info && level != LOG_LEVEL_TEXT) {
Branch: 2 branches
BranchTrueFalseTotal
if (g_config.options.show_file_info && level != LOG_LEVEL_TEXT) { 0 6 6
if (g_config.options.show_file_info && level != LOG_LEVEL_TEXT) { 0 0 0
805 0 pos += snprintf(buf + pos, sizeof(buf) - pos, "(%s:%d) ", file, line);
806 }
807
808 6 ▶ if (pos + len < sizeof(buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos + len < sizeof(buf)) { 6 0 6
809 6 memcpy(buf + pos, message, len);
810 6 pos += len;
811 6 buf[pos] = '\0'; // 记得补终止符
812 }
813
814 // 确保以换行符结尾
815 6 ▶ if (pos >= sizeof(buf) - 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= sizeof(buf) - 2) { 0 6 6
816 0 pos = sizeof(buf) - 2;
817 }
818 6 ▶ if (buf[pos-1] != '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (buf[pos-1] != '\n') { 0 6 6
819 0 buf[pos++] = '\n';
820 0 buf[pos] = '\0';
821 }
822
823 // 输出到控制台
824 6 ▶ if (g_config.options.targets & LOG_TARGET_CONSOLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.options.targets & LOG_TARGET_CONSOLE) { 0 6 6
825 0 ▶ if (level >= LOG_LEVEL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if (level >= LOG_LEVEL_ERROR) { 0 0 0
826 0 fputs(buf, stderr);
827 } else {
828 0 fputs(buf, stdout);
829 }
830 }
831 // 输出到文件
832 6 ▶ if ((g_config.options.targets & LOG_TARGET_FILE) && g_config.log_file != NULL) {
Branch: 2 branches
BranchTrueFalseTotal
if ((g_config.options.targets & LOG_TARGET_FILE) && g_config.log_file != NULL) { 6 0 6
if ((g_config.options.targets & LOG_TARGET_FILE) && g_config.log_file != NULL) { 6 0 6
833 6 fputs(buf, g_config.log_file);
834
835 6 switch (g_config.options.mode) {
836 6 case LOG_MODE_CYCLE:{
837 6 long size = ftell(g_config.log_file);
838
839 6 ▶ if (size > g_config.options.max_file_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > g_config.options.max_file_size) { 6 0 6
840 6 log_close_file();
841 6 log_rotate_file();
842 6 log_open_file();
843 }
844 6 break;
845 }
846 0 case LOG_MODE_DAILY: {
847 char date_str[9];
848 0 strftime(date_str, sizeof(date_str), "%Y%m%d", tm_now);
849 0 ▶ if (strcmp(date_str, g_config.cur_log_day) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(date_str, g_config.cur_log_day) != 0) { 0 0 0
850 0 log_close_file();
851 0 log_open_file();
852 }
853 0 break;
854 }
855 0 case LOG_MODE_HOURLY: {
856 char hour_str[3];
857 0 strftime(hour_str, sizeof(hour_str), "%H", tm_now);
858 0 ▶ if (strcmp(hour_str, g_config.cur_log_hour) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(hour_str, g_config.cur_log_hour) != 0) { 0 0 0
859 0 log_close_file();
860 0 log_open_file();
861 }
862 0 break;
863 }
864 }
865 }
866
867 // 立即刷新缓冲
868 6 ▶ if (g_config.options.flush_immediately) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.options.flush_immediately) { 6 0 6
869 6 flushLog();
870 }
871
872 // 如果是FATAL级别,终止程序
873 6 ▶ if (level == LOG_LEVEL_FATAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (level == LOG_LEVEL_FATAL) { 0 6 6
874 0 closeLog();
875 0 unlock_log();
876 0 exit(EXIT_FAILURE);
877 }
878 6 unlock_log();
879 }
880  
881 0 void rep_write(const char *message) {
882 0 lock_log();
883 0 ▶ if (!g_config.options.with_rep) { unlock_log(); return;}
Branch: 1 branch
BranchTrueFalseTotal
if (!g_config.options.with_rep) { unlock_log(); return;} 0 0 0
884 0 size_t len = strlen(message);
885 0 ▶ if (len == 0) { unlock_log(); return;}
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { unlock_log(); return;} 0 0 0
886  
887 // 输出到控制台
888 0 ▶ if (g_config.options.targets & LOG_TARGET_CONSOLE)
Branch: 1 branch
BranchTrueFalseTotal
if (g_config.options.targets & LOG_TARGET_CONSOLE) 0 0 0
889 {
890 0 fputs(message, stdout);
891 0 ▶ if (message[len - 1] != '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (message[len - 1] != '\n') 0 0 0
892 {
893 0 fputs("\n", stdout);
894 }
895 }
896
897 // 输出到文件
898 0 ▶ if ((g_config.options.targets & LOG_TARGET_FILE) && g_config.rep_file >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((g_config.options.targets & LOG_TARGET_FILE) && g_config.rep_file >= 0) { 0 0 0
if ((g_config.options.targets & LOG_TARGET_FILE) && g_config.rep_file >= 0) { 0 0 0
899 0 flock(g_config.rep_file, LOCK_EX);
900 //write() 会直接进入内核缓冲区,不会被用户态的 stdio 缓冲影响。不需要 fflush(),因为根本没用到 FILE* 的缓冲。
901 0 ssize_t wlen = write(g_config.rep_file, message, len);
902
903 0 ▶ if (message[len - 1] != '\n')
Branch: 1 branch
BranchTrueFalseTotal
if (message[len - 1] != '\n') 0 0 0
904 {
905 0 wlen = write(g_config.rep_file,"\n", 1);
906 }
907
908 0 flock(g_config.rep_file, LOCK_UN); // 解锁
909 0 switch (g_config.options.mode) {
910 0 case LOG_MODE_CYCLE:{
911 //long size = ftell(g_config.rep_file);
912 0 long size = (long)lseek(g_config.rep_file, 0, SEEK_END);
913 0 ▶ if (size > g_config.options.max_file_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > g_config.options.max_file_size) { 0 0 0
914 0 rep_close_file();
915 0 rep_rotate_file();
916 0 rep_open_file();
917 }
918 0 break;
919 }
920 0 case LOG_MODE_DAILY: {
921 char date_str[9];
922
923 time_t now;
924 struct tm *tm_now;
925 0 time(&now);
926 0 tm_now = localtime(&now);
927
928 0 strftime(date_str, sizeof(date_str), "%Y%m%d", tm_now);
929 0 ▶ if (strcmp(date_str, g_config.cur_rep_day) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(date_str, g_config.cur_rep_day) != 0) { 0 0 0
930 0 rep_close_file();
931 0 rep_open_file();
932 }
933 0 break;
934 }
935 0 case LOG_MODE_HOURLY: {
936 char hour_str[3];
937
938 time_t now;
939 struct tm *tm_now;
940 0 time(&now);
941 0 tm_now = localtime(&now);
942
943 0 strftime(hour_str, sizeof(hour_str), "%H", tm_now);
944  
945 0 ▶ if (strcmp(hour_str, g_config.cur_rep_hour) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(hour_str, g_config.cur_rep_hour) != 0) { 0 0 0
946 0 rep_close_file();
947 0 rep_open_file();
948 }
949 0 break;
950 }
951 }
952 }
953 0 unlock_log();
954 }
« blib/lib/XS/Log.pm ↑ Summary Log.xs »