-
Notifications
You must be signed in to change notification settings - Fork 63
/
tlog.c
2162 lines (1802 loc) · 51.6 KB
/
tlog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tinylog
* Copyright (C) 2018-2024 Nick Peng <[email protected]>
* https://github.com/pymumu/tinylog
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "tlog.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
#endif
#ifndef unlikely
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif
#define TLOG_BUFF_SIZE (1024 * 128)
#define TLOG_TMP_LEN 128
#define TLOG_LOG_SIZE (1024 * 1024 * 50)
#define TLOG_LOG_COUNT 32
#define TLOG_LOG_NAME_LEN 256
#define TLOG_BUFF_LEN (PATH_MAX + TLOG_LOG_NAME_LEN * 3)
#define TLOG_SUFFIX_GZ ".gz"
#define TLOG_SUFFIX_LOG ""
#define TLOG_MAX_LINE_SIZE_SET (1024 * 8)
#define TLOG_MIN_LINE_SIZE_SET (128)
#define TLOG_SEGMENT_MAGIC 0xFF446154
struct linux_dirent64 {
unsigned long long d_ino;
long long d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};
struct tlog_log {
char *buff;
int buffsize;
int start;
int end;
int ext_end;
int fd;
int fd_lock;
off_t filesize;
char logdir[PATH_MAX];
char logname[TLOG_LOG_NAME_LEN];
char suffix[TLOG_LOG_NAME_LEN];
char pending_logfile[PATH_MAX];
int rename_pending;
int fail;
int logsize;
int logcount;
int block;
int dropped;
int nocompress;
int zip_pid;
int multi_log;
int logscreen;
int logscreen_color;
int segment_log;
int max_line_size;
int print_errmsg;
tlog_output_func output_func;
void *private_data;
time_t last_try;
time_t last_waitpid;
mode_t file_perm;
mode_t archive_perm;
int mode_changed;
int waiters;
int is_exit;
struct tlog_log *next;
pthread_mutex_t lock;
pthread_cond_t client_cond;
};
struct tlog {
struct tlog_log *root;
struct tlog_log *log;
struct tlog_log *notify_log;
int run;
pthread_t tid;
pthread_mutex_t lock;
pthread_cond_t cond;
tlog_log_output_func output_func;
struct tlog_log *wait_on_log;
int is_wait;
int output_no_prefix;
char gzip_cmd[PATH_MAX];
tlog_format_func root_format;
tlog_early_print_func tlog_early_print;
tlog_log_output_func early_print_output;
int early_print_disable;
int early_print_with_screen;
int early_print_no_prefix;
int early_print_color;
void *early_print_userptr;
};
struct tlog_segment_log_head {
struct tlog_loginfo info;
unsigned short len;
char data[0];
} __attribute__((packed));
struct tlog_segment_head {
unsigned int magic;
unsigned short len;
char data[0];
} __attribute__((packed));
struct oldest_log {
char name[TLOG_LOG_NAME_LEN];
time_t mtime;
struct tlog_log *log;
};
struct count_log {
int lognum;
struct tlog_log *log;
};
struct tlog_info_inter {
struct tlog_loginfo info;
void *userptr;
};
typedef int (*list_callback)(const char *name, struct dirent *entry, void *user);
typedef int (*vprint_callback)(char *buff, int maxlen, void *userptr, const char *format, va_list ap);
static struct tlog tlog;
static tlog_level tlog_set_level = TLOG_INFO;
unsigned int tlog_localtime_lock;
static const char *tlog_level_str[] = {
"DEBUG",
"INFO",
"NOTICE",
"WARN",
"ERROR",
"FATAL",
};
static inline void _tlog_spin_lock(unsigned int *lock)
{
while (1) {
int i;
for (i = 0; i < 10000; i++) {
if (__sync_bool_compare_and_swap(lock, 0, 1)) {
return;
}
}
sched_yield();
}
}
static inline void _tlog_spin_unlock(unsigned int *lock)
{
__sync_bool_compare_and_swap(lock, 1, 0);
}
static int _tlog_mkdir(const char *path)
{
char path_c[PATH_MAX];
char *path_end;
char str;
int len;
if (access(path, F_OK) == 0) {
return 0;
}
while (*path == ' ') {
path++;
}
strncpy(path_c, path, sizeof(path_c) - 1);
path_c[sizeof(path_c) - 1] = '\0';
len = strnlen(path_c, sizeof(path_c) - 1);
path_c[len] = '/';
path_c[len + 1] = '\0';
path_end = path_c;
/* create directory recursively */
while (*path_end != 0) {
if (*path_end != '/') {
path_end++;
continue;
}
if (path_end == path_c) {
path_end++;
continue;
}
str = *path_end;
*path_end = '\0';
if (access(path_c, F_OK) == 0) {
*path_end = str;
path_end++;
continue;
}
if (mkdir(path_c, 0750) != 0) {
return -1;
}
*path_end = str;
path_end++;
}
return 0;
}
static struct tm *_tlog_localtime(time_t *timep, struct tm *tm)
{
static time_t last_time;
static struct tm last_tm;
/* localtime_r has a global timezone lock, it's about 8 times slower than gmtime
* this code is used to speed up localtime_r call.
*/
_tlog_spin_lock(&tlog_localtime_lock);
if (*timep == last_time) {
*tm = last_tm;
} else {
_tlog_spin_unlock(&tlog_localtime_lock);
tm = localtime_r(timep, tm);
_tlog_spin_lock(&tlog_localtime_lock);
if (tm) {
last_time = *timep;
last_tm = *tm;
}
}
_tlog_spin_unlock(&tlog_localtime_lock);
return tm;
}
static int _tlog_getmtime(struct tlog_time *log_mtime, const char *file)
{
struct tm tm;
struct stat sb;
if (stat(file, &sb) != 0) {
return -1;
}
if (_tlog_localtime(&sb.st_mtime, &tm) == NULL) {
return -1;
}
log_mtime->year = tm.tm_year + 1900;
log_mtime->mon = tm.tm_mon + 1;
log_mtime->mday = tm.tm_mday;
log_mtime->hour = tm.tm_hour;
log_mtime->min = tm.tm_min;
log_mtime->sec = tm.tm_sec;
log_mtime->usec = 0;
return 0;
}
static int _tlog_gettime(struct tlog_time *cur_time)
{
struct tm tm;
struct timeval tmval;
if (gettimeofday(&tmval, NULL) != 0) {
return -1;
}
if (_tlog_localtime(&tmval.tv_sec, &tm) == NULL) {
return -1;
}
cur_time->year = tm.tm_year + 1900;
cur_time->mon = tm.tm_mon + 1;
cur_time->mday = tm.tm_mday;
cur_time->hour = tm.tm_hour;
cur_time->min = tm.tm_min;
cur_time->sec = tm.tm_sec;
cur_time->usec = tmval.tv_usec;
return 0;
}
void tlog_set_maxline_size(struct tlog_log *log, int size)
{
if (log == NULL) {
return;
}
if (size < TLOG_MIN_LINE_SIZE_SET) {
size = TLOG_MIN_LINE_SIZE_SET;
} else if (size > TLOG_MAX_LINE_SIZE_SET) {
size = TLOG_MAX_LINE_SIZE_SET;
}
log->max_line_size = size;
}
void tlog_logcount(struct tlog_log *log, int count)
{
if (log == NULL) {
return;
}
if (count < 0) {
count = 0;
}
log->logcount = count;
}
void tlog_set_permission(struct tlog_log *log, mode_t file, mode_t archive)
{
log->file_perm = file;
log->archive_perm = archive;
log->mode_changed = 1;
}
int tlog_localtime(struct tlog_time *tm)
{
return _tlog_gettime(tm);
}
tlog_log *tlog_get_root(void)
{
return tlog.root;
}
void tlog_set_private(tlog_log *log, void *private_data)
{
if (log == NULL) {
return;
}
log->private_data = private_data;
}
void *tlog_get_private(tlog_log *log)
{
if (log == NULL) {
return NULL;
}
return log->private_data;
}
static int _tlog_root_default_format(char *buff, int maxlen, struct tlog_loginfo *info, void *userptr, const char *format, va_list ap)
{
int len = 0;
int total_len = 0;
struct tlog_time *tm = &info->time;
void *unused __attribute__((unused));
unused = userptr;
if (tlog.output_no_prefix == 0) {
if (tlog.root->multi_log) {
/* format prefix */
len = snprintf(buff, maxlen, "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5d][%4s][%17s:%-4d] ",
tm->year, tm->mon, tm->mday, tm->hour, tm->min, tm->sec, tm->usec / 1000, getpid(),
tlog_get_level_string(info->level), info->file, info->line);
} else {
/* format prefix */
len = snprintf(buff, maxlen, "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5s][%17s:%-4d] ",
tm->year, tm->mon, tm->mday, tm->hour, tm->min, tm->sec, tm->usec / 1000,
tlog_get_level_string(info->level), info->file, info->line);
}
}
if (len < 0 || len >= maxlen) {
return -1;
}
buff += len;
total_len += len;
maxlen -= len;
/* format log message */
len = vsnprintf(buff, maxlen, format, ap);
if (len < 0 || len == maxlen) {
return -1;
}
buff += len;
total_len += len;
/* return total length */
return total_len;
}
static int _tlog_root_log_buffer(char *buff, int maxlen, void *userptr, const char *format, va_list ap)
{
int len = 0;
int log_len = 0;
struct tlog_info_inter *info_inter = (struct tlog_info_inter *)userptr;
struct tlog_segment_log_head *log_head = NULL;
int max_format_len = 0;
if (tlog.root_format == NULL) {
return -1;
}
if (tlog.root->segment_log) {
log_head = (struct tlog_segment_log_head *)buff;
len += sizeof(*log_head);
memcpy(&log_head->info, &info_inter->info, sizeof(log_head->info));
}
max_format_len = maxlen - len - 2;
buff[maxlen - 1] = 0;
log_len = tlog.root_format(buff + len, max_format_len, &info_inter->info, info_inter->userptr, format, ap);
if (log_len < 0) {
return -1;
} else if (log_len >= max_format_len) {
buff[len + max_format_len - 2] = '.';
buff[len + max_format_len - 3] = '.';
buff[len + max_format_len - 4] = '.';
log_len = max_format_len - 1;
}
len += log_len;
/* add new line character*/
if (*(buff + len - 1) != '\n' && len + 1 < maxlen - 1) {
*(buff + len) = '\n';
len++;
log_len++;
}
if (tlog.root->segment_log && log_head != NULL) {
if (len + 1 < maxlen - 1) {
*(buff + len) = '\0';
len++;
}
log_head->len = log_len;
}
return len;
}
static int _tlog_print_buffer(char *buff, int maxlen, void *userptr, const char *format, va_list ap)
{
int len;
int total_len = 0;
void *unused __attribute__((unused));
unused = userptr;
/* format log message */
len = vsnprintf(buff, maxlen, format, ap);
if (len < 0 || len == maxlen) {
return -1;
}
buff += len;
total_len += len;
/* return total length */
return total_len;
}
static int _tlog_need_drop(struct tlog_log *log)
{
int maxlen = 0;
int ret = -1;
if (log->block) {
return -1;
}
pthread_mutex_lock(&tlog.lock);
if (log->end == log->start) {
if (log->ext_end == 0) {
/* if buffer is empty */
maxlen = log->buffsize - log->end;
}
} else if (log->end > log->start) {
maxlen = log->buffsize - log->end;
} else {
/* if reverse */
maxlen = log->start - log->end;
}
/* if free buffer length is less than min line length */
if (maxlen < log->max_line_size) {
log->dropped++;
ret = 0;
}
pthread_mutex_unlock(&tlog.lock);
return ret;
}
static int _tlog_vprintf(struct tlog_log *log, vprint_callback print_callback, void *userptr, const char *format, va_list ap)
{
int len;
int maxlen = 0;
struct tlog_segment_head *segment_head = NULL;
if (log == NULL || format == NULL) {
return -1;
}
char buff[log->max_line_size];
if (log->buff == NULL) {
return -1;
}
if (unlikely(log->logcount <= 0 && log->logscreen == 0)) {
return 0;
}
if (_tlog_need_drop(log) == 0) {
return -1;
}
len = print_callback(buff, sizeof(buff), userptr, format, ap);
if (len <= 0) {
return -1;
} else if (len >= log->max_line_size) {
len = log->max_line_size;
buff[len - 1] = '\0';
buff[len - 2] = '\n';
buff[len - 3] = '.';
buff[len - 4] = '.';
buff[len - 5] = '.';
}
pthread_mutex_lock(&tlog.lock);
do {
if (log->end == log->start) {
if (log->ext_end == 0) {
/* if buffer is empty */
maxlen = log->buffsize - log->end;
}
} else if (log->end > log->start) {
maxlen = log->buffsize - log->end;
} else {
/* if reverse */
maxlen = log->start - log->end;
}
/* if free buffer length is less than min line length */
if (maxlen < log->max_line_size) {
if (log->end != log->start) {
tlog.notify_log = log;
pthread_cond_signal(&tlog.cond);
}
/* if drop message, increase statistics and return */
if (log->block == 0) {
log->dropped++;
pthread_mutex_unlock(&tlog.lock);
return -1;
}
pthread_mutex_unlock(&tlog.lock);
pthread_mutex_lock(&log->lock);
log->waiters++;
/* block wait for free buffer */
int ret = pthread_cond_wait(&log->client_cond, &log->lock);
log->waiters--;
pthread_mutex_unlock(&log->lock);
if (ret < 0) {
return -1;
}
pthread_mutex_lock(&tlog.lock);
}
} while (maxlen < log->max_line_size);
if (log->segment_log) {
segment_head = (struct tlog_segment_head *)(log->buff + log->end);
memcpy(segment_head->data, buff, len);
log->end += len + sizeof(*segment_head) + 1;
segment_head->len = len + 1;
segment_head->data[len] = '\0';
segment_head->magic = TLOG_SEGMENT_MAGIC;
} else {
/* write log to buffer */
memcpy(log->buff + log->end, buff, len);
log->end += len;
}
/* if remain buffer is not enough for a line, move end to start of buffer. */
if (log->end > log->buffsize - log->max_line_size) {
log->ext_end = log->end;
log->end = 0;
}
if (tlog.is_wait) {
tlog.notify_log = log;
pthread_cond_signal(&tlog.cond);
}
pthread_mutex_unlock(&tlog.lock);
return len;
}
int tlog_vprintf(struct tlog_log *log, const char *format, va_list ap)
{
return _tlog_vprintf(log, _tlog_print_buffer, NULL, format, ap);
}
int tlog_printf(struct tlog_log *log, const char *format, ...)
{
int len;
va_list ap;
va_start(ap, format);
len = tlog_vprintf(log, format, ap);
va_end(ap);
return len;
}
int tlog_stdout_with_color(tlog_level level, const char *buff, int bufflen)
{
int unused __attribute__((unused));
const char *color = NULL;
switch (level) {
case TLOG_DEBUG:
color = "\033[0;94m";
break;
case TLOG_NOTICE:
color = "\033[0;97m";
break;
case TLOG_WARN:
color = "\033[0;33m";
break;
case TLOG_ERROR:
color = "\033[0;31m";
break;
case TLOG_FATAL:
color = "\033[31;1m";
break;
default:
unused = write(STDOUT_FILENO, buff, bufflen);
return bufflen;
}
if (color != NULL) {
fprintf(stdout, "%s%.*s\033[0m\n", color, bufflen - 1, buff);
} else {
fprintf(stdout, "%s", buff);
}
return bufflen;
}
static int _tlog_early_print(struct tlog_info_inter *info_inter, const char *format, va_list ap)
{
char log_buf[TLOG_MAX_LINE_LEN];
size_t len = 0;
size_t out_len = 0;
struct tlog_time cur_time;
int unused __attribute__((unused));
if (tlog.early_print_disable) {
return 0;
}
if (_tlog_gettime(&cur_time) != 0) {
return -1;
}
if (tlog.tlog_early_print != NULL) {
tlog.tlog_early_print(&info_inter->info, format, ap);
return out_len;
}
if (tlog.early_print_no_prefix == 0) {
len = snprintf(log_buf, sizeof(log_buf), "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d,%.3d][%5s][%17s:%-4d] ",
cur_time.year, cur_time.mon, cur_time.mday, cur_time.hour, cur_time.min, cur_time.sec, cur_time.usec / 1000,
tlog_get_level_string(info_inter->info.level), info_inter->info.file, info_inter->info.line);
}
out_len = len;
len = vsnprintf(log_buf + out_len, sizeof(log_buf) - out_len - 1, format, ap);
out_len += len;
if (len <= 0) {
return -1;
} else if (len >= sizeof(log_buf) - 1) {
out_len = sizeof(log_buf) - 1;
}
if (log_buf[out_len - 1] != '\n') {
log_buf[out_len] = '\n';
out_len++;
}
if (out_len + 1 < sizeof(log_buf) - out_len - 1) {
log_buf[out_len] = '\0';
}
if (tlog.early_print_output != NULL) {
len = tlog.early_print_output(&info_inter->info, log_buf, out_len, tlog.early_print_userptr);
if (tlog.early_print_with_screen == 0) {
return len;
}
}
if (tlog.early_print_color) {
unused = tlog_stdout_with_color(info_inter->info.level, log_buf, out_len);
} else {
unused = write(STDOUT_FILENO, log_buf, out_len);
}
return out_len;
}
int tlog_vext(tlog_level level, const char *file, int line, const char *func, void *userptr, const char *format, va_list ap)
{
struct tlog_info_inter info_inter;
if (level < tlog_set_level) {
return 0;
}
if (level >= TLOG_END) {
return -1;
}
info_inter.info.file = file;
info_inter.info.line = line;
info_inter.info.func = func;
info_inter.info.level = level;
info_inter.userptr = userptr;
if (_tlog_gettime(&info_inter.info.time) != 0) {
return -1;
}
if (tlog.root == NULL) {
return _tlog_early_print(&info_inter, format, ap);
}
if (unlikely(tlog.root->logsize <= 0)) {
return 0;
}
return _tlog_vprintf(tlog.root, _tlog_root_log_buffer, &info_inter, format, ap);
}
int tlog_ext(tlog_level level, const char *file, int line, const char *func, void *userptr, const char *format, ...)
{
int len;
va_list ap;
va_start(ap, format);
len = tlog_vext(level, file, line, func, userptr, format, ap);
va_end(ap);
return len;
}
static int _tlog_rename_logfile(struct tlog_log *log, const char *log_file)
{
char archive_file[TLOG_BUFF_LEN];
struct tlog_time logtime;
int i = 0;
if (_tlog_getmtime(&logtime, log_file) != 0) {
return -1;
}
snprintf(archive_file, sizeof(archive_file), "%s/%s-%.4d%.2d%.2d-%.2d%.2d%.2d%s",
log->logdir, log->logname, logtime.year, logtime.mon, logtime.mday,
logtime.hour, logtime.min, logtime.sec, log->suffix);
while (access(archive_file, F_OK) == 0) {
i++;
snprintf(archive_file, sizeof(archive_file), "%s/%s-%.4d%.2d%.2d-%.2d%.2d%.2d-%d%s",
log->logdir, log->logname, logtime.year, logtime.mon,
logtime.mday, logtime.hour, logtime.min, logtime.sec, i, log->suffix);
}
if (rename(log_file, archive_file) != 0) {
return -1;
}
chmod(archive_file, log->archive_perm);
return 0;
}
static int _tlog_list_dir(const char *path, list_callback callback, void *userptr)
{
DIR *dir = NULL;
struct dirent *ent;
int ret = 0;
const char *unused __attribute__((unused)) = path;
dir = opendir(path);
if (dir == NULL) {
fprintf(stderr, "tlog: open directory failed, %s\n", strerror(errno));
goto errout;
}
while ((ent = readdir(dir)) != NULL) {
if (strncmp(".", ent->d_name, 2) == 0 || strncmp("..", ent->d_name, 3) == 0) {
continue;
}
ret = callback(path, ent, userptr);
if (ret != 0) {
goto errout;
}
}
closedir(dir);
return 0;
errout:
if (dir) {
closedir(dir);
dir = NULL;
}
return -1;
}
static int _tlog_count_log_callback(const char *path, struct dirent *entry, void *userptr)
{
struct count_log *count_log = (struct count_log *)userptr;
struct tlog_log *log = count_log->log;
char logname[TLOG_LOG_NAME_LEN * 2];
const char *unused __attribute__((unused)) = path;
if (strstr(entry->d_name, log->suffix) == NULL) {
return 0;
}
snprintf(logname, sizeof(logname), "%s-", log->logname);
int len = strnlen(logname, sizeof(logname));
if (strncmp(logname, entry->d_name, len) != 0) {
return 0;
}
count_log->lognum++;
return 0;
}
static int _tlog_get_oldest_callback(const char *path, struct dirent *entry, void *userptr)
{
struct stat sb;
char filename[TLOG_BUFF_LEN];
struct oldest_log *oldestlog = (struct oldest_log *)userptr;
struct tlog_log *log = oldestlog->log;
char logname[TLOG_LOG_NAME_LEN * 2];
/* if not a log file, skip */
if (strstr(entry->d_name, log->suffix) == NULL) {
return 0;
}
/* if not tlog log file, skip */
snprintf(logname, sizeof(logname), "%s-", log->logname);
int len = strnlen(logname, sizeof(logname));
if (strncmp(logname, entry->d_name, len) != 0) {
return 0;
}
/* get log file mtime */
snprintf(filename, sizeof(filename), "%s/%s", path, entry->d_name);
if (stat(filename, &sb) != 0) {
return -1;
}
if (oldestlog->mtime == 0 || oldestlog->mtime > sb.st_mtime) {
oldestlog->mtime = sb.st_mtime;
strncpy(oldestlog->name, entry->d_name, sizeof(oldestlog->name));
oldestlog->name[sizeof(oldestlog->name) - 1] = '\0';
return 0;
}
return 0;
}
static int _tlog_remove_oldestlog(struct tlog_log *log)
{
struct oldest_log oldestlog;
oldestlog.name[0] = 0;
oldestlog.mtime = 0;
oldestlog.log = log;
/* get oldest log file name */
if (_tlog_list_dir(log->logdir, _tlog_get_oldest_callback, &oldestlog) != 0) {
return -1;
}
char filename[PATH_MAX * 2];
snprintf(filename, sizeof(filename), "%s/%s", log->logdir, oldestlog.name);
/* delete */
unlink(filename);
return 0;
}
static int _tlog_remove_oldlog(struct tlog_log *log)
{
struct count_log count_log;
int i = 0;
count_log.lognum = 0;
count_log.log = log;
/* get total log file number */
if (_tlog_list_dir(log->logdir, _tlog_count_log_callback, &count_log) != 0) {
fprintf(stderr, "tlog: get log file count failed.\n");
return -1;
}
/* remove last N log files */
for (i = 0; i < count_log.lognum - log->logcount; i++) {
_tlog_remove_oldestlog(log);
}
return 0;
}
static void _tlog_log_unlock(struct tlog_log *log)
{
char lock_file[PATH_MAX * 2];
if (log->fd_lock <= 0) {
return;
}
snprintf(lock_file, sizeof(lock_file), "%s/%s.lock", log->logdir, log->logname);
unlink(lock_file);
close(log->fd_lock);
log->fd_lock = -1;
}
static int _tlog_log_lock(struct tlog_log *log)
{
char lock_file[PATH_MAX * 2];
int fd;
if (log->multi_log == 0) {
return 0;
}
snprintf(lock_file, sizeof(lock_file), "%s/%s.lock", log->logdir, log->logname);
fd = open(lock_file, O_RDWR | O_CREAT | O_CLOEXEC, S_IRUSR | S_IWUSR);
if (fd == -1) {
fprintf(stderr, "tlog: create lock file failed, %s", strerror(errno));
return -1;
}
if (lockf(fd, F_TLOCK, 0) < 0) {
goto errout;
}
log->fd_lock = fd;
return 0;
errout:
if (fd > 0) {
close(fd);
}
return -1;
}
static void _tlog_wait_pid(struct tlog_log *log, int wait_hang)
{
int status;
if (log->zip_pid <= 0) {
return;
}
int option = (wait_hang == 0) ? WNOHANG : 0;
/* check and obtain gzip process status*/
if (waitpid(log->zip_pid, &status, option) <= 0) {
return;
}
/* gzip process exited */