forked from PDXostc/reliable_multicast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmc_log.c
286 lines (219 loc) · 5.98 KB
/
rmc_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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (C) 2018, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author: Magnus Feuer ([email protected])
// Simple logging
#include "rmc_common.h"
#include "reliable_multicast.h"
#include "rmc_log.h"
#include "string.h"
#include <unistd.h>
#include <stdlib.h>
static usec_timestamp_t start_time = 0;
int _rmc_log_level = RMC_LOG_LEVEL_NONE;
int _rmc_log_use_color = -1;
int _rmc_log_use_color_calculated = 0;
FILE *_rmc_log_file = 0;
// Run when the library is loaded
static void __attribute__((constructor)) set_log_level_on_env(void)
{
char* log_level = getenv("RMC_LOG_LEVEL");
if (!log_level)
return;
rmc_set_log_level(atoi(log_level));
}
void rmc_log_set_start_time(void)
{
start_time = rmc_usec_monotonic_timestamp();
}
usec_timestamp_t rmc_log_get_start_time(void)
{
return start_time;
}
void rmc_log_use_color(int use_color)
{
_rmc_log_use_color = use_color;
_rmc_log_use_color_calculated = 0;
}
int rmc_set_log_level(int log_level)
{
if (log_level < RMC_LOG_LEVEL_NONE || log_level > RMC_LOG_LEVEL_DEBUG) {
rmc_log(RMC_LOG_LEVEL_WARNING, __FUNCTION__, __FILE__, __LINE__, -1,
"Illegal log level: %d. Legal values [%d-%d]", log_level, RMC_LOG_LEVEL_NONE, RMC_LOG_LEVEL_DEBUG);
return 1;
}
_rmc_log_level = log_level;
return 0;
}
void rmc_log_set_file(FILE* file)
{
_rmc_log_file = file;
// If necessary, recalculate if we should use colors or not.
if (_rmc_log_use_color_calculated) {
if (isatty(fileno(_rmc_log_file)))
_rmc_log_use_color = 1;
else
_rmc_log_use_color = 0;
}
}
const char* rmc_log_color_flashing_red()
{
return _rmc_log_use_color?"\033[5;38;2;192;0;0m":"";
}
const char* rmc_log_color_light_red()
{
return _rmc_log_use_color?"\033[38;2;255;204;204m":"";
}
const char* rmc_log_color_red()
{
return _rmc_log_use_color?"\033[38;2;192;0;0m":"";
}
const char* rmc_log_color_dark_red()
{
return _rmc_log_use_color?"\033[38;2;255;0;0m":"";
}
const char* rmc_log_color_orange()
{
return _rmc_log_use_color?"\033[38;2;255;128;0m":"";
}
const char* rmc_log_color_yellow()
{
return _rmc_log_use_color?"\033[38;2;255;255;0m":"";
}
const char* rmc_log_color_light_blue()
{
return _rmc_log_use_color?"\033[38;2;0;255;255m":"";
}
const char* rmc_log_color_blue()
{
return _rmc_log_use_color?"\033[38;2;0;128;255m":"";
}
const char* rmc_log_color_dark_blue()
{
return _rmc_log_use_color?"\033[38;2;0;0;255m":"";
}
const char* rmc_log_color_light_green()
{
return _rmc_log_use_color?"\033[38;2;153;255;153m":"";
}
const char* rmc_log_color_green()
{
return _rmc_log_use_color?"\033[38;2;0;255;0m":"";
}
const char* rmc_log_color_dark_green()
{
return _rmc_log_use_color?"\033[38;2;0;204;0m":"";
}
const char* rmc_log_color_faint()
{
return _rmc_log_use_color?"\033[2m":"";
}
const char* rmc_log_color_none()
{
return _rmc_log_use_color?"\033[0m":"";
}
const char* rmc_index_color(int index)
{
switch(index) {
case -1:
return rmc_log_color_faint();
case 0:
return rmc_log_color_dark_blue();
case 1:
return rmc_log_color_dark_green();
case 2:
return rmc_log_color_light_blue();
case 3:
return rmc_log_color_light_green();
case 4:
return rmc_log_color_light_red();
case 5:
return rmc_log_color_green();
case 6:
return rmc_log_color_blue();
case 7:
return rmc_log_color_dark_red();
case 8:
return rmc_log_color_red();
default:
return rmc_log_color_none();
}
}
void rmc_log(int log_level, const char* func, const char* file, int line, uint16_t index, const char* fmt, ...)
{
const char* color = 0;
const char* tag = 0;
char index_str[32];
va_list ap;
// Default rmc_log_file, if not set.
if (!_rmc_log_file)
_rmc_log_file = stdout;
// If use color is -1, then check if we are on a tty or not
if (_rmc_log_use_color == -1) {
_rmc_log_use_color_calculated = 1;
if (isatty(fileno(_rmc_log_file)))
_rmc_log_use_color = 1;
else
_rmc_log_use_color = 0;
}
switch(index) {
case RMC_NIL_INDEX:
strcpy(index_str, " ");
break;
case RMC_MULTICAST_INDEX:
sprintf(index_str, "%s[UDP]%s", rmc_log_color_orange(), rmc_log_color_none());
break;
case RMC_LISTEN_INDEX:
sprintf(index_str, "%s[CTL]%s", rmc_log_color_yellow(), rmc_log_color_none());
break;
default:
sprintf(index_str, "%s[%.3d]%s", rmc_index_color(index), index, rmc_log_color_none());
}
switch(log_level) {
case RMC_LOG_LEVEL_DEBUG:
color = rmc_log_color_none();
tag = "[D]";
break;
case RMC_LOG_LEVEL_COMMENT:
color = rmc_log_color_green();
tag = "[C]";
break;
case RMC_LOG_LEVEL_INFO:
color = rmc_log_color_blue();
tag = "[I]";
break;
case RMC_LOG_LEVEL_WARNING:
color = rmc_log_color_orange();
tag = "[W]";
break;
case RMC_LOG_LEVEL_ERROR:
color = rmc_log_color_red();
tag = "[E]";
break;
case RMC_LOG_LEVEL_FATAL:
color = rmc_log_color_flashing_red();
tag = "[F]";
break;
default:
color = rmc_log_color_none();
tag = "[?]";
break;
}
fprintf(_rmc_log_file, "%s%s%s %ld %s %s%s:%d%s ",
color,
tag,
rmc_log_color_none(),
start_time?((rmc_usec_monotonic_timestamp() - start_time)/1000):0 ,
index_str,
rmc_log_color_faint(),
file,
line,
rmc_log_color_none());
va_start(ap, fmt);
vfprintf(_rmc_log_file, fmt, ap);
va_end(ap);
fputc('\n', _rmc_log_file);
}
// 8592