-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.cpp
More file actions
110 lines (91 loc) · 2.99 KB
/
log.cpp
File metadata and controls
110 lines (91 loc) · 2.99 KB
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
#include "log.h"
#include <malloc.h>
#include <string.h>
#include <unistd.h>
char *logPath;
char buf[1024];
int inited = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static const char *levelColors[] = {"\033[94m", "\033[36m", "\033[32m", "\033[33m", "\033[31m", "\033[35m", "\033[35m"};
static const char *levelStrings[] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"};
int mkdirs(const char *path, const mode_t mode, const int fail_on_exist)
{
int result = 0;
char *dir;
do
{
if (NULL == path)
{
errno = EINVAL;
result = -1;
break;
}
char *s_path=(char*)malloc(strlen(path)+1);
strcpy(s_path, path);
free(s_path);
if ((dir = strrchr(s_path, '/')))
{
*dir = '\0';
result = mkdirs(path, mode, fail_on_exist);
*dir = '/';
if (result)
break;
}
if (strlen(path))
{
if ((result = mkdir(path, mode)))
{
if ((EEXIST == result) && (0 == fail_on_exist))
result = 0;
else
break;
}
}
} while (0);
return result;
}
void logInit(const char *setLogPath)
{
logPath = (char *)malloc(strlen(setLogPath) + 1);
strcpy(logPath, setLogPath);
if (access(setLogPath, F_OK))
{
if (mkdirs(logPath, S_IRWXU, 0) == -1)
{
printf("%s\n", strerror(errno));
}
}
extrapidLog(LOG_INFO, "libLog", "Init libLog");
inited = 1;
}
void extrapidLog(const int logLevel, const char *moduleName, const char *fmt, ...)
{
pthread_mutex_lock(&lock);
time_t timep;
time(&timep);
struct tm *nowTime;
nowTime = localtime(&timep);
char timeString[22] = {0};
snprintf(timeString, 22, "[%d-%s%d-%s%d_%s%d:%s%d:%s%d]", 1900 + nowTime->tm_year, nowTime->tm_mon < 9 ? "0" : "", 1 + nowTime->tm_mon, nowTime->tm_mday < 10 ? "0" : "", nowTime->tm_mday, nowTime->tm_hour < 10 ? "0" : "", nowTime->tm_hour, nowTime->tm_min < 10 ? "0" : "", nowTime->tm_min, nowTime->tm_sec < 10 ? "0" : "", nowTime->tm_sec);
char logTimeLevelLib[128] = {0};
char logToFile[128] = {0};
sprintf(logTimeLevelLib, "%s %s%-5s\033[0m[%s] ", timeString, levelColors[logLevel], levelStrings[logLevel], moduleName);
sprintf(logToFile, "%s %-5s[%s] ", timeString, levelStrings[logLevel], moduleName);
memset(buf, 0, sizeof(buf));
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
printf("%s%s\n", logTimeLevelLib, buf);
if (inited == 1)
{
FILE *fp;
char fileName[1024];
sprintf(fileName, "%s/%d-%d-%d.log", logPath, 1900 + nowTime->tm_year, 1 + nowTime->tm_mon, nowTime->tm_mday);
if ((fp = fopen(fileName, "a")) == NULL)
printf("%s%s\n", logTimeLevelLib, strerror(errno));
fprintf(fp, "%s%s\n", logToFile, buf);
fclose(fp);
}
pthread_mutex_unlock(&lock);
}