Commit | Line | Data |
---|---|---|
beb0fb75 PP |
1 | /* |
2 | * This is zf_log.h, modified with Babeltrace prefixes. | |
3 | * See <https://github.com/wonder-mice/zf_log/>. | |
4 | * See logging/LICENSE in the Babeltrace source tree. | |
5 | */ | |
6 | ||
7 | #pragma once | |
8 | ||
9 | #ifndef BABELTRACE_LOGGING_INTERNAL_H | |
10 | #define BABELTRACE_LOGGING_INTERNAL_H | |
11 | ||
91d81473 | 12 | #include <errno.h> |
373c938b PP |
13 | #include <stdlib.h> |
14 | #include <stdio.h> | |
91d81473 | 15 | #include <string.h> |
3fadfbc0 | 16 | #include <babeltrace2/logging.h> |
91d81473 | 17 | #include "common/macros.h" |
beb0fb75 PP |
18 | |
19 | /* To detect incompatible changes you can define BT_LOG_VERSION_REQUIRED to be | |
20 | * the current value of BT_LOG_VERSION before including this file (or via | |
21 | * compiler command line): | |
22 | * | |
23 | * #define BT_LOG_VERSION_REQUIRED 4 | |
578e048b | 24 | * #include "logging.h" |
beb0fb75 PP |
25 | * |
26 | * Compilation will fail when included file has different version. | |
27 | */ | |
28 | #define BT_LOG_VERSION 4 | |
29 | #if defined(BT_LOG_VERSION_REQUIRED) | |
30 | #if BT_LOG_VERSION_REQUIRED != BT_LOG_VERSION | |
31 | #error different bt_log version required | |
32 | #endif | |
33 | #endif | |
34 | ||
35 | /* Log level guideline: | |
36 | * - BT_LOG_FATAL - happened something impossible and absolutely unexpected. | |
37 | * Process can't continue and must be terminated. | |
38 | * Example: division by zero, unexpected modifications from other thread. | |
39 | * - BT_LOG_ERROR - happened something possible, but highly unexpected. The | |
40 | * process is able to recover and continue execution. | |
41 | * Example: out of memory (could also be FATAL if not handled properly). | |
42 | * - BT_LOG_WARN - happened something that *usually* should not happen and | |
43 | * significantly changes application behavior for some period of time. | |
44 | * Example: configuration file not found, auth error. | |
45 | * - BT_LOG_INFO - happened significant life cycle event or major state | |
46 | * transition. | |
47 | * Example: app started, user logged in. | |
48 | * - BT_LOG_DEBUG - minimal set of events that could help to reconstruct the | |
49 | * execution path. Usually disabled in release builds. | |
50 | * - BT_LOG_VERBOSE - all other events. Usually disabled in release builds. | |
51 | * | |
52 | * *Ideally*, log file of debugged, well tested, production ready application | |
53 | * should be empty or very small. Choosing a right log level is as important as | |
54 | * providing short and self descriptive log message. | |
55 | */ | |
56 | #define BT_LOG_VERBOSE BT_LOGGING_LEVEL_VERBOSE | |
57 | #define BT_LOG_DEBUG BT_LOGGING_LEVEL_DEBUG | |
58 | #define BT_LOG_INFO BT_LOGGING_LEVEL_INFO | |
59 | #define BT_LOG_WARN BT_LOGGING_LEVEL_WARN | |
60 | #define BT_LOG_ERROR BT_LOGGING_LEVEL_ERROR | |
61 | #define BT_LOG_FATAL BT_LOGGING_LEVEL_FATAL | |
62 | #define BT_LOG_NONE BT_LOGGING_LEVEL_NONE | |
63 | ||
64 | /* "Current" log level is a compile time check and has no runtime overhead. Log | |
65 | * level that is below current log level it said to be "disabled". Otherwise, | |
66 | * it's "enabled". Log messages that are disabled has no runtime overhead - they | |
67 | * are converted to no-op by preprocessor and then eliminated by compiler. | |
68 | * Current log level is configured per compilation module (.c/.cpp/.m file) by | |
69 | * defining BT_LOG_DEF_LEVEL or BT_LOG_LEVEL. BT_LOG_LEVEL has higer priority | |
70 | * and when defined overrides value provided by BT_LOG_DEF_LEVEL. | |
71 | * | |
72 | * Common practice is to define default current log level with BT_LOG_DEF_LEVEL | |
73 | * in build script (e.g. Makefile, CMakeLists.txt, gyp, etc.) for the entire | |
74 | * project or target: | |
75 | * | |
76 | * CC_ARGS := -DBT_LOG_DEF_LEVEL=BT_LOG_INFO | |
77 | * | |
78 | * And when necessary to override it with BT_LOG_LEVEL in .c/.cpp/.m files | |
79 | * before including bt_log.h: | |
80 | * | |
81 | * #define BT_LOG_LEVEL BT_LOG_VERBOSE | |
578e048b | 82 | * #include "logging.h" |
beb0fb75 | 83 | * |
3f3b1761 PP |
84 | * If both BT_LOG_DEF_LEVEL and BT_LOG_LEVEL are undefined, then |
85 | * BT_LOG_INFO will be used for release builds (BT_DEBUG_MODE is NOT | |
86 | * defined) and BT_LOG_DEBUG otherwise (BT_DEBUG_MODE is defined). | |
beb0fb75 PP |
87 | */ |
88 | #if defined(BT_LOG_LEVEL) | |
89 | #define _BT_LOG_LEVEL BT_LOG_LEVEL | |
90 | #elif defined(BT_LOG_DEF_LEVEL) | |
91 | #define _BT_LOG_LEVEL BT_LOG_DEF_LEVEL | |
92 | #else | |
3f3b1761 | 93 | #ifdef BT_DEBUG_MODE |
beb0fb75 | 94 | #define _BT_LOG_LEVEL BT_LOG_DEBUG |
3f3b1761 PP |
95 | #else |
96 | #define _BT_LOG_LEVEL BT_LOG_INFO | |
beb0fb75 PP |
97 | #endif |
98 | #endif | |
99 | ||
100 | /* "Output" log level is a runtime check. When log level is below output log | |
101 | * level it said to be "turned off" (or just "off" for short). Otherwise it's | |
102 | * "turned on" (or just "on"). Log levels that were "disabled" (see | |
103 | * BT_LOG_LEVEL and BT_LOG_DEF_LEVEL) can't be "turned on", but "enabled" log | |
104 | * levels could be "turned off". Only messages with log level which is | |
105 | * "turned on" will reach output facility. All other messages will be ignored | |
106 | * (and their arguments will not be evaluated). Output log level is a global | |
107 | * property and configured per process using bt_log_set_output_level() function | |
108 | * which can be called at any time. | |
109 | * | |
110 | * Though in some cases it could be useful to configure output log level per | |
111 | * compilation module or per library. There are two ways to achieve that: | |
112 | * - Define BT_LOG_OUTPUT_LEVEL to expresion that evaluates to desired output | |
113 | * log level. | |
114 | * - Copy bt_log.h and bt_log.c files into your library and build it with | |
115 | * BT_LOG_LIBRARY_PREFIX defined to library specific prefix. See | |
116 | * BT_LOG_LIBRARY_PREFIX for more details. | |
117 | * | |
118 | * When defined, BT_LOG_OUTPUT_LEVEL must evaluate to integral value that | |
119 | * corresponds to desired output log level. Use it only when compilation module | |
120 | * is required to have output log level which is different from global output | |
121 | * log level set by bt_log_set_output_level() function. For other cases, | |
122 | * consider defining BT_LOG_LEVEL or using bt_log_set_output_level() function. | |
123 | * | |
124 | * Example: | |
125 | * | |
126 | * #define BT_LOG_OUTPUT_LEVEL g_module_log_level | |
578e048b | 127 | * #include "logging.h" |
beb0fb75 PP |
128 | * static int g_module_log_level = BT_LOG_INFO; |
129 | * static void foo() { | |
130 | * BT_LOGI("Will check g_module_log_level for output log level"); | |
131 | * } | |
132 | * void debug_log(bool on) { | |
133 | * g_module_log_level = on? BT_LOG_DEBUG: BT_LOG_INFO; | |
134 | * } | |
135 | * | |
136 | * Note on performance. This expression will be evaluated each time message is | |
137 | * logged (except when message log level is "disabled" - see BT_LOG_LEVEL for | |
138 | * details). Keep this expression as simple as possible, otherwise it will not | |
139 | * only add runtime overhead, but also will increase size of call site (which | |
140 | * will result in larger executable). The prefered way is to use integer | |
141 | * variable (as in example above). If structure must be used, log_level field | |
142 | * must be the first field in this structure: | |
143 | * | |
144 | * #define BT_LOG_OUTPUT_LEVEL (g_config.log_level) | |
578e048b | 145 | * #include "logging.h" |
beb0fb75 PP |
146 | * struct config { |
147 | * int log_level; | |
148 | * unsigned other_field; | |
149 | * [...] | |
150 | * }; | |
151 | * static config g_config = {BT_LOG_INFO, 0, ...}; | |
152 | * | |
153 | * This allows compiler to generate more compact load instruction (no need to | |
154 | * specify offset since it's zero). Calling a function to get output log level | |
155 | * is generaly a bad idea, since it will increase call site size and runtime | |
156 | * overhead even further. | |
157 | */ | |
158 | #if defined(BT_LOG_OUTPUT_LEVEL) | |
159 | #define _BT_LOG_OUTPUT_LEVEL BT_LOG_OUTPUT_LEVEL | |
160 | #else | |
00a52b39 PP |
161 | /* |
162 | * We disallow this to make sure Babeltrace modules always | |
163 | * have their own local log level. | |
164 | */ | |
165 | #error No log level symbol specified: please define BT_LOG_OUTPUT_LEVEL before including this header. | |
beb0fb75 PP |
166 | #endif |
167 | ||
168 | /* "Tag" is a compound string that could be associated with a log message. It | |
169 | * consists of tag prefix and tag (both are optional). | |
170 | * | |
171 | * Tag prefix is a global property and configured per process using | |
172 | * bt_log_set_tag_prefix() function. Tag prefix identifies context in which | |
173 | * component or module is running (e.g. process name). For example, the same | |
174 | * library could be used in both client and server processes that work on the | |
175 | * same machine. Tag prefix could be used to easily distinguish between them. | |
176 | * For more details about tag prefix see bt_log_set_tag_prefix() function. Tag | |
177 | * prefix | |
178 | * | |
179 | * Tag identifies component or module. It is configured per compilation module | |
180 | * (.c/.cpp/.m file) by defining BT_LOG_TAG or BT_LOG_DEF_TAG. BT_LOG_TAG has | |
181 | * higer priority and when defined overrides value provided by BT_LOG_DEF_TAG. | |
182 | * When defined, value must evaluate to (const char *), so for strings double | |
183 | * quotes must be used. | |
184 | * | |
185 | * Default tag could be defined with BT_LOG_DEF_TAG in build script (e.g. | |
186 | * Makefile, CMakeLists.txt, gyp, etc.) for the entire project or target: | |
187 | * | |
188 | * CC_ARGS := -DBT_LOG_DEF_TAG=\"MISC\" | |
189 | * | |
190 | * And when necessary could be overriden with BT_LOG_TAG in .c/.cpp/.m files | |
191 | * before including bt_log.h: | |
192 | * | |
193 | * #define BT_LOG_TAG "MAIN" | |
578e048b | 194 | * #include "logging.h" |
beb0fb75 PP |
195 | * |
196 | * If both BT_LOG_DEF_TAG and BT_LOG_TAG are undefined no tag will be added to | |
197 | * the log message (tag prefix still could be added though). | |
198 | * | |
199 | * Output example: | |
200 | * | |
201 | * 04-29 22:43:20.244 40059 1299 I hello.MAIN Number of arguments: 1 | |
202 | * | | | |
203 | * | +- tag (e.g. module) | |
204 | * +- tag prefix (e.g. process name) | |
205 | */ | |
206 | #if defined(BT_LOG_TAG) | |
207 | #define _BT_LOG_TAG BT_LOG_TAG | |
208 | #elif defined(BT_LOG_DEF_TAG) | |
209 | #define _BT_LOG_TAG BT_LOG_DEF_TAG | |
210 | #else | |
211 | #define _BT_LOG_TAG 0 | |
212 | #endif | |
213 | ||
214 | /* Source location is part of a log line that describes location (function or | |
215 | * method name, file name and line number, e.g. "runloop@main.cpp:68") of a | |
216 | * log statement that produced it. | |
217 | * Source location formats are: | |
218 | * - BT_LOG_SRCLOC_NONE - don't add source location to log line. | |
219 | * - BT_LOG_SRCLOC_SHORT - add source location in short form (file and line | |
220 | * number, e.g. "@main.cpp:68"). | |
221 | * - BT_LOG_SRCLOC_LONG - add source location in long form (function or method | |
222 | * name, file and line number, e.g. "runloop@main.cpp:68"). | |
223 | */ | |
224 | #define BT_LOG_SRCLOC_NONE 0 | |
225 | #define BT_LOG_SRCLOC_SHORT 1 | |
226 | #define BT_LOG_SRCLOC_LONG 2 | |
227 | ||
228 | /* Source location format is configured per compilation module (.c/.cpp/.m | |
229 | * file) by defining BT_LOG_DEF_SRCLOC or BT_LOG_SRCLOC. BT_LOG_SRCLOC has | |
230 | * higer priority and when defined overrides value provided by | |
231 | * BT_LOG_DEF_SRCLOC. | |
232 | * | |
233 | * Common practice is to define default format with BT_LOG_DEF_SRCLOC in | |
234 | * build script (e.g. Makefile, CMakeLists.txt, gyp, etc.) for the entire | |
235 | * project or target: | |
236 | * | |
237 | * CC_ARGS := -DBT_LOG_DEF_SRCLOC=BT_LOG_SRCLOC_LONG | |
238 | * | |
239 | * And when necessary to override it with BT_LOG_SRCLOC in .c/.cpp/.m files | |
240 | * before including bt_log.h: | |
241 | * | |
242 | * #define BT_LOG_SRCLOC BT_LOG_SRCLOC_NONE | |
578e048b | 243 | * #include "logging.h" |
beb0fb75 PP |
244 | * |
245 | * If both BT_LOG_DEF_SRCLOC and BT_LOG_SRCLOC are undefined, then | |
3f3b1761 PP |
246 | * BT_LOG_SRCLOC_NONE will be used for release builds (BT_DEBUG_MODE is |
247 | * NOT defined) and BT_LOG_SRCLOC_LONG otherwise (BT_DEBUG_MODE is | |
248 | * defined). | |
beb0fb75 PP |
249 | */ |
250 | #if defined(BT_LOG_SRCLOC) | |
251 | #define _BT_LOG_SRCLOC BT_LOG_SRCLOC | |
252 | #elif defined(BT_LOG_DEF_SRCLOC) | |
253 | #define _BT_LOG_SRCLOC BT_LOG_DEF_SRCLOC | |
254 | #else | |
3f3b1761 | 255 | #ifdef BT_DEBUG_MODE |
beb0fb75 | 256 | #define _BT_LOG_SRCLOC BT_LOG_SRCLOC_LONG |
3f3b1761 PP |
257 | #else |
258 | #define _BT_LOG_SRCLOC BT_LOG_SRCLOC_NONE | |
beb0fb75 PP |
259 | #endif |
260 | #endif | |
261 | #if BT_LOG_SRCLOC_LONG == _BT_LOG_SRCLOC | |
262 | #define _BT_LOG_SRCLOC_FUNCTION _BT_LOG_FUNCTION | |
263 | #else | |
264 | #define _BT_LOG_SRCLOC_FUNCTION 0 | |
265 | #endif | |
266 | ||
267 | /* Censoring provides conditional logging of secret information, also known as | |
268 | * Personally Identifiable Information (PII) or Sensitive Personal Information | |
269 | * (SPI). Censoring can be either enabled (BT_LOG_CENSORED) or disabled | |
270 | * (BT_LOG_UNCENSORED). When censoring is enabled, log statements marked as | |
271 | * "secrets" will be ignored and will have zero overhead (arguments also will | |
272 | * not be evaluated). | |
273 | */ | |
274 | #define BT_LOG_CENSORED 1 | |
275 | #define BT_LOG_UNCENSORED 0 | |
276 | ||
277 | /* Censoring is configured per compilation module (.c/.cpp/.m file) by defining | |
278 | * BT_LOG_DEF_CENSORING or BT_LOG_CENSORING. BT_LOG_CENSORING has higer priority | |
279 | * and when defined overrides value provided by BT_LOG_DEF_CENSORING. | |
280 | * | |
281 | * Common practice is to define default censoring with BT_LOG_DEF_CENSORING in | |
282 | * build script (e.g. Makefile, CMakeLists.txt, gyp, etc.) for the entire | |
283 | * project or target: | |
284 | * | |
285 | * CC_ARGS := -DBT_LOG_DEF_CENSORING=BT_LOG_CENSORED | |
286 | * | |
287 | * And when necessary to override it with BT_LOG_CENSORING in .c/.cpp/.m files | |
288 | * before including bt_log.h (consider doing it only for debug purposes and be | |
289 | * very careful not to push such temporary changes to source control): | |
290 | * | |
291 | * #define BT_LOG_CENSORING BT_LOG_UNCENSORED | |
578e048b | 292 | * #include "logging.h" |
beb0fb75 PP |
293 | * |
294 | * If both BT_LOG_DEF_CENSORING and BT_LOG_CENSORING are undefined, then | |
3f3b1761 PP |
295 | * BT_LOG_CENSORED will be used for release builds (BT_DEBUG_MODE is NOT |
296 | * defined) and BT_LOG_UNCENSORED otherwise (BT_DEBUG_MODE is defined). | |
beb0fb75 PP |
297 | */ |
298 | #if defined(BT_LOG_CENSORING) | |
299 | #define _BT_LOG_CENSORING BT_LOG_CENSORING | |
300 | #elif defined(BT_LOG_DEF_CENSORING) | |
301 | #define _BT_LOG_CENSORING BT_LOG_DEF_CENSORING | |
302 | #else | |
3f3b1761 | 303 | #ifdef BT_DEBUG_MODE |
beb0fb75 | 304 | #define _BT_LOG_CENSORING BT_LOG_UNCENSORED |
3f3b1761 PP |
305 | #else |
306 | #define _BT_LOG_CENSORING BT_LOG_CENSORED | |
beb0fb75 PP |
307 | #endif |
308 | #endif | |
309 | ||
310 | /* Check censoring at compile time. Evaluates to true when censoring is disabled | |
311 | * (i.e. when secrets will be logged). For example: | |
312 | * | |
313 | * #if BT_LOG_SECRETS | |
314 | * char ssn[16]; | |
315 | * getSocialSecurityNumber(ssn); | |
316 | * BT_LOGI("Customer ssn: %s", ssn); | |
317 | * #endif | |
318 | * | |
319 | * See BT_LOG_SECRET() macro for a more convenient way of guarding single log | |
320 | * statement. | |
321 | */ | |
322 | #define BT_LOG_SECRETS (BT_LOG_UNCENSORED == _BT_LOG_CENSORING) | |
323 | ||
324 | /* Static (compile-time) initialization support allows to configure logging | |
325 | * before entering main() function. This mostly useful in C++ where functions | |
326 | * and methods could be called during initialization of global objects. Those | |
327 | * functions and methods could record log messages too and for that reason | |
328 | * static initialization of logging configuration is customizable. | |
329 | * | |
330 | * Macros below allow to specify values to use for initial configuration: | |
331 | * - BT_LOG_EXTERN_TAG_PREFIX - tag prefix (default: none) | |
332 | * - BT_LOG_EXTERN_GLOBAL_FORMAT - global format options (default: see | |
333 | * BT_LOG_MEM_WIDTH in bt_log.c) | |
334 | * - BT_LOG_EXTERN_GLOBAL_OUTPUT - global output facility (default: stderr or | |
335 | * platform specific, see BT_LOG_USE_XXX macros in bt_log.c) | |
336 | * - BT_LOG_EXTERN_GLOBAL_OUTPUT_LEVEL - global output log level (default: 0 - | |
337 | * all levals are "turned on") | |
338 | * | |
339 | * For example, in log_config.c: | |
340 | * | |
578e048b | 341 | * #include "logging.h" |
beb0fb75 PP |
342 | * BT_LOG_DEFINE_TAG_PREFIX = "MyApp"; |
343 | * BT_LOG_DEFINE_GLOBAL_FORMAT = {CUSTOM_MEM_WIDTH}; | |
344 | * BT_LOG_DEFINE_GLOBAL_OUTPUT = {BT_LOG_PUT_STD, custom_output_callback, 0}; | |
345 | * BT_LOG_DEFINE_GLOBAL_OUTPUT_LEVEL = BT_LOG_INFO; | |
346 | * | |
347 | * However, to use any of those macros bt_log library must be compiled with | |
348 | * following macros defined: | |
349 | * - to use BT_LOG_DEFINE_TAG_PREFIX define BT_LOG_EXTERN_TAG_PREFIX | |
350 | * - to use BT_LOG_DEFINE_GLOBAL_FORMAT define BT_LOG_EXTERN_GLOBAL_FORMAT | |
351 | * - to use BT_LOG_DEFINE_GLOBAL_OUTPUT define BT_LOG_EXTERN_GLOBAL_OUTPUT | |
352 | * - to use BT_LOG_DEFINE_GLOBAL_OUTPUT_LEVEL define | |
353 | * BT_LOG_EXTERN_GLOBAL_OUTPUT_LEVEL | |
354 | * | |
355 | * When bt_log library compiled with one of BT_LOG_EXTERN_XXX macros defined, | |
356 | * corresponding BT_LOG_DEFINE_XXX macro MUST be used exactly once somewhere. | |
357 | * Otherwise build will fail with link error (undefined symbol). | |
358 | */ | |
4f1c7f2e JG |
359 | #define BT_LOG_DEFINE_TAG_PREFIX BT_HIDDEN const char *_bt_log_tag_prefix |
360 | #define BT_LOG_DEFINE_GLOBAL_FORMAT BT_HIDDEN bt_log_format _bt_log_global_format | |
361 | #define BT_LOG_DEFINE_GLOBAL_OUTPUT BT_HIDDEN bt_log_output _bt_log_global_output | |
362 | #define BT_LOG_DEFINE_GLOBAL_OUTPUT_LEVEL BT_HIDDEN int _bt_log_global_output_lvl | |
beb0fb75 PP |
363 | |
364 | /* Pointer to global format options. Direct modification is not allowed. Use | |
365 | * bt_log_set_mem_width() instead. Could be used to initialize bt_log_spec | |
366 | * structure: | |
367 | * | |
368 | * const bt_log_output g_output = {BT_LOG_PUT_STD, output_callback, 0}; | |
369 | * const bt_log_spec g_spec = {BT_LOG_GLOBAL_FORMAT, &g_output}; | |
370 | * BT_LOGI_AUX(&g_spec, "Hello"); | |
371 | */ | |
372 | #define BT_LOG_GLOBAL_FORMAT ((const bt_log_format *)&_bt_log_global_format) | |
373 | ||
374 | /* Pointer to global output variable. Direct modification is not allowed. Use | |
375 | * bt_log_set_output_v() or bt_log_set_output_p() instead. Could be used to | |
376 | * initialize bt_log_spec structure: | |
377 | * | |
378 | * const bt_log_format g_format = {40}; | |
379 | * const bt_log_spec g_spec = {g_format, BT_LOG_GLOBAL_OUTPUT}; | |
380 | * BT_LOGI_AUX(&g_spec, "Hello"); | |
381 | */ | |
382 | #define BT_LOG_GLOBAL_OUTPUT ((const bt_log_output *)&_bt_log_global_output) | |
383 | ||
384 | /* When defined, all library symbols produced by linker will be prefixed with | |
385 | * provided value. That allows to use bt_log library privately in another | |
386 | * libraries without exposing bt_log symbols in their original form (to avoid | |
387 | * possible conflicts with other libraries / components that also could use | |
388 | * bt_log for logging). Value must be without quotes, for example: | |
389 | * | |
390 | * CC_ARGS := -DBT_LOG_LIBRARY_PREFIX=my_lib_ | |
391 | * | |
392 | * Note, that in this mode BT_LOG_LIBRARY_PREFIX must be defined when building | |
393 | * bt_log library AND it also must be defined to the same value when building | |
394 | * a library that uses it. For example, consider fictional KittyHttp library | |
395 | * that wants to use bt_log for logging. First approach that could be taken is | |
396 | * to add bt_log.h and bt_log.c to the KittyHttp's source code tree directly. | |
397 | * In that case it will be enough just to define BT_LOG_LIBRARY_PREFIX in | |
398 | * KittyHttp's build script: | |
399 | * | |
400 | * // KittyHttp/CMakeLists.txt | |
401 | * target_compile_definitions(KittyHttp PRIVATE | |
402 | * "BT_LOG_LIBRARY_PREFIX=KittyHttp_") | |
403 | * | |
404 | * If KittyHttp doesn't want to include bt_log source code in its source tree | |
405 | * and wants to build bt_log as a separate library than bt_log library must be | |
406 | * built with BT_LOG_LIBRARY_PREFIX defined to KittyHttp_ AND KittyHttp library | |
407 | * itself also needs to define BT_LOG_LIBRARY_PREFIX to KittyHttp_. It can do | |
408 | * so either in its build script, as in example above, or by providing a | |
409 | * wrapper header that KittyHttp library will need to use instead of bt_log.h: | |
410 | * | |
411 | * // KittyHttpLogging.h | |
412 | * #define BT_LOG_LIBRARY_PREFIX KittyHttp_ | |
578e048b | 413 | * #include "logging.h" |
beb0fb75 PP |
414 | * |
415 | * Regardless of the method chosen, the end result is that bt_log symbols will | |
416 | * be prefixed with "KittyHttp_", so if a user of KittyHttp (say DogeBrowser) | |
417 | * also uses bt_log for logging, they will not interferer with each other. Both | |
418 | * will have their own log level, output facility, format options etc. | |
419 | */ | |
420 | #ifdef BT_LOG_LIBRARY_PREFIX | |
421 | #define _BT_LOG_DECOR__(prefix, name) prefix ## name | |
422 | #define _BT_LOG_DECOR_(prefix, name) _BT_LOG_DECOR__(prefix, name) | |
423 | #define _BT_LOG_DECOR(name) _BT_LOG_DECOR_(BT_LOG_LIBRARY_PREFIX, name) | |
424 | ||
425 | #define bt_log_set_tag_prefix _BT_LOG_DECOR(bt_log_set_tag_prefix) | |
426 | #define bt_log_set_mem_width _BT_LOG_DECOR(bt_log_set_mem_width) | |
427 | #define bt_log_set_output_level _BT_LOG_DECOR(bt_log_set_output_level) | |
428 | #define bt_log_set_output_v _BT_LOG_DECOR(bt_log_set_output_v) | |
429 | #define bt_log_set_output_p _BT_LOG_DECOR(bt_log_set_output_p) | |
430 | #define bt_log_out_stderr_callback _BT_LOG_DECOR(bt_log_out_stderr_callback) | |
431 | #define _bt_log_tag_prefix _BT_LOG_DECOR(_bt_log_tag_prefix) | |
432 | #define _bt_log_global_format _BT_LOG_DECOR(_bt_log_global_format) | |
433 | #define _bt_log_global_output _BT_LOG_DECOR(_bt_log_global_output) | |
434 | #define _bt_log_global_output_lvl _BT_LOG_DECOR(_bt_log_global_output_lvl) | |
435 | #define _bt_log_write_d _BT_LOG_DECOR(_bt_log_write_d) | |
436 | #define _bt_log_write_aux_d _BT_LOG_DECOR(_bt_log_write_aux_d) | |
437 | #define _bt_log_write _BT_LOG_DECOR(_bt_log_write) | |
438 | #define _bt_log_write_aux _BT_LOG_DECOR(_bt_log_write_aux) | |
439 | #define _bt_log_write_mem_d _BT_LOG_DECOR(_bt_log_write_mem_d) | |
440 | #define _bt_log_write_mem_aux_d _BT_LOG_DECOR(_bt_log_write_mem_aux_d) | |
441 | #define _bt_log_write_mem _BT_LOG_DECOR(_bt_log_write_mem) | |
442 | #define _bt_log_write_mem_aux _BT_LOG_DECOR(_bt_log_write_mem_aux) | |
443 | #define _bt_log_stderr_spec _BT_LOG_DECOR(_bt_log_stderr_spec) | |
444 | #endif | |
445 | ||
446 | #if defined(__printflike) | |
995dc21f PP |
447 | #define _BT_LOG_PRINTFLIKE(str_index, first_to_check) \ |
448 | __printflike(str_index, first_to_check) | |
0a0f7d69 MJ |
449 | #elif defined(__MINGW_PRINTF_FORMAT) |
450 | #define _BT_LOG_PRINTFLIKE(str_index, first_to_check) \ | |
451 | __attribute__((format(__MINGW_PRINTF_FORMAT, str_index, first_to_check))) | |
995dc21f PP |
452 | #elif defined(__GNUC__) |
453 | #define _BT_LOG_PRINTFLIKE(str_index, first_to_check) \ | |
454 | __attribute__((format(__printf__, str_index, first_to_check))) | |
beb0fb75 | 455 | #else |
995dc21f | 456 | #define _BT_LOG_PRINTFLIKE(str_index, first_to_check) |
beb0fb75 PP |
457 | #endif |
458 | ||
459 | #if (defined(_WIN32) || defined(_WIN64)) && !defined(__GNUC__) | |
460 | #define _BT_LOG_FUNCTION __FUNCTION__ | |
461 | #else | |
462 | #define _BT_LOG_FUNCTION __func__ | |
463 | #endif | |
464 | ||
465 | #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) | |
466 | #define _BT_LOG_INLINE __inline | |
467 | #define _BT_LOG_IF(cond) \ | |
468 | __pragma(warning(push)) \ | |
469 | __pragma(warning(disable:4127)) \ | |
470 | if(cond) \ | |
471 | __pragma(warning(pop)) | |
472 | #define _BT_LOG_WHILE(cond) \ | |
473 | __pragma(warning(push)) \ | |
474 | __pragma(warning(disable:4127)) \ | |
475 | while(cond) \ | |
476 | __pragma(warning(pop)) | |
477 | #else | |
478 | #define _BT_LOG_INLINE inline | |
479 | #define _BT_LOG_IF(cond) if(cond) | |
480 | #define _BT_LOG_WHILE(cond) while(cond) | |
481 | #endif | |
482 | #define _BT_LOG_NEVER _BT_LOG_IF(0) | |
483 | #define _BT_LOG_ONCE _BT_LOG_WHILE(0) | |
484 | ||
485 | #ifdef __cplusplus | |
486 | extern "C" { | |
487 | #endif | |
488 | ||
489 | /* Set tag prefix. Prefix will be separated from the tag with dot ('.'). | |
490 | * Use 0 or empty string to disable (default). Common use is to set it to | |
491 | * the process (or build target) name (e.g. to separate client and server | |
492 | * processes). Function will NOT copy provided prefix string, but will store the | |
493 | * pointer. Hence specified prefix string must remain valid. See | |
494 | * BT_LOG_DEFINE_TAG_PREFIX for a way to set it before entering main() function. | |
495 | * See BT_LOG_TAG for more information about tag and tag prefix. | |
496 | */ | |
497 | void bt_log_set_tag_prefix(const char *const prefix); | |
498 | ||
499 | /* Set number of bytes per log line in memory (ASCII-HEX) output. Example: | |
500 | * | |
501 | * I hello.MAIN 4c6f72656d20697073756d20646f6c6f Lorem ipsum dolo | |
502 | * |<- w bytes ->| |<- w chars ->| | |
503 | * | |
504 | * See BT_LOGF_MEM and BT_LOGF_MEM_AUX for more details. | |
505 | */ | |
506 | void bt_log_set_mem_width(const unsigned w); | |
507 | ||
508 | /* Set "output" log level. See BT_LOG_LEVEL and BT_LOG_OUTPUT_LEVEL for more | |
509 | * info about log levels. | |
510 | */ | |
511 | void bt_log_set_output_level(const int lvl); | |
512 | ||
513 | /* Put mask is a set of flags that define what fields will be added to each | |
514 | * log message. Default value is BT_LOG_PUT_STD and other flags could be used to | |
515 | * alter its behavior. See bt_log_set_output_v() for more details. | |
516 | * | |
3f3b1761 PP |
517 | * Note about BT_LOG_PUT_SRC: it will be added only in debug builds |
518 | * (BT_DEBUG_MODE is defined). | |
beb0fb75 PP |
519 | */ |
520 | enum | |
521 | { | |
522 | BT_LOG_PUT_CTX = 1 << 0, /* context (time, pid, tid, log level) */ | |
523 | BT_LOG_PUT_TAG = 1 << 1, /* tag (including tag prefix) */ | |
524 | BT_LOG_PUT_SRC = 1 << 2, /* source location (file, line, function) */ | |
525 | BT_LOG_PUT_MSG = 1 << 3, /* message text (formatted string) */ | |
526 | BT_LOG_PUT_STD = 0xffff, /* everything (default) */ | |
527 | }; | |
528 | ||
529 | typedef struct bt_log_message | |
530 | { | |
531 | int lvl; /* Log level of the message */ | |
532 | const char *tag; /* Associated tag (without tag prefix) */ | |
533 | char *buf; /* Buffer start */ | |
534 | char *e; /* Buffer end (last position where EOL with 0 could be written) */ | |
535 | char *p; /* Buffer content end (append position) */ | |
536 | char *tag_b; /* Prefixed tag start */ | |
537 | char *tag_e; /* Prefixed tag end (if != tag_b, points to msg separator) */ | |
538 | char *msg_b; /* Message start (expanded format string) */ | |
539 | } | |
540 | bt_log_message; | |
541 | ||
542 | /* Type of output callback function. It will be called for each log line allowed | |
543 | * by both "current" and "output" log levels ("enabled" and "turned on"). | |
544 | * Callback function is allowed to modify content of the buffers pointed by the | |
545 | * msg, but it's not allowed to modify any of msg fields. Buffer pointed by msg | |
546 | * is UTF-8 encoded (no BOM mark). | |
547 | */ | |
548 | typedef void (*bt_log_output_cb)(const bt_log_message *msg, void *arg); | |
549 | ||
550 | /* Format options. For more details see bt_log_set_mem_width(). | |
551 | */ | |
552 | typedef struct bt_log_format | |
553 | { | |
554 | unsigned mem_width; /* Bytes per line in memory (ASCII-HEX) dump */ | |
555 | } | |
556 | bt_log_format; | |
557 | ||
558 | /* Output facility. | |
559 | */ | |
560 | typedef struct bt_log_output | |
561 | { | |
562 | unsigned mask; /* What to put into log line buffer (see BT_LOG_PUT_XXX) */ | |
563 | void *arg; /* User provided output callback argument */ | |
564 | bt_log_output_cb callback; /* Output callback function */ | |
565 | } | |
566 | bt_log_output; | |
567 | ||
568 | /* Set output callback function. | |
569 | * | |
570 | * Mask allows to control what information will be added to the log line buffer | |
571 | * before callback function is invoked. Default mask value is BT_LOG_PUT_STD. | |
572 | */ | |
573 | void bt_log_set_output_v(const unsigned mask, void *const arg, | |
574 | const bt_log_output_cb callback); | |
575 | static _BT_LOG_INLINE void bt_log_set_output_p(const bt_log_output *const output) | |
576 | { | |
577 | bt_log_set_output_v(output->mask, output->arg, output->callback); | |
578 | } | |
579 | ||
580 | /* Used with _AUX macros and allows to override global format and output | |
581 | * facility. Use BT_LOG_GLOBAL_FORMAT and BT_LOG_GLOBAL_OUTPUT for values from | |
582 | * global configuration. Example: | |
583 | * | |
584 | * static const bt_log_output module_output = { | |
585 | * BT_LOG_PUT_STD, 0, custom_output_callback | |
586 | * }; | |
587 | * static const bt_log_spec module_spec = { | |
588 | * BT_LOG_GLOBAL_FORMAT, &module_output | |
589 | * }; | |
590 | * BT_LOGI_AUX(&module_spec, "Position: %ix%i", x, y); | |
591 | * | |
592 | * See BT_LOGF_AUX and BT_LOGF_MEM_AUX for details. | |
593 | */ | |
594 | typedef struct bt_log_spec | |
595 | { | |
596 | const bt_log_format *format; | |
597 | const bt_log_output *output; | |
598 | } | |
599 | bt_log_spec; | |
600 | ||
601 | #ifdef __cplusplus | |
602 | } | |
603 | #endif | |
604 | ||
605 | /* Execute log statement if condition is true. Example: | |
606 | * | |
607 | * BT_LOG_IF(1 < 2, BT_LOGI("Log this")); | |
608 | * BT_LOG_IF(1 > 2, BT_LOGI("Don't log this")); | |
609 | * | |
610 | * Keep in mind though, that if condition can't be evaluated at compile time, | |
611 | * then it will be evaluated at run time. This will increase exectuable size | |
612 | * and can have noticeable performance overhead. Try to limit conditions to | |
613 | * expressions that can be evaluated at compile time. | |
614 | */ | |
615 | #define BT_LOG_IF(cond, f) do { _BT_LOG_IF((cond)) { f; } } _BT_LOG_ONCE | |
616 | ||
617 | /* Mark log statement as "secret". Log statements that are marked as secrets | |
618 | * will NOT be executed when censoring is enabled (see BT_LOG_CENSORED). | |
619 | * Example: | |
620 | * | |
621 | * BT_LOG_SECRET(BT_LOGI("Credit card: %s", credit_card)); | |
622 | * BT_LOG_SECRET(BT_LOGD_MEM(cipher, cipher_sz, "Cipher bytes:")); | |
623 | */ | |
624 | #define BT_LOG_SECRET(f) BT_LOG_IF(BT_LOG_SECRETS, f) | |
625 | ||
626 | /* Check "current" log level at compile time (ignoring "output" log level). | |
627 | * Evaluates to true when specified log level is enabled. For example: | |
628 | * | |
629 | * #if BT_LOG_ENABLED_DEBUG | |
630 | * const char *const g_enum_strings[] = { | |
631 | * "enum_value_0", "enum_value_1", "enum_value_2" | |
632 | * }; | |
633 | * #endif | |
634 | * // ... | |
635 | * #if BT_LOG_ENABLED_DEBUG | |
636 | * BT_LOGD("enum value: %s", g_enum_strings[v]); | |
637 | * #endif | |
638 | * | |
639 | * See BT_LOG_LEVEL for details. | |
640 | */ | |
641 | #define BT_LOG_ENABLED(lvl) ((lvl) >= _BT_LOG_LEVEL) | |
642 | #define BT_LOG_ENABLED_VERBOSE BT_LOG_ENABLED(BT_LOG_VERBOSE) | |
643 | #define BT_LOG_ENABLED_DEBUG BT_LOG_ENABLED(BT_LOG_DEBUG) | |
644 | #define BT_LOG_ENABLED_INFO BT_LOG_ENABLED(BT_LOG_INFO) | |
645 | #define BT_LOG_ENABLED_WARN BT_LOG_ENABLED(BT_LOG_WARN) | |
646 | #define BT_LOG_ENABLED_ERROR BT_LOG_ENABLED(BT_LOG_ERROR) | |
647 | #define BT_LOG_ENABLED_FATAL BT_LOG_ENABLED(BT_LOG_FATAL) | |
648 | ||
649 | /* Check "output" log level at run time (taking into account "current" log | |
650 | * level as well). Evaluates to true when specified log level is turned on AND | |
651 | * enabled. For example: | |
652 | * | |
653 | * if (BT_LOG_ON_DEBUG) | |
654 | * { | |
655 | * char hash[65]; | |
656 | * sha256(data_ptr, data_sz, hash); | |
657 | * BT_LOGD("data: len=%u, sha256=%s", data_sz, hash); | |
658 | * } | |
659 | * | |
660 | * See BT_LOG_OUTPUT_LEVEL for details. | |
661 | */ | |
662 | #define BT_LOG_ON(lvl) \ | |
663 | (BT_LOG_ENABLED((lvl)) && (lvl) >= _BT_LOG_OUTPUT_LEVEL) | |
664 | #define BT_LOG_ON_VERBOSE BT_LOG_ON(BT_LOG_VERBOSE) | |
665 | #define BT_LOG_ON_DEBUG BT_LOG_ON(BT_LOG_DEBUG) | |
666 | #define BT_LOG_ON_INFO BT_LOG_ON(BT_LOG_INFO) | |
667 | #define BT_LOG_ON_WARN BT_LOG_ON(BT_LOG_WARN) | |
668 | #define BT_LOG_ON_ERROR BT_LOG_ON(BT_LOG_ERROR) | |
669 | #define BT_LOG_ON_FATAL BT_LOG_ON(BT_LOG_FATAL) | |
670 | ||
671 | #ifdef __cplusplus | |
672 | extern "C" { | |
673 | #endif | |
674 | ||
675 | extern const char *_bt_log_tag_prefix; | |
676 | extern bt_log_format _bt_log_global_format; | |
677 | extern bt_log_output _bt_log_global_output; | |
678 | extern int _bt_log_global_output_lvl; | |
679 | extern const bt_log_spec _bt_log_stderr_spec; | |
680 | ||
681 | BT_HIDDEN | |
682 | void _bt_log_write_d( | |
683 | const char *const func, const char *const file, const unsigned line, | |
684 | const int lvl, const char *const tag, | |
685 | const char *const fmt, ...) _BT_LOG_PRINTFLIKE(6, 7); | |
686 | ||
687 | BT_HIDDEN | |
688 | void _bt_log_write_aux_d( | |
689 | const char *const func, const char *const file, const unsigned line, | |
690 | const bt_log_spec *const log, const int lvl, const char *const tag, | |
691 | const char *const fmt, ...) _BT_LOG_PRINTFLIKE(7, 8); | |
692 | ||
693 | BT_HIDDEN | |
694 | void _bt_log_write( | |
695 | const int lvl, const char *const tag, | |
696 | const char *const fmt, ...) _BT_LOG_PRINTFLIKE(3, 4); | |
697 | ||
698 | BT_HIDDEN | |
699 | void _bt_log_write_aux( | |
700 | const bt_log_spec *const log, const int lvl, const char *const tag, | |
701 | const char *const fmt, ...) _BT_LOG_PRINTFLIKE(4, 5); | |
702 | ||
703 | BT_HIDDEN | |
704 | void _bt_log_write_mem_d( | |
705 | const char *const func, const char *const file, const unsigned line, | |
706 | const int lvl, const char *const tag, | |
707 | const void *const d, const unsigned d_sz, | |
708 | const char *const fmt, ...) _BT_LOG_PRINTFLIKE(8, 9); | |
709 | ||
710 | BT_HIDDEN | |
711 | void _bt_log_write_mem_aux_d( | |
712 | const char *const func, const char *const file, const unsigned line, | |
713 | const bt_log_spec *const log, const int lvl, const char *const tag, | |
714 | const void *const d, const unsigned d_sz, | |
715 | const char *const fmt, ...) _BT_LOG_PRINTFLIKE(9, 10); | |
716 | ||
717 | BT_HIDDEN | |
718 | void _bt_log_write_mem( | |
719 | const int lvl, const char *const tag, | |
720 | const void *const d, const unsigned d_sz, | |
721 | const char *const fmt, ...) _BT_LOG_PRINTFLIKE(5, 6); | |
722 | ||
723 | BT_HIDDEN | |
724 | void _bt_log_write_mem_aux( | |
725 | const bt_log_spec *const log, const int lvl, const char *const tag, | |
726 | const void *const d, const unsigned d_sz, | |
727 | const char *const fmt, ...) _BT_LOG_PRINTFLIKE(6, 7); | |
728 | ||
729 | #ifdef __cplusplus | |
730 | } | |
731 | #endif | |
732 | ||
733 | /* Message logging macros: | |
734 | * - BT_LOGV("format string", args, ...) | |
735 | * - BT_LOGD("format string", args, ...) | |
736 | * - BT_LOGI("format string", args, ...) | |
737 | * - BT_LOGW("format string", args, ...) | |
738 | * - BT_LOGE("format string", args, ...) | |
739 | * - BT_LOGF("format string", args, ...) | |
740 | * | |
263c5cef | 741 | * Message and error string (errno) logging macros: |
c4f13c31 PP |
742 | * - BT_LOGV_ERRNO("initial message", "format string", args, ...) |
743 | * - BT_LOGD_ERRNO("initial message", "format string", args, ...) | |
744 | * - BT_LOGI_ERRNO("initial message", "format string", args, ...) | |
745 | * - BT_LOGW_ERRNO("initial message", "format string", args, ...) | |
746 | * - BT_LOGE_ERRNO("initial message", "format string", args, ...) | |
747 | * - BT_LOGF_ERRNO("initial message", "format string", args, ...) | |
263c5cef | 748 | * |
beb0fb75 PP |
749 | * Memory logging macros: |
750 | * - BT_LOGV_MEM(data_ptr, data_sz, "format string", args, ...) | |
751 | * - BT_LOGD_MEM(data_ptr, data_sz, "format string", args, ...) | |
752 | * - BT_LOGI_MEM(data_ptr, data_sz, "format string", args, ...) | |
753 | * - BT_LOGW_MEM(data_ptr, data_sz, "format string", args, ...) | |
754 | * - BT_LOGE_MEM(data_ptr, data_sz, "format string", args, ...) | |
755 | * - BT_LOGF_MEM(data_ptr, data_sz, "format string", args, ...) | |
756 | * | |
757 | * Auxiliary logging macros: | |
758 | * - BT_LOGV_AUX(&log_instance, "format string", args, ...) | |
759 | * - BT_LOGD_AUX(&log_instance, "format string", args, ...) | |
760 | * - BT_LOGI_AUX(&log_instance, "format string", args, ...) | |
761 | * - BT_LOGW_AUX(&log_instance, "format string", args, ...) | |
762 | * - BT_LOGE_AUX(&log_instance, "format string", args, ...) | |
763 | * - BT_LOGF_AUX(&log_instance, "format string", args, ...) | |
764 | * | |
765 | * Auxiliary memory logging macros: | |
766 | * - BT_LOGV_MEM_AUX(&log_instance, data_ptr, data_sz, "format string", args, ...) | |
767 | * - BT_LOGD_MEM_AUX(&log_instance, data_ptr, data_sz, "format string", args, ...) | |
768 | * - BT_LOGI_MEM_AUX(&log_instance, data_ptr, data_sz, "format string", args, ...) | |
769 | * - BT_LOGW_MEM_AUX(&log_instance, data_ptr, data_sz, "format string", args, ...) | |
770 | * - BT_LOGE_MEM_AUX(&log_instance, data_ptr, data_sz, "format string", args, ...) | |
771 | * - BT_LOGF_MEM_AUX(&log_instance, data_ptr, data_sz, "format string", args, ...) | |
772 | * | |
773 | * Preformatted string logging macros: | |
774 | * - BT_LOGV_STR("preformatted string"); | |
775 | * - BT_LOGD_STR("preformatted string"); | |
776 | * - BT_LOGI_STR("preformatted string"); | |
777 | * - BT_LOGW_STR("preformatted string"); | |
778 | * - BT_LOGE_STR("preformatted string"); | |
779 | * - BT_LOGF_STR("preformatted string"); | |
780 | * | |
781 | * Explicit log level and tag macros: | |
782 | * - BT_LOG_WRITE(level, tag, "format string", args, ...) | |
783 | * - BT_LOG_WRITE_MEM(level, tag, data_ptr, data_sz, "format string", args, ...) | |
784 | * - BT_LOG_WRITE_AUX(&log_instance, level, tag, "format string", args, ...) | |
785 | * - BT_LOG_WRITE_MEM_AUX(&log_instance, level, tag, data_ptr, data_sz, | |
786 | * "format string", args, ...) | |
787 | * | |
788 | * Format string follows printf() conventions. Both data_ptr and data_sz could | |
789 | * be 0. Tag can be 0 as well. Most compilers will verify that type of arguments | |
790 | * match format specifiers in format string. | |
791 | * | |
792 | * Library assuming UTF-8 encoding for all strings (char *), including format | |
793 | * string itself. | |
794 | */ | |
795 | #if BT_LOG_SRCLOC_NONE == _BT_LOG_SRCLOC | |
796 | #define BT_LOG_WRITE(lvl, tag, ...) \ | |
797 | do { \ | |
798 | if (BT_LOG_ON(lvl)) \ | |
799 | _bt_log_write(lvl, tag, __VA_ARGS__); \ | |
800 | } _BT_LOG_ONCE | |
801 | #define BT_LOG_WRITE_MEM(lvl, tag, d, d_sz, ...) \ | |
802 | do { \ | |
803 | if (BT_LOG_ON(lvl)) \ | |
804 | _bt_log_write_mem(lvl, tag, d, d_sz, __VA_ARGS__); \ | |
805 | } _BT_LOG_ONCE | |
806 | #define BT_LOG_WRITE_AUX(log, lvl, tag, ...) \ | |
807 | do { \ | |
808 | if (BT_LOG_ON(lvl)) \ | |
809 | _bt_log_write_aux(log, lvl, tag, __VA_ARGS__); \ | |
810 | } _BT_LOG_ONCE | |
811 | #define BT_LOG_WRITE_MEM_AUX(log, lvl, tag, d, d_sz, ...) \ | |
812 | do { \ | |
813 | if (BT_LOG_ON(lvl)) \ | |
814 | _bt_log_write_mem_aux(log, lvl, tag, d, d_sz, __VA_ARGS__); \ | |
815 | } _BT_LOG_ONCE | |
816 | #else | |
817 | #define BT_LOG_WRITE(lvl, tag, ...) \ | |
818 | do { \ | |
819 | if (BT_LOG_ON(lvl)) \ | |
820 | _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION, __FILE__, __LINE__, \ | |
821 | lvl, tag, __VA_ARGS__); \ | |
822 | } _BT_LOG_ONCE | |
823 | #define BT_LOG_WRITE_MEM(lvl, tag, d, d_sz, ...) \ | |
824 | do { \ | |
825 | if (BT_LOG_ON(lvl)) \ | |
826 | _bt_log_write_mem_d(_BT_LOG_SRCLOC_FUNCTION, __FILE__, __LINE__, \ | |
827 | lvl, tag, d, d_sz, __VA_ARGS__); \ | |
828 | } _BT_LOG_ONCE | |
829 | #define BT_LOG_WRITE_AUX(log, lvl, tag, ...) \ | |
830 | do { \ | |
831 | if (BT_LOG_ON(lvl)) \ | |
832 | _bt_log_write_aux_d(_BT_LOG_SRCLOC_FUNCTION, __FILE__, __LINE__, \ | |
833 | log, lvl, tag, __VA_ARGS__); \ | |
834 | } _BT_LOG_ONCE | |
835 | #define BT_LOG_WRITE_MEM_AUX(log, lvl, tag, d, d_sz, ...) \ | |
836 | do { \ | |
837 | if (BT_LOG_ON(lvl)) \ | |
838 | _bt_log_write_mem_aux_d(_BT_LOG_SRCLOC_FUNCTION, __FILE__, __LINE__, \ | |
839 | log, lvl, tag, d, d_sz, __VA_ARGS__); \ | |
840 | } _BT_LOG_ONCE | |
841 | #endif | |
842 | ||
05509012 MJ |
843 | #define BT_LOG_WRITE_ERRNO(lvl, tag, _msg, _fmt, args...) \ |
844 | do { \ | |
845 | const char *error_str; \ | |
846 | error_str = g_strerror(errno); \ | |
847 | BT_LOG_WRITE(lvl, tag, _msg ": %s" _fmt, error_str, ## args); \ | |
848 | } _BT_LOG_ONCE | |
263c5cef | 849 | |
beb0fb75 PP |
850 | static _BT_LOG_INLINE void _bt_log_unused(const int dummy, ...) {(void)dummy;} |
851 | ||
852 | #define _BT_LOG_UNUSED(...) \ | |
853 | do { _BT_LOG_NEVER _bt_log_unused(0, __VA_ARGS__); } _BT_LOG_ONCE | |
854 | ||
855 | #if BT_LOG_ENABLED_VERBOSE | |
856 | #define BT_LOGV(...) \ | |
857 | BT_LOG_WRITE(BT_LOG_VERBOSE, _BT_LOG_TAG, __VA_ARGS__) | |
263c5cef JG |
858 | #define BT_LOGV_ERRNO(...) \ |
859 | BT_LOG_WRITE_ERRNO(BT_LOG_VERBOSE, _BT_LOG_TAG, __VA_ARGS__) | |
beb0fb75 PP |
860 | #define BT_LOGV_AUX(log, ...) \ |
861 | BT_LOG_WRITE_AUX(log, BT_LOG_VERBOSE, _BT_LOG_TAG, __VA_ARGS__) | |
862 | #define BT_LOGV_MEM(d, d_sz, ...) \ | |
863 | BT_LOG_WRITE_MEM(BT_LOG_VERBOSE, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
864 | #define BT_LOGV_MEM_AUX(log, d, d_sz, ...) \ | |
865 | BT_LOG_WRITE_MEM(log, BT_LOG_VERBOSE, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
866 | #else | |
867 | #define BT_LOGV(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
868 | #define BT_LOGV_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
869 | #define BT_LOGV_MEM(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
870 | #define BT_LOGV_MEM_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
871 | #endif | |
872 | ||
873 | #if BT_LOG_ENABLED_DEBUG | |
874 | #define BT_LOGD(...) \ | |
875 | BT_LOG_WRITE(BT_LOG_DEBUG, _BT_LOG_TAG, __VA_ARGS__) | |
263c5cef JG |
876 | #define BT_LOGD_ERRNO(...) \ |
877 | BT_LOG_WRITE_ERRNO(BT_LOG_DEBUG, _BT_LOG_TAG, __VA_ARGS__) | |
beb0fb75 PP |
878 | #define BT_LOGD_AUX(log, ...) \ |
879 | BT_LOG_WRITE_AUX(log, BT_LOG_DEBUG, _BT_LOG_TAG, __VA_ARGS__) | |
880 | #define BT_LOGD_MEM(d, d_sz, ...) \ | |
881 | BT_LOG_WRITE_MEM(BT_LOG_DEBUG, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
882 | #define BT_LOGD_MEM_AUX(log, d, d_sz, ...) \ | |
883 | BT_LOG_WRITE_MEM_AUX(log, BT_LOG_DEBUG, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
884 | #else | |
885 | #define BT_LOGD(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
886 | #define BT_LOGD_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
887 | #define BT_LOGD_MEM(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
888 | #define BT_LOGD_MEM_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
889 | #endif | |
890 | ||
891 | #if BT_LOG_ENABLED_INFO | |
892 | #define BT_LOGI(...) \ | |
893 | BT_LOG_WRITE(BT_LOG_INFO, _BT_LOG_TAG, __VA_ARGS__) | |
263c5cef JG |
894 | #define BT_LOGI_ERRNO(...) \ |
895 | BT_LOG_WRITE_ERRNO(BT_LOG_INFO, _BT_LOG_TAG, __VA_ARGS__) | |
beb0fb75 PP |
896 | #define BT_LOGI_AUX(log, ...) \ |
897 | BT_LOG_WRITE_AUX(log, BT_LOG_INFO, _BT_LOG_TAG, __VA_ARGS__) | |
898 | #define BT_LOGI_MEM(d, d_sz, ...) \ | |
899 | BT_LOG_WRITE_MEM(BT_LOG_INFO, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
900 | #define BT_LOGI_MEM_AUX(log, d, d_sz, ...) \ | |
901 | BT_LOG_WRITE_MEM_AUX(log, BT_LOG_INFO, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
902 | #else | |
903 | #define BT_LOGI(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
904 | #define BT_LOGI_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
905 | #define BT_LOGI_MEM(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
906 | #define BT_LOGI_MEM_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
907 | #endif | |
908 | ||
909 | #if BT_LOG_ENABLED_WARN | |
910 | #define BT_LOGW(...) \ | |
911 | BT_LOG_WRITE(BT_LOG_WARN, _BT_LOG_TAG, __VA_ARGS__) | |
263c5cef JG |
912 | #define BT_LOGW_ERRNO(...) \ |
913 | BT_LOG_WRITE_ERRNO(BT_LOG_WARN, _BT_LOG_TAG, __VA_ARGS__) | |
beb0fb75 PP |
914 | #define BT_LOGW_AUX(log, ...) \ |
915 | BT_LOG_WRITE_AUX(log, BT_LOG_WARN, _BT_LOG_TAG, __VA_ARGS__) | |
916 | #define BT_LOGW_MEM(d, d_sz, ...) \ | |
917 | BT_LOG_WRITE_MEM(BT_LOG_WARN, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
918 | #define BT_LOGW_MEM_AUX(log, d, d_sz, ...) \ | |
919 | BT_LOG_WRITE_MEM_AUX(log, BT_LOG_WARN, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
920 | #else | |
921 | #define BT_LOGW(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
922 | #define BT_LOGW_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
923 | #define BT_LOGW_MEM(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
924 | #define BT_LOGW_MEM_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
925 | #endif | |
926 | ||
927 | #if BT_LOG_ENABLED_ERROR | |
928 | #define BT_LOGE(...) \ | |
929 | BT_LOG_WRITE(BT_LOG_ERROR, _BT_LOG_TAG, __VA_ARGS__) | |
263c5cef JG |
930 | #define BT_LOGE_ERRNO(...) \ |
931 | BT_LOG_WRITE_ERRNO(BT_LOG_ERROR, _BT_LOG_TAG, __VA_ARGS__) | |
beb0fb75 PP |
932 | #define BT_LOGE_AUX(log, ...) \ |
933 | BT_LOG_WRITE_AUX(log, BT_LOG_ERROR, _BT_LOG_TAG, __VA_ARGS__) | |
934 | #define BT_LOGE_MEM(d, d_sz, ...) \ | |
935 | BT_LOG_WRITE_MEM(BT_LOG_ERROR, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
936 | #define BT_LOGE_MEM_AUX(log, d, d_sz, ...) \ | |
937 | BT_LOG_WRITE_MEM_AUX(log, BT_LOG_ERROR, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
938 | #else | |
939 | #define BT_LOGE(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
940 | #define BT_LOGE_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
941 | #define BT_LOGE_MEM(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
942 | #define BT_LOGE_MEM_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
943 | #endif | |
944 | ||
945 | #if BT_LOG_ENABLED_FATAL | |
946 | #define BT_LOGF(...) \ | |
947 | BT_LOG_WRITE(BT_LOG_FATAL, _BT_LOG_TAG, __VA_ARGS__) | |
263c5cef JG |
948 | #define BT_LOGF_ERRNO(...) \ |
949 | BT_LOG_WRITE_ERRNO(BT_LOG_FATAL, _BT_LOG_TAG, __VA_ARGS__) | |
beb0fb75 PP |
950 | #define BT_LOGF_AUX(log, ...) \ |
951 | BT_LOG_WRITE_AUX(log, BT_LOG_FATAL, _BT_LOG_TAG, __VA_ARGS__) | |
952 | #define BT_LOGF_MEM(d, d_sz, ...) \ | |
953 | BT_LOG_WRITE_MEM(BT_LOG_FATAL, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
954 | #define BT_LOGF_MEM_AUX(log, d, d_sz, ...) \ | |
955 | BT_LOG_WRITE_MEM_AUX(log, BT_LOG_FATAL, _BT_LOG_TAG, d, d_sz, __VA_ARGS__) | |
956 | #else | |
957 | #define BT_LOGF(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
958 | #define BT_LOGF_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
959 | #define BT_LOGF_MEM(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
960 | #define BT_LOGF_MEM_AUX(...) _BT_LOG_UNUSED(__VA_ARGS__) | |
961 | #endif | |
962 | ||
963 | #define BT_LOGV_STR(s) BT_LOGV("%s", (s)) | |
964 | #define BT_LOGD_STR(s) BT_LOGD("%s", (s)) | |
965 | #define BT_LOGI_STR(s) BT_LOGI("%s", (s)) | |
966 | #define BT_LOGW_STR(s) BT_LOGW("%s", (s)) | |
967 | #define BT_LOGE_STR(s) BT_LOGE("%s", (s)) | |
968 | #define BT_LOGF_STR(s) BT_LOGF("%s", (s)) | |
969 | ||
970 | #ifdef __cplusplus | |
971 | extern "C" { | |
972 | #endif | |
973 | ||
974 | /* Output to standard error stream. Library uses it by default, though in few | |
975 | * cases it could be necessary to specify it explicitly. For example, when | |
976 | * bt_log library is compiled with BT_LOG_EXTERN_GLOBAL_OUTPUT, application must | |
977 | * define and initialize global output variable: | |
978 | * | |
979 | * BT_LOG_DEFINE_GLOBAL_OUTPUT = {BT_LOG_OUT_STDERR}; | |
980 | * | |
981 | * Another example is when using custom output, stderr could be used as a | |
982 | * fallback when custom output facility failed to initialize: | |
983 | * | |
984 | * bt_log_set_output_v(BT_LOG_OUT_STDERR); | |
985 | */ | |
986 | enum { BT_LOG_OUT_STDERR_MASK = BT_LOG_PUT_STD }; | |
1b83271b PP |
987 | |
988 | BT_HIDDEN | |
beb0fb75 PP |
989 | void bt_log_out_stderr_callback(const bt_log_message *const msg, void *arg); |
990 | #define BT_LOG_OUT_STDERR BT_LOG_OUT_STDERR_MASK, 0, bt_log_out_stderr_callback | |
991 | ||
992 | /* Predefined spec for stderr. Uses global format options (BT_LOG_GLOBAL_FORMAT) | |
993 | * and BT_LOG_OUT_STDERR. Could be used to force output to stderr for a | |
994 | * particular message. Example: | |
995 | * | |
996 | * f = fopen("foo.log", "w"); | |
997 | * if (!f) | |
998 | * BT_LOGE_AUX(BT_LOG_STDERR, "Failed to open log file"); | |
999 | */ | |
1000 | #define BT_LOG_STDERR (&_bt_log_stderr_spec) | |
1001 | ||
373c938b PP |
1002 | static inline |
1003 | int bt_log_get_level_from_env(const char *var) | |
1004 | { | |
1005 | const char *varval = getenv(var); | |
1006 | int level = BT_LOG_NONE; | |
1007 | ||
1008 | if (!varval) { | |
1009 | goto end; | |
1010 | } | |
1011 | ||
1012 | if (strcmp(varval, "VERBOSE") == 0 || | |
1013 | strcmp(varval, "V") == 0) { | |
1014 | level = BT_LOG_VERBOSE; | |
1015 | } else if (strcmp(varval, "DEBUG") == 0 || | |
1016 | strcmp(varval, "D") == 0) { | |
1017 | level = BT_LOG_DEBUG; | |
1018 | } else if (strcmp(varval, "INFO") == 0 || | |
1019 | strcmp(varval, "I") == 0) { | |
1020 | level = BT_LOG_INFO; | |
1021 | } else if (strcmp(varval, "WARN") == 0 || | |
1022 | strcmp(varval, "WARNING") == 0 || | |
1023 | strcmp(varval, "W") == 0) { | |
1024 | level = BT_LOG_WARN; | |
1025 | } else if (strcmp(varval, "ERROR") == 0 || | |
1026 | strcmp(varval, "E") == 0) { | |
1027 | level = BT_LOG_ERROR; | |
1028 | } else if (strcmp(varval, "FATAL") == 0 || | |
1029 | strcmp(varval, "F") == 0) { | |
1030 | level = BT_LOG_FATAL; | |
1031 | } else if (strcmp(varval, "NONE") == 0 || | |
1032 | strcmp(varval, "N") == 0) { | |
1033 | level = BT_LOG_NONE; | |
1034 | } else { | |
1035 | /* Should we warn here? How? */ | |
1036 | } | |
1037 | ||
1038 | end: | |
1039 | return level; | |
1040 | } | |
1041 | ||
5d2d01c4 PP |
1042 | #define BT_LOG_LEVEL_EXTERN_SYMBOL(_level_sym) \ |
1043 | extern int _level_sym | |
1044 | ||
1045 | #define BT_LOG_INIT_LOG_LEVEL(_level_sym, _env_var) \ | |
1046 | BT_HIDDEN int _level_sym = BT_LOG_NONE; \ | |
1047 | static \ | |
1048 | void __attribute__((constructor)) _bt_log_level_ctor(void) \ | |
1049 | { \ | |
1050 | _level_sym = bt_log_get_level_from_env(_env_var); \ | |
1051 | } | |
1052 | ||
beb0fb75 PP |
1053 | #ifdef __cplusplus |
1054 | } | |
1055 | #endif | |
1056 | ||
5d2d01c4 | 1057 | #endif /* BABELTRACE_LOGGING_INTERNAL_H */ |