forked from geekerliu/dsd_debug
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.c
More file actions
59 lines (49 loc) · 1.19 KB
/
debug.c
File metadata and controls
59 lines (49 loc) · 1.19 KB
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
#include <stdio.h>
#include <sys/time.h>
#include "debug.h"
static const char * const log_level_names[] = {
"ERR",
"WARN",
"NOTICE",
"INFO",
"DEBUG",
};
#define DSD_PRINT_READ fprintf(stderr, "\033[31m")// ERR
#define DSD_PRINT_YELLOW fprintf(stderr, "\033[33m")// WARN
#define DSD_PRINT_WHITE fprintf(stderr, "\033[37m")// NOTICE
#define DSD_PRINT_BLUE fprintf(stderr, "\033[34m")// INFO
#define DSD_PRINT_GREEN fprintf(stderr, "\033[32m")// DEBUG
int log_level = DSD_ERR;
void time_in_microseconds(int n)
{
unsigned long long now;
struct timeval tv;
gettimeofday(&tv, NULL);
now = ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
switch (n) {
case 0: DSD_PRINT_READ; break;
case 1: DSD_PRINT_YELLOW; break;
case 2: DSD_PRINT_WHITE; break;
case 3: DSD_PRINT_BLUE; break;
case 4: DSD_PRINT_GREEN; break;
default: return;
}
fprintf(stderr, "%s:[%llu:%d]", log_level_names[n], now / 1000000, (int)(now % 1000000));
}
void dsd_set_log_level(int level)
{
log_level = level;
}
int dsd_log_filter(int filter)
{
int i;
if (log_level & filter) {
for (i = 0; i < DSD_COUNT; i++) {
if (filter == (1 << i)) {
time_in_microseconds(i);
return 1;
}
}
}
return 0;
}