-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.cpp
49 lines (44 loc) · 1.08 KB
/
logging.cpp
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
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "configfile.h"
#include "logging.h"
#include "timer.h"
static const char *LOG_LEVEL_NAMES[] = { "", "ERROR", "WARNING", "INFO", "DEBUG" };
static FILE *logFile;
Timer logTimer;
void logInit(const char *fileName) {
if (!strcmp(fileName, "stdout")) {
logFile = stdout;
} else if (!strcmp(fileName, "stderr")) {
logFile = stderr;
} else {
logFile = fopen(fileName, "at");
}
assert(logFile);
logTimer.reset();
}
void vlog(int level, const char *format, va_list vl) {
if (level <= cfgLogLevel) {
u64 millis = logTimer.get();
fprintf(logFile, "[%7llu.%03llu] [%s] ",
millis / 1000, millis % 1000, LOG_LEVEL_NAMES[level]);
vfprintf(logFile, format, vl);
fprintf(logFile, "\n");
fflush(logFile);
}
}
void log(int level, const char *format, ...) {
va_list vl;
va_start(vl, format);
vlog(level, format, vl);
va_end(vl);
}
void die(const char *format, ...) {
va_list vl;
va_start(vl, format);
vlog(LOG_ERROR, format, vl);
va_end(vl);
exit(1);
}