-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
276 lines (247 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
#ifdef __cplusplus
extern "C" {
#endif
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#include <winsock2.h>
#include <timeapi.h>
#include <process.h>
#pragma comment( lib,"winmm.lib" )
#define gettimeofday(ptv, NULL)\
(*ptv).tv_usec = timeGetTime();\
(*ptv).tv_sec = (*ptv).tv_usec / 1000;\
(*ptv).tv_usec %= 1000;\
(*ptv).tv_usec *= 1000;
#define pthread_mutex_init(pMutex, attr) !(pMutex = CreateMutexA(NULL, FALSE, NULL))
#define pthread_mutex_lock(pMutex) WaitForSingleObject(pMutex, INFINITE);
#define pthread_mutex_unlock(pMutex) ReleaseMutex(pMutex)
#define pthread_mutex_destroy(pMutex) CloseHandle(pMutex)
#define pthread_mutex_t void
static HANDLE ghMutex = NULL;
#else
#include <stdarg.h>
#include <sys/time.h>
#include <pthread.h>
#include <unistd.h>
#define _snprintf snprintf
#define Sleep(time) usleep(time*1000)
static pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t *ghMutex = &s_mutex;
#endif
#include "log.h"
#define MAX_LOG_SEQ 0x400
#define MAX_LOG_LINE_SIZE 0x1000
#define DEL_LOG_FILE_SIZE 20000000
static const char *s_strLogType[] =
{
"EMERG",
"ALERT",
"CRIT",
"ERROR",
"WARN",
"NOTICE",
"INFO",
"DEBUG"
};
S_LOG g_log = { {0}, 0, 0, 0, 1, 30000000, LOG_LEVEL_DEBUG };
//功 能: 删除超时日志文件
static void log_del_timeout_file(S_LOG *pLog)
{
if (pLog->hour == 0)
return;
#ifdef _WIN32
{
//system("del *.log");
const DWORD nBufferLength = MAX_LOG_PATH_NAME_T;
WIN32_FIND_DATAA ffd;
char delFile[MAX_LOG_PATH_NAME_T] = {0};
HANDLE hFind = NULL;
FILETIME SystemTimeAsFileTime;
hFind = FindFirstFileA("*.log",&ffd);
GetSystemTimeAsFileTime(&SystemTimeAsFileTime);
while (INVALID_HANDLE_VALUE != hFind && strcmp(ffd.cFileName, delFile))
{
strcpy(delFile, ffd.cFileName);
FindNextFileA(hFind, &ffd);
//时间以10分钟为单位,6代表60分钟,144是24小时
if (ffd.ftLastWriteTime.dwHighDateTime + pLog->hour * 6 < SystemTimeAsFileTime.dwHighDateTime)
{
DeleteFileA(delFile);
}
}
FindClose(hFind);
}
#else
{
char delFile[MAX_LOG_PATH_NAME_T] = {0};
//时间以分钟为单位
sprintf(delFile, "find -mmin +%u -name \"*.log\" -exec rm -f {} \\;", (unsigned int)(pLog->hour * 60));
system(delFile);
}
#endif
}
//功 能: 创建一个新的日志文件
//参 数: pLog指定的日志结构体
//返回值:
static void log_new_file(S_LOG *pLog)
{
int len = 0;
log_del_timeout_file(pLog);
if (NULL != pLog->fp && (len = fclose(pLog->fp)))
{
printf("fclose failed ! pLog->strPathName=%s.\r\n", pLog->strPathName);
return;
}
if (pLog->count > 1)
{
char buf[MAX_LOG_PATH_NAME_T] = { 0 };
char buf2[MAX_LOG_PATH_NAME_T] = { 0 };
sprintf(buf, "%s.%u", pLog->strPathName, (unsigned int)(pLog->count - 1));
remove(buf);
for (len = pLog->count - 2; len > 0; len--)
{
sprintf(buf, "%s.%u", pLog->strPathName, len);
sprintf(buf2, "%s.%u", pLog->strPathName, len + 1);
rename(buf, buf2);
}
rename(pLog->strPathName, buf);
}
if(NULL == (pLog->fp = fopen(pLog->strPathName, "wb+")))
{
printf("fopen failed ! pLog->strPathName=%s.\r\n", pLog->strPathName);
return;
}
pLog->seq = 0;
return;
}
//功 能: 释放日志文件对应的文件指针
//参 数: pLog指定的日志结构体
//返回值:
void log_close(S_LOG *pLog)
{
pthread_mutex_lock(ghMutex);
if (pLog != NULL && pLog->fp != NULL)
{
fclose(pLog->fp);
pLog->fp = NULL;
}
//printf("close\n");
pthread_mutex_destroy(ghMutex);
}
//功 能: 写日志到指定的日志文件中
//参 数: pLog指定的日志结构体,level代表日志信息等级,后面的参数如printf
//返回值:
void log_write(S_LOG *pLog, E_LOG_LEVEL level, const char *msgfmt, ...)
{
if (level > pLog->level)
{
return;
}
{
va_list ap;
char message[MAX_LOG_LINE_SIZE+1] = {0};
size_t len;
time_t t;
struct timeval tv;
time(&t);
len = strftime(message, MAX_LOG_LINE_SIZE,"%Y-%m-%d %H:%M:%S", localtime(&t));
gettimeofday(&tv, NULL);
len += _snprintf(&message[len], MAX_LOG_LINE_SIZE-len, ".%ld", tv.tv_usec);
len += _snprintf(&message[len], MAX_LOG_LINE_SIZE-len, "[%s]", s_strLogType[level]);
message[MAX_LOG_LINE_SIZE] = '\0';
va_start(ap, msgfmt);
vsnprintf(&message[len], MAX_LOG_LINE_SIZE - len, msgfmt, ap);
va_end(ap);
pthread_mutex_lock(ghMutex);
if (NULL == pLog->fp)
{
pthread_mutex_unlock(ghMutex);
printf("%s\n", (char*)message);
return;
}
if (pLog->count != 0 && ++pLog->seq > MAX_LOG_SEQ)
{
if ((long)pLog->size < ftell(pLog->fp))
{
log_new_file(pLog);
}
else
{
pLog->seq = 0;
}
}
fprintf(pLog->fp, "%s\n", (char*)message);
fflush(pLog->fp);
pthread_mutex_unlock(ghMutex);
}
}
//功 能: 设置默认日志文件输出等级和名字
//参 数: char strPathName[MAX_LOG_PATH_NAME_T];//日志文件路径名前缀,为空时会自动获取
// size_t seq;//写入seq次日志后做一次文件切割判断
// size_t hour;//保存时间小时数,超过时间会被删除
// size_t count;//日志文件保存个数,为0时不换新文件,为1时单个文件超大后刷新重写
// size_t size;//日志文件大小超过多大换新
// E_LOG_LEVEL level;//打印日志级别
//返回值: 0为成功
int log_open(S_LOG *pLog, const char *strPathName, E_LOG_LEVEL level, size_t count, size_t size, size_t hour)
{
int len = 0;
char szPath[MAX_LOG_PATH_NAME_T] = { 0 };
if (pLog == NULL)
{
pLog = &g_log;
}
pLog->size = size;
if (pLog->size == 0)
pLog->size = DEL_LOG_FILE_SIZE;
pLog->hour = hour;
pLog->count = count;
pLog->level = level;
if (level < LOG_LEVEL_EMERG || level > LOG_LEVEL_DEBUG)
{
printf("Log level setting error\n");
return -1;
}
if(strPathName != NULL && strPathName[0] != 0)
strncpy(pLog->strPathName, strPathName, sizeof(pLog->strPathName));
else
{
#ifdef _WIN32
if (!GetModuleFileNameA(NULL, szPath, MAX_LOG_PATH_NAME_T))
{
printf("GetModuleFileName failed (%d)\n", GetLastError());
return -2;
}
len = strlen(szPath);
while (len > 0 && szPath[--len] != '\\')
{
if (szPath[len] == '.')
{
szPath[len] = '\0';
break;
}
}
#else
readlink("/proc/self/exe", szPath, MAX_LOG_PATH_NAME_T);
#endif
strncat(pLog->strPathName, &szPath[0], sizeof(pLog->strPathName) - strlen(pLog->strPathName) - 1);
strncat(pLog->strPathName, ".log", sizeof(pLog->strPathName) - strlen(pLog->strPathName) - 1);
pLog->strPathName[sizeof(pLog->strPathName) - 1] = '\0';
}
len = pthread_mutex_init(ghMutex, NULL);
if (len != 0)
{
printf("Create mutex error\n");
return -3;
}
pthread_mutex_unlock(ghMutex);
if (NULL == (pLog->fp = fopen(pLog->strPathName, "ab+")))
{
printf("fopen failed ! pLog->strPathName=%s.\r\n", pLog->strPathName);
return -4;
}
return 0;
}
#ifdef __cplusplus
}
#endif