-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
98 lines (84 loc) · 2.17 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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/types.h>
#include<stdarg.h>
#include<sys/stat.h>
#include<time.h>
#include<sys/timeb.h>
#include<pthread.h>
#include<unistd.h>
#include "common.h"
#include "log.h"
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
extern char g_log_path[FILEPATH_LEN];
extern char program_name[128];
int CreateDir(const char *sPathName)
{
char DirName[256] = {'\0'};
strcpy(DirName,sPathName);
int i=0;
int len = strlen(DirName);
if(DirName[len-1]!='/')
strcat(DirName,"/");
len = strlen(DirName);
for(i=1; i<len; i++)
{
if(DirName[i]=='/')
{
DirName[i] = 0;
if(access(DirName,F_OK)!=0)
{
if(mkdir(DirName,0755)==-1)
{
printf("mkdir error!");
return -1;
}
}
DirName[i] = '/';
}
}
return 0;
}
void log_write(int level,char *format,...)
{
FILE *fp = NULL;
time_t t;
struct tm *ptm = NULL;
struct timeb tp;
char file[FILEPATH_LEN] = {'\0'};
va_list ap;
t = time(NULL);
ptm = localtime(&t);
ftime(&tp);
sprintf(file,"%s/%s_log[%04d-%02d-%02d]",g_log_path,program_name,1900+ptm->tm_year,ptm->tm_mon+1,ptm->tm_mday);
if(0!=CreateDir(g_log_path)) return;
pthread_mutex_lock(&(log_mutex));
if((fp = fopen(file,"a+"))== NULL) return ;
va_start(ap,format);
switch(level)
{
case INFO:
fprintf(fp,"¡¾%02d-%02d-%02d.%03d¡¿INFO:",ptm->tm_hour,ptm->tm_min,ptm->tm_sec,tp.millitm);
break;
case DEBUG:
fprintf(fp,"¡¾%02d-%02d-%02d.%03d¡¿DEBUG:",ptm->tm_hour,ptm->tm_min,ptm->tm_sec,tp.millitm);
break;
case WARN:
fprintf(fp,"¡¾%02d-%02d-%02d.%03d¡¿WARN:",ptm->tm_hour,ptm->tm_min,ptm->tm_sec,tp.millitm);
break;
case ERROR:
fprintf(fp,"¡¾%02d-%02d-%02d.%03d¡¿ERROR:",ptm->tm_hour,ptm->tm_min,ptm->tm_sec,tp.millitm);
break;
default:
fprintf(fp,"¡¾%02d-%02d-%02d.%03d¡¿OTHER:",ptm->tm_hour,ptm->tm_min,ptm->tm_sec,tp.millitm);
break;
}
vfprintf(fp, format, ap);
va_end(ap);
if (strlen(format) > 0 && '\n' != format[strlen(format) - 1]) fprintf(fp, "\n");
fclose(fp);
pthread_mutex_unlock(&(log_mutex));
return;
}