forked from slepp/rtlmux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slog.c
425 lines (371 loc) · 11.9 KB
/
slog.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* The MIT License (MIT)
*
* Copyleft (C) 2015 Sun Dro (a.k.a. kala13x)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdarg.h>
#include <limits.h>
#include <errno.h>
#include <time.h>
#include "slog.h"
#ifdef __MACH__
#include <mach/mach_time.h>
#define ORWL_NANO (+1.0E-9)
#define ORWL_GIGA UINT64_C(1000000000)
static double orwl_timebase = 0.0;
static uint64_t orwl_timestart = 0;
struct timespec orwl_gettime(void) {
// be more careful in a multithreaded environement
if (!orwl_timestart) {
mach_timebase_info_data_t tb = { 0 };
mach_timebase_info(&tb);
orwl_timebase = tb.numer;
orwl_timebase /= tb.denom;
orwl_timestart = mach_absolute_time();
}
struct timespec t;
double diff = (mach_absolute_time() - orwl_timestart) * orwl_timebase;
t.tv_sec = diff * ORWL_NANO;
t.tv_nsec = diff - (t.tv_sec * ORWL_GIGA);
return t;
}
#endif
/* Max size of string */
#define MAXMSG 8196
/* Flags */
static SlogFlags slg;
static pthread_mutex_t slog_mutex;
/*
* slog_get_date - Intialize date with system date.
* Argument is pointer of SlogDate structure.
*/
void slog_get_date(SlogDate *sdate)
{
time_t rawtime;
struct tm timeinfo;
struct timespec now;
rawtime = time(NULL);
localtime_r(&rawtime, &timeinfo);
/* Get System Date */
sdate->year = timeinfo.tm_year+1900;
sdate->mon = timeinfo.tm_mon+1;
sdate->day = timeinfo.tm_mday;
sdate->hour = timeinfo.tm_hour;
sdate->min = timeinfo.tm_min;
sdate->sec = timeinfo.tm_sec;
/* Get micro seconds */
#ifdef __MACH__
now = orwl_gettime();
#else
clock_gettime(CLOCK_MONOTONIC, &now);
#endif
sdate->usec = now.tv_nsec / 10000000;
}
/*
* Get library version. Function returns version and build number of slog
* library. Return value is char pointer. Argument min is flag for output
* format. If min is 1, function returns version in full format, if flag
* is 0 function returns only version numbers, For examle: 1.3.0
-*/
const char* slog_version(int min)
{
static char verstr[128];
/* Version short */
if (min) sprintf(verstr, "%d.%d.%d",
SLOGVERSION_MAX, SLOGVERSION_MIN, SLOGBUILD_NUM);
/* Version long */
else sprintf(verstr, "%d.%d build %d (%s)",
SLOGVERSION_MAX, SLOGVERSION_MIN, SLOGBUILD_NUM, __DATE__);
return verstr;
}
/*
* strclr - Colorize string. Function takes color value and string
* and returns colorized string as char pointer. First argument clr
* is color value (if it is invalid, function retunrs NULL) and second
* is string with va_list of arguments which one we want to colorize.
*/
char* strclr(const char* clr, char* str, ...)
{
/* String buffers */
static char output[MAXMSG];
char string[MAXMSG];
/* Read args */
va_list args;
va_start(args, str);
vsprintf(string, str, args);
va_end(args);
/* Colorize string */
sprintf(output, "%s%s%s", clr, string, CLR_RESET);
return output;
}
/*
* log_to_file - Save log in file. Argument aut is string which
* we want to log. Argument fname is log file path and sdate is
* SlogDate structure variable, we need it to create filename.
*/
void slog_to_file(char *out, const char *fname, SlogDate *sdate)
{
/* Used variables */
char filename[PATH_MAX];
/* Create log filename with date */
if (slg.filestamp)
{
snprintf(filename, sizeof(filename), "%s-%02d-%02d-%02d.log",
fname, sdate->year, sdate->mon, sdate->day);
}
else snprintf(filename, sizeof(filename), "%s.log", fname);
/* Open file pointer */
FILE *fp = fopen(filename, "a");
if (fp == NULL) return;
/* Write key in file */
fprintf(fp, "%s", out);
/* Close file pointer */
fclose(fp);
}
/*
* parse_config - Parse config file. Argument cfg_name is path
* of config file name to be parsed. Function opens config file
* and parses LOGLEVEL and LOGTOFILE flags from it.
*/
int parse_config(const char *cfg_name)
{
/* Used variables */
FILE *file;
char *line = NULL;
size_t len = 0;
ssize_t read;
int ret = 0;
/* Open file pointer */
file = fopen(cfg_name, "r");
if(file == NULL) return 0;
/* Line-by-line read cfg file */
while ((read = getline(&line, &len, file)) != -1)
{
/* Find level in file */
if(strstr(line, "LOGLEVEL") != NULL)
{
/* Get logtofile flag */
slg.level = atoi(line+8);
ret = 1;
}
if(strstr(line, "LOGFILELEVEL") != NULL)
{
/* Get logtofile flag */
slg.file_level = atoi(line+12);
ret = 1;
}
else if(strstr(line, "LOGTOFILE") != NULL)
{
/* Get log level */
slg.to_file = atoi(line+9);
ret = 1;
}
else if(strstr(line, "PRETTYLOG") != NULL)
{
/* Get log type */
slg.pretty = atoi(line+9);
ret = 1;
}
else if(strstr(line, "FILESTAMP") != NULL)
{
/* Get filestamp */
slg.filestamp = atoi(line+9);
ret = 1;
}
}
/* Cleanup */
if (line) free(line);
fclose(file);
return ret;
}
/*
* Retunr string in slog format. Function takes arguments
* and returns string in slog format without printing and
* saveing in file. Return value is char pointer.
*/
char* slog_get(SlogDate *pDate, char *msg, ...)
{
/* Used variables */
static char output[MAXMSG];
char string[MAXMSG];
/* Read args */
va_list args;
va_start(args, msg);
vsprintf(string, msg, args);
va_end(args);
/* Generate output string with date */
sprintf(output, "%02d.%02d.%02d-%02d:%02d:%02d.%02d - %s",
pDate->year, pDate->mon, pDate->day, pDate->hour,
pDate->min, pDate->sec, pDate->usec, string);
/* Return output */
return output;
}
/*
* slog - Log exiting process. Function takes arguments and saves
* log in file if LOGTOFILE flag is enabled from config. Otherwise
* it just prints log without saveing in file. Argument level is
* logging level and flag is slog flags defined in slog.h header.
*/
void slog(int level, int flag, const char *msg, ...)
{
/* Lock for safe */
if (slg.td_safe)
{
if (pthread_mutex_lock(&slog_mutex))
{
printf("<%s:%d> %s: [ERROR] Can not lock mutex: %d\n",
__FILE__, __LINE__, __FUNCTION__, errno);
exit(EXIT_FAILURE);
}
}
/* Used variables */
SlogDate mdate;
char string[MAXMSG];
char prints[MAXMSG];
char color[32], alarm[32];
char *output;
slog_get_date(&mdate);
bzero(string, sizeof(string));
bzero(prints, sizeof(prints));
bzero(color, sizeof(color));
bzero(alarm, sizeof(alarm));
/* Read args */
va_list args;
va_start(args, msg);
vsprintf(string, msg, args);
va_end(args);
/* Check logging levels */
if(!level || level <= slg.level || level <= slg.file_level)
{
/* Handle flags */
switch(flag)
{
case SLOG_LIVE:
strncpy(color, CLR_NORMAL, sizeof(color));
strncpy(alarm, "LIVE", sizeof(alarm));
break;
case SLOG_INFO:
strncpy(color, CLR_GREEN, sizeof(color));
strncpy(alarm, "INFO", sizeof(alarm));
break;
case SLOG_WARN:
strncpy(color, CLR_YELLOW, sizeof(color));
strncpy(alarm, "WARN", sizeof(alarm));
break;
case SLOG_DEBUG:
strncpy(color, CLR_BLUE, sizeof(color));
strncpy(alarm, "DEBUG", sizeof(alarm));
break;
case SLOG_ERROR:
strncpy(color, CLR_RED, sizeof(color));
strncpy(alarm, "ERROR", sizeof(alarm));
break;
case SLOG_FATAL:
strncpy(color, CLR_RED, sizeof(color));
strncpy(alarm, "FATAL", sizeof(alarm));
break;
case SLOG_PANIC:
strncpy(color, CLR_WHITE, sizeof(color));
strncpy(alarm, "PANIC", sizeof(alarm));
break;
case SLOG_NONE:
strncpy(prints, string, sizeof(string));
break;
default:
strncpy(prints, string, sizeof(string));
flag = SLOG_NONE;
break;
}
/* Print output */
if (level <= slg.level || slg.pretty)
{
if (flag != SLOG_NONE) sprintf(prints, "[%s] %s", strclr(color, alarm), string);
if (level <= slg.level) printf("%s", slog_get(&mdate, "%s\n", prints));
}
/* Save log in file */
if (slg.to_file && level <= slg.file_level)
{
if (slg.pretty) output = slog_get(&mdate, "%s\n", prints);
else
{
if (flag != SLOG_NONE) sprintf(prints, "[%s] %s", alarm, string);
output = slog_get(&mdate, "%s\n", prints);
}
/* Add log line to file */
slog_to_file(output, slg.fname, &mdate);
}
}
/* Done, unlock mutex */
if (slg.td_safe)
{
if (pthread_mutex_unlock(&slog_mutex))
{
printf("<%s:%d> %s: [ERROR] Can not deinitialize mutex: %d\n",
__FILE__, __LINE__, __FUNCTION__, errno);
exit(EXIT_FAILURE);
}
}
}
/*
* Initialize slog library. Function parses config file and reads log
* level and save to file flag from config. First argument is file name
* where log will be saved and second argument conf is config file path
* to be parsedand third argument lvl is log level for this message.
*/
void slog_init(const char* fname, const char* conf, int lvl, int flvl, int t_safe)
{
int status = 0;
/* Set up default values */
slg.level = lvl;
slg.file_level = flvl;
slg.to_file = 0;
slg.pretty = 0;
slg.filestamp = 1;
slg.td_safe = t_safe;
/* Init mutex sync */
if (t_safe)
{
/* Init mutex attribute */
pthread_mutexattr_t m_attr;
if (pthread_mutexattr_init(&m_attr) ||
pthread_mutexattr_settype(&m_attr, PTHREAD_MUTEX_RECURSIVE) ||
pthread_mutex_init(&slog_mutex, &m_attr) ||
pthread_mutexattr_destroy(&m_attr))
{
printf("<%s:%d> %s: [ERROR] Can not initialize mutex: %d\n",
__FILE__, __LINE__, __FUNCTION__, errno);
slg.td_safe = 0;
}
}
/* Parse config file */
if (conf != NULL)
{
slg.fname = fname;
status = parse_config(conf);
}
/* Handle config parser status */
if (!status) slog(0, SLOG_INFO, "Initializing logger values without config");
else slog(0, SLOG_INFO, "Loading logger config from: %s", conf);
}