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