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