1
+ #define WOB_FILE "log.c"
2
+
3
+ #define _POSIX_C_SOURCE 199506L
4
+
5
+ #define COLOR_RESET "\x1B[0m"
6
+ #define COLOR_WHITE "\x1B[1;37m"
7
+ #define COLOR_BLACK "\x1B[0;30m"
8
+ #define COLOR_BLUE "\x1B[0;34m"
9
+ #define COLOR_LIGHT_BLUE "\x1B[1;34m"
10
+ #define COLOR_GREEN "\x1B[0;32m"
11
+ #define COLOR_LIGHT_GREEN "\x1B[1;32m"
12
+ #define COLOR_CYAN "\x1B[0;36m"
13
+ #define COLOR_LIGHT_CYAN "\x1B[1;36m"
14
+ #define COLOR_RED "\x1B[0;31m"
15
+ #define COLOR_LIGHT_RED "\x1B[1;31m"
16
+ #define COLOR_PURPLE "\x1B[0;35m"
17
+ #define COLOR_LIGHT_PURPLE "\x1B[1;35m"
18
+ #define COLOR_BROWN "\x1B[0;33m"
19
+ #define COLOR_YELLOW "\x1B[1;33m"
20
+ #define COLOR_GRAY "\x1B[0;30m"
21
+ #define COLOR_LIGHT_GRAY "\x1B[0;37m"
22
+
23
+ #include <errno.h>
24
+ #include <stdarg.h>
25
+ #include <stdbool.h>
26
+ #include <stdint.h>
27
+ #include <stdio.h>
28
+ #include <stdlib.h>
29
+ #include <string.h>
30
+ #include <time.h>
31
+ #include <unistd.h>
32
+
33
+ #include "log.h"
34
+
35
+ static _wob_log_importance min_importance_to_log = _WOB_LOG_WARN ;
36
+
37
+ static bool use_colors = false;
38
+
39
+ static const char * verbosity_names [] = {
40
+ "DEBUG" ,
41
+ "INFO" ,
42
+ "WARN" ,
43
+ "ERROR" ,
44
+ };
45
+
46
+ static const char * verbosity_colors [] = {
47
+ COLOR_LIGHT_CYAN ,
48
+ COLOR_GREEN ,
49
+ COLOR_YELLOW ,
50
+ COLOR_LIGHT_RED ,
51
+ };
52
+
53
+ void
54
+ _wob_log (const _wob_log_importance importance , const char * file , const int line , const char * fmt , ...)
55
+ {
56
+ if (importance < min_importance_to_log ) {
57
+ return ;
58
+ }
59
+
60
+ struct timespec ts ;
61
+ if (clock_gettime (CLOCK_REALTIME , & ts ) != 0 ) {
62
+ fprintf (stderr , "clock_gettime() failed: %s\n" , strerror (errno ));
63
+ ts .tv_sec = 0 ;
64
+ ts .tv_nsec = 0 ;
65
+ }
66
+
67
+ // formatting time via localtime() requires open syscall (to read /etc/localtime)
68
+ // and that is problematic with seccomp rules in place
69
+ if (use_colors ) {
70
+ fprintf (
71
+ stderr ,
72
+ "%jd.%06ld %s%-5s%s %s%s:%d:%s " ,
73
+ (intmax_t ) ts .tv_sec ,
74
+ ts .tv_nsec / 1000 ,
75
+ verbosity_colors [importance ],
76
+ verbosity_names [importance ],
77
+ COLOR_RESET ,
78
+ COLOR_GRAY ,
79
+ file ,
80
+ line ,
81
+ COLOR_RESET );
82
+ }
83
+ else {
84
+ fprintf (stderr , "%jd.%06ld %s %s:%d: " , (intmax_t ) ts .tv_sec , ts .tv_nsec / 1000 , verbosity_names [importance ], file , line );
85
+ }
86
+
87
+ va_list args ;
88
+ va_start (args , fmt );
89
+ vfprintf (stderr , fmt , args );
90
+ va_end (args );
91
+ fprintf (stderr , "\n" );
92
+ }
93
+
94
+ void
95
+ _wob_log_set_level (const _wob_log_importance importance )
96
+ {
97
+ min_importance_to_log = importance ;
98
+ }
99
+
100
+ void
101
+ wob_log_use_colors (const bool colors )
102
+ {
103
+ use_colors = colors ;
104
+ }
105
+
106
+ void
107
+ wob_log_inc_verbosity (void )
108
+ {
109
+ if (min_importance_to_log != _WOB_LOG_DEBUG ) {
110
+ min_importance_to_log -= 1 ;
111
+ wob_log_debug ("Set log level to %s" , verbosity_names [min_importance_to_log ]);
112
+ }
113
+ }
0 commit comments