-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlog.c
331 lines (287 loc) · 8.32 KB
/
mlog.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <time.h>
#include <sys/time.h>
#ifdef MIKELIBC_THREADS
#include <pthread.h>
#endif
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include "mlog.h"
#include "madt.h"
#ifdef MIKELIBC_THREADS
pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
// A global handler to represent the default logger.
mlog_handle_t g_handle = -1;
void vmlogf(mlog_t *logger, logseverity_t severity, const char *fmt, va_list argp) {
assert( logger != NULL );
char timebuf[TIMEBUF];
struct timeval tv;
const char *tf0 = "";
const char *tf1 = "%a %b %e %H:%M:%S";
const char *tf2 = "%a %b %e %H:%M:%S %Z (UTC%z) %Y";
const char *tf;
// Only continue if the severity of this message >= the logger severity.
if (severity < logger->severity) {
goto CLEANUP_AND_RETURN;
}
// If there's a callback installed, use it instead.
if (logger->callback != NULL) {
char buffer[MAX_LOG];
vsnprintf(buffer, MAX_LOG, fmt, argp);
logger->callback(logger->severity, buffer, logger->cb_data);
return;
}
// Otherwise, we use ours.
switch (logger->logtime) {
case LOCNOZONE:
case UTC:
tf = tf1;
break;
case LOCWZONE:
tf = tf2;
break;
default:
/* NONE */
tf = tf0;
break;
}
if (logger->logtime)
{
gettimeofday(&tv, NULL);
if (logger->logtime == UTC) {
strftime(timebuf, TIMEBUF, tf, gmtime(&tv.tv_sec));
} else {
strftime(timebuf, TIMEBUF, tf, localtime(&tv.tv_sec));
}
}
switch (logger->loggertype) {
case MLOG_NONE:
goto CLEANUP_AND_RETURN;
case MLOG_SYSLOG:
fprintf(stderr, "Warning: syslog support not implemented.\n");
/* fall through for now */
case MLOG_STDOUT:
case MLOG_STDERR:
case MLOG_FILE:
// FIXME - use hires timer and provide milliseconds?
assert( logger->logfile != NULL );
if (logger->logtime)
fprintf(logger->logfile, "%s ", timebuf);
if (logger->logtime == UTC)
fprintf(logger->logfile, "UTC ");
switch (severity) {
case MLOG_TRACE:
fprintf(logger->logfile, "ALL: ");
break;
case MLOG_DEBUG:
fprintf(logger->logfile, "DEBUG: ");
break;
case MLOG_INFO:
fprintf(logger->logfile, "INFO: ");
break;
case MLOG_WARNING:
fprintf(logger->logfile, "WARNING: ");
break;
case MLOG_ERROR:
fprintf(logger->logfile, "ERROR: ");
break;
case MLOG_CRITICAL:
fprintf(logger->logfile, "CRITICAL: ");
break;
default:
fprintf(logger->logfile, "LOG: ");
break;
}
vfprintf(logger->logfile, fmt, argp);
fprintf(logger->logfile, "\n");
break;
default:
fprintf(stderr, "Unsupported logger type: %d\n", logger->loggertype);
}
CLEANUP_AND_RETURN:
return;
}
/*
* The linked-list of loggers.
*/
mlog_t *mloggers = NULL;
// The global incrementing counter for handlers. Not the same as a count.
mlog_handle_t next_handle = 0;
// The global count of logger handlers.
uint32_t nloggers = 0;
/*
* Create a new logger and return the handle for it.
*/
mlog_handle_t
get_mlogger(loggertype_t loggertype, logseverity_t severity, logtime_t logtime) {
mlog_t *new_logger = (mlog_t *)malloc(sizeof(mlog_t));
mlog_t *current = mloggers;
if (new_logger == NULL) {
fprintf(stderr, "FATAL: malloc returned NULL\n");
return -1;
}
#ifdef MIKELIBC_THREADS
pthread_mutex_lock(&logging_mutex);
#endif
// FIXME: need a constructor
mlog_handle_t handle = next_handle;
new_logger->loggertype = loggertype;
new_logger->severity = severity;
new_logger->logtime = logtime;
new_logger->handle = handle;
new_logger->path = NULL;
new_logger->next = NULL;
new_logger->logfile = NULL;
new_logger->callback = NULL;
new_logger->cb_data = NULL;
next_handle++;
nloggers++;
mlinked_list_add(mloggers, new_logger, current);
// Set the default logger to 0.
if (g_handle < 0) {
g_handle = 0;
}
// FIXME: terrible workaround
setloggertype(handle, loggertype, NULL);
#ifdef MIKELIBC_THREADS
pthread_mutex_unlock(&logging_mutex);
#endif
return handle;
}
int
mlogger_check(mlog_handle_t handle, mlog_t *current) {
if (current->handle == handle) {
return 1;
} else {
return 0;
}
}
int
shutdown_mlogger(mlog_handle_t handle) {
mlog_t *current = NULL;
mlog_t *previous = NULL;
mlog_t *freenode = NULL;
#ifdef MIKELIBC_THREADS
pthread_mutex_lock(&logging_mutex);
#endif
mlinked_list_remove(mloggers, current, previous, freenode, mlogger_check, handle);
#ifdef MIKELIBC_THREADS
pthread_mutex_unlock(&logging_mutex);
#endif
if (freenode == NULL) {
return 0;
} else {
free(freenode);
nloggers--;
if (nloggers == 0) {
g_handle = -1;
}
return 1;
}
}
void
shutdown_all_mloggers() {
mlog_t *current = NULL;
mlog_t *previous = NULL;
#ifdef MIKELIBC_THREADS
pthread_mutex_lock(&logging_mutex);
#endif
current = previous = mloggers;
while (current != NULL) {
current = current->next;
free(previous);
nloggers--;
previous = current;
}
assert( nloggers == 0 );
g_handle = -1;
mloggers = NULL;
#ifdef MIKELIBC_THREADS
pthread_mutex_unlock(&logging_mutex);
#endif
}
mlog_t*
find_mlogger(mlog_handle_t handle) {
mlog_t *current = NULL;
mlinked_list_find(mloggers, current, mlogger_check, handle);
return current;
}
void mlogf(mlog_handle_t handle, logseverity_t severity, const char *fmt, ...) {
if (handle < 0) {
// Unset
return;
}
va_list argp;
#ifdef MIKELIBC_THREADS
pthread_mutex_lock(&logging_mutex);
#endif
mlog_t *logger = find_mlogger(handle);
if (logger != NULL) {
va_start(argp, fmt);
vmlogf(logger, severity, fmt, argp);
va_end(argp);
} else {
fprintf(stderr, "Bad logging handle: %d\n", handle);
}
#ifdef MIKELIBC_THREADS
pthread_mutex_unlock(&logging_mutex);
#endif
}
void setloggertime(mlog_handle_t handle, logtime_t tstype) {
mlog_t *logger = find_mlogger(handle);
if (logger != NULL) {
logger->logtime = tstype;
} else {
fprintf(stderr, "Invalid logger handle: %d\n", handle);
}
}
int setloggertype(mlog_handle_t handle, loggertype_t type, char* path) {
mlog_t *logger = find_mlogger(handle);
if (logger != NULL) {
logger->loggertype = type;
if (type == MLOG_FILE)
{
if (path != NULL)
{
/* FIXME: We should have to specify appending or not. */
if ((logger->logfile = fopen(path, "w")) == NULL)
{
perror("logfile");
logger->loggertype = MLOG_NONE;
return 0;
}
} else {
fprintf(stderr, "Warning: Requested file logger, but path is NULL\n");
return 0;
}
} else if (type == MLOG_STDERR) {
logger->logfile = stderr;
} else {
logger->logfile = stdout;
}
} else {
fprintf(stderr, "Invalid logger handle %d\n", handle);
return 0;
}
return 1;
}
void setloggersev(mlog_handle_t handle, logseverity_t severity) {
mlog_t *logger = find_mlogger(handle);
if (logger != NULL) {
logger->severity = severity;
} else {
fprintf(stderr, "Invalid logger handle %d\n", handle);
}
}
void register_mlog_callback(mlog_handle_t handle, mlog_cb_t cb_fn_ptr, void *cb_ptr) {
mlog_t *logger = find_mlogger(handle);
if (logger != NULL) {
logger->callback = cb_fn_ptr;
logger->cb_data = cb_ptr;
} else {
fprintf(stderr, "Invalid logger handle %d\n", handle);
}
}