-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
90 lines (78 loc) · 1.41 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
/*
This software is in the public domain. Where that dedication is not recognized,
you are granted a perpetual, irrevocable license to copy and modify this file
as you see fit.
*/
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
enum lvl_t loglvl;
void log_va(enum lvl_t lvl, const char* fmt, va_list args)
{
if (lvl < loglvl) return;
FILE* out = stderr;
switch (lvl) {
case DEBUG:
fprintf(out, "[DEBUG] ");
break;
case INFO:
fprintf(out, "[INFO] ");
break;
case WARNING:
fprintf(out, "[WARNING] ");
break;
case ERROR:
fprintf(out, "[ERROR] ");
break;
case CRITICAL:
fprintf(out, "[CRITICAL] ");
break;
default:
fprintf(out, "[?!] ");
break;
}
vfprintf(out, fmt, args);
fprintf(out, "\n");
fflush(out);
}
void dbgf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_va(DEBUG, fmt, args);
va_end(args);
}
void infof(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_va(INFO, fmt, args);
va_end(args);
}
void warnf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_va(WARNING, fmt, args);
va_end(args);
}
void errf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_va(ERROR, fmt, args);
va_end(args);
}
void critf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
log_va(CRITICAL, fmt, args);
va_end(args);
exit(255);
}
void setloglvl(enum lvl_t lvl)
{
loglvl = lvl;
}