-
Notifications
You must be signed in to change notification settings - Fork 19
/
neat_log.c
263 lines (223 loc) · 5.62 KB
/
neat_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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <stdint.h>
#include "neat.h"
#include "neat_internal.h"
#include "neat_core.h"
#ifdef NEAT_LOG
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "neat_log.h"
#define KNRM "\x1B[0m"
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define YEL "\x1B[33m"
#define BLU "\x1B[34m"
#define MAG "\x1B[35m"
#define CYN "\x1B[36m"
#define WHT "\x1B[37m"
/*
* Initiate log system
* - currently supports stderr and file
*/
uint8_t
nt_log_init(struct neat_ctx *ctx)
{
// set initial timestamp
gettimeofday(&(ctx->tv_init), NULL);
if (ctx->neat_log_fd == NULL) {
ctx->neat_log_fd = stderr;
}
nt_log(ctx, NEAT_LOG_INFO, "%s - opening logfile ...", __func__);
return RETVAL_SUCCESS;
}
/*
* Set the NEAT log level
*/
void
neat_log_level(struct neat_ctx *ctx, uint8_t level)
{
switch (level) {
case NEAT_LOG_OFF:
ctx->log_level = NEAT_LOG_OFF;
break;
case NEAT_LOG_ERROR:
ctx->log_level = NEAT_LOG_ERROR;
break;
case NEAT_LOG_WARNING:
ctx->log_level = NEAT_LOG_WARNING;
break;
case NEAT_LOG_INFO:
ctx->log_level = NEAT_LOG_INFO;
break;
case NEAT_LOG_DEBUG:
ctx->log_level = NEAT_LOG_DEBUG;
break;
default:
ctx->log_level = NEAT_LOG_DEBUG;
fprintf(stderr, "%s - unknown log-level - using default\n", __func__);
break;
}
}
uint8_t
neat_log_file(struct neat_ctx *ctx, const char* file_name)
{
// determine output fd
if (file_name != NULL) {
nt_log(ctx, NEAT_LOG_INFO, "%s - using logfile: %s", __func__, file_name);
ctx->neat_log_fd = fopen(file_name, "w");
if (ctx->neat_log_fd == NULL) {
ctx->neat_log_fd = stderr;
nt_log(ctx, NEAT_LOG_ERROR, "%s - could not open logfile, using stderr", __func__);
return RETVAL_FAILURE;
}
return RETVAL_SUCCESS;
} else {
ctx->neat_log_fd = stderr;
return RETVAL_SUCCESS;
}
}
/*
* Write log entry
*/
void
nt_log(struct neat_ctx *ctx, uint8_t level, const char* format, ...)
{
struct timeval tv_now, tv_diff;
if (ctx == NULL) {
#ifdef STATIC_LOG
printf("[STATIC_LOG] ");
va_list argptr;
va_start(argptr, format);
vprintf(format, argptr);
va_end(argptr);
printf("\n");
#endif
return;
}
// skip unwanted loglevels
if (ctx->log_level < level) {
return;
}
if (ctx->neat_log_fd == NULL) {
fprintf(stderr, "neat_log_fd is NULL - nt_log_init() required!\n");
return;
}
gettimeofday(&tv_now, NULL);
tv_diff.tv_sec = tv_now.tv_sec - ctx->tv_init.tv_sec;
if (ctx->tv_init.tv_usec <= tv_now.tv_usec) {
tv_diff.tv_usec = tv_now.tv_usec - ctx->tv_init.tv_usec;
} else {
tv_diff.tv_sec -= 1;
tv_diff.tv_usec = 1000000 + tv_now.tv_usec - ctx->tv_init.tv_usec;
}
if (isatty(fileno(ctx->neat_log_fd))) {
switch (level) {
case NEAT_LOG_ERROR:
fprintf(ctx->neat_log_fd, RED);
break;
case NEAT_LOG_WARNING:
fprintf(ctx->neat_log_fd, YEL);
break;
case NEAT_LOG_INFO:
fprintf(ctx->neat_log_fd, GRN);
break;
case NEAT_LOG_DEBUG:
//fprintf(neat_log_fd, WHT);
break;
}
}
fprintf(ctx->neat_log_fd, "[%4ld.%06ld]", (long)tv_diff.tv_sec, (long)tv_diff.tv_usec);
switch (level) {
case NEAT_LOG_ERROR:
fprintf(ctx->neat_log_fd, "[ERR] ");
break;
case NEAT_LOG_WARNING:
fprintf(ctx->neat_log_fd, "[WRN] ");
break;
case NEAT_LOG_INFO:
fprintf(ctx->neat_log_fd, "[INF] ");
break;
case NEAT_LOG_DEBUG:
fprintf(ctx->neat_log_fd, "[DBG] ");
break;
}
va_list argptr;
va_start(argptr, format);
vfprintf(ctx->neat_log_fd, format, argptr);
va_end(argptr);
fprintf(ctx->neat_log_fd, "\n"); // xxx:ugly solution...
if (isatty(fileno(ctx->neat_log_fd))) {
fprintf(ctx->neat_log_fd, KNRM);
}
}
/*
* Write log entry for usrsctp
*/
void
neat_log_usrsctp(const char* format, ...)
{
#if 0
// this neats to get a ctx from somwhere!
if (ctx->neat_log_fd == NULL) {
fprintf(stderr, "neat_log_fd is NULL - nt_log_init() required!\n");
return;
}
fprintf(ctx->neat_log_fd, "[DBG] ");
va_list argptr;
va_start(argptr, format);
vfprintf(ctx->neat_log_fd, format, argptr);
va_end(argptr);
#endif
}
/*
* Close logfile
*/
uint8_t
nt_log_close(struct neat_ctx *ctx)
{
if (ctx == NULL) {
return RETVAL_FAILURE;
}
nt_log(ctx, NEAT_LOG_INFO, "%s - closing logfile ...", __func__);
if (ctx->neat_log_fd != stderr) {
if (fclose(ctx->neat_log_fd) == 0) {
return RETVAL_SUCCESS;
} else {
return RETVAL_FAILURE;
}
}
return RETVAL_SUCCESS;
}
#else // NEAT_LOG
uint8_t
nt_log_init(struct neat_ctx *ctx) {
return RETVAL_SUCCESS;
}
void
neat_log_level(struct neat_ctx *ctx, uint8_t level) {
return;
}
uint8_t
neat_log_file(struct neat_ctx *ctx, const char* file_name)
{
return RETVAL_SUCCESS;
}
void
nt_log(struct neat_ctx *ctx, uint8_t level, const char* format, ...)
{
return;
}
void
neat_log_usrsctp(const char* format, ...)
{
return;
}
uint8_t
nt_log_close(struct neat_ctx *ctx)
{
return RETVAL_SUCCESS;
}
#endif