forked from 0xmalloc/c-log
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.cpp
More file actions
173 lines (150 loc) · 4.26 KB
/
log.cpp
File metadata and controls
173 lines (150 loc) · 4.26 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
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
#include "log.h"
#include <sys/file.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <libgen.h>
LogWriter WARN_W;
LogWriter INFO_W;
__thread char LogWriter::m_buffer[M_LOG_BUFFSIZE];
bool logInit(LogLevel l, const char* modulename, const char* logdir, bool verbose) {
if (access(logdir, 0) == -1) {
if (mkdir(logdir, S_IRWXU) < 0) {
fprintf(stderr, "%s create folder failed\n", logdir);
return false;
}
}
time_t now = time(&now);;
struct tm vtm;
localtime_r(&now, &vtm);
char location_str[LogWriter::M_LOG_PATH_LEN] = {0};
snprintf(location_str, LogWriter::M_LOG_PATH_LEN, "%s/%s.info.%d%02d%02d-%02d",logdir,modulename,1900+vtm.tm_year,vtm.tm_mon + 1,vtm.tm_mday,vtm.tm_hour);
INFO_W.loginit(l, location_str, verbose);
snprintf(location_str, LogWriter::M_LOG_PATH_LEN, "%s/%s.error.%d%02d%02d-%02d", logdir,modulename,1900+vtm.tm_year,vtm.tm_mon + 1,vtm.tm_mday,vtm.tm_hour);
if(l > LL_WARNING)
WARN_W.loginit(l, location_str, verbose);
else
WARN_W.loginit(LL_WARNING, location_str, verbose);
return true;
}
LogWriter::LogWriter() {
m_system_level = LL_NOTICE;
fp = NULL;
m_issync = false;
m_isappend = true;
m_filelocation[0] ='\0';
pthread_mutex_init(&m_mutex, NULL);
}
LogWriter::~LogWriter(){
logclose();
}
const char* LogWriter::logLevelToString(LogLevel l) {
switch ( l ) {
case LL_DEBUG:
return "DEBUG";
case LL_TRACE:
return "TRACE";
case LL_NOTICE:
return "NOTICE";
case LL_WARNING:
return "WARN" ;
case LL_ERROR:
return "ERROR";
default:
return "UNKNOWN";
}
}
bool LogWriter::checklevel(LogLevel l) {
if(l >= m_system_level)
return true;
else
return false;
}
bool LogWriter::loginit(LogLevel l, const char *filelocation, bool verbose, bool append, bool issync) {
MACRO_RET(NULL != fp, false);
m_system_level = l;
m_isappend = append;
m_issync = issync;
m_verbose = verbose;
if(strlen(filelocation) >= (sizeof(m_filelocation) -1)) {
fprintf(stderr, "the path of log file is too long:%lu limit:%lu\n", strlen(filelocation), sizeof(m_filelocation) -1);
exit(0);
}
strncpy(m_filelocation, filelocation, sizeof(m_filelocation));
m_filelocation[sizeof(m_filelocation) -1] = '\0';
if('\0' == m_filelocation[0]) {
fp = stdout;
fprintf(stderr, "now all the running-information are going to put to stderr\n");
return true;
}
fp = fopen(m_filelocation, append ? "a":"w");
if(fp == NULL) {
fprintf(stderr, "cannot open log file,file location is %s\n", m_filelocation);
exit(0);
}
//setvbuf (fp, io_cached_buf, _IOLBF, sizeof(io_cached_buf)); //buf set _IONBF _IOLBF _IOFBF
setvbuf (fp, (char *)NULL, _IOLBF, 0);
fprintf(stderr, "now all the running-information are going to the file %s\n", m_filelocation);
return true;
}
int LogWriter::premakestr(char* buffer, LogLevel l) {
time_t now;
now = time(&now);;
struct tm vtm;
localtime_r(&now, &vtm);
return snprintf(buffer, M_LOG_BUFFSIZE, "[%d-%02d-%02d %02d:%02d:%02d] %s ",
1900+vtm.tm_year, vtm.tm_mon + 1, vtm.tm_mday, vtm.tm_hour, vtm.tm_min, vtm.tm_sec, logLevelToString(l));
}
bool LogWriter::log(LogLevel l, const char* logformat,...) {
MACRO_RET(!checklevel(l), false);
char * start = m_buffer;
int size = premakestr(start, l);
va_list args;
va_start(args, logformat);
size += vsnprintf(start+size, M_LOG_BUFFSIZE-size, logformat, args);
va_end(args);
if(m_verbose)
fprintf(stderr, "%s", m_buffer);
if(fp)
write(m_buffer, size);
return true;
}
bool LogWriter::write(char *pbuffer, int len) {
if(0 != access(m_filelocation, W_OK)) {
pthread_mutex_lock(&m_mutex);
if(0 != access(m_filelocation, W_OK)) {
logclose();
loginit(m_system_level, m_filelocation, m_isappend, m_issync);
}
pthread_mutex_unlock(&m_mutex);
}
if(1 == fwrite(pbuffer, len, 1, fp)) {
if(m_issync)
fflush(fp);
*pbuffer='\0';
}
else {
int x = errno;
fprintf(stderr, "Failed to write to logfile. errno:%s message:%s", strerror(x), pbuffer);
return false;
}
return true;
}
LogLevel LogWriter::getlevel() {
return m_system_level;
}
bool LogWriter::logclose() {
if(fp == NULL)
return false;
fflush(fp);
fclose(fp);
fp = NULL;
return true;
}
void LogWriter::removeConsole() {
m_verbose = false;
}