-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
83 lines (74 loc) · 1.71 KB
/
debug.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
#include "debug.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
static FILE *logfile = NULL;
char *levelString(DebugLevel level)
{
switch (level)
{
case INFO:
return " INFO";
case ERROR:
return " ERROR";
case VERBOSE:
return "VERBOSE";
case WARNING:
return "WARNING";
}
return "UNKNOWN";
}
void debugLog(DebugLevel level, const char *fmt, ...)
{
if (logfile == NULL)
{
logfile = fopen("debug.log", "w");
}
va_list args;
va_start(args, fmt);
char logtime[9];
time_t rawtime;
struct tm *curtime;
time(&rawtime);
curtime = localtime(&rawtime);
strftime(logtime, sizeof(logtime), "%H:%M:%S", curtime);
fprintf(logfile, "%s %s | ", logtime, levelString(level));
vfprintf(logfile, fmt, args);
fprintf(logfile, "\n");
fflush(logfile);
va_end(args);
}
void debugLogDone()
{
if (logfile != NULL)
fprintf(logfile, "done\n");
}
void closeDebugLog()
{
if (logfile != NULL)
fclose(logfile);
}
void debug(DebugLevel level, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
char logtime[9];
time_t rawtime;
struct tm *curtime;
time(&rawtime);
curtime = localtime(&rawtime);
strftime(logtime, sizeof(logtime), "%H:%M:%S", curtime);
printf("%s %s | ", logtime, levelString(level));
vprintf(fmt, args);
printf("\n");
va_end(args);
}
void debugDone() {
printf("done\n");
}