-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
339 lines (293 loc) · 6.7 KB
/
log.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
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <errno.h>
#include <pthread.h>
#include "dirent.h"
#include "log.h"
#define LOG_BUFFER_SIZE (64*1024)
#define MAX_LOGFILE_SIZE (5*1024*1024)
#define MAX_LOGFILE_NUM (5)
/* #define FilePath "./log/" */
#define LOG_PATH "/home/ubuntu/log/"
#define FileNameLen 64
#define PUB_LOG_MT
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static FILE *gpLogFile = NULL;
char FilePath[256] = {'/', 'w','w','w','/','l', 'o', 'g', '/', 0};
/* Functions Used Internally */
static int log_msg(const char *file, int line, const char *function, pub_log_level level, const char *msg);
static const char *get_level_str(pub_log_level level);
/*
@获取路径下文件个数
*/
static int get_file_num(const char *file)
{
DIR *dir = NULL;
int num = 0;
struct dirent *entry;
if((dir = opendir(file))==NULL){
fprintf(stderr,"opendir(%s) failed!\n", file);
return -1;
}
else
{
while(entry=readdir(dir))
{
if(entry->d_type == 8)
{
num++;
}
}
closedir(dir);
}
return num;
}
/*
@获取日志文件大小
*/
static unsigned long get_file_size(const char *path)
{
unsigned long filesize = -1;
if(strlen(path)==0)
return -1;
struct stat statbuff;
if(stat(path, &statbuff) < 0)
{
return filesize;
}
else
{
filesize = statbuff.st_size;
}
return filesize;
}
/*
@获取最老文件
*/
static int obtain_oldest_file(char *filename)
{
DIR *dir = NULL;
int dirn=0,dirIndex=0;
struct dirent **namelist = NULL;
char cmd[32];
memset(filename,0,FileNameLen);
//路径不存在则返回空文件名
if((dir = opendir(FilePath))==NULL)
{
return -1;
}
//排序查找
dirn=scandir(FilePath, &namelist, 0, alphasort);
for(dirIndex=0;dirIndex<dirn;dirIndex++)
{
if(namelist[dirIndex]->d_type == 8)
{
//找到一个刷新文件名
sprintf(filename,"%s%s",FilePath,namelist[dirIndex]->d_name);
break;
}
}
for(dirIndex=0;dirIndex<dirn;dirIndex++)
{
free(namelist[dirIndex]);
}
free(namelist);
closedir(dir);
return 0;
}
/*
@获取最新的文件
*/
static int obtain_latest_file(char *filename)
{
DIR *dir = NULL;
int dirn=0,dirIndex=0;
struct dirent **namelist = NULL;
char cmd[32];
memset(filename,0,FileNameLen);
//路径不存在则返回空文件名
if((dir = opendir(FilePath))==NULL)
{
//创建日志路径
memset(cmd,0,sizeof(cmd));
sprintf(cmd,"mkdir -p %s",FilePath);
system(cmd);
return -1;
}
//排序查找
dirn=scandir(FilePath, &namelist, 0, alphasort);
for(dirIndex=0;dirIndex<dirn;dirIndex++)
{
if(namelist[dirIndex]->d_type == 8)
{
//找到一个刷新文件名
sprintf(filename,"%s%s",FilePath,namelist[dirIndex]->d_name);
}
}
for(dirIndex=0;dirIndex<dirn;dirIndex++)
{
free(namelist[dirIndex]);
}
free(namelist);
closedir(dir);
return 0;
}
/*
@删除日志
*/
static void delete_file(char *filePath)
{
if(filePath==NULL)
return;
remove(filePath);
}
/*
@创建文件
*/
static int create_file()
{
struct tm p;
struct timeval now;
time_t timep;
FILE *fp = NULL;
char fileName[FileNameLen];
timep=time(NULL);
localtime_r(&timep,&p);
gettimeofday(&now, NULL);
//如果当前文件已经达到个数上限,则删除最老文件,然后创建新文件
if(get_file_num(FilePath)>= MAX_LOGFILE_NUM)
{
obtain_oldest_file(fileName);
delete_file(fileName);
}
//
sprintf(fileName, "%s%04d-%02d-%02d-%02d-%02d-%02d-%03d.log",FilePath,
p.tm_year + 1900, p.tm_mon + 1, p.tm_mday,
p.tm_hour, p.tm_min, p.tm_sec, (int)now.tv_usec/1000);
fp = fopen(fileName, "w");
if (fp == NULL)
{
return -1;
}
fflush(fp);
fclose(fp);
return 0;
}
static int log_msg(const char *file, int line, const char *function, pub_log_level level, const char *msg)
{
FILE *fp = stderr;
char time_str[64] = {0};
const char *level_str;
struct tm tm;
time_t timevalue = 0;
pthread_mutex_lock(&mutex);
if (level == PUB_LOG_LEVEL_DEBUG) {
fp = stdout;
}
else
{
if (gpLogFile != NULL){
/* if file size is two big, close it first */
long size = ftell(gpLogFile);
if (size > MAX_LOGFILE_SIZE){
fclose(gpLogFile);
gpLogFile = NULL;
}
}
if (gpLogFile == NULL){
char fileName[FileNameLen] = {0};
//获取最新的日志文件
obtain_latest_file(fileName);
//获取不到正常的文件大小
if((get_file_size(fileName)>MAX_LOGFILE_SIZE)){
create_file();
}
obtain_latest_file(fileName);
gpLogFile=fopen(fileName, "a");
if(gpLogFile==NULL){
pthread_mutex_unlock(&mutex);
return -1;
}
}
fp = gpLogFile;
}
timevalue=time(NULL);
localtime_r(&timevalue,&tm);
struct timeval now;
gettimeofday(&now, NULL);
sprintf(time_str, "%04d-%02d-%02d %02d:%02d:%02d-%03d",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, (int)now.tv_usec/1000);
level_str = get_level_str(level);
#ifdef PUB_LOG_MT
fprintf(fp, "[%s] [%s] [%s(%d): %s] %s\n",
time_str,
level_str,
file, line, function,
msg);
#else /* PUB_LOG_MT */
fprintf(fp, "[%s] [%d] [%s] [%s(%d): %s] %s\n",
time_str, (int)getpid(),
level_str,
file, line, func,
msg);
#endif /* PUB_LOG_MT */
fflush(fp);
pthread_mutex_unlock(&mutex);
return 0;
}
static
const char *get_level_str(pub_log_level level)
{
const char *p = "UNKNOWN";
switch (level) {
case PUB_LOG_LEVEL_INFO:
p = "INFO";
break;
case PUB_LOG_LEVEL_WARNING:
p = "WARNING";
break;
case PUB_LOG_LEVEL_ERROR:
p = "ERROR";
break;
case PUB_LOG_LEVEL_DEBUG:
p = "DEBUG";
break;
default:
p = "UNKNOWN";
break;
}
return p;
}
void _pub_module(const char *path)
{
char command[1024] = {0};
snprintf(FilePath, 256, "%s%s/", LOG_PATH, path);
snprintf(command, 1024, "mkdir -p %s", FilePath);
system(command);
}
int _pub_log(const char *file, int line, const char *function, pub_log_level level, const char *fmt, ...)
{
char msg[LOG_BUFFER_SIZE];
va_list args;
memset(msg, 0, sizeof(msg));
va_start(args, fmt);
vsnprintf(msg, sizeof(msg), fmt, args);
va_end(args);
log_msg(file, line, function, level, msg);
return 0;
}
void _pub_assert(const char *file, int line, const char *function, int expr, const char *expr_str)
{
if (!expr) {
_pub_log(file, line, function, PUB_LOG_LEVEL_DEBUG, "Assertion failed: %s.", expr_str);
exit(1);
}
}