-
Notifications
You must be signed in to change notification settings - Fork 2
/
logs.c
74 lines (55 loc) · 1.47 KB
/
logs.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
#include <stdarg.h>
#include <stdio.h>
#include "logs.h"
static LogLevel LOG_LEVEL = ERRORS;
void set_log_level(LogLevel level) {
switch(level) {
case NO_LOGS:
case ERRORS:
case INFO:
case GORY: LOG_LEVEL = level; break;
default: LOG_LEVEL = ERRORS; break;
}
}
void info(const char* fmt, ...) {
if (LOG_LEVEL < INFO) return;
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
void error(const char* fmt, ...) {
if (LOG_LEVEL < ERRORS) return;
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
void gory(const char* fmt, ...) {
if (LOG_LEVEL < GORY) return;
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
void print_log(LogLevel level_to_use, const char* fmt, ...) {
switch (level_to_use) {
case NO_LOGS: return;
case INFO: if (LOG_LEVEL < INFO) return; break;
case GORY: if (LOG_LEVEL < GORY) return; break;
default: if (LOG_LEVEL < ERRORS) return; break;
}
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
void print_bytes(LogLevel level, u_int8_t* bytes, unsigned int n_bytes) {
for (unsigned int i = 0 ; i < n_bytes ; i++) {
if (i > 0 && (i % 32) == 0) {
print_log(level, "\n");
}
print_log(level, "%02x ", bytes[i]);
}
print_log(level, "\n");
}