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