-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
197 lines (160 loc) · 4.2 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
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "log.h"
FILE* logFp = NULL;
char logFn[512];
FILE* ioFps[eIoCnt];
char ioFns[eIoCnt][512];
int ioFpsInit = -1;
TcError openLog(const char* name)
{
if (logFp)
{
printError(logLog, "log has been opened with %s. Please closelog() then openLog() to reopen with different log file name \n", logFn);
return TcE_Invalid_Argument;
}
strncpy(logFn, name, sizeof(logFn));
logFn[sizeof(logFn)-1] = 0;
logFp = fopen(logFn, "w");
if (NULL == logFp)
{
fprintf(stderr, "failed to open %s for writting\n", name);
return TcE_Not_Found;
}
return TcE_OK;
}
void closeLog(void)
{
if (logFp)
{
fclose(logFp);
logFp = NULL;
}
}
void msgLog(const char* label, int enable, const char* fmt, ...)
{
char buff[512] = {0};
va_list ap;
if ((0 == enable) || (logFp == NULL))
return;
va_start(ap, fmt);
sprintf(buff, "%s ", label);
strcat(buff, fmt);
vfprintf(logFp, buff, ap);
fflush(logFp);
}
void msgLogEx(const char* label, int enable, const char* func, const char* file, int line, const char* fmt, ...)
{
char buff[512] = {0};
char msg[1024] = {0};
va_list ap;
va_start(ap, fmt);
if ((logFp == NULL) || (!enable))
return;
sprintf(buff, "%s:%d: %s %s(): ", file, line, label, func);
strcat(buff, fmt);
vsprintf(msg, buff, ap);
fprintf(logFp, "%s", msg);
fflush(logFp);
}
void warnLog(const char* label, int enable, const char* func, const char* file, int line, const char* fmt, ...)
{
char buff[512] = {0};
va_list ap;
va_start(ap, fmt);
if ((logFp == NULL) || (!enable))
return;
sprintf(buff, "%s WARNING: %s(), <%s,%d> : ", label, func, file, line);
strcat(buff, fmt);
vfprintf(logFp, buff, ap);
fflush(logFp);
}
void errorLog(const char* label, int enable, const char* func, const char* file, int line, const char* fmt, ...)
{
char buff[512] = {0};
char msg[1024] = {0};
va_list ap;
va_start(ap, fmt);
sprintf(buff, "ERROR: %s %s() <%s, %d> ", label, func, file, line);
strcat(buff, fmt);
vsprintf(msg, buff, ap);
if (logFp)
{
fprintf(logFp, "%s", msg);
fflush(logFp);
}
fprintf(stderr, "%s", msg);
fflush(stderr);
}
TcError openIo(eIo io, const char* name)
{
if (ioFpsInit == -1)
{
memset(ioFps, 0, sizeof(ioFps));
ioFpsInit = 0;
}
if (io >= eIoCnt)
{
printLog(logLog, "invalid io index=%X max=%X\n", io, eIoCnt);
return TcE_Invalid_Argument;
}
if (ioFps[io])
{
printLog(logLog, "opened io=%X filename=%s\n", io, ioFns[io]);
return TcE_Invalid_Argument;
}
if ((ioFps[io] = fopen(name, "w")) == NULL)
{
printLog(logLog, "cannot open filename=%s for writting\n", name);
return TcE_Failed;
}
strncpy(ioFns[io], name, sizeof(ioFns[0]));
ioFns[io][sizeof(ioFns[0])] = 0;
return TcE_OK;
}
void closeIo(eIo io)
{
if (io >= eIoCnt)
return;
if (ioFpsInit == -1)
return;
if (ioFps[io])
{
fclose(ioFps[io]);
ioFps[io] = NULL;
}
}
void printIo(eIo io, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (ioFps[io])
{
vfprintf(ioFps[io], fmt, ap);
}
}
void assertWithLog(const char* func, const char* file, int line, const char* expr)
{
char buff[512] = {0};
sprintf(buff, "ASSERT: assert(%s) : %s() <%s, %d> \n", expr, func, file, line);
if (logFp)
{
fprintf(logFp, "%s", buff);
fflush(logFp);
}
fprintf(stderr, "%s", buff);
fflush(stderr);
}
void exitWithLog(const char* func, const char* file, int line, const char* expr)
{
char buff[512] = {0};
sprintf(buff, "EXIT: exit(%s) : %s() <%s, %d> \n", expr, func, file, line);
if (logFp)
{
fprintf(logFp, "%s", buff);
fflush(logFp);
}
fprintf(stderr, "%s", buff);
fflush(stderr);
}