-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog.c
More file actions
139 lines (110 loc) · 3.61 KB
/
log.c
File metadata and controls
139 lines (110 loc) · 3.61 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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "log.h"
#define ITCAST_DEBUG_FILE_ "mymon.log"
#define ITCAST_MAX_STRING_LEN 10240
//Level类别
#define IC_NO_LOG_LEVEL 0
#define IC_DEBUG_LEVEL 1
#define IC_INFO_LEVEL 2
#define IC_WARNING_LEVEL 3
#define IC_ERROR_LEVEL 4
int LogLevel[5] = {IC_NO_LOG_LEVEL, IC_DEBUG_LEVEL, IC_INFO_LEVEL, IC_WARNING_LEVEL, IC_ERROR_LEVEL};
//Level的名称
char ICLevelName[5][10] = {"NOLOG", "DEBUG", "INFO", "WARNING", "ERROR"};
static int ITCAST_Error_GetCurTime(char* strTime)
{
struct tm* tmTime = NULL;
size_t timeLen = 0;
time_t tTime = 0;
tTime = time(NULL);
tmTime = localtime(&tTime);
//timeLen = strftime(strTime, 33, "%Y(Y)%m(M)%d(D)%H(H)%M(M)%S(S)", tmTime);
timeLen = strftime(strTime, 33, "%Y.%m.%d %H:%M:%S", tmTime);
return timeLen;
}
static int ITCAST_Error_OpenFile(int* pf)
{
char fileName[1024];
memset(fileName, 0, sizeof(fileName));
#ifdef WIN32
sprintf(fileName, "c:\\itcast\\%s",ITCAST_DEBUG_FILE_);
#else
sprintf(fileName, "%s/%s", "./", ITCAST_DEBUG_FILE_);
#endif
*pf = open(fileName, O_WRONLY|O_CREAT|O_APPEND, 0666);
if(*pf < 0)
{
return -1;
}
return 0;
}
static void ITCAST_Error_Core(const char *file, int line, int level, int status, const char *fmt, va_list args)
{
char str[ITCAST_MAX_STRING_LEN];
int strLen = 0;
char tmpStr[64];
int tmpStrLen = 0;
int pf = 0;
//初始化
memset(str, 0, ITCAST_MAX_STRING_LEN);
memset(tmpStr, 0, 64);
//加入LOG时间
tmpStrLen = ITCAST_Error_GetCurTime(tmpStr);
tmpStrLen = sprintf(str, "[%s] ", tmpStr);
strLen = tmpStrLen;
//加入LOG等级
tmpStrLen = sprintf(str+strLen, "[%s] ", ICLevelName[level]);
strLen += tmpStrLen;
//加入LOG状态
if (status != 0)
{
tmpStrLen = sprintf(str+strLen, "[ERRNO is %d] ", status);
}
else
{
tmpStrLen = sprintf(str+strLen, "[SUCCESS] ");
}
strLen += tmpStrLen;
//加入LOG信息
tmpStrLen = vsprintf(str+strLen, fmt, args);
strLen += tmpStrLen;
//加入LOG发生文件
tmpStrLen = sprintf(str+strLen, " [%s]", file);
strLen += tmpStrLen;
//加入LOG发生行数
tmpStrLen = sprintf(str+strLen, " [%d]\n", line);
strLen += tmpStrLen;
//打开LOG文件
if(ITCAST_Error_OpenFile(&pf))
{
return ;
}
//写入LOG文件
write(pf, str, strLen);
//IC_Log_Error_WriteFile(str);
//关闭文件
close(pf);
return ;
}
void ITCAST_LOG(const char *file, int line, int level, int status, const char *fmt, ...)
{
va_list args;
//判断是否需要写LOG
// if(level!=IC_DEBUG_LEVEL && level!=IC_INFO_LEVEL && level!=IC_WARNING_LEVEL && level!=IC_ERROR_LEVEL)
if(level == IC_NO_LOG_LEVEL)
{
return ;
}
//调用核心的写LOG函数
va_start(args, fmt);
ITCAST_Error_Core(file, line, level, status, fmt, args);
va_end(args);
return ;
}