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