-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
43 lines (34 loc) · 853 Bytes
/
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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "log.h"
static const char *logLevelTbl[] = {
[NONE] "NONE",
[INFO] "INFO",
[WARN] "WARN",
[ERROR] "ERROR",
[FATAL] "FATAL,"
};
static LogLevel logLevel = NONE;
void setLogLevel(LogLevel level)
{
logLevel = level;
}
void msgLog(LogLevel level, const char *function, const char *fmt, ...)
{
time_t now = time(NULL);
struct tm tm;
char timestamp[80];
va_list ap;
strftime(timestamp, sizeof (timestamp), "%Y-%m-%dT%H:%M:%S", localtime_r(&now, &tm));
fprintf(stdout, "%s:%s:%s: ", timestamp, logLevelTbl[level], function);
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
fprintf(stdout, "\n");
fflush(stdout);
if (level == FATAL) {
exit(-1);
}
}