-
Notifications
You must be signed in to change notification settings - Fork 0
/
logg.c
123 lines (111 loc) · 2.66 KB
/
logg.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <stdbool.h>
#include <time.h>
#include <string.h>
#include <libgen.h>
#include "logg.h"
/* default - enable ERROR and INFO logs at startup*/
static bool debug_map [] = {
[ERROR] = true,
[PACKETS] = false,
[FSM] = false,
[INFO] = true,
[DETAIL] = false,
};
/*
* for now lets make file logging default, later we can add a option
* to redirect to stdout
*/
static time_t start_time;
static char logflnam[50];
static FILE *logf;
static bool log_enabled = false;
/*
* Create a log file and open it, log file name is based on the
* time string and the name of the program
*/
void init_logger (char *name)
{
struct tm *tm;
start_time = time(NULL);
tm = localtime(&start_time);
strftime(logflnam, sizeof logflnam, "log/%a%d%b%I:%M:%S%p%Y.", tm);
strcat(logflnam, name);
strcat(logflnam, ".log");
//logf = stdout; log_enabled = true; return;
logf = fopen(logflnam, "w");
if (!logf) {
perror("Could not open log file");
/* continue without logging */
return;
}
log_enabled = true;
}
void debug_print (debug_t d, char *format, ...)
{
va_list ap;
int n;
if (!debug_map[d])
return;
if (!log_enabled)
return;
va_start(ap, format);
n = vfprintf(logf, format, ap);
fflush(logf);
if (n < 0) perror("vprintf");
va_end(ap);
}
void debug_enable (debug_t d)
{
debug_map[d] = true;
}
void debug_disable (debug_t d)
{
debug_map[d] = false;
}
static char* to_str (debug_t d)
{
switch (d) {
case ERROR: return "ERROR";
case PACKETS: return "PACKET";
case FSM: return "FSM";
case INFO: return "INFO";
case DETAIL: return "DETAIL";
default: return "Unknown";
}
}
int get_debuglevels (char *s, size_t siz)
{
int i, n = 0;
if (!s)
return n;
/* we need 23 chars per debug level; now we hav 5 levels, buf shud b >115 */
s[0] = '\0';
n += sprintf(s,"\n\n");
for (i = 0; i < (sizeof debug_map / sizeof debug_map[0]); i++) {
if ((n + 23) > siz) break;
n += sprintf(s + n, " %6s debug %s\n", to_str(i),
debug_map[i] ? "enabled" : "disabled");
}
return n;
}
#if 0
int main (int argc, char *argv[])
{
init_logger(basename(argv[0]));
debug_disable(PACKETS);
debug_disable(DETAIL);
DEBUG_PAK("packet %d\n", 9);
DEBUG_ERR("error dumb\n");
DEBUG_FSM("state X\n");
DEBUG_INF("casual \n");
DEBUG_DET("var x = %d", 8);
// get_debuglevels(NULL);
debug_enable(DETAIL);
DEBUG_DET("another fine detail\n");
return 0;
}
#endif