Debian Codegraph @

src/log.c

raw symbols SCIP
1 /*
2 * AIDE (Advanced Intrusion Detection Environment)
3 *
4 * Copyright (C) 2020,2022,2023,2025 Hannes von Haugwitz
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "config.h"cxx . . $ `<file>/include/config.h`/
22 #include <stdarg.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <pthread.h>
28
29 #include "log.h"cxx . . $ `<file>/include/log.h`/
30 #include "errorcodes.h"cxx . . $ `<file>/include/errorcodes.h`/
31 #include "util.h"cxx . . $ `<file>/include/util.h`/
32
33 LOG_LEVELcxx . . $ LOG_LEVEL# prev_log_levelcxx . . $ prev_log_level. = LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET.;
34 LOG_LEVELcxx . . $ LOG_LEVEL# log_levelcxx . . $ log_level. = LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET.;
35
36 typedef struct log_cachecxx . . $ log_cache# {
37 LOG_LEVELcxx . . $ LOG_LEVEL# levelcxx . . $ log_cache#level.;
38 char *messagecxx . . $ log_cache#message.;
39 } log_cachecxx . . $ log_cache#;
40
41 log_cachecxx . . $ log_cache# *cached_linescxx . . $ cached_lines. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
42 int ncachedlinescxx . . $ ncachedlines. = 0;
43
44 int colored_logcxx . . $ colored_log. = -1;
45
46 pthread_mutex_tcxx . . $ pthread_mutex_t# log_mutexcxx . . $ log_mutex. = PTHREAD_MUTEX_INITIALIZERcxx . . $ `<external>/14712363683955484316/pthread.h:90:9`!;
47
48 struct log_levelcxx . . $ log_level# {
49 LOG_LEVELcxx . . $ LOG_LEVEL# log_levelcxx . . $ log_level#log_level.;
50 const char *namecxx . . $ log_level#name.;
51 const char *log_stringcxx . . $ log_level#log_string.;
52 const char *colored_log_stringcxx . . $ log_level#colored_log_string.;
53 };
54
55 static struct log_levelcxx . . $ log_level# log_level_arraycxx . . $ log_level_array.[] = {
56 { LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR., "error", " ERROR", COLOR_B_REDcxx . . $ `include/util.h:58:9`! " ERROR" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
57 { LOG_LEVEL_WARNINGcxx . . $ LOG_LEVEL_WARNING., "warning", "WARNING", COLOR_B_YELLOWcxx . . $ `include/util.h:60:9`! "WARNING" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
58 { LOG_LEVEL_NOTICEcxx . . $ LOG_LEVEL_NOTICE., "notice", " NOTICE", COLOR_L_ORANGEcxx . . $ `include/util.h:51:9`! " NOTICE" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
59 { LOG_LEVEL_INFOcxx . . $ LOG_LEVEL_INFO., "info", " INFO", COLOR_L_GREENcxx . . $ `include/util.h:50:9`! " INFO" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
60 { LOG_LEVEL_COMPAREcxx . . $ LOG_LEVEL_COMPARE., "compare", "COMPARE", COLOR_B_BLUEcxx . . $ `include/util.h:61:9`! "COMPARE" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
61 { LOG_LEVEL_RULEcxx . . $ LOG_LEVEL_RULE., "rule", " RULE", COLOR_L_BLUEcxx . . $ `include/util.h:52:9`! " RULE" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
62 { LOG_LEVEL_CONFIGcxx . . $ LOG_LEVEL_CONFIG., "config", " CONFIG", COLOR_L_CYANcxx . . $ `include/util.h:54:9`! " CONFIG" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
63 { LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "debug", " DEBUG", COLOR_B_PURPLEcxx . . $ `include/util.h:62:9`! " DEBUG" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
64 { LOG_LEVEL_LIMITcxx . . $ LOG_LEVEL_LIMIT., "limit", " LIMIT", COLOR_L_GRAYcxx . . $ `include/util.h:55:9`! " LIMIT" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
65 { LOG_LEVEL_THREADcxx . . $ LOG_LEVEL_THREAD., "thread", " THREAD", COLOR_B_CYANcxx . . $ `include/util.h:63:9`! " THREAD" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
66 { LOG_LEVEL_TRACEcxx . . $ LOG_LEVEL_TRACE., "trace", " TRACE", COLOR_L_PURPLEcxx . . $ `include/util.h:53:9`! " TRACE" COLOR_RESETcxx . . $ `include/util.h:66:9`! },
67 { 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! , NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! },
68 };
69
70 static const char* get_log_stringcxx . . $ get_log_string(3ac032694a5a1d08).(LOG_LEVELcxx . . $ LOG_LEVEL# level) {
71 if (colored_logcxx . . $ colored_log.) {
72 return log_level_arraycxx . . $ log_level_array.[level-1].colored_log_stringcxx . . $ log_level#colored_log_string.;
73 } else {
74 return log_level_arraycxx . . $ log_level_array.[level-1].log_stringcxx . . $ log_level#log_string.;
75 }
76 }
77
78 static void cache_linecxx . . $ cache_line(4e14377549b89a5a).(LOG_LEVELcxx . . $ LOG_LEVEL#, const char*, va_listcxx . . $ va_list#)
79 #ifdef __GNUC__cxx . . $ `<built-in>:8:9`!
80 __attribute__ ((format (printf, 2, 0)))
81 #endif
82 ;
83 static void cache_linecxx . . $ cache_line(4e14377549b89a5a).(LOG_LEVELcxx . . $ LOG_LEVEL# level, const char* format, va_listcxx . . $ va_list# ap) {
84 int n;
85
86 cached_linescxx . . $ cached_lines. = realloccxx . . $ realloc(417ab160f6baadc6).(cached_linescxx . . $ cached_lines., (ncachedlinescxx . . $ ncachedlines.+1) * sizeof(log_cachecxx . . $ log_cache#)); /* freed in log_cached_lines() */
87 if (cached_linescxx . . $ cached_lines. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
88 stderr_msgcxx . . $ stderr_msg(326aeb464d16410c).("%s: realloc: failed to allocate memory\n", get_log_stringcxx . . $ get_log_string(3ac032694a5a1d08).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.));
89 exitcxx . . $ exit(3ca292c338df9bb3).(MEMORY_ALLOCATION_FAILUREcxx . . $ `include/errorcodes.h:33:9`!);
90 }
91
92 cached_linescxx . . $ cached_lines.[ncachedlinescxx . . $ ncachedlines.].levelcxx . . $ log_cache#level. = level;
93 cached_linescxx . . $ cached_lines.[ncachedlinescxx . . $ ncachedlines.].messagecxx . . $ log_cache#message. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
94
95 va_listcxx . . $ va_list# aq;
96 va_copycxx . . $ `<external>/8293021631987149590/stdarg.h:57:9`!(aq, ap);
97 n = vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, format, aq) + 1;
98 va_endcxx . . $ `<external>/8293021631987149590/stdarg.h:53:9`!(aq);
99
100 int size = n * sizeof(char);
101 cached_linescxx . . $ cached_lines.[ncachedlinescxx . . $ ncachedlines.].messagecxx . . $ log_cache#message. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(size); /* freed in log_cached_lines() */
102 if (cached_linescxx . . $ cached_lines.[ncachedlinescxx . . $ ncachedlines.].messagecxx . . $ log_cache#message. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
103 stderr_msgcxx . . $ stderr_msg(326aeb464d16410c).("%s: malloc: failed to allocate %d bytes of memory\n", get_log_stringcxx . . $ get_log_string(3ac032694a5a1d08).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.), size);
104 exitcxx . . $ exit(3ca292c338df9bb3).(MEMORY_ALLOCATION_FAILUREcxx . . $ `include/errorcodes.h:33:9`!);
105 }
106
107 vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(cached_linescxx . . $ cached_lines.[ncachedlinescxx . . $ ncachedlines.].messagecxx . . $ log_cache#message., n, format, ap);
108 ncachedlinescxx . . $ ncachedlines.++;
109 }
110
111 const char * get_log_level_namecxx . . $ get_log_level_name(3ac032694a5a1d08).(LOG_LEVELcxx . . $ LOG_LEVEL# level) {
112 return level?log_level_arraycxx . . $ log_level_array.[level-1].namecxx . . $ log_level#name.:NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
113 }
114
115 static void log_cached_linescxx . . $ log_cached_lines(49f6e7a06ebc5aa8).(void) {
116 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&log_mutexcxx . . $ log_mutex.);
117 for(int i = 0; i < ncachedlinescxx . . $ ncachedlines.; ++i) {
118 LOG_LEVELcxx . . $ LOG_LEVEL# level = cached_linescxx . . $ cached_lines.[i].levelcxx . . $ log_cache#level.;
119 if (level == LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR. || level <= log_levelcxx . . $ log_level.) {
120 char *msg_safe = stresccxx . . $ stresc(a8b8e32347182212).(cached_linescxx . . $ cached_lines.[i].messagecxx . . $ log_cache#message.);
121 stderr_msgcxx . . $ stderr_msg(326aeb464d16410c).("%s: %s\n", get_log_stringcxx . . $ get_log_string(3ac032694a5a1d08).(level), msg_safe);
122 freecxx . . $ free(5d22bdacc48458e8).(msg_safe);
123 }
124 freecxx . . $ free(5d22bdacc48458e8).(cached_linescxx . . $ cached_lines.[i].messagecxx . . $ log_cache#message.);
125 }
126 ncachedlinescxx . . $ ncachedlines. = 0;
127 freecxx . . $ free(5d22bdacc48458e8).(cached_linescxx . . $ cached_lines.);
128 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&log_mutexcxx . . $ log_mutex.);
129 }
130
131 static void vlog_msgcxx . . $ vlog_msg(4e14377549b89a5a).(LOG_LEVELcxx . . $ LOG_LEVEL#, const char*, va_listcxx . . $ va_list#)
132 #ifdef __GNUC__cxx . . $ `<built-in>:8:9`!
133 __attribute__ ((format (printf, 2, 0)))
134 #endif
135 ;
136 static void vlog_msgcxx . . $ vlog_msg(4e14377549b89a5a).(LOG_LEVELcxx . . $ LOG_LEVEL# level,const char* format, va_listcxx . . $ va_list# ap) {
137 if (level != LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR. && (log_levelcxx . . $ log_level. == LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET. || colored_logcxx . . $ colored_log. < 0)) {
138 pthread_mutex_lockcxx . . $ pthread_mutex_lock(d89ae235521888f9).(&log_mutexcxx . . $ log_mutex.);
139 cache_linecxx . . $ cache_line(4e14377549b89a5a).(level, format, ap);
140 pthread_mutex_unlockcxx . . $ pthread_mutex_unlock(d89ae235521888f9).(&log_mutexcxx . . $ log_mutex.);
141 } else if (level == LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR. || level <= log_levelcxx . . $ log_level.) {
142 va_listcxx . . $ va_list# aq;
143 va_copycxx . . $ `<external>/8293021631987149590/stdarg.h:57:9`!(aq, ap);
144 int n = vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, format, aq) + 1;
145 va_endcxx . . $ `<external>/8293021631987149590/stdarg.h:53:9`!(aq);
146
147 int size = n * sizeof(char);
148 char *msg_unsafe = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(size);
149 if (msg_unsafe == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) {
150 stderr_msgcxx . . $ stderr_msg(326aeb464d16410c).("%s: malloc: failed to allocate %d bytes of memory\n", get_log_stringcxx . . $ get_log_string(3ac032694a5a1d08).(LOG_LEVEL_ERRORcxx . . $ LOG_LEVEL_ERROR.), size);
151 exitcxx . . $ exit(3ca292c338df9bb3).(MEMORY_ALLOCATION_FAILUREcxx . . $ `include/errorcodes.h:33:9`!);
152 }
153
154 vsnprintfcxx . . $ vsnprintf(a85eea89d1bd834f).(msg_unsafe, n, format, ap);
155 char *msg_safe = stresccxx . . $ stresc(a8b8e32347182212).(msg_unsafe);
156 freecxx . . $ free(5d22bdacc48458e8).(msg_unsafe);
157 stderr_msgcxx . . $ stderr_msg(326aeb464d16410c).("%s: %s\n", get_log_stringcxx . . $ get_log_string(3ac032694a5a1d08).(level), msg_safe);
158 freecxx . . $ free(5d22bdacc48458e8).(msg_safe);
159 }
160 }
161
162 boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! is_log_level_unsetcxx . . $ is_log_level_unset(c9857a73d68e59ba).(void) {
163 return log_levelcxx . . $ log_level. == LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET.;
164 }
165
166 LOG_LEVELcxx . . $ LOG_LEVEL# get_log_level_from_stringcxx . . $ get_log_level_from_string(1c46f449ef604568).(char* val) {
167 struct log_levelcxx . . $ log_level# *level;
168
169 for (level = log_level_arraycxx . . $ log_level_array.; level->log_levelcxx . . $ log_level#log_level. != 0; level++) {
170 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(val, level->namecxx . . $ log_level#name.) == 0) {
171 return level->log_levelcxx . . $ log_level#log_level.;
172 }
173 }
174 return LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET.;
175 }
176
177 void set_colored_logcxx . . $ set_colored_log(b45ddcbc8423892c).(boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! color) {
178 colored_logcxx . . $ colored_log. = color;
179 if (ncachedlinescxx . . $ ncachedlines. && log_levelcxx . . $ log_level. != LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET.) {
180 log_cached_linescxx . . $ log_cached_lines(49f6e7a06ebc5aa8).();
181 }
182 }
183
184 void set_log_levelcxx . . $ set_log_level(acc15b7e06406d4e).(LOG_LEVELcxx . . $ LOG_LEVEL# level) {
185 log_levelcxx . . $ log_level. = level;
186 if (colored_logcxx . . $ colored_log. >= 0 && ncachedlinescxx . . $ ncachedlines. && log_levelcxx . . $ log_level. != LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET.) {
187 log_cached_linescxx . . $ log_cached_lines(49f6e7a06ebc5aa8).();
188 }
189 }
190
191 LOG_LEVELcxx . . $ LOG_LEVEL# toogle_log_levelcxx . . $ toogle_log_level(e02209862df775c8).(LOG_LEVELcxx . . $ LOG_LEVEL# level) {
192 if (prev_log_levelcxx . . $ prev_log_level. != LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET. && log_levelcxx . . $ log_level. != level) {
193 set_log_levelcxx . . $ set_log_level(acc15b7e06406d4e).(level);
194 } else if (log_levelcxx . . $ log_level. != level || prev_log_levelcxx . . $ prev_log_level. != LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET.) {
195 if (prev_log_levelcxx . . $ prev_log_level. == LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET.) {
196 prev_log_levelcxx . . $ prev_log_level. = log_levelcxx . . $ log_level.;
197 set_log_levelcxx . . $ set_log_level(acc15b7e06406d4e).(level);
198 } else {
199 set_log_levelcxx . . $ set_log_level(acc15b7e06406d4e).(prev_log_levelcxx . . $ prev_log_level.);
200 prev_log_levelcxx . . $ prev_log_level. = LOG_LEVEL_UNSETcxx . . $ LOG_LEVEL_UNSET.;
201 }
202 }
203 return log_levelcxx . . $ log_level.;
204 }
205
206 void log_msgcxx . . $ log_msg(e585f2b5603b5bf9).(LOG_LEVELcxx . . $ LOG_LEVEL# level, const char* format, ...) {
207 va_listcxx . . $ va_list# argp;
208 va_startcxx . . $ `<external>/8293021631987149590/stdarg.h:51:9`!(argp, format);
209 vlog_msgcxx . . $ vlog_msg(4e14377549b89a5a).(level, format, argp);
210 va_endcxx . . $ `<external>/8293021631987149590/stdarg.h:53:9`!(argp);
211 }