-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklog.c
122 lines (109 loc) · 2.95 KB
/
klog.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <stdbool.h>
#include "klog.h"
static FILE *fp;
static KLOGLEVEL global_loglevel;
static pthread_mutex_t global_log_lock;
static char *global_file_path;
static unsigned int max_file_size;
static unsigned int now_file_size;
static unsigned int now_file_num;
static bool console_log;
static const char *klog_level_str[] = {
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
};
static const char *klog_level_color[] = {
"\x1B[0;90m", "\x1B[0;36m", "\x1B[0;32m", "\x1B[0;33m", "\x1B[0;31m", "\x1B[0;35m"
};
int klog_init(char *file_path)
{
global_loglevel = KLOG_INFO;
if (pthread_mutex_init(&global_log_lock, NULL) != 0) {
return -1;
}
if (file_path == NULL) {
console_log = true;
} else {
console_log = false;
fp = fopen(file_path, "a");
if (fp == NULL) {
return -1;
}
global_file_path = strdup(file_path);
//max_file_size = 128 * 1024 * 1024;
max_file_size = 150;
}
return 0;
}
void klog_destroy(void)
{
if (fp)
fclose(fp);
}
void klog_set_loglevel(unsigned int loglevel)
{
global_loglevel = loglevel;
}
void klog_set_max_file_size(unsigned int max_size)
{
max_file_size = max_size;
}
void klog_log(unsigned int loglevel, const char *file, const char *func,
int line, const char *fmt, ...)
{
if (loglevel < global_loglevel) {
return;
}
time_t t = time(NULL);
struct tm lt;
localtime_r(&t, <);
va_list strs;
va_start(strs, fmt);
pthread_mutex_lock(&global_log_lock);
if (console_log) {
printf("%02d:%02d:%02d " // TIME
"%s:%d(%s) " // CODE LINE
"%s%s:\x1B[0m ", // LOG LEVEL
lt.tm_hour, lt.tm_min, lt.tm_sec,
file, line, func,
klog_level_color[loglevel], klog_level_str[loglevel]);
printf(fmt, strs);
printf("\n");
fflush(stdout);
} else {
now_file_size +=
fprintf(fp, "[%04d-%02d-%02d %02d:%02d:%02d] " // TIME
"%s:%d(%s) " // CODE
"%s: ", // LOG LEVEL
lt.tm_year + 1900, lt.tm_mon + 1, lt.tm_mday,
lt.tm_hour, lt.tm_min, lt.tm_sec,
file, line, func,
klog_level_str[loglevel]);
now_file_size +=
vfprintf(fp, fmt, strs);
now_file_size +=
fprintf(fp, "\n");
if (now_file_size >= max_file_size) {
now_file_num = now_file_num % 5;
char *new_file_path = malloc(strlen(global_file_path)+3);
sprintf(new_file_path, "%s.%d", global_file_path, now_file_num);
close(fp);
rename(global_file_path, new_file_path);
now_file_num++;
fp = fopen(global_file_path, "w");
if (fp == NULL) {
//TODO rename, fopen error handling
}
now_file_size = 0;
}
fflush(fp);
}
pthread_mutex_unlock(&global_log_lock);
va_end(strs);
}