Commit | Line | Data |
---|---|---|
beb0fb75 PP |
1 | /* |
2 | * This is zf_log.c, modified with Babeltrace prefixes. | |
3 | * See <https://github.com/wonder-mice/zf_log/>. | |
4 | * See LICENSE. | |
5 | */ | |
6 | ||
7 | #include <babeltrace/babeltrace-internal.h> | |
557d8f39 | 8 | #include <babeltrace/common-internal.h> |
c0c0c8b4 | 9 | #include <pthread.h> |
4ee41178 | 10 | #include <assert.h> |
beb0fb75 | 11 | |
f61ad754 MJ |
12 | #ifdef __CYGWIN__ |
13 | extern unsigned long pthread_getsequence_np(pthread_t *); | |
14 | #endif | |
15 | ||
beb0fb75 PP |
16 | /* When defined, Android log (android/log.h) will be used by default instead of |
17 | * stderr (ignored on non-Android platforms). Date, time, pid and tid (context) | |
18 | * will be provided by Android log. Android log features will be used to output | |
19 | * log level and tag. | |
20 | */ | |
21 | #ifdef BT_LOG_USE_ANDROID_LOG | |
22 | #undef BT_LOG_USE_ANDROID_LOG | |
23 | #if defined(__ANDROID__) | |
24 | #define BT_LOG_USE_ANDROID_LOG 1 | |
25 | #else | |
26 | #define BT_LOG_USE_ANDROID_LOG 0 | |
27 | #endif | |
28 | #else | |
29 | #define BT_LOG_USE_ANDROID_LOG 0 | |
30 | #endif | |
31 | /* When defined, NSLog (uses Apple System Log) will be used instead of stderr | |
32 | * (ignored on non-Apple platforms). Date, time, pid and tid (context) will be | |
33 | * provided by NSLog. Curiously, doesn't use NSLog() directly, but piggybacks on | |
34 | * non-public CFLog() function. Both use Apple System Log internally, but it's | |
35 | * easier to call CFLog() from C than NSLog(). Current implementation doesn't | |
36 | * support "%@" format specifier. | |
37 | */ | |
38 | #ifdef BT_LOG_USE_NSLOG | |
39 | #undef BT_LOG_USE_NSLOG | |
40 | #if defined(__APPLE__) && defined(__MACH__) | |
41 | #define BT_LOG_USE_NSLOG 1 | |
42 | #else | |
43 | #define BT_LOG_USE_NSLOG 0 | |
44 | #endif | |
45 | #else | |
46 | #define BT_LOG_USE_NSLOG 0 | |
47 | #endif | |
48 | /* When defined, OutputDebugString() will be used instead of stderr (ignored on | |
49 | * non-Windows platforms). Uses OutputDebugStringA() variant and feeds it with | |
50 | * UTF-8 data. | |
51 | */ | |
52 | #ifdef BT_LOG_USE_DEBUGSTRING | |
53 | #undef BT_LOG_USE_DEBUGSTRING | |
54 | #if defined(_WIN32) || defined(_WIN64) | |
55 | #define BT_LOG_USE_DEBUGSTRING 1 | |
56 | #else | |
57 | #define BT_LOG_USE_DEBUGSTRING 0 | |
58 | #endif | |
59 | #else | |
60 | #define BT_LOG_USE_DEBUGSTRING 0 | |
61 | #endif | |
62 | /* When defined, bt_log library will not contain definition of tag prefix | |
63 | * variable. In that case it must be defined elsewhere using | |
64 | * BT_LOG_DEFINE_TAG_PREFIX macro, for example: | |
65 | * | |
66 | * BT_LOG_DEFINE_TAG_PREFIX = "ProcessName"; | |
67 | * | |
68 | * This allows to specify custom value for static initialization and avoid | |
69 | * overhead of setting this value in runtime. | |
70 | */ | |
71 | #ifdef BT_LOG_EXTERN_TAG_PREFIX | |
72 | #undef BT_LOG_EXTERN_TAG_PREFIX | |
73 | #define BT_LOG_EXTERN_TAG_PREFIX 1 | |
74 | #else | |
75 | #define BT_LOG_EXTERN_TAG_PREFIX 0 | |
76 | #endif | |
77 | /* When defined, bt_log library will not contain definition of global format | |
78 | * variable. In that case it must be defined elsewhere using | |
79 | * BT_LOG_DEFINE_GLOBAL_FORMAT macro, for example: | |
80 | * | |
81 | * BT_LOG_DEFINE_GLOBAL_FORMAT = {MEM_WIDTH}; | |
82 | * | |
83 | * This allows to specify custom value for static initialization and avoid | |
84 | * overhead of setting this value in runtime. | |
85 | */ | |
86 | #ifdef BT_LOG_EXTERN_GLOBAL_FORMAT | |
87 | #undef BT_LOG_EXTERN_GLOBAL_FORMAT | |
88 | #define BT_LOG_EXTERN_GLOBAL_FORMAT 1 | |
89 | #else | |
90 | #define BT_LOG_EXTERN_GLOBAL_FORMAT 0 | |
91 | #endif | |
92 | /* When defined, bt_log library will not contain definition of global output | |
93 | * variable. In that case it must be defined elsewhere using | |
94 | * BT_LOG_DEFINE_GLOBAL_OUTPUT macro, for example: | |
95 | * | |
96 | * BT_LOG_DEFINE_GLOBAL_OUTPUT = {BT_LOG_PUT_STD, custom_output_callback}; | |
97 | * | |
98 | * This allows to specify custom value for static initialization and avoid | |
99 | * overhead of setting this value in runtime. | |
100 | */ | |
101 | #ifdef BT_LOG_EXTERN_GLOBAL_OUTPUT | |
102 | #undef BT_LOG_EXTERN_GLOBAL_OUTPUT | |
103 | #define BT_LOG_EXTERN_GLOBAL_OUTPUT 1 | |
104 | #else | |
105 | #define BT_LOG_EXTERN_GLOBAL_OUTPUT 0 | |
106 | #endif | |
107 | /* When defined, bt_log library will not contain definition of global output | |
108 | * level variable. In that case it must be defined elsewhere using | |
109 | * BT_LOG_DEFINE_GLOBAL_OUTPUT_LEVEL macro, for example: | |
110 | * | |
111 | * BT_LOG_DEFINE_GLOBAL_OUTPUT_LEVEL = BT_LOG_WARN; | |
112 | * | |
113 | * This allows to specify custom value for static initialization and avoid | |
114 | * overhead of setting this value in runtime. | |
115 | */ | |
116 | #ifdef BT_LOG_EXTERN_GLOBAL_OUTPUT_LEVEL | |
117 | #undef BT_LOG_EXTERN_GLOBAL_OUTPUT_LEVEL | |
118 | #define BT_LOG_EXTERN_GLOBAL_OUTPUT_LEVEL 1 | |
119 | #else | |
120 | #define BT_LOG_EXTERN_GLOBAL_OUTPUT_LEVEL 0 | |
121 | #endif | |
122 | /* When defined, implementation will prefer smaller code size over speed. | |
123 | * Very rough estimate is that code will be up to 2x smaller and up to 2x | |
124 | * slower. Disabled by default. | |
125 | */ | |
126 | #ifdef BT_LOG_OPTIMIZE_SIZE | |
127 | #undef BT_LOG_OPTIMIZE_SIZE | |
128 | #define BT_LOG_OPTIMIZE_SIZE 1 | |
129 | #else | |
130 | #define BT_LOG_OPTIMIZE_SIZE 0 | |
131 | #endif | |
deaa6f85 | 132 | /* Size of the log line buffer. The buffer is a globally allocated per thread. |
beb0fb75 PP |
133 | */ |
134 | #ifndef BT_LOG_BUF_SZ | |
deaa6f85 | 135 | #define BT_LOG_BUF_SZ (4 * 4096) |
beb0fb75 PP |
136 | #endif |
137 | /* Default number of bytes in one line of memory output. For large values | |
138 | * BT_LOG_BUF_SZ also must be increased. | |
139 | */ | |
140 | #ifndef BT_LOG_MEM_WIDTH | |
141 | #define BT_LOG_MEM_WIDTH 32 | |
142 | #endif | |
143 | /* String to put in the end of each log line (can be empty). Its value used by | |
144 | * stderr output callback. Its size used as a default value for BT_LOG_EOL_SZ. | |
145 | */ | |
146 | #ifndef BT_LOG_EOL | |
147 | #define BT_LOG_EOL "\n" | |
148 | #endif | |
149 | /* Default delimiter that separates parts of log message. Can NOT contain '%' | |
150 | * or '\0'. | |
151 | * | |
152 | * Log message format specifications can override (or ignore) this value. For | |
153 | * more details see BT_LOG_MESSAGE_CTX_FORMAT, BT_LOG_MESSAGE_SRC_FORMAT and | |
154 | * BT_LOG_MESSAGE_TAG_FORMAT. | |
155 | */ | |
156 | #ifndef BT_LOG_DEF_DELIMITER | |
157 | #define BT_LOG_DEF_DELIMITER " " | |
158 | #endif | |
159 | /* Specifies log message context format. Log message context includes date, | |
160 | * time, process id, thread id and message's log level. Custom information can | |
161 | * be added as well. Supported fields: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, | |
162 | * MILLISECOND, PID, TID, LEVEL, S(str), F_INIT(statements), | |
163 | * F_UINT(width, value). | |
164 | * | |
165 | * Must be defined as a tuple, for example: | |
166 | * | |
167 | * #define BT_LOG_MESSAGE_CTX_FORMAT (YEAR, S("."), MONTH, S("."), DAY, S(" > ")) | |
168 | * | |
169 | * In that case, resulting log message will be: | |
170 | * | |
171 | * 2016.12.22 > TAG function@filename.c:line Message text | |
172 | * | |
173 | * Note, that tag, source location and message text are not impacted by | |
174 | * this setting. See BT_LOG_MESSAGE_TAG_FORMAT and BT_LOG_MESSAGE_SRC_FORMAT. | |
175 | * | |
176 | * If message context must be visually separated from the rest of the message, | |
177 | * it must be reflected in context format (notice trailing S(" > ") in the | |
178 | * example above). | |
179 | * | |
180 | * S(str) adds constant string str. String can NOT contain '%' or '\0'. | |
181 | * | |
182 | * F_INIT(statements) adds initialization statement(s) that will be evaluated | |
183 | * once for each log message. All statements are evaluated in specified order. | |
184 | * Several F_INIT() fields can be used in every log message format | |
185 | * specification. Fields, like F_UINT(width, value), are allowed to use results | |
186 | * of initialization statements. If statement introduces variables (or other | |
187 | * names, like structures) they must be prefixed with "f_". Statements must be | |
188 | * enclosed into additional "()". Example: | |
189 | * | |
190 | * #define BT_LOG_MESSAGE_CTX_FORMAT \ | |
191 | * (F_INIT(( struct rusage f_ru; getrusage(RUSAGE_SELF, &f_ru); )), \ | |
192 | * YEAR, S("."), MONTH, S("."), DAY, S(" "), \ | |
193 | * F_UINT(5, f_ru.ru_nsignals), \ | |
194 | * S(" ")) | |
195 | * | |
196 | * F_UINT(width, value) adds unsigned integer value extended with up to width | |
197 | * spaces (for alignment purposes). Value can be any expression that evaluates | |
198 | * to unsigned integer. If expression contains non-standard functions, they | |
199 | * must be declared with F_INIT(). Example: | |
200 | * | |
201 | * #define BT_LOG_MESSAGE_CTX_FORMAT \ | |
202 | * (YEAR, S("."), MONTH, S("."), DAY, S(" "), \ | |
203 | * F_INIT(( unsigned tickcount(); )), \ | |
204 | * F_UINT(5, tickcount()), \ | |
205 | * S(" ")) | |
206 | * | |
207 | * Other log message format specifications follow same rules, but have a | |
208 | * different set of supported fields. | |
209 | */ | |
210 | #ifndef BT_LOG_MESSAGE_CTX_FORMAT | |
211 | #define BT_LOG_MESSAGE_CTX_FORMAT \ | |
212 | (MONTH, S("-"), DAY, S(BT_LOG_DEF_DELIMITER), \ | |
213 | HOUR, S(":"), MINUTE, S(":"), SECOND, S("."), MILLISECOND, S(BT_LOG_DEF_DELIMITER), \ | |
214 | PID, S(BT_LOG_DEF_DELIMITER), TID, S(BT_LOG_DEF_DELIMITER), \ | |
215 | LEVEL, S(BT_LOG_DEF_DELIMITER)) | |
216 | #endif | |
217 | /* Example: | |
218 | */ | |
219 | /* Specifies log message tag format. It includes tag prefix and tag. Custom | |
220 | * information can be added as well. Supported fields: | |
221 | * TAG(prefix_delimiter, tag_delimiter), S(str), F_INIT(statements), | |
222 | * F_UINT(width, value). | |
223 | * | |
224 | * TAG(prefix_delimiter, tag_delimiter) adds following string to log message: | |
225 | * | |
226 | * PREFIX<prefix_delimiter>TAG<tag_delimiter> | |
227 | * | |
228 | * Prefix delimiter will be used only when prefix is not empty. Tag delimiter | |
229 | * will be used only when prefixed tag is not empty. Example: | |
230 | * | |
231 | * #define BT_LOG_TAG_FORMAT (S("["), TAG(".", ""), S("] ")) | |
232 | * | |
233 | * See BT_LOG_MESSAGE_CTX_FORMAT for details. | |
234 | */ | |
235 | #ifndef BT_LOG_MESSAGE_TAG_FORMAT | |
236 | #define BT_LOG_MESSAGE_TAG_FORMAT \ | |
237 | (TAG(".", BT_LOG_DEF_DELIMITER)) | |
238 | #endif | |
239 | /* Specifies log message source location format. It includes function name, | |
240 | * file name and file line. Custom information can be added as well. Supported | |
241 | * fields: FUNCTION, FILENAME, FILELINE, S(str), F_INIT(statements), | |
242 | * F_UINT(width, value). | |
243 | * | |
244 | * See BT_LOG_MESSAGE_CTX_FORMAT for details. | |
245 | */ | |
246 | #ifndef BT_LOG_MESSAGE_SRC_FORMAT | |
247 | #define BT_LOG_MESSAGE_SRC_FORMAT \ | |
248 | (FUNCTION, S("@"), FILENAME, S(":"), FILELINE, S(BT_LOG_DEF_DELIMITER)) | |
249 | #endif | |
250 | /* Fields that can be used in log message format specifications (see above). | |
251 | * Mentioning them here explicitly, so we know that nobody else defined them | |
252 | * before us. See BT_LOG_MESSAGE_CTX_FORMAT for details. | |
253 | */ | |
254 | #define YEAR YEAR | |
255 | #define MONTH MONTH | |
256 | #define DAY DAY | |
257 | #define MINUTE MINUTE | |
258 | #define SECOND SECOND | |
259 | #define MILLISECOND MILLISECOND | |
260 | #define PID PID | |
261 | #define TID TID | |
262 | #define LEVEL LEVEL | |
263 | #define TAG(prefix_delim, tag_delim) TAG(prefix_delim, tag_delim) | |
264 | #define FUNCTION FUNCTION | |
265 | #define FILENAME FILENAME | |
266 | #define FILELINE FILELINE | |
267 | #define S(str) S(str) | |
268 | #define F_INIT(statements) F_INIT(statements) | |
269 | #define F_UINT(width, value) F_UINT(width, value) | |
270 | /* Number of bytes to reserve for EOL in the log line buffer (must be >0). | |
271 | * Must be larger than or equal to length of BT_LOG_EOL with terminating null. | |
272 | */ | |
273 | #ifndef BT_LOG_EOL_SZ | |
274 | #define BT_LOG_EOL_SZ sizeof(BT_LOG_EOL) | |
275 | #endif | |
276 | /* Compile instrumented version of the library to facilitate unit testing. | |
277 | */ | |
278 | #ifndef BT_LOG_INSTRUMENTED | |
279 | #define BT_LOG_INSTRUMENTED 0 | |
280 | #endif | |
281 | ||
282 | #if defined(__linux__) | |
283 | #if !defined(__ANDROID__) && !defined(_GNU_SOURCE) | |
284 | #define _GNU_SOURCE | |
285 | #endif | |
286 | #endif | |
287 | #if defined(__MINGW32__) | |
288 | #ifdef __STRICT_ANSI__ | |
289 | #undef __STRICT_ANSI__ | |
290 | #endif | |
291 | #endif | |
f6ccaed9 | 292 | #include <babeltrace/assert-internal.h> |
beb0fb75 PP |
293 | #include <ctype.h> |
294 | #include <string.h> | |
295 | #include <time.h> | |
296 | #include <stdarg.h> | |
297 | #include <stddef.h> | |
298 | #include <stdlib.h> | |
299 | #include <stdio.h> | |
00a52b39 PP |
300 | |
301 | #define BT_LOG_OUTPUT_LEVEL dummy | |
302 | ||
beb0fb75 PP |
303 | #include <babeltrace/logging-internal.h> |
304 | #include <babeltrace/logging.h> | |
305 | ||
306 | #if defined(_WIN32) || defined(_WIN64) | |
307 | #include <windows.h> | |
308 | #else | |
309 | #include <unistd.h> | |
310 | #include <sys/time.h> | |
311 | #if defined(__linux__) | |
312 | #include <linux/limits.h> | |
94ddf5ca MJ |
313 | #elif (defined(__sun__) || defined(__CYGWIN__)) |
314 | /* Solaris and Cygwin have no sys/syslimits.h */ | |
beb0fb75 PP |
315 | #else |
316 | #include <sys/syslimits.h> | |
317 | #endif | |
318 | #endif | |
319 | ||
320 | #if defined(__linux__) | |
321 | #include <sys/prctl.h> | |
322 | #include <sys/types.h> | |
323 | #if !defined(__ANDROID__) | |
324 | #include <sys/syscall.h> | |
325 | #endif | |
326 | #endif | |
327 | #if defined(__MACH__) | |
328 | #include <pthread.h> | |
329 | #endif | |
330 | ||
331 | #define INLINE _BT_LOG_INLINE | |
332 | #define VAR_UNUSED(var) (void)var | |
333 | #define RETVAL_UNUSED(expr) do { while(expr) break; } while(0) | |
334 | #define STATIC_ASSERT(name, cond) \ | |
335 | typedef char assert_##name[(cond)? 1: -1] | |
336 | #define ASSERT_UNREACHABLE(why) assert(!sizeof(why)) | |
337 | #ifndef _countof | |
338 | #define _countof(xs) (sizeof(xs) / sizeof((xs)[0])) | |
339 | #endif | |
340 | ||
341 | #if BT_LOG_INSTRUMENTED | |
342 | #define INSTRUMENTED_CONST | |
343 | #else | |
344 | #define INSTRUMENTED_CONST const | |
345 | #endif | |
346 | ||
347 | #define _PP_PASTE_2(a, b) a ## b | |
348 | #define _PP_CONCAT_2(a, b) _PP_PASTE_2(a, b) | |
349 | ||
350 | #define _PP_PASTE_3(a, b, c) a ## b ## c | |
351 | #define _PP_CONCAT_3(a, b, c) _PP_PASTE_3(a, b, c) | |
352 | ||
353 | /* Microsoft C preprocessor is a piece of shit. This moron treats __VA_ARGS__ | |
354 | * as a single token and requires additional expansion to realize that it's | |
355 | * actually a list. If not for it, there would be no need in this extra | |
356 | * expansion. | |
357 | */ | |
358 | #define _PP_ID(x) x | |
359 | #define _PP_NARGS_N(_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,...) _24 | |
360 | #define _PP_NARGS(...) _PP_ID(_PP_NARGS_N(__VA_ARGS__,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)) | |
361 | ||
362 | /* There is a more efficient way to implement this, but it requires | |
363 | * working C preprocessor. Unfortunately, Microsoft Visual Studio doesn't | |
364 | * have one. | |
365 | */ | |
366 | #define _PP_HEAD__(x, ...) x | |
367 | #define _PP_HEAD_(...) _PP_ID(_PP_HEAD__(__VA_ARGS__, ~)) | |
368 | #define _PP_HEAD(xs) _PP_HEAD_ xs | |
369 | #define _PP_TAIL_(x, ...) (__VA_ARGS__) | |
370 | #define _PP_TAIL(xs) _PP_TAIL_ xs | |
371 | #define _PP_UNTUPLE_(...) __VA_ARGS__ | |
372 | #define _PP_UNTUPLE(xs) _PP_UNTUPLE_ xs | |
373 | ||
374 | /* Apply function macro to each element in tuple. Output is not | |
375 | * enforced to be a tuple. | |
376 | */ | |
377 | #define _PP_MAP_1(f, xs) f(_PP_HEAD(xs)) | |
378 | #define _PP_MAP_2(f, xs) f(_PP_HEAD(xs)) _PP_MAP_1(f, _PP_TAIL(xs)) | |
379 | #define _PP_MAP_3(f, xs) f(_PP_HEAD(xs)) _PP_MAP_2(f, _PP_TAIL(xs)) | |
380 | #define _PP_MAP_4(f, xs) f(_PP_HEAD(xs)) _PP_MAP_3(f, _PP_TAIL(xs)) | |
381 | #define _PP_MAP_5(f, xs) f(_PP_HEAD(xs)) _PP_MAP_4(f, _PP_TAIL(xs)) | |
382 | #define _PP_MAP_6(f, xs) f(_PP_HEAD(xs)) _PP_MAP_5(f, _PP_TAIL(xs)) | |
383 | #define _PP_MAP_7(f, xs) f(_PP_HEAD(xs)) _PP_MAP_6(f, _PP_TAIL(xs)) | |
384 | #define _PP_MAP_8(f, xs) f(_PP_HEAD(xs)) _PP_MAP_7(f, _PP_TAIL(xs)) | |
385 | #define _PP_MAP_9(f, xs) f(_PP_HEAD(xs)) _PP_MAP_8(f, _PP_TAIL(xs)) | |
386 | #define _PP_MAP_10(f, xs) f(_PP_HEAD(xs)) _PP_MAP_9(f, _PP_TAIL(xs)) | |
387 | #define _PP_MAP_11(f, xs) f(_PP_HEAD(xs)) _PP_MAP_10(f, _PP_TAIL(xs)) | |
388 | #define _PP_MAP_12(f, xs) f(_PP_HEAD(xs)) _PP_MAP_11(f, _PP_TAIL(xs)) | |
389 | #define _PP_MAP_13(f, xs) f(_PP_HEAD(xs)) _PP_MAP_12(f, _PP_TAIL(xs)) | |
390 | #define _PP_MAP_14(f, xs) f(_PP_HEAD(xs)) _PP_MAP_13(f, _PP_TAIL(xs)) | |
391 | #define _PP_MAP_15(f, xs) f(_PP_HEAD(xs)) _PP_MAP_14(f, _PP_TAIL(xs)) | |
392 | #define _PP_MAP_16(f, xs) f(_PP_HEAD(xs)) _PP_MAP_15(f, _PP_TAIL(xs)) | |
393 | #define _PP_MAP_17(f, xs) f(_PP_HEAD(xs)) _PP_MAP_16(f, _PP_TAIL(xs)) | |
394 | #define _PP_MAP_18(f, xs) f(_PP_HEAD(xs)) _PP_MAP_17(f, _PP_TAIL(xs)) | |
395 | #define _PP_MAP_19(f, xs) f(_PP_HEAD(xs)) _PP_MAP_18(f, _PP_TAIL(xs)) | |
396 | #define _PP_MAP_20(f, xs) f(_PP_HEAD(xs)) _PP_MAP_19(f, _PP_TAIL(xs)) | |
397 | #define _PP_MAP_21(f, xs) f(_PP_HEAD(xs)) _PP_MAP_20(f, _PP_TAIL(xs)) | |
398 | #define _PP_MAP_22(f, xs) f(_PP_HEAD(xs)) _PP_MAP_21(f, _PP_TAIL(xs)) | |
399 | #define _PP_MAP_23(f, xs) f(_PP_HEAD(xs)) _PP_MAP_22(f, _PP_TAIL(xs)) | |
400 | #define _PP_MAP_24(f, xs) f(_PP_HEAD(xs)) _PP_MAP_23(f, _PP_TAIL(xs)) | |
401 | #define _PP_MAP(f, xs) _PP_CONCAT_2(_PP_MAP_, _PP_NARGS xs) (f, xs) | |
402 | ||
403 | /* Apply function macro to each element in tuple in reverse order. | |
404 | * Output is not enforced to be a tuple. | |
405 | */ | |
406 | #define _PP_RMAP_1(f, xs) f(_PP_HEAD(xs)) | |
407 | #define _PP_RMAP_2(f, xs) _PP_RMAP_1(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
408 | #define _PP_RMAP_3(f, xs) _PP_RMAP_2(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
409 | #define _PP_RMAP_4(f, xs) _PP_RMAP_3(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
410 | #define _PP_RMAP_5(f, xs) _PP_RMAP_4(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
411 | #define _PP_RMAP_6(f, xs) _PP_RMAP_5(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
412 | #define _PP_RMAP_7(f, xs) _PP_RMAP_6(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
413 | #define _PP_RMAP_8(f, xs) _PP_RMAP_7(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
414 | #define _PP_RMAP_9(f, xs) _PP_RMAP_8(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
415 | #define _PP_RMAP_10(f, xs) _PP_RMAP_9(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
416 | #define _PP_RMAP_11(f, xs) _PP_RMAP_10(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
417 | #define _PP_RMAP_12(f, xs) _PP_RMAP_11(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
418 | #define _PP_RMAP_13(f, xs) _PP_RMAP_12(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
419 | #define _PP_RMAP_14(f, xs) _PP_RMAP_13(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
420 | #define _PP_RMAP_15(f, xs) _PP_RMAP_14(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
421 | #define _PP_RMAP_16(f, xs) _PP_RMAP_15(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
422 | #define _PP_RMAP_17(f, xs) _PP_RMAP_16(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
423 | #define _PP_RMAP_18(f, xs) _PP_RMAP_17(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
424 | #define _PP_RMAP_19(f, xs) _PP_RMAP_18(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
425 | #define _PP_RMAP_20(f, xs) _PP_RMAP_19(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
426 | #define _PP_RMAP_21(f, xs) _PP_RMAP_20(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
427 | #define _PP_RMAP_22(f, xs) _PP_RMAP_21(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
428 | #define _PP_RMAP_23(f, xs) _PP_RMAP_22(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
429 | #define _PP_RMAP_24(f, xs) _PP_RMAP_23(f, _PP_TAIL(xs)) f(_PP_HEAD(xs)) | |
430 | #define _PP_RMAP(f, xs) _PP_CONCAT_2(_PP_RMAP_, _PP_NARGS xs) (f, xs) | |
431 | ||
432 | /* Used to implement _BT_LOG_MESSAGE_FORMAT_CONTAINS() macro. All possible | |
433 | * fields must be mentioned here. Not counting F_INIT() here because it's | |
434 | * somewhat special and is handled spearatly (at least for now). | |
435 | */ | |
436 | #define _BT_LOG_MESSAGE_FORMAT_MASK__ (0<<0) | |
437 | #define _BT_LOG_MESSAGE_FORMAT_MASK__YEAR (1<<1) | |
438 | #define _BT_LOG_MESSAGE_FORMAT_MASK__MONTH (1<<2) | |
439 | #define _BT_LOG_MESSAGE_FORMAT_MASK__DAY (1<<3) | |
440 | #define _BT_LOG_MESSAGE_FORMAT_MASK__HOUR (1<<4) | |
441 | #define _BT_LOG_MESSAGE_FORMAT_MASK__MINUTE (1<<5) | |
442 | #define _BT_LOG_MESSAGE_FORMAT_MASK__SECOND (1<<6) | |
443 | #define _BT_LOG_MESSAGE_FORMAT_MASK__MILLISECOND (1<<7) | |
444 | #define _BT_LOG_MESSAGE_FORMAT_MASK__PID (1<<8) | |
445 | #define _BT_LOG_MESSAGE_FORMAT_MASK__TID (1<<9) | |
446 | #define _BT_LOG_MESSAGE_FORMAT_MASK__LEVEL (1<<10) | |
447 | #define _BT_LOG_MESSAGE_FORMAT_MASK__TAG(ps, ts) (1<<11) | |
448 | #define _BT_LOG_MESSAGE_FORMAT_MASK__FUNCTION (1<<12) | |
449 | #define _BT_LOG_MESSAGE_FORMAT_MASK__FILENAME (1<<13) | |
450 | #define _BT_LOG_MESSAGE_FORMAT_MASK__FILELINE (1<<14) | |
451 | #define _BT_LOG_MESSAGE_FORMAT_MASK__S(s) (1<<15) | |
452 | #define _BT_LOG_MESSAGE_FORMAT_MASK__F_INIT(expr) (0<<16) | |
453 | #define _BT_LOG_MESSAGE_FORMAT_MASK__F_UINT(w, v) (1<<17) | |
454 | #define _BT_LOG_MESSAGE_FORMAT_MASK(field) \ | |
455 | _PP_CONCAT_3(_BT_LOG_MESSAGE_FORMAT_MASK_, _, field) | |
456 | ||
457 | /* Logical "or" of masks of fields used in specified format specification. | |
458 | */ | |
459 | #define _BT_LOG_MESSAGE_FORMAT_FIELDS(format) \ | |
460 | (0 _PP_MAP(| _BT_LOG_MESSAGE_FORMAT_MASK, format)) | |
461 | ||
462 | /* Expands to expressions that evaluates to true if field is used in | |
463 | * specified format specification. Example: | |
464 | * | |
465 | * #if _BT_LOG_MESSAGE_FORMAT_CONTAINS(F_UINT, BT_LOG_MESSAGE_CTX_FORMAT) | |
466 | * ... | |
467 | * #endif | |
468 | */ | |
469 | #define _BT_LOG_MESSAGE_FORMAT_CONTAINS(field, format) \ | |
470 | (_BT_LOG_MESSAGE_FORMAT_MASK(field) & _BT_LOG_MESSAGE_FORMAT_FIELDS(format)) | |
471 | ||
472 | /* Same, but checks all supported format specifications. | |
473 | */ | |
474 | #define _BT_LOG_MESSAGE_FORMAT_FIELD_USED(field) \ | |
475 | (_BT_LOG_MESSAGE_FORMAT_CONTAINS(field, BT_LOG_MESSAGE_CTX_FORMAT) || \ | |
476 | _BT_LOG_MESSAGE_FORMAT_CONTAINS(field, BT_LOG_MESSAGE_TAG_FORMAT) || \ | |
477 | _BT_LOG_MESSAGE_FORMAT_CONTAINS(field, BT_LOG_MESSAGE_SRC_FORMAT)) | |
478 | ||
479 | #define _BT_LOG_MESSAGE_FORMAT_DATETIME_USED \ | |
480 | (_BT_LOG_MESSAGE_FORMAT_CONTAINS(YEAR, BT_LOG_MESSAGE_CTX_FORMAT) || \ | |
481 | _BT_LOG_MESSAGE_FORMAT_CONTAINS(MONTH, BT_LOG_MESSAGE_CTX_FORMAT) || \ | |
482 | _BT_LOG_MESSAGE_FORMAT_CONTAINS(DAY, BT_LOG_MESSAGE_CTX_FORMAT) || \ | |
483 | _BT_LOG_MESSAGE_FORMAT_CONTAINS(HOUR, BT_LOG_MESSAGE_CTX_FORMAT) || \ | |
484 | _BT_LOG_MESSAGE_FORMAT_CONTAINS(MINUTE, BT_LOG_MESSAGE_CTX_FORMAT) || \ | |
485 | _BT_LOG_MESSAGE_FORMAT_CONTAINS(SECOND, BT_LOG_MESSAGE_CTX_FORMAT) || \ | |
486 | _BT_LOG_MESSAGE_FORMAT_CONTAINS(MILLISECOND, BT_LOG_MESSAGE_CTX_FORMAT)) | |
487 | ||
488 | #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) | |
489 | #pragma warning(disable:4204) /* nonstandard extension used: non-constant aggregate initializer */ | |
490 | #define memccpy _memccpy | |
491 | #endif | |
492 | ||
994e8fa3 MJ |
493 | #if (defined(_MSC_VER) && !defined(__INTEL_COMPILER)) || \ |
494 | (defined(__MINGW64__) && !defined(__USE_MINGW_ANSI_STDIO)) | |
beb0fb75 PP |
495 | #define vsnprintf(s, sz, fmt, va) fake_vsnprintf(s, sz, fmt, va) |
496 | static int fake_vsnprintf(char *s, size_t sz, const char *fmt, va_list ap) | |
497 | { | |
498 | const int n = vsnprintf_s(s, sz, _TRUNCATE, fmt, ap); | |
499 | return 0 < n? n: (int)sz + 1; /* no need in _vscprintf() for now */ | |
500 | } | |
501 | #if BT_LOG_OPTIMIZE_SIZE | |
502 | #define snprintf(s, sz, ...) fake_snprintf(s, sz, __VA_ARGS__) | |
503 | static int fake_snprintf(char *s, size_t sz, const char *fmt, ...) | |
504 | { | |
505 | va_list va; | |
506 | va_start(va, fmt); | |
507 | const int n = fake_vsnprintf(s, sz, fmt, va); | |
508 | va_end(va); | |
509 | return n; | |
510 | } | |
511 | #endif | |
512 | #endif | |
513 | ||
514 | typedef void (*time_cb)(struct tm *const tm, unsigned *const usec); | |
515 | typedef void (*pid_cb)(int *const pid, int *const tid); | |
516 | typedef void (*buffer_cb)(bt_log_message *msg, char *buf); | |
517 | ||
518 | typedef struct src_location | |
519 | { | |
520 | const char *const func; | |
521 | const char *const file; | |
522 | const unsigned line; | |
523 | } | |
524 | src_location; | |
525 | ||
526 | typedef struct mem_block | |
527 | { | |
528 | const void *const d; | |
529 | const unsigned d_sz; | |
530 | } | |
531 | mem_block; | |
532 | ||
533 | static void time_callback(struct tm *const tm, unsigned *const usec); | |
534 | static void pid_callback(int *const pid, int *const tid); | |
535 | static void buffer_callback(bt_log_message *msg, char *buf); | |
536 | ||
537 | STATIC_ASSERT(eol_fits_eol_sz, sizeof(BT_LOG_EOL) <= BT_LOG_EOL_SZ); | |
538 | STATIC_ASSERT(eol_sz_greater_than_zero, 0 < BT_LOG_EOL_SZ); | |
539 | STATIC_ASSERT(eol_sz_less_than_buf_sz, BT_LOG_EOL_SZ < BT_LOG_BUF_SZ); | |
beb0fb75 PP |
540 | static const char c_hex[] = "0123456789abcdef"; |
541 | ||
deaa6f85 PP |
542 | static char __thread logging_buf[4 * 4096]; |
543 | ||
beb0fb75 PP |
544 | static INSTRUMENTED_CONST unsigned g_buf_sz = BT_LOG_BUF_SZ - BT_LOG_EOL_SZ; |
545 | static INSTRUMENTED_CONST time_cb g_time_cb = time_callback; | |
546 | static INSTRUMENTED_CONST pid_cb g_pid_cb = pid_callback; | |
547 | static INSTRUMENTED_CONST buffer_cb g_buffer_cb = buffer_callback; | |
548 | ||
549 | #if BT_LOG_USE_ANDROID_LOG | |
550 | #include <android/log.h> | |
551 | ||
552 | static INLINE int android_lvl(const int lvl) | |
553 | { | |
554 | switch (lvl) | |
555 | { | |
556 | case BT_LOG_VERBOSE: | |
557 | return ANDROID_LOG_VERBOSE; | |
558 | case BT_LOG_DEBUG: | |
559 | return ANDROID_LOG_DEBUG; | |
560 | case BT_LOG_INFO: | |
561 | return ANDROID_LOG_INFO; | |
562 | case BT_LOG_WARN: | |
563 | return ANDROID_LOG_WARN; | |
564 | case BT_LOG_ERROR: | |
565 | return ANDROID_LOG_ERROR; | |
566 | case BT_LOG_FATAL: | |
567 | return ANDROID_LOG_FATAL; | |
568 | default: | |
569 | ASSERT_UNREACHABLE("Bad log level"); | |
570 | return ANDROID_LOG_UNKNOWN; | |
571 | } | |
572 | } | |
573 | ||
574 | static void out_android_callback(const bt_log_message *const msg, void *arg) | |
575 | { | |
576 | VAR_UNUSED(arg); | |
577 | *msg->p = 0; | |
578 | const char *tag = msg->p; | |
579 | if (msg->tag_e != msg->tag_b) | |
580 | { | |
581 | tag = msg->tag_b; | |
582 | *msg->tag_e = 0; | |
583 | } | |
584 | __android_log_print(android_lvl(msg->lvl), tag, "%s", msg->msg_b); | |
585 | } | |
586 | ||
587 | enum { OUT_ANDROID_MASK = BT_LOG_PUT_STD & ~BT_LOG_PUT_CTX }; | |
588 | #define OUT_ANDROID OUT_ANDROID_MASK, 0, out_android_callback | |
589 | #endif | |
590 | ||
591 | #if BT_LOG_USE_NSLOG | |
592 | #include <CoreFoundation/CoreFoundation.h> | |
593 | CF_EXPORT void CFLog(int32_t level, CFStringRef format, ...); | |
594 | ||
595 | static INLINE int apple_lvl(const int lvl) | |
596 | { | |
597 | switch (lvl) | |
598 | { | |
599 | case BT_LOG_VERBOSE: | |
600 | return 7; /* ASL_LEVEL_DEBUG / kCFLogLevelDebug */; | |
601 | case BT_LOG_DEBUG: | |
602 | return 7; /* ASL_LEVEL_DEBUG / kCFLogLevelDebug */; | |
603 | case BT_LOG_INFO: | |
604 | return 6; /* ASL_LEVEL_INFO / kCFLogLevelInfo */; | |
605 | case BT_LOG_WARN: | |
606 | return 4; /* ASL_LEVEL_WARNING / kCFLogLevelWarning */; | |
607 | case BT_LOG_ERROR: | |
608 | return 3; /* ASL_LEVEL_ERR / kCFLogLevelError */; | |
609 | case BT_LOG_FATAL: | |
610 | return 0; /* ASL_LEVEL_EMERG / kCFLogLevelEmergency */; | |
611 | default: | |
612 | ASSERT_UNREACHABLE("Bad log level"); | |
613 | return 0; /* ASL_LEVEL_EMERG / kCFLogLevelEmergency */; | |
614 | } | |
615 | } | |
616 | ||
617 | static void out_nslog_callback(const bt_log_message *const msg, void *arg) | |
618 | { | |
619 | VAR_UNUSED(arg); | |
620 | *msg->p = 0; | |
621 | CFLog(apple_lvl(msg->lvl), CFSTR("%s"), msg->tag_b); | |
622 | } | |
623 | ||
624 | enum { OUT_NSLOG_MASK = BT_LOG_PUT_STD & ~BT_LOG_PUT_CTX }; | |
625 | #define OUT_NSLOG OUT_NSLOG_MASK, 0, out_nslog_callback | |
626 | #endif | |
627 | ||
628 | #if BT_LOG_USE_DEBUGSTRING | |
629 | #include <windows.h> | |
630 | ||
631 | static void out_debugstring_callback(const bt_log_message *const msg, void *arg) | |
632 | { | |
633 | VAR_UNUSED(arg); | |
634 | msg->p[0] = '\n'; | |
635 | msg->p[1] = '\0'; | |
636 | OutputDebugStringA(msg->buf); | |
637 | } | |
638 | ||
639 | enum { OUT_DEBUGSTRING_MASK = BT_LOG_PUT_STD }; | |
640 | #define OUT_DEBUGSTRING OUT_DEBUGSTRING_MASK, 0, out_debugstring_callback | |
641 | #endif | |
642 | ||
643 | BT_HIDDEN | |
644 | void bt_log_out_stderr_callback(const bt_log_message *const msg, void *arg) | |
645 | { | |
646 | VAR_UNUSED(arg); | |
647 | const size_t eol_len = sizeof(BT_LOG_EOL) - 1; | |
648 | memcpy(msg->p, BT_LOG_EOL, eol_len); | |
649 | #if defined(_WIN32) || defined(_WIN64) | |
650 | /* WriteFile() is atomic for local files opened with FILE_APPEND_DATA and | |
651 | without FILE_WRITE_DATA */ | |
652 | WriteFile(GetStdHandle(STD_ERROR_HANDLE), msg->buf, | |
653 | (DWORD)(msg->p - msg->buf + eol_len), 0, 0); | |
654 | #else | |
655 | /* write() is atomic for buffers less than or equal to PIPE_BUF. */ | |
656 | RETVAL_UNUSED(write(STDERR_FILENO, msg->buf, | |
657 | (size_t)(msg->p - msg->buf) + eol_len)); | |
658 | #endif | |
659 | } | |
660 | ||
661 | static const bt_log_output out_stderr = {BT_LOG_OUT_STDERR}; | |
662 | ||
663 | #if !BT_LOG_EXTERN_TAG_PREFIX | |
664 | BT_LOG_DEFINE_TAG_PREFIX = 0; | |
665 | #endif | |
666 | ||
667 | #if !BT_LOG_EXTERN_GLOBAL_FORMAT | |
668 | BT_LOG_DEFINE_GLOBAL_FORMAT = {BT_LOG_MEM_WIDTH}; | |
669 | #endif | |
670 | ||
671 | #if !BT_LOG_EXTERN_GLOBAL_OUTPUT | |
672 | #if BT_LOG_USE_ANDROID_LOG | |
673 | BT_LOG_DEFINE_GLOBAL_OUTPUT = {OUT_ANDROID}; | |
674 | #elif BT_LOG_USE_NSLOG | |
675 | BT_LOG_DEFINE_GLOBAL_OUTPUT = {OUT_NSLOG}; | |
676 | #elif BT_LOG_USE_DEBUGSTRING | |
677 | BT_LOG_DEFINE_GLOBAL_OUTPUT = {OUT_DEBUGSTRING}; | |
678 | #else | |
679 | BT_LOG_DEFINE_GLOBAL_OUTPUT = {BT_LOG_OUT_STDERR}; | |
680 | #endif | |
681 | #endif | |
682 | ||
683 | #if !BT_LOG_EXTERN_GLOBAL_OUTPUT_LEVEL | |
684 | BT_LOG_DEFINE_GLOBAL_OUTPUT_LEVEL = 0; | |
685 | #endif | |
686 | ||
4f1c7f2e | 687 | BT_HIDDEN |
beb0fb75 PP |
688 | const bt_log_spec _bt_log_stderr_spec = |
689 | { | |
690 | BT_LOG_GLOBAL_FORMAT, | |
691 | &out_stderr, | |
692 | }; | |
693 | ||
694 | static const bt_log_spec global_spec = | |
695 | { | |
696 | BT_LOG_GLOBAL_FORMAT, | |
697 | BT_LOG_GLOBAL_OUTPUT, | |
698 | }; | |
699 | ||
700 | #if _BT_LOG_MESSAGE_FORMAT_CONTAINS(LEVEL, BT_LOG_MESSAGE_CTX_FORMAT) | |
701 | static char lvl_char(const int lvl) | |
702 | { | |
703 | switch (lvl) | |
704 | { | |
705 | case BT_LOG_VERBOSE: | |
706 | return 'V'; | |
707 | case BT_LOG_DEBUG: | |
708 | return 'D'; | |
709 | case BT_LOG_INFO: | |
710 | return 'I'; | |
711 | case BT_LOG_WARN: | |
712 | return 'W'; | |
713 | case BT_LOG_ERROR: | |
714 | return 'E'; | |
715 | case BT_LOG_FATAL: | |
716 | return 'F'; | |
717 | default: | |
718 | ASSERT_UNREACHABLE("Bad log level"); | |
719 | return '?'; | |
720 | } | |
721 | } | |
722 | #endif | |
723 | ||
724 | #define GCCVER_LESS(MAJOR, MINOR, PATCH) \ | |
725 | (__GNUC__ < MAJOR || \ | |
726 | (__GNUC__ == MAJOR && (__GNUC_MINOR__ < MINOR || \ | |
727 | (__GNUC_MINOR__ == MINOR && __GNUC_PATCHLEVEL__ < PATCH)))) | |
728 | ||
729 | #if !defined(__clang__) && defined(__GNUC__) && GCCVER_LESS(4,7,0) | |
730 | #define __atomic_load_n(vp, model) __sync_fetch_and_add(vp, 0) | |
731 | #define __atomic_fetch_add(vp, n, model) __sync_fetch_and_add(vp, n) | |
732 | #define __atomic_sub_fetch(vp, n, model) __sync_sub_and_fetch(vp, n) | |
733 | #define __atomic_or_fetch(vp, n, model) __sync_or_and_fetch(vp, n) | |
734 | #define __atomic_and_fetch(vp, n, model) __sync_and_and_fetch(vp, n) | |
735 | /* Note: will not store old value of *vp in *ep (non-standard behaviour) */ | |
736 | #define __atomic_compare_exchange_n(vp, ep, d, weak, smodel, fmodel) \ | |
737 | __sync_bool_compare_and_swap(vp, *(ep), d) | |
738 | #endif | |
739 | ||
740 | #if !BT_LOG_OPTIMIZE_SIZE && !defined(_WIN32) && !defined(_WIN64) | |
741 | #define TCACHE | |
742 | #define TCACHE_STALE (0x40000000) | |
743 | #define TCACHE_FLUID (0x40000000 | 0x80000000) | |
744 | static unsigned g_tcache_mode = TCACHE_STALE; | |
745 | static struct timeval g_tcache_tv = {0, 0}; | |
f833fd04 | 746 | static struct tm g_tcache_tm = {0}; |
beb0fb75 PP |
747 | |
748 | static INLINE int tcache_get(const struct timeval *const tv, struct tm *const tm) | |
749 | { | |
750 | unsigned mode; | |
751 | mode = __atomic_load_n(&g_tcache_mode, __ATOMIC_RELAXED); | |
752 | if (0 == (mode & TCACHE_FLUID)) | |
753 | { | |
754 | mode = __atomic_fetch_add(&g_tcache_mode, 1, __ATOMIC_ACQUIRE); | |
755 | if (0 == (mode & TCACHE_FLUID)) | |
756 | { | |
757 | if (g_tcache_tv.tv_sec == tv->tv_sec) | |
758 | { | |
759 | *tm = g_tcache_tm; | |
760 | __atomic_sub_fetch(&g_tcache_mode, 1, __ATOMIC_RELEASE); | |
761 | return !0; | |
762 | } | |
763 | __atomic_or_fetch(&g_tcache_mode, TCACHE_STALE, __ATOMIC_RELAXED); | |
764 | } | |
765 | __atomic_sub_fetch(&g_tcache_mode, 1, __ATOMIC_RELEASE); | |
766 | } | |
767 | return 0; | |
768 | } | |
769 | ||
770 | static INLINE void tcache_set(const struct timeval *const tv, struct tm *const tm) | |
771 | { | |
772 | unsigned stale = TCACHE_STALE; | |
773 | if (__atomic_compare_exchange_n(&g_tcache_mode, &stale, TCACHE_FLUID, | |
774 | 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) | |
775 | { | |
776 | g_tcache_tv = *tv; | |
777 | g_tcache_tm = *tm; | |
778 | __atomic_and_fetch(&g_tcache_mode, ~TCACHE_FLUID, __ATOMIC_RELEASE); | |
779 | } | |
780 | } | |
781 | #endif | |
782 | ||
783 | static void time_callback(struct tm *const tm, unsigned *const msec) | |
784 | { | |
785 | #if !_BT_LOG_MESSAGE_FORMAT_DATETIME_USED | |
786 | VAR_UNUSED(tm); | |
787 | VAR_UNUSED(msec); | |
788 | #else | |
789 | #if defined(_WIN32) || defined(_WIN64) | |
790 | SYSTEMTIME st; | |
791 | GetLocalTime(&st); | |
792 | tm->tm_year = st.wYear; | |
793 | tm->tm_mon = st.wMonth; | |
794 | tm->tm_mday = st.wDay; | |
795 | tm->tm_wday = st.wDayOfWeek; | |
796 | tm->tm_hour = st.wHour; | |
797 | tm->tm_min = st.wMinute; | |
798 | tm->tm_sec = st.wSecond; | |
799 | *msec = st.wMilliseconds; | |
800 | #else | |
801 | struct timeval tv; | |
802 | gettimeofday(&tv, 0); | |
803 | #ifndef TCACHE | |
804 | localtime_r(&tv.tv_sec, tm); | |
805 | #else | |
806 | if (!tcache_get(&tv, tm)) | |
807 | { | |
808 | localtime_r(&tv.tv_sec, tm); | |
809 | tcache_set(&tv, tm); | |
810 | } | |
811 | #endif | |
812 | *msec = (unsigned)tv.tv_usec / 1000; | |
813 | #endif | |
814 | #endif | |
815 | } | |
816 | ||
817 | static void pid_callback(int *const pid, int *const tid) | |
818 | { | |
819 | #if !_BT_LOG_MESSAGE_FORMAT_CONTAINS(PID, BT_LOG_MESSAGE_CTX_FORMAT) | |
820 | VAR_UNUSED(pid); | |
821 | #else | |
822 | #if defined(_WIN32) || defined(_WIN64) | |
823 | *pid = GetCurrentProcessId(); | |
824 | #else | |
825 | *pid = getpid(); | |
826 | #endif | |
827 | #endif | |
828 | ||
829 | #if !_BT_LOG_MESSAGE_FORMAT_CONTAINS(TID, BT_LOG_MESSAGE_CTX_FORMAT) | |
830 | VAR_UNUSED(tid); | |
831 | #else | |
832 | #if defined(_WIN32) || defined(_WIN64) | |
833 | *tid = GetCurrentThreadId(); | |
f61ad754 MJ |
834 | #elif defined(__CYGWIN__) |
835 | pthread_t thr = pthread_self(); | |
836 | *tid = (int)pthread_getsequence_np(&thr); | |
c0c0c8b4 MJ |
837 | #elif defined(__sun__) |
838 | *tid = (int)pthread_self(); | |
beb0fb75 PP |
839 | #elif defined(__ANDROID__) |
840 | *tid = gettid(); | |
841 | #elif defined(__linux__) | |
842 | *tid = syscall(SYS_gettid); | |
843 | #elif defined(__MACH__) | |
844 | *tid = (int)pthread_mach_thread_np(pthread_self()); | |
845 | #else | |
846 | #define Platform not supported | |
847 | #endif | |
848 | #endif | |
849 | } | |
850 | ||
851 | static void buffer_callback(bt_log_message *msg, char *buf) | |
852 | { | |
853 | msg->e = (msg->p = msg->buf = buf) + g_buf_sz; | |
854 | } | |
855 | ||
856 | #if _BT_LOG_MESSAGE_FORMAT_CONTAINS(FUNCTION, BT_LOG_MESSAGE_SRC_FORMAT) | |
857 | static const char *funcname(const char *func) | |
858 | { | |
859 | return func? func: ""; | |
860 | } | |
861 | #endif | |
862 | ||
863 | #if _BT_LOG_MESSAGE_FORMAT_CONTAINS(FILENAME, BT_LOG_MESSAGE_SRC_FORMAT) | |
864 | static const char *filename(const char *file) | |
865 | { | |
866 | const char *f = file; | |
867 | for (const char *p = file; 0 != *p; ++p) | |
868 | { | |
869 | if ('/' == *p || '\\' == *p) | |
870 | { | |
871 | f = p + 1; | |
872 | } | |
873 | } | |
874 | return f; | |
875 | } | |
876 | #endif | |
877 | ||
878 | static INLINE size_t nprintf_size(bt_log_message *const msg) | |
879 | { | |
880 | // *nprintf() always puts 0 in the end when input buffer is not empty. This | |
881 | // 0 is not desired because its presence sets (ctx->p) to (ctx->e - 1) which | |
882 | // leaves space for one more character. Some put_xxx() functions don't use | |
883 | // *nprintf() and could use that last character. In that case log line will | |
884 | // have multiple (two) half-written parts which is confusing. To workaround | |
885 | // that we allow *nprintf() to write its 0 in the eol area (which is always | |
886 | // not empty). | |
887 | return (size_t)(msg->e - msg->p + 1); | |
888 | } | |
889 | ||
890 | static INLINE void put_nprintf(bt_log_message *const msg, const int n) | |
891 | { | |
892 | if (0 < n) | |
893 | { | |
894 | msg->p = n < msg->e - msg->p? msg->p + n: msg->e; | |
895 | } | |
896 | } | |
897 | ||
898 | static INLINE char *put_padding_r(const unsigned w, const char wc, | |
899 | char *p, char *e) | |
900 | { | |
901 | for (char *const b = e - w; b < p; *--p = wc) {} | |
902 | return p; | |
903 | } | |
904 | ||
905 | static char *put_integer_r(unsigned v, const int sign, | |
906 | const unsigned w, const char wc, char *const e) | |
907 | { | |
908 | static const char _signs[] = {'-', '0', '+'}; | |
ee07b1e9 | 909 | const char *const signs = _signs + 1; |
beb0fb75 PP |
910 | char *p = e; |
911 | do { *--p = '0' + v % 10; } while (0 != (v /= 10)); | |
912 | if (0 == sign) return put_padding_r(w, wc, p, e); | |
913 | if ('0' != wc) | |
914 | { | |
915 | *--p = signs[sign]; | |
916 | return put_padding_r(w, wc, p, e); | |
917 | } | |
918 | p = put_padding_r(w, wc, p, e + 1); | |
919 | *--p = signs[sign]; | |
920 | return p; | |
921 | } | |
922 | ||
923 | static INLINE char *put_uint_r(const unsigned v, const unsigned w, const char wc, | |
924 | char *const e) | |
925 | { | |
926 | return put_integer_r(v, 0, w, wc, e); | |
927 | } | |
928 | ||
929 | static INLINE char *put_int_r(const int v, const unsigned w, const char wc, | |
930 | char *const e) | |
931 | { | |
932 | return 0 <= v? put_integer_r((unsigned)v, 0, w, wc, e) | |
933 | : put_integer_r((unsigned)-v, -1, w, wc, e); | |
934 | } | |
935 | ||
936 | static INLINE char *put_stringn(const char *const s_p, const char *const s_e, | |
937 | char *const p, char *const e) | |
938 | { | |
939 | const ptrdiff_t m = e - p; | |
940 | ptrdiff_t n = s_e - s_p; | |
941 | if (n > m) | |
942 | { | |
943 | n = m; | |
944 | } | |
945 | memcpy(p, s_p, n); | |
946 | return p + n; | |
947 | } | |
948 | ||
949 | static INLINE char *put_string(const char *s, char *p, char *const e) | |
950 | { | |
951 | const ptrdiff_t n = e - p; | |
952 | char *const c = (char *)memccpy(p, s, '\0', n); | |
953 | return 0 != c? c - 1: e; | |
954 | } | |
955 | ||
956 | static INLINE char *put_uint(unsigned v, const unsigned w, const char wc, | |
957 | char *const p, char *const e) | |
958 | { | |
959 | char buf[16]; | |
960 | char *const se = buf + _countof(buf); | |
961 | char *sp = put_uint_r(v, w, wc, se); | |
962 | return put_stringn(sp, se, p, e); | |
963 | } | |
964 | ||
965 | #define PUT_CSTR_R(p, STR) \ | |
966 | do { \ | |
967 | for (unsigned i = sizeof(STR) - 1; 0 < i--;) { \ | |
968 | *--(p) = (STR)[i]; \ | |
969 | } \ | |
970 | } _BT_LOG_ONCE | |
971 | ||
972 | #define PUT_CSTR_CHECKED(p, e, STR) \ | |
973 | do { \ | |
974 | for (unsigned i = 0; (e) > (p) && (sizeof(STR) - 1) > i; ++i) { \ | |
975 | *(p)++ = (STR)[i]; \ | |
976 | } \ | |
977 | } _BT_LOG_ONCE | |
978 | ||
979 | /* F_INIT field support. | |
980 | */ | |
981 | #define _BT_LOG_MESSAGE_FORMAT_INIT__ | |
982 | #define _BT_LOG_MESSAGE_FORMAT_INIT__YEAR | |
983 | #define _BT_LOG_MESSAGE_FORMAT_INIT__MONTH | |
984 | #define _BT_LOG_MESSAGE_FORMAT_INIT__DAY | |
985 | #define _BT_LOG_MESSAGE_FORMAT_INIT__HOUR | |
986 | #define _BT_LOG_MESSAGE_FORMAT_INIT__MINUTE | |
987 | #define _BT_LOG_MESSAGE_FORMAT_INIT__SECOND | |
988 | #define _BT_LOG_MESSAGE_FORMAT_INIT__MILLISECOND | |
989 | #define _BT_LOG_MESSAGE_FORMAT_INIT__PID | |
990 | #define _BT_LOG_MESSAGE_FORMAT_INIT__TID | |
991 | #define _BT_LOG_MESSAGE_FORMAT_INIT__LEVEL | |
992 | #define _BT_LOG_MESSAGE_FORMAT_INIT__TAG(ps, ts) | |
993 | #define _BT_LOG_MESSAGE_FORMAT_INIT__FUNCTION | |
994 | #define _BT_LOG_MESSAGE_FORMAT_INIT__FILENAME | |
995 | #define _BT_LOG_MESSAGE_FORMAT_INIT__FILELINE | |
996 | #define _BT_LOG_MESSAGE_FORMAT_INIT__S(s) | |
997 | #define _BT_LOG_MESSAGE_FORMAT_INIT__F_INIT(expr) _PP_UNTUPLE(expr); | |
998 | #define _BT_LOG_MESSAGE_FORMAT_INIT__F_UINT(w, v) | |
999 | #define _BT_LOG_MESSAGE_FORMAT_INIT(field) \ | |
1000 | _PP_CONCAT_3(_BT_LOG_MESSAGE_FORMAT_INIT_, _, field) | |
1001 | ||
1002 | /* Implements generation of printf-like format string for log message | |
1003 | * format specification. | |
1004 | */ | |
1005 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__ "" | |
1006 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__YEAR "%04u" | |
1007 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__MONTH "%02u" | |
1008 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__DAY "%02u" | |
1009 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__HOUR "%02u" | |
1010 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__MINUTE "%02u" | |
1011 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__SECOND "%02u" | |
1012 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__MILLISECOND "%03u" | |
1013 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__PID "%5i" | |
1014 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__TID "%5i" | |
1015 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__LEVEL "%c" | |
1016 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__TAG UNDEFINED | |
1017 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__FUNCTION "%s" | |
1018 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__FILENAME "%s" | |
1019 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__FILELINE "%u" | |
1020 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__S(s) s | |
1021 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__F_INIT(expr) "" | |
1022 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT__F_UINT(w, v) "%" #w "u" | |
1023 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_FMT(field) \ | |
1024 | _PP_CONCAT_3(_BT_LOG_MESSAGE_FORMAT_PRINTF_FMT_, _, field) | |
1025 | ||
1026 | /* Implements generation of printf-like format parameters for log message | |
1027 | * format specification. | |
1028 | */ | |
1029 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__ | |
1030 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__YEAR ,(unsigned)(tm.tm_year + 1900) | |
1031 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__MONTH ,(unsigned)(tm.tm_mon + 1) | |
1032 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__DAY ,(unsigned)tm.tm_mday | |
1033 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__HOUR ,(unsigned)tm.tm_hour | |
1034 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__MINUTE ,(unsigned)tm.tm_min | |
1035 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__SECOND ,(unsigned)tm.tm_sec | |
1036 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__MILLISECOND ,(unsigned)msec | |
1037 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__PID ,pid | |
1038 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__TID ,tid | |
1039 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__LEVEL ,(char)lvl_char(msg->lvl) | |
1040 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__TAG UNDEFINED | |
1041 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__FUNCTION ,funcname(src->func) | |
1042 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__FILENAME ,filename(src->file) | |
1043 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__FILELINE ,src->line | |
1044 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__S(s) | |
1045 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__F_INIT(expr) | |
1046 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL__F_UINT(w, v) ,v | |
1047 | #define _BT_LOG_MESSAGE_FORMAT_PRINTF_VAL(field) \ | |
1048 | _PP_CONCAT_3(_BT_LOG_MESSAGE_FORMAT_PRINTF_VAL_, _, field) | |
1049 | ||
1050 | /* Implements generation of put_xxx_t statements for log message specification. | |
1051 | */ | |
1052 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__ | |
1053 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__YEAR p = put_uint_r(tm.tm_year + 1900, 4, '0', p); | |
1054 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__MONTH p = put_uint_r((unsigned)tm.tm_mon + 1, 2, '0', p); | |
1055 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__DAY p = put_uint_r((unsigned)tm.tm_mday, 2, '0', p); | |
1056 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__HOUR p = put_uint_r((unsigned)tm.tm_hour, 2, '0', p); | |
1057 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__MINUTE p = put_uint_r((unsigned)tm.tm_min, 2, '0', p); | |
1058 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__SECOND p = put_uint_r((unsigned)tm.tm_sec, 2, '0', p); | |
1059 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__MILLISECOND p = put_uint_r(msec, 3, '0', p); | |
1060 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__PID p = put_int_r(pid, 5, ' ', p); | |
1061 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__TID p = put_int_r(tid, 5, ' ', p); | |
1062 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__LEVEL *--p = lvl_char(msg->lvl); | |
1063 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__TAG UNDEFINED | |
1064 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__FUNCTION UNDEFINED | |
1065 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__FILENAME UNDEFINED | |
1066 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__FILELINE UNDEFINED | |
1067 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__S(s) PUT_CSTR_R(p, s); | |
1068 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__F_INIT(expr) | |
1069 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R__F_UINT(w, v) p = put_uint_r(v, w, ' ', p); | |
1070 | #define _BT_LOG_MESSAGE_FORMAT_PUT_R(field) \ | |
1071 | _PP_CONCAT_3(_BT_LOG_MESSAGE_FORMAT_PUT_R_, _, field) | |
1072 | ||
1073 | static void put_ctx(bt_log_message *const msg) | |
1074 | { | |
1075 | _PP_MAP(_BT_LOG_MESSAGE_FORMAT_INIT, BT_LOG_MESSAGE_CTX_FORMAT) | |
1076 | #if !_BT_LOG_MESSAGE_FORMAT_FIELDS(BT_LOG_MESSAGE_CTX_FORMAT) | |
1077 | VAR_UNUSED(msg); | |
1078 | #else | |
1079 | #if _BT_LOG_MESSAGE_FORMAT_DATETIME_USED | |
1080 | struct tm tm; | |
1081 | unsigned msec; | |
1082 | g_time_cb(&tm, &msec); | |
1083 | #endif | |
1084 | #if _BT_LOG_MESSAGE_FORMAT_CONTAINS(PID, BT_LOG_MESSAGE_CTX_FORMAT) || \ | |
1085 | _BT_LOG_MESSAGE_FORMAT_CONTAINS(TID, BT_LOG_MESSAGE_CTX_FORMAT) | |
1086 | int pid, tid; | |
1087 | g_pid_cb(&pid, &tid); | |
1088 | #endif | |
1089 | ||
1090 | #if BT_LOG_OPTIMIZE_SIZE | |
1091 | int n; | |
1092 | n = snprintf(msg->p, nprintf_size(msg), | |
1093 | _PP_MAP(_BT_LOG_MESSAGE_FORMAT_PRINTF_FMT, BT_LOG_MESSAGE_CTX_FORMAT) | |
1094 | _PP_MAP(_BT_LOG_MESSAGE_FORMAT_PRINTF_VAL, BT_LOG_MESSAGE_CTX_FORMAT)); | |
1095 | put_nprintf(msg, n); | |
1096 | #else | |
1097 | char buf[64]; | |
1098 | char *const e = buf + sizeof(buf); | |
1099 | char *p = e; | |
1100 | _PP_RMAP(_BT_LOG_MESSAGE_FORMAT_PUT_R, BT_LOG_MESSAGE_CTX_FORMAT) | |
1101 | msg->p = put_stringn(p, e, msg->p, msg->e); | |
1102 | #endif | |
1103 | #endif | |
1104 | } | |
1105 | ||
1106 | #define PUT_TAG(msg, tag, prefix_delim, tag_delim) \ | |
1107 | do { \ | |
1108 | const char *ch; \ | |
1109 | msg->tag_b = msg->p; \ | |
1110 | if (0 != (ch = _bt_log_tag_prefix)) { \ | |
1111 | for (;msg->e != msg->p && 0 != (*msg->p = *ch); ++msg->p, ++ch) {} \ | |
1112 | } \ | |
1113 | if (0 != (ch = tag) && 0 != tag[0]) { \ | |
1114 | if (msg->tag_b != msg->p) { \ | |
1115 | PUT_CSTR_CHECKED(msg->p, msg->e, prefix_delim); \ | |
1116 | } \ | |
1117 | for (;msg->e != msg->p && 0 != (*msg->p = *ch); ++msg->p, ++ch) {} \ | |
1118 | } \ | |
1119 | msg->tag_e = msg->p; \ | |
1120 | if (msg->tag_b != msg->p) { \ | |
1121 | PUT_CSTR_CHECKED(msg->p, msg->e, tag_delim); \ | |
1122 | } \ | |
1123 | } _BT_LOG_ONCE | |
1124 | ||
1125 | /* Implements simple put statements for log message specification. | |
1126 | */ | |
1127 | #define _BT_LOG_MESSAGE_FORMAT_PUT__ | |
1128 | #define _BT_LOG_MESSAGE_FORMAT_PUT__YEAR UNDEFINED | |
1129 | #define _BT_LOG_MESSAGE_FORMAT_PUT__MONTH UNDEFINED | |
1130 | #define _BT_LOG_MESSAGE_FORMAT_PUT__DAY UNDEFINED | |
1131 | #define _BT_LOG_MESSAGE_FORMAT_PUT__HOUR UNDEFINED | |
1132 | #define _BT_LOG_MESSAGE_FORMAT_PUT__MINUTE UNDEFINED | |
1133 | #define _BT_LOG_MESSAGE_FORMAT_PUT__SECOND UNDEFINED | |
1134 | #define _BT_LOG_MESSAGE_FORMAT_PUT__MILLISECOND UNDEFINED | |
1135 | #define _BT_LOG_MESSAGE_FORMAT_PUT__PID UNDEFINED | |
1136 | #define _BT_LOG_MESSAGE_FORMAT_PUT__TID UNDEFINED | |
1137 | #define _BT_LOG_MESSAGE_FORMAT_PUT__LEVEL UNDEFINED | |
1138 | #define _BT_LOG_MESSAGE_FORMAT_PUT__TAG(pd, td) PUT_TAG(msg, tag, pd, td); | |
1139 | #define _BT_LOG_MESSAGE_FORMAT_PUT__FUNCTION msg->p = put_string(funcname(src->func), msg->p, msg->e); | |
1140 | #define _BT_LOG_MESSAGE_FORMAT_PUT__FILENAME msg->p = put_string(filename(src->file), msg->p, msg->e); | |
1141 | #define _BT_LOG_MESSAGE_FORMAT_PUT__FILELINE msg->p = put_uint(src->line, 0, '\0', msg->p, msg->e); | |
1142 | #define _BT_LOG_MESSAGE_FORMAT_PUT__S(s) PUT_CSTR_CHECKED(msg->p, msg->e, s); | |
1143 | #define _BT_LOG_MESSAGE_FORMAT_PUT__F_INIT(expr) | |
1144 | #define _BT_LOG_MESSAGE_FORMAT_PUT__F_UINT(w, v) msg->p = put_uint(v, w, ' ', msg->p, msg->e); | |
1145 | #define _BT_LOG_MESSAGE_FORMAT_PUT(field) \ | |
1146 | _PP_CONCAT_3(_BT_LOG_MESSAGE_FORMAT_PUT_, _, field) | |
1147 | ||
1148 | static void put_tag(bt_log_message *const msg, const char *const tag) | |
1149 | { | |
1150 | _PP_MAP(_BT_LOG_MESSAGE_FORMAT_INIT, BT_LOG_MESSAGE_TAG_FORMAT) | |
1151 | #if !_BT_LOG_MESSAGE_FORMAT_CONTAINS(TAG, BT_LOG_MESSAGE_TAG_FORMAT) | |
1152 | VAR_UNUSED(tag); | |
1153 | #endif | |
1154 | #if !_BT_LOG_MESSAGE_FORMAT_FIELDS(BT_LOG_MESSAGE_TAG_FORMAT) | |
1155 | VAR_UNUSED(msg); | |
1156 | #else | |
1157 | _PP_MAP(_BT_LOG_MESSAGE_FORMAT_PUT, BT_LOG_MESSAGE_TAG_FORMAT) | |
1158 | #endif | |
1159 | } | |
1160 | ||
1161 | static void put_src(bt_log_message *const msg, const src_location *const src) | |
1162 | { | |
1163 | _PP_MAP(_BT_LOG_MESSAGE_FORMAT_INIT, BT_LOG_MESSAGE_SRC_FORMAT) | |
1164 | #if !_BT_LOG_MESSAGE_FORMAT_CONTAINS(FUNCTION, BT_LOG_MESSAGE_SRC_FORMAT) && \ | |
1165 | !_BT_LOG_MESSAGE_FORMAT_CONTAINS(FILENAME, BT_LOG_MESSAGE_SRC_FORMAT) && \ | |
1166 | !_BT_LOG_MESSAGE_FORMAT_CONTAINS(FILELINE, BT_LOG_MESSAGE_SRC_FORMAT) | |
1167 | VAR_UNUSED(src); | |
1168 | #endif | |
1169 | #if !_BT_LOG_MESSAGE_FORMAT_FIELDS(BT_LOG_MESSAGE_SRC_FORMAT) | |
1170 | VAR_UNUSED(msg); | |
1171 | #else | |
1172 | #if BT_LOG_OPTIMIZE_SIZE | |
1173 | int n; | |
1174 | n = snprintf(msg->p, nprintf_size(msg), | |
1175 | _PP_MAP(_BT_LOG_MESSAGE_FORMAT_PRINTF_FMT, BT_LOG_MESSAGE_SRC_FORMAT) | |
1176 | _PP_MAP(_BT_LOG_MESSAGE_FORMAT_PRINTF_VAL, BT_LOG_MESSAGE_SRC_FORMAT)); | |
1177 | put_nprintf(msg, n); | |
1178 | #else | |
1179 | _PP_MAP(_BT_LOG_MESSAGE_FORMAT_PUT, BT_LOG_MESSAGE_SRC_FORMAT) | |
1180 | #endif | |
1181 | #endif | |
1182 | } | |
1183 | ||
1184 | static void put_msg(bt_log_message *const msg, | |
1185 | const char *const fmt, va_list va) | |
1186 | { | |
1187 | int n; | |
1188 | msg->msg_b = msg->p; | |
1189 | n = vsnprintf(msg->p, nprintf_size(msg), fmt, va); | |
1190 | put_nprintf(msg, n); | |
1191 | } | |
1192 | ||
1193 | static void output_mem(const bt_log_spec *log, bt_log_message *const msg, | |
1194 | const mem_block *const mem) | |
1195 | { | |
1196 | if (0 == mem->d || 0 == mem->d_sz) | |
1197 | { | |
1198 | return; | |
1199 | } | |
1200 | const unsigned char *mem_p = (const unsigned char *)mem->d; | |
1201 | const unsigned char *const mem_e = mem_p + mem->d_sz; | |
1202 | const unsigned char *mem_cut; | |
1203 | const ptrdiff_t mem_width = (ptrdiff_t)log->format->mem_width; | |
1204 | char *const hex_b = msg->msg_b; | |
1205 | char *const ascii_b = hex_b + 2 * mem_width + 2; | |
1206 | char *const ascii_e = ascii_b + mem_width; | |
1207 | if (msg->e < ascii_e) | |
1208 | { | |
1209 | return; | |
1210 | } | |
1211 | while (mem_p != mem_e) | |
1212 | { | |
1213 | char *hex = hex_b; | |
1214 | char *ascii = ascii_b; | |
1215 | for (mem_cut = mem_width < mem_e - mem_p? mem_p + mem_width: mem_e; | |
1216 | mem_cut != mem_p; ++mem_p) | |
1217 | { | |
1218 | const unsigned char ch = *mem_p; | |
1219 | *hex++ = c_hex[(0xf0 & ch) >> 4]; | |
1220 | *hex++ = c_hex[(0x0f & ch)]; | |
1221 | *ascii++ = isprint(ch)? (char)ch: '?'; | |
1222 | } | |
1223 | while (hex != ascii_b) | |
1224 | { | |
1225 | *hex++ = ' '; | |
1226 | } | |
1227 | msg->p = ascii; | |
1228 | log->output->callback(msg, log->output->arg); | |
1229 | } | |
1230 | } | |
1231 | ||
1232 | BT_HIDDEN | |
1233 | void bt_log_set_tag_prefix(const char *const prefix) | |
1234 | { | |
1235 | _bt_log_tag_prefix = prefix; | |
1236 | } | |
1237 | ||
1238 | BT_HIDDEN | |
1239 | void bt_log_set_mem_width(const unsigned w) | |
1240 | { | |
1241 | _bt_log_global_format.mem_width = w; | |
1242 | } | |
1243 | ||
1244 | BT_HIDDEN | |
1245 | void bt_log_set_output_level(const int lvl) | |
1246 | { | |
1247 | _bt_log_global_output_lvl = lvl; | |
1248 | } | |
1249 | ||
1250 | BT_HIDDEN | |
1251 | void bt_log_set_output_v(const unsigned mask, void *const arg, | |
1252 | const bt_log_output_cb callback) | |
1253 | { | |
1254 | _bt_log_global_output.mask = mask; | |
1255 | _bt_log_global_output.arg = arg; | |
1256 | _bt_log_global_output.callback = callback; | |
1257 | } | |
1258 | ||
1259 | static void _bt_log_write_imp( | |
1260 | const bt_log_spec *log, | |
1261 | const src_location *const src, const mem_block *const mem, | |
1262 | const int lvl, const char *const tag, const char *const fmt, va_list va) | |
1263 | { | |
1264 | bt_log_message msg; | |
deaa6f85 | 1265 | char *buf = logging_buf; |
beb0fb75 PP |
1266 | const unsigned mask = log->output->mask; |
1267 | msg.lvl = lvl; | |
1268 | msg.tag = tag; | |
1269 | g_buffer_cb(&msg, buf); | |
557d8f39 PP |
1270 | const char *rst_color_p = bt_common_color_reset(); |
1271 | const char *rst_color_e = rst_color_p + strlen(rst_color_p); | |
1272 | const char *color_p = ""; | |
1273 | const char *color_e = color_p; | |
1274 | ||
1275 | switch (lvl) { | |
1276 | case BT_LOG_INFO: | |
1277 | color_p = bt_common_color_fg_blue(); | |
1278 | color_e = color_p + strlen(color_p); | |
1279 | break; | |
1280 | case BT_LOG_WARN: | |
1281 | color_p = bt_common_color_fg_yellow(); | |
1282 | color_e = color_p + strlen(color_p); | |
1283 | break; | |
1284 | case BT_LOG_ERROR: | |
1285 | case BT_LOG_FATAL: | |
1286 | color_p = bt_common_color_fg_red(); | |
1287 | color_e = color_p + strlen(color_p); | |
1288 | break; | |
1289 | default: | |
1290 | break; | |
1291 | } | |
1292 | ||
1293 | msg.p = put_stringn(color_p, color_e, msg.p, msg.e); | |
1294 | ||
beb0fb75 PP |
1295 | if (BT_LOG_PUT_CTX & mask) |
1296 | { | |
1297 | put_ctx(&msg); | |
1298 | } | |
1299 | if (BT_LOG_PUT_TAG & mask) | |
1300 | { | |
1301 | put_tag(&msg, tag); | |
1302 | } | |
1303 | if (0 != src && BT_LOG_PUT_SRC & mask) | |
1304 | { | |
1305 | put_src(&msg, src); | |
1306 | } | |
1307 | if (BT_LOG_PUT_MSG & mask) | |
1308 | { | |
1309 | put_msg(&msg, fmt, va); | |
1310 | } | |
557d8f39 | 1311 | msg.p = put_stringn(rst_color_p, rst_color_e, msg.p, msg.e); |
beb0fb75 PP |
1312 | log->output->callback(&msg, log->output->arg); |
1313 | if (0 != mem && BT_LOG_PUT_MSG & mask) | |
1314 | { | |
1315 | output_mem(log, &msg, mem); | |
1316 | } | |
1317 | } | |
1318 | ||
1319 | BT_HIDDEN | |
1320 | void _bt_log_write_d( | |
1321 | const char *const func, const char *const file, const unsigned line, | |
1322 | const int lvl, const char *const tag, | |
1323 | const char *const fmt, ...) | |
1324 | { | |
1325 | const src_location src = {func, file, line}; | |
1326 | va_list va; | |
1327 | va_start(va, fmt); | |
1328 | _bt_log_write_imp(&global_spec, &src, 0, lvl, tag, fmt, va); | |
1329 | va_end(va); | |
1330 | } | |
1331 | ||
1332 | BT_HIDDEN | |
1333 | void _bt_log_write_aux_d( | |
1334 | const char *const func, const char *const file, const unsigned line, | |
1335 | const bt_log_spec *const log, const int lvl, const char *const tag, | |
1336 | const char *const fmt, ...) | |
1337 | { | |
1338 | const src_location src = {func, file, line}; | |
1339 | va_list va; | |
1340 | va_start(va, fmt); | |
1341 | _bt_log_write_imp(log, &src, 0, lvl, tag, fmt, va); | |
1342 | va_end(va); | |
1343 | } | |
1344 | ||
1345 | BT_HIDDEN | |
1346 | void _bt_log_write(const int lvl, const char *const tag, | |
1347 | const char *const fmt, ...) | |
1348 | { | |
1349 | va_list va; | |
1350 | va_start(va, fmt); | |
1351 | _bt_log_write_imp(&global_spec, 0, 0, lvl, tag, fmt, va); | |
1352 | va_end(va); | |
1353 | } | |
1354 | ||
1355 | BT_HIDDEN | |
1356 | void _bt_log_write_aux( | |
1357 | const bt_log_spec *const log, const int lvl, const char *const tag, | |
1358 | const char *const fmt, ...) | |
1359 | { | |
1360 | va_list va; | |
1361 | va_start(va, fmt); | |
1362 | _bt_log_write_imp(log, 0, 0, lvl, tag, fmt, va); | |
1363 | va_end(va); | |
1364 | } | |
1365 | ||
1366 | BT_HIDDEN | |
1367 | void _bt_log_write_mem_d( | |
1368 | const char *const func, const char *const file, const unsigned line, | |
1369 | const int lvl, const char *const tag, | |
1370 | const void *const d, const unsigned d_sz, | |
1371 | const char *const fmt, ...) | |
1372 | { | |
1373 | const src_location src = {func, file, line}; | |
1374 | const mem_block mem = {d, d_sz}; | |
1375 | va_list va; | |
1376 | va_start(va, fmt); | |
1377 | _bt_log_write_imp(&global_spec, &src, &mem, lvl, tag, fmt, va); | |
1378 | va_end(va); | |
1379 | } | |
1380 | ||
1381 | BT_HIDDEN | |
1382 | void _bt_log_write_mem_aux_d( | |
1383 | const char *const func, const char *const file, const unsigned line, | |
1384 | const bt_log_spec *const log, const int lvl, const char *const tag, | |
1385 | const void *const d, const unsigned d_sz, | |
1386 | const char *const fmt, ...) | |
1387 | { | |
1388 | const src_location src = {func, file, line}; | |
1389 | const mem_block mem = {d, d_sz}; | |
1390 | va_list va; | |
1391 | va_start(va, fmt); | |
1392 | _bt_log_write_imp(log, &src, &mem, lvl, tag, fmt, va); | |
1393 | va_end(va); | |
1394 | } | |
1395 | ||
1396 | BT_HIDDEN | |
1397 | void _bt_log_write_mem(const int lvl, const char *const tag, | |
1398 | const void *const d, const unsigned d_sz, | |
1399 | const char *const fmt, ...) | |
1400 | { | |
1401 | const mem_block mem = {d, d_sz}; | |
1402 | va_list va; | |
1403 | va_start(va, fmt); | |
1404 | _bt_log_write_imp(&global_spec, 0, &mem, lvl, tag, fmt, va); | |
1405 | va_end(va); | |
1406 | } | |
1407 | ||
1408 | BT_HIDDEN | |
1409 | void _bt_log_write_mem_aux( | |
1410 | const bt_log_spec *const log, const int lvl, const char *const tag, | |
1411 | const void *const d, const unsigned d_sz, | |
1412 | const char *const fmt, ...) | |
1413 | { | |
1414 | const mem_block mem = {d, d_sz}; | |
1415 | va_list va; | |
1416 | va_start(va, fmt); | |
1417 | _bt_log_write_imp(log, 0, &mem, lvl, tag, fmt, va); | |
1418 | va_end(va); | |
1419 | } |