Add `.clang-format` file for C++ source files
[babeltrace.git] / CONTRIBUTING.adoc
CommitLineData
cd4aac1e
PP
1// Render with Asciidoctor
2
28b765ac 3= Babeltrace{nbsp}2 contributor's guide
f9ded0e0 4Jérémie Galarneau, Philippe Proulx
28b765ac
PP
519 November 2020
6:toc: left
7:toclevels: 3
8:bt2: Babeltrace{nbsp}2
beb0fb75 9
f9ded0e0 10This is a partial contributor's guide for the
28b765ac 11https://babeltrace.org[{bt2}] project. If you have any
cd4aac1e
PP
12questions that are not answered by this guide, please post them on
13https://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev[Babeltrace's
14mailing list].
15
28b765ac 16== {bt2} library
f9ded0e0 17
cd4aac1e 18=== Object reference counting and lifetime
f9ded0e0 19
28b765ac
PP
20This section covers the rationale behind the design of {bt2}'s
21object lifetime management. This applies to the {bt2} library, as
cd4aac1e
PP
22well as to the CTF writer library (although the public reference
23counting functions are not named the same way).
f9ded0e0 24
28b765ac
PP
25Starting from Babeltrace{nbsp}2.0, all publicly exposed objects inherit
26a common base: `bt_object`. This base provides a number of facilities to
f9ded0e0
PP
27all objects, chief amongst which are lifetime management functions.
28
cd4aac1e
PP
29The lifetime of some public objects is managed by reference counting. In
30this case, the API offers the `+bt_*_get_ref()+` and `+bt_*_put_ref()+`
31functions which respectively increment and decrement an object's
32reference count.
f9ded0e0 33
28b765ac 34As far as lifetime management in concerned, {bt2} makes a clear
f9ded0e0
PP
35distinction between regular objects, which have a single parent, and
36root objects, which don't.
37
cd4aac1e 38==== The problem
f9ded0e0
PP
39
40Let us consider a problematic case to illustrate the need for this
41distinction.
42
28b765ac 43A user of the {bt2} library creates a trace class, which _has_ a
cd4aac1e
PP
44stream class (the class of a stream) and that stream class, in turn,
45_has_ an event class (the class of an event).
f9ded0e0
PP
46
47Nothing prevents this user from releasing his reference on any one of
48these objects in any order. However, all objects in the
49__trace--stream class--event class__ hierarchy can be retrieved
50from any other.
51
52For instance, the user could discard his reference on both the event
cd4aac1e
PP
53class and the stream class, only keeping a reference on the trace class.
54From this trace class reference, stream classes can be enumerated,
55providing the user with a new reference to the stream class he discarded
56earlier. Event classes can also be enumerated from stream classes,
57providing the user with references to the individual event classes.
f9ded0e0
PP
58
59Conversely, the user could also hold a reference to an event class and
cd4aac1e 60retrieve its parent stream class. The trace class, in turn, can then be
f9ded0e0
PP
61retrieved from the stream class.
62
63This example illustrates what could be interpreted as a circular
64reference dependency existing between these objects. Of course, if the
65objects in such a scenario were to hold references to each other (in
66both directions), we would be in presence of a circular ownership
67resulting in a leak of both objects as their reference counts would
68never reach zero.
69
70Nonetheless, the API must offer the guarantee that holding a node to any
71node of the graph keeps all other reachable nodes alive.
72
cd4aac1e 73==== The solution
f9ded0e0 74
28b765ac 75The scheme employed in {bt2} to break this cycle consists in the
cd4aac1e 76"children" holding _reverse component references_ to their parents. That
56e18c4c
PP
77is, in the context of the trace IR, that event classes hold a reference
78to their parent stream class and stream classes hold a reference to
cd4aac1e 79their parent trace class.
f9ded0e0 80
cd4aac1e 81On the other hand, parents hold _claiming aggregation references_ to
f9ded0e0
PP
82their children. A claiming aggregation reference means that the object
83being referenced should not be deleted as long as the reference still
84exists. In this respect, it can be said that parents truly hold the
85ownership of their children, since they control their lifetime.
86Conversely, the reference counting mechanism is leveraged by children to
87notify parents that no other child indirectly exposes the parent.
88
89When a parented object's reference count reaches zero, it invokes
cd4aac1e
PP
90`+bt_*_put_ref()+` on its parent and does _not_ free itself. However,
91from that point, the object depends on its parent to signal the moment
92when it can be safely reclaimed.
93
94The invocation of `+bt_*_put_ref()+` by the last children holding a
95reference to its parent might trigger a cascade of `+bt_*_put_ref()+`
96from child to parent. Eventually, a **root** object is reached. At that
97point, if this orphaned object's reference count reaches zero, the
98object invokes the destructor method defined by everyone of its children
99as part of their base `struct bt_object`. The key point here is that the
100cascade of destructor will necessarily originate from the root and
101propagate in preorder to the children. These children will propagate the
102destruction to their own children before reclaiming their own memory.
103This ensures that a node's pointer to its parent is _always_ valid since
104the parent has the responsibility of tearing-down their children before
105cleaning themselves up.
106
107Assuming a reference to an object is _acquired_ by calling
108`+bt_*_get_ref()+` while its reference count is zero, the object
109acquires, in turn, a reference on its parent using `+bt_*_get_ref()+`.
110At that point, the child can be thought of as having converted its weak
111reference to its parent into a regular reference. That is why this
112reference is referred to as a _claiming_ aggregation reference.
113
cd4aac1e 114==== Caveats
f9ded0e0
PP
115
116This scheme imposes a number of strict rules defining the relation
117between objects:
118
119* Objects may only have one parent.
120* Objects, beside the root, are only retrievable from their direct
121 parent or children.
122
cd4aac1e 123==== Example
f9ded0e0 124
cd4aac1e
PP
125The initial situation is rather simple: **User{nbsp}A** is holding a
126reference to a trace class, **TC1**. As per the rules previously
127enounced, stream classes **SC1** and **SC2** don't hold a reference to
128**TC1** since their own reference counts are zero. The same holds true
129for **EC1**, **EC2** and **EC3** with respect to **SC1** and **SC2**.
f9ded0e0
PP
130
131image::doc/contributing-images/bt-ref01.png[]
132
cd4aac1e
PP
133In this second step, we can see that **User{nbsp}A** has acquired a
134reference on **SC2** through the trace class, **TC1**.
f9ded0e0
PP
135
136The stream class's reference count transitions from zero to one,
137triggering the acquisition of a strong reference on **TC1** from
138**SC2**.
139
cd4aac1e
PP
140Hence, at this point, the trace class's ownership is shared by
141**User{nbsp}A** and **SC2**.
f9ded0e0
PP
142
143image::doc/contributing-images/bt-ref02.png[]
144
cd4aac1e
PP
145Next, **User{nbsp}A** acquires a reference on the **EC3** event class
146through its parent stream class, **SC2**. Again, the transition of an
147object's reference count from 0 to 1 triggers the acquisition of a
148reference on its parent.
f9ded0e0 149
cd4aac1e 150Note that SC2's reference count was incremented to 2. The trace class's
f9ded0e0
PP
151reference count remains unchanged.
152
153image::doc/contributing-images/bt-ref03.png[]
154
cd4aac1e
PP
155**User{nbsp}A** decides to drop its reference on **SC2**. **SC2**'s
156reference count returns back to 1, everything else remaining unchanged.
f9ded0e0
PP
157
158image::doc/contributing-images/bt-ref04.png[]
159
cd4aac1e
PP
160**User{nbsp}A** can then decide to drop its reference on the trace
161class. This results in a reversal of the initial situation:
162**User{nbsp}A** now owns an event, **EC3**, which is keeping everything
163else alive and reachable.
f9ded0e0
PP
164
165image::doc/contributing-images/bt-ref05.png[]
166
cd4aac1e 167If another object, **User{nbsp}B**, enters the picture and acquires a
f9ded0e0
PP
168reference on the **SC1** stream class, we see that **SC1**'s reference
169count transitioned from 0 to 1, triggering the acquisition of a
170reference on **TC1**.
171
172image::doc/contributing-images/bt-ref06.png[]
173
cd4aac1e
PP
174**User{nbsp}B** hands off a reference to **EC1**, acquired through
175**SC1**, to another object, **User{nbsp}C**. The acquisition of a
176reference on **EC1**, which transitions from 0 to 1, triggers the
177acquisition of a reference on its parent, **SC1**.
f9ded0e0
PP
178
179image::doc/contributing-images/bt-ref07.png[]
180
cd4aac1e 181At some point, **User{nbsp}A** releases its reference on **EC3**. Since
f9ded0e0
PP
182**EC3**'s reference count transitions to zero, it releases its reference
183on **SC2**. **SC2**'s reference count, in turn, reaches zero and it
184releases its reference to **TC1**.
185
186**TC1**'s reference count is now 1 and no further action is taken.
187
188image::doc/contributing-images/bt-ref08.png[]
189
cd4aac1e
PP
190**User{nbsp}B** releases its reference on **SC1**. **User{nbsp}C**
191becomes the sole owner of the whole hierarchy through his ownership of
192**EC1**.
f9ded0e0
PP
193
194image::doc/contributing-images/bt-ref09.png[]
195
cd4aac1e
PP
196Finally, **User{nbsp}C** releases his ownership of **EC1**, triggering
197the release of the whole hierarchy. Let's walk through the reclamation
198of the whole graph.
f9ded0e0 199
cd4aac1e
PP
200Mirroring what happened when **User{nbsp}A** released its last reference
201on **EC3**, the release of **EC1** by **User{nbsp}C** causes its
202reference count to fall to zero.
f9ded0e0
PP
203
204This transition to zero causes **EC1** to release its reference on
205**SC1**. **SC1**'s reference count reaching zero causes it to release
206its reference on **TC1**.
207
208image::doc/contributing-images/bt-ref10.png[]
209
210Since the reference count of **TC1**, a root object, has reached zero,
211it invokes the destructor method on its children. This method is
212recursive and causes the stream classes to call the destructor method on
213their event classes.
214
215The event classes are reached and, having no children of their own, are
216reclaimed.
217
218image::doc/contributing-images/bt-ref11.png[]
219
220The stream classes having destroyed their children, are then reclaimed
cd4aac1e 221by the trace class.
f9ded0e0
PP
222
223image::doc/contributing-images/bt-ref12.png[]
224
225Finally, the stream classes having been reclaimed, **TC1** is reclaimed.
226
227image::doc/contributing-images/bt-ref13.png[]
228
229
f9ded0e0 230== Logging
beb0fb75
PP
231
232Logging is a great instrument for a developer to be able to collect
233information about a running software.
234
28b765ac 235{bt2} is a complex software with many layers. When a {bt2}
beb0fb75 236graph fails to run, what caused the failure? It could be caused by any
cd4aac1e
PP
237component, any message iterator, and any deeply nested validation of a
238CTF IR object (within the `ctf` plugin), for example. With the
239appropriate logging statements manually placed in the source code, we
240can find the cause of a bug faster.
beb0fb75 241
cd4aac1e
PP
242While <<choose-a-log-level,care must be taken>> when placing _DEBUG_ to
243_FATAL_ logging statements, you should liberally instrument your
28b765ac 244{bt2} module with _TRACE_ logging statements to help future you
cd4aac1e 245and other developers understand what's happening at run time.
beb0fb75 246
f9ded0e0 247=== Logging API
beb0fb75 248
28b765ac 249The {bt2} logging API is internal: it is not exposed to the users
cd4aac1e 250of the library; only to their developers. The only thing that a library
4a41523d 251user can control is the current log level of the library itself with
beb0fb75 252`bt_logging_set_global_level()` and the initial library's log level with
cd4aac1e 253the `LIBBABELTRACE2_INIT_LOG_LEVEL` environment variable.
beb0fb75
PP
254
255This API is based on https://github.com/wonder-mice/zf_log[zf_log], a
256lightweight, yet featureful, MIT-licensed core logging library for C and
cd4aac1e
PP
257pass:[C++]. The zf_log source files were modified to have the `BT_` and
258`bt_` prefixes, and other small changes, like color support and using
259the project's `BT_DEBUG_MODE` definition instead of the standard
260`NDEBUG`.
beb0fb75
PP
261
262The logging functions are implemented in the logging convenience
cd4aac1e 263library (`src/logging` directory).
beb0fb75 264
cd4aac1e 265[[logging-headers]]
f9ded0e0 266==== Headers
beb0fb75
PP
267
268The logging API headers are:
269
3fadfbc0 270`<babeltrace2/logging.h>`::
cd4aac1e 271 Public header which a library user can use to set and get
a12f3d62 272 libbabeltrace2's current log level.
beb0fb75 273
cd4aac1e 274`"logging/log.h"`::
28b765ac 275 Internal, generic logging API which you can use in any {bt2}
cd4aac1e
PP
276 module. This is the translation of `zf_log.h`.
277+
278This header offers the <<gen-logging-statements,generic logging
279statement macros>>.
beb0fb75 280
cd4aac1e
PP
281`"lib/logging.h"`::
282 Specific internal header to use within the library.
283+
284This header defines `BT_LOG_OUTPUT_LEVEL` to a custom, library-wide
285hidden symbol which is the library's current log level before including
286`"logging/log.h"`.
287+
288This header offers the <<lib-logging-statements,library-specific logging
289statement macros>>.
beb0fb75 290
d9c39b0a 291`"logging/comp-logging.h"`::
cd4aac1e
PP
292 Specific internal header to use within a component class.
293+
294This header offers the <<comp-logging-statements,component-specific
295logging statement macros>>.
beb0fb75 296
cd4aac1e 297[[log-levels]]
f9ded0e0 298==== Log levels
beb0fb75 299
cd4aac1e
PP
300The internal logging API offers the following log levels, in ascending
301order of severity:
beb0fb75 302
cd4aac1e
PP
303[options="header,autowidth",cols="4"]
304|===
305|Log level name
306|Log level short name
307|Internal API enumerator
308|Public API enumerator
309
ef267d12
PP
310|_TRACE_
311|`T`
312|`BT_LOG_TRACE`
313|`BT_LOGGING_LEVEL_TRACE`
cd4aac1e
PP
314
315|_DEBUG_
316|`D`
317|`BT_LOG_DEBUG`
318|`BT_LOGGING_LEVEL_DEBUG`
319
320|_INFO_
321|`I`
322|`BT_LOG_INFO`
323|`BT_LOGGING_LEVEL_INFO`
324
770538dd 325|_WARNING_
cd4aac1e 326|`W`
770538dd
PP
327|`BT_LOG_WARNING`
328|`BT_LOGGING_LEVEL_WARNING`
cd4aac1e
PP
329
330|_ERROR_
331|`E`
332|`BT_LOG_ERROR`
333|`BT_LOGGING_LEVEL_ERROR`
334
335|_FATAL_
336|`F`
337|`BT_LOG_FATAL`
338|`BT_LOGGING_LEVEL_FATAL`
339
340|_NONE_
341|`N`
342|`BT_LOG_NONE`
343|`BT_LOGGING_LEVEL_NONE`
344|===
345
346The short name is accepted by the log level environment variables and by
347the CLI's `--log-level` options.
beb0fb75 348
cd4aac1e 349See <<choose-a-log-level,how to decide which one to use>> below.
beb0fb75 350
cd4aac1e 351There are two important log level expressions:
beb0fb75
PP
352
353[[build-time-log-level]]Build-time, minimal log level::
cd4aac1e
PP
354 The minimal log level, or build-time log level, is set at build time
355 and determines the minimal log level of the logging statements which
356 can be executed. This applies to all the modules (CLI, library,
357 plugins, bindings, etc.).
beb0fb75
PP
358+
359All the logging statements with a level below this level are **not built
360at all**. All the logging statements with a level equal to or greater
f9ded0e0
PP
361than this level _can_ be executed, depending on the
362<<run-time-log-level,run-time log level>>.
beb0fb75
PP
363+
364You can set this level at configuration time with the
365`BABELTRACE_MINIMAL_LOG_LEVEL` environment variable, for example:
366+
367--
368----
770538dd 369$ BABELTRACE_MINIMAL_LOG_LEVEL=INFO ./configure
beb0fb75
PP
370----
371--
372+
cd4aac1e
PP
373The default build-time log level is `DEBUG`. For optimal performance,
374set it to `INFO`, which effectively disables all fast path logging in
28b765ac 375all the {bt2} modules. You can't set it to `WARNING`, `ERROR`,
dd22a91f 376`FATAL`, or `NONE` because the impact on performance is minuscule
28b765ac 377starting from the _INFO_ log level anyway and we want any {bt2}
dd22a91f 378build to always be able to print _INFO_-level logs.
beb0fb75
PP
379+
380The library's public API provides `bt_logging_get_minimal_level()` to
381get the configured minimal log level.
382
383[[run-time-log-level]]Run-time, dynamic log level::
cd4aac1e
PP
384 The dynamic log level is set at run time and determines the current,
385 _active_ log level. All the logging statements with a level below
386 this level are not executed, **but they still evaluate the
387 condition**. All the logging statements with a level equal to or
388 greater than this level are executed, provided that their level is
389 also <<build-time-log-level,enabled at build time>>.
beb0fb75 390+
f9ded0e0
PP
391`zf_log` has a concept of a global run-time log level which uses the
392`_bt_log_global_output_lvl` symbol. In practice, we never use this
393symbol, and always make sure that `BT_LOG_OUTPUT_LEVEL` is defined to a
cd4aac1e
PP
394module-wise expression before including `"logging/log.h"`.
395+
396In the library, `"lib/logging.h"` defines its own
397`BT_LOG_OUTPUT_LEVEL` to the library's log level symbol before it
398includes `"logging/log.h"` itself.
beb0fb75 399+
a12f3d62 400In libbabeltrace2, the user can set the current run-time log level with
beb0fb75
PP
401the `bt_logging_set_global_level()` function, for example:
402+
403--
404[source,c]
405----
406bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO);
407----
408--
409+
410The library's initial run-time log level is defined by the
cd4aac1e
PP
411`LIBBABELTRACE2_INIT_LOG_LEVEL` environment variable, or set to _NONE_
412if this environment variable is undefined.
413+
414Other modules have their own way of setting their run-time log level.
415+
416For example, the CLI uses the `BABELTRACE_CLI_LOG_LEVEL` environment
417variable, as well as its global `--log-level` option:
418+
419----
420$ babeltrace2 --log-level=I ...
421----
422+
423The components use their own log level (as returned by
424`bt_component_get_logging_level()`). With the CLI, you can set a
425specific component's log level with its own, position-dependent
426`--log-level` option:
427+
428----
429$ babeltrace2 /path/to/trace -c sink.ctf.fs --log-level=D
430----
431+
432Code which is common to the whole project, for example `src/common`
433and `src/compat`, use function parameters to get its run-time log
434level, for example:
435+
436[source,c]
437----
438BT_HIDDEN
439char *bt_common_get_home_plugin_path(int log_level);
440----
beb0fb75 441+
cd4aac1e
PP
442Typically, when a logging-enabled module calls such a function, it
443passes its own log level expression directly (`BT_LOG_OUTPUT_LEVEL`):
beb0fb75 444+
cd4aac1e
PP
445[source,c]
446----
447path = bt_common_get_home_plugin_path(BT_LOG_OUTPUT_LEVEL);
448----
449+
450Otherwise, just pass `BT_LOG_NONE`:
451+
452----
453path = bt_common_get_home_plugin_path(BT_LOG_NONE);
454----
455
cd4aac1e
PP
456[[gen-logging-statements]]
457==== Generic logging statement macros
458
28b765ac 459The {bt2} logging statement macros work just like `printf()`
cd4aac1e
PP
460(except the `+BT_LOG*_STR()+` ones) and contain their <<log-levels,log
461level>> (short name) in their name.
beb0fb75 462
cd4aac1e
PP
463Each of the following macros evaluate the
464<<build-time-log-level,build-time log level>> definition and
465<<run-time-log-level,run-time log level>> expression (as defined by
466`BT_LOG_OUTPUT_LEVEL`) to log conditionally.
beb0fb75 467
28b765ac
PP
468See <<logging-instrument-c-file-gen>> and
469<<logging-instrument-h-file-gen>> to learn how to be able to use the
470following macros.
beb0fb75 471
ef267d12
PP
472`+BT_LOGT("format string", ...)+`::
473 Generic trace logging statement.
beb0fb75 474
cd4aac1e
PP
475`+BT_LOGD("format string", ...)+`::
476 Generic debug logging statement.
beb0fb75 477
cd4aac1e
PP
478`+BT_LOGI("format string", ...)+`::
479 Generic info logging statement.
beb0fb75 480
cd4aac1e
PP
481`+BT_LOGW("format string", ...)+`::
482 Generic warning logging statement.
beb0fb75 483
cd4aac1e
PP
484`+BT_LOGE("format string", ...)+`::
485 Generic error logging statement.
beb0fb75 486
cd4aac1e
PP
487`+BT_LOGF("format string", ...)+`::
488 Generic fatal logging statement.
beb0fb75 489
ef267d12
PP
490`+BT_LOGT_STR("preformatted string")+`::
491 Generic preformatted string trace logging statement.
beb0fb75 492
cd4aac1e
PP
493`+BT_LOGD_STR("preformatted string")+`::
494 Generic preformatted string debug logging statement.
beb0fb75 495
cd4aac1e
PP
496`+BT_LOGI_STR("preformatted string")+`::
497 Generic preformatted string info logging statement.
beb0fb75 498
cd4aac1e
PP
499`+BT_LOGW_STR("preformatted string")+`::
500 Generic preformatted string warning logging statement.
beb0fb75 501
cd4aac1e
PP
502`+BT_LOGE_STR("preformatted string")+`::
503 Generic preformatted string error logging statement.
beb0fb75 504
cd4aac1e
PP
505`+BT_LOGF_STR("preformatted string")+`::
506 Generic preformatted string fatal logging statement.
beb0fb75 507
ef267d12
PP
508`+BT_LOGT_MEM(data_ptr, data_size, "format string", ...)+`::
509 Generic memory trace logging statement.
beb0fb75 510
cd4aac1e
PP
511`+BT_LOGD_MEM(data_ptr, data_size, "format string", ...)+`::
512 Generic memory debug logging statement.
b81626f9 513
cd4aac1e
PP
514`+BT_LOGI_MEM(data_ptr, data_size, "format string", ...)+`::
515 Generic memory info logging statement.
b81626f9 516
cd4aac1e
PP
517`+BT_LOGW_MEM(data_ptr, data_size, "format string", ...)+`::
518 Generic memory warning logging statement.
b81626f9 519
cd4aac1e
PP
520`+BT_LOGE_MEM(data_ptr, data_size, "format string", ...)+`::
521 Generic memory error logging statement.
b81626f9 522
cd4aac1e
PP
523`+BT_LOGF_MEM(data_ptr, data_size, "format string", ...)+`::
524 Generic memory fatal logging statement.
b81626f9 525
ef267d12
PP
526`+BT_LOGT_ERRNO("initial message", "format string", ...)+`::
527 Generic `errno` string trace logging statement.
b81626f9 528
cd4aac1e
PP
529`+BT_LOGD_ERRNO("initial message", "format string", ...)+`::
530 Generic `errno` string debug logging statement.
beb0fb75 531
cd4aac1e
PP
532`+BT_LOGI_ERRNO("initial message", "format string", ...)+`::
533 Generic `errno` string info logging statement.
beb0fb75 534
cd4aac1e
PP
535`+BT_LOGW_ERRNO("initial message", "format string", ...)+`::
536 Generic `errno` string warning logging statement.
beb0fb75 537
cd4aac1e
PP
538`+BT_LOGE_ERRNO("initial message", "format string", ...)+`::
539 Generic `errno` string error logging statement.
beb0fb75 540
cd4aac1e
PP
541`+BT_LOGF_ERRNO("initial message", "format string", ...)+`::
542 Generic `errno` string fatal logging statement.
beb0fb75 543
cd4aac1e
PP
544[[lib-logging-statements]]
545==== Library-specific logging statement macros
546
28b765ac
PP
547The {bt2} library contains an internal logging API based on the
548generic logging framework. You can use it to log known {bt2}
cd4aac1e
PP
549objects without having to manually log each member.
550
28b765ac
PP
551See <<logging-instrument-c-file-lib>>
552and <<logging-instrument-h-file-lib>> to
cd4aac1e
PP
553learn how to be able to use the following macros.
554
555The library logging statement macros are named `+BT_LIB_LOG*()+` instead
556of `+BT_LOG*()+`:
557
ef267d12
PP
558`+BT_LIB_LOGT("format string", ...)+`::
559 Library trace logging statement.
cd4aac1e
PP
560
561`+BT_LIB_LOGD("format string", ...)+`::
562 Library debug logging statement.
563
564`+BT_LIB_LOGI("format string", ...)+`::
565 Library info logging statement.
566
567`+BT_LIB_LOGW("format string", ...)+`::
568 Library warning logging statement.
569
570`+BT_LIB_LOGE("format string", ...)+`::
571 Library error logging statement.
572
573`+BT_LIB_LOGF("format string", ...)+`::
574 Library fatal logging statement.
575
3cd4c495
PP
576`+BT_LIB_LOGW_APPEND_CAUSE("format string", ...)+`::
577 Library warning logging statement, and unconditional error cause
578 appending.
579
580`+BT_LIB_LOGE_APPEND_CAUSE("format string", ...)+`::
581 Library error logging statement, and unconditional error cause
582 appending.
583
584`+BT_LIB_LOGF_APPEND_CAUSE("format string", ...)+`::
585 Library fatal logging statement, and unconditional error cause
586 appending.
587
cd4aac1e
PP
588The macros above accept the typical `printf()` conversion specifiers
589with the following limitations:
590
591* The `+*+` width specifier is not accepted.
592* The `+*+` precision specifier is not accepted.
593* The `j` and `t` length modifiers are not accepted.
594* The `n` format specifier is not accepted.
595* The format specifiers defined in `<inttypes.h>` are not accepted,
596 except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
597 `PRIi64`.
598
28b765ac 599The {bt2} library custom conversion specifier is accepted. Its
cd4aac1e
PP
600syntax is either `%!u` to format a UUID (`bt_uuid` type), or:
601
602. Introductory `%!` sequence.
603
604. **Optional**: `[` followed by a custom prefix for the printed fields
605 of this specifier, followed by `]`. The standard form is to end this
606 prefix with `-` so that, for example, with the prefix `tc-`, the
607 complete field name becomes `tc-addr`.
608
609. **Optional**: `pass:[+]` to print extended object members. This
610 depends on the provided format specifier.
611
612. Format specifier (see below).
613
614The available format specifiers are:
615
616[options="header,autowidth",cols="3"]
617|===
618|Specifier
619|Object
620|Expected C type
621
622|`F`
623|Trace IR field class
6769570a 624|`+const struct bt_field_class *+`
cd4aac1e
PP
625
626|`f`
627|Trace IR field
6769570a 628|`+const struct bt_field *+`
cd4aac1e
PP
629
630|`P`
631|Trace IR field path
6769570a 632|`+const struct bt_field_path *+`
cd4aac1e
PP
633
634|`E`
635|Trace IR event class
6769570a 636|`+const struct bt_event_class *+`
cd4aac1e
PP
637
638|`e`
639|Trace IR event
6769570a 640|`+const struct bt_event *+`
cd4aac1e
PP
641
642|`S`
643|Trace IR stream class.
6769570a 644|`+const struct bt_stream_class *+`
cd4aac1e
PP
645
646|`s`
647|Trace IR stream
6769570a 648|`+const struct bt_stream *+`
cd4aac1e
PP
649
650|`a`
651|Trace IR packet
6769570a 652|`+const struct bt_packet *+`
cd4aac1e
PP
653
654|`T`
655|Trace IR trace class
6769570a 656|`+const struct bt_trace_class *+`
cd4aac1e
PP
657
658|`t`
659|Trace IR trace
6769570a 660|`+const struct bt_trace *+`
cd4aac1e
PP
661
662|`K`
663|Trace IR clock class
6769570a 664|`+const struct bt_clock_class *+`
cd4aac1e
PP
665
666|`k`
667|Trace IR clock snapshot
6769570a 668|`+const struct bt_clock_snapshot *+`
cd4aac1e
PP
669
670|`v`
671|Value object
6769570a
PP
672|`+const struct bt_value *+`
673
674|`R`
675|Integer range set
676|`const struct bt_integer_range_set *`
cd4aac1e
PP
677
678|`n`
679|Message
6769570a 680|`+const struct bt_message *+`
cd4aac1e 681
a3f0c7db
SM
682|`I`
683|Message iterator class
684|`struct bt_message_iterator_class *`
685
cd4aac1e
PP
686|`i`
687|Message iterator
688|`struct bt_message_iterator *`
689
690|`C`
691|Component class
692|`struct bt_component_class *`
693
694|`c`
695|Component
6769570a 696|`+const struct bt_component *+`
cd4aac1e
PP
697
698|`p`
699|Port
6769570a 700|`+const struct bt_port *+`
cd4aac1e
PP
701
702|`x`
703|Connection
6769570a 704|`+const struct bt_connection *+`
cd4aac1e
PP
705
706|`g`
707|Graph
6769570a 708|`+const struct bt_graph *+`
cd4aac1e 709
b70d57a1
PP
710|`z`
711|Interrupter
712|`+struct bt_interrupter *+`
713
cd4aac1e
PP
714|`l`
715|Plugin
6769570a 716|`+const struct bt_plugin *+`
cd4aac1e 717
553c4bab
PP
718|`r`
719|Error cause
6769570a 720|`+const struct bt_error_cause *+`
553c4bab 721
cd4aac1e
PP
722|`o`
723|Object pool
6769570a 724|`+const struct bt_object_pool *+`
cd4aac1e
PP
725
726|`O`
727|Object
6769570a 728|`+const struct bt_object *+`
cd4aac1e
PP
729|===
730
731Conversion specifier examples:
732
733* `%!f`
734* `%![my-event-]+e`
735* `%!t`
736* `%!+F`
737
738The ``, `` string (comma and space) is printed between individual
739fields, but **not after the last one**. Therefore, you must put this
740separator in the format string between two conversion specifiers, for
741example:
742
743[source,c]
744----
745BT_LIB_LOGW("Message: count=%u, %!E, %!+K", count, event_class, clock_class);
746----
747
748Example with a custom prefix:
749
750[source,c]
751----
752BT_LIB_LOGI("Some message: %![ec-a-]e, %![ec-b-]+e", ec_a, ec_b);
753----
754
28b765ac 755It is safe to pass `NULL` as any {bt2} object parameter: the macros
cd4aac1e
PP
756only print its null address.
757
758WARNING: Build-time `printf()` format checks are disabled for the
759`+BT_LIB_LOG*()+` macros because there are custom conversion specifiers,
760so make sure to test your logging statements.
761
cd4aac1e
PP
762[[comp-logging-statements]]
763==== Component-specific logging statement macros
764
765There are available logging macros for components. They prepend a prefix
766including the component's name to the logging message.
767
28b765ac
PP
768See <<logging-instrument-c-file-compcls>> and
769<<logging-instrument-h-file-compcls>> to learn how to be able to use the
cd4aac1e
PP
770following macros.
771
772The component logging statement macros are named `+BT_COMP_LOG*()+`
773instead of `+BT_LOG*()+`:
774
ef267d12
PP
775`+BT_COMP_LOGT("format string", ...)+`::
776 Component trace logging statement.
cd4aac1e
PP
777
778`+BT_COMP_LOGD("format string", ...)+`::
779 Component debug logging statement.
780
781`+BT_COMP_LOGI("format string", ...)+`::
782 Component info logging statement.
783
784`+BT_COMP_LOGW("format string", ...)+`::
785 Component warning logging statement.
786
787`+BT_COMP_LOGE("format string", ...)+`::
788 Component error logging statement.
789
790`+BT_COMP_LOGF("format string", ...)+`::
791 Component fatal logging statement.
792
ef267d12
PP
793`+BT_COMP_LOGT_STR("preformatted string")+`::
794 Component preformatted string trace logging statement.
cd4aac1e
PP
795
796`+BT_COMP_LOGD_STR("preformatted string")+`::
797 Component preformatted string debug logging statement.
798
799`+BT_COMP_LOGI_STR("preformatted string")+`::
800 Component preformatted string info logging statement.
801
802`+BT_COMP_LOGW_STR("preformatted string")+`::
803 Component preformatted string warning logging statement.
804
805`+BT_COMP_LOGE_STR("preformatted string")+`::
806 Component preformatted string error logging statement.
807
808`+BT_COMP_LOGF_STR("preformatted string")+`::
809 Component preformatted string fatal logging statement.
810
ef267d12
PP
811`+BT_COMP_LOGT_ERRNO("initial message", "format string", ...)+`::
812 Component `errno` string trace logging statement.
cd4aac1e
PP
813
814`+BT_COMP_LOGD_ERRNO("initial message", "format string", ...)+`::
815 Component `errno` string debug logging statement.
816
817`+BT_COMP_LOGI_ERRNO("initial message", "format string", ...)+`::
818 Component `errno` string info logging statement.
819
820`+BT_COMP_LOGW_ERRNO("initial message", "format string", ...)+`::
821 Component `errno` string warning logging statement.
822
823`+BT_COMP_LOGE_ERRNO("initial message", "format string", ...)+`::
824 Component `errno` string error logging statement.
825
826`+BT_COMP_LOGF_ERRNO("initial message", "format string", ...)+`::
827 Component `errno` string fatal logging statement.
828
ef267d12
PP
829`+BT_COMP_LOGT_MEM(data_ptr, data_size, "format string", ...)+`::
830 Component memory trace logging statement.
cd4aac1e
PP
831
832`+BT_COMP_LOGD_MEM(data_ptr, data_size, "format string", ...)+`::
833 Component memory debug logging statement.
834
835`+BT_COMP_LOGI_MEM(data_ptr, data_size, "format string", ...)+`::
836 Component memory info logging statement.
837
838`+BT_COMP_LOGW_MEM(data_ptr, data_size, "format string", ...)+`::
839 Component memory warning logging statement.
840
841`+BT_COMP_LOGE_MEM(data_ptr, data_size, "format string", ...)+`::
842 Component memory error logging statement.
843
844`+BT_COMP_LOGF_MEM(data_ptr, data_size, "format string", ...)+`::
845 Component memory fatal logging statement.
beb0fb75 846
f9ded0e0 847==== Conditional logging
beb0fb75 848
cd4aac1e 849`+BT_LOG_IF(cond, statement)+`::
beb0fb75
PP
850 Execute `statement` only if `cond` is true.
851+
852Example:
853+
854--
855[source,c]
856----
857BT_LOG_IF(i < count / 2, BT_LOGD("Log this: i=%d", i));
858----
859--
860
861To check the <<build-time-log-level,build-time log level>>:
862
863[source,c]
864----
865#if BT_LOG_ENABLED_DEBUG
866...
867#endif
868----
869
cd4aac1e
PP
870This tests if the _DEBUG_ level was enabled at build time. This means
871that the current, <<run-time-log-level,run-time log level>> _could_ be
f9ded0e0
PP
872_DEBUG_, but it could also be higher. The rule of thumb is to use only
873logging statements at the same level in a `BT_LOG_ENABLED_*` conditional
874block.
beb0fb75
PP
875
876The available definitions for build-time conditions are:
877
ef267d12 878* `BT_LOG_ENABLED_TRACE`
beb0fb75
PP
879* `BT_LOG_ENABLED_DEBUG`
880* `BT_LOG_ENABLED_INFO`
770538dd 881* `BT_LOG_ENABLED_WARNING`
beb0fb75
PP
882* `BT_LOG_ENABLED_ERROR`
883* `BT_LOG_ENABLED_FATAL`
884
885To check the current, <<run-time-log-level,run-time log level>>:
886
887[source,c]
888----
889if (BT_LOG_ON_DEBUG) {
890 ...
891}
892----
893
894This tests if the _DEBUG_ log level is dynamically turned on
cd4aac1e 895(implies that it's also enabled at build time). This check could have a
beb0fb75
PP
896noticeable impact on performance.
897
898The available definitions for run-time conditions are:
899
ef267d12 900* `BT_LOG_ON_TRACE`
beb0fb75
PP
901* `BT_LOG_ON_DEBUG`
902* `BT_LOG_ON_INFO`
770538dd 903* `BT_LOG_ON_WARNING`
beb0fb75
PP
904* `BT_LOG_ON_ERROR`
905* `BT_LOG_ON_FATAL`
906
cd4aac1e
PP
907Those macros check the module-specific log level symbol (defined by
908`BT_LOG_OUTPUT_LEVEL`).
beb0fb75
PP
909
910Never, ever write code which would be executed only to compute the
911fields of a logging statement outside a conditional logging scope,
912for example:
913
914[source,c]
915----
916int number = get_number_of_event_classes_with_property_x(...);
917BT_LOGD("Bla bla: number=%d", number);
918----
919
920Do this instead:
921
922[source,c]
923----
924if (BT_LOG_ON_DEBUG) {
925 int number = get_number_of_event_classes_with_property_x(...);
926 BT_LOGD("Bla bla: number=%d", number);
927}
928----
929
930Or even this:
931
932[source,c]
933----
934BT_LOGD("Bla bla: number=%d", get_number_of_event_classes_with_property_x(...));
935----
936
cd4aac1e 937=== Guides
beb0fb75 938
cd4aac1e
PP
939[[logging-instrument-c-file-gen]]
940==== Instrument a C source file (generic)
beb0fb75 941
cd4aac1e
PP
942To instrument a C source file (`.c`):
943
944. At the top of the file, before the first `#include` line (if any),
945 define your file's <<choose-a-logging-tag,logging tag>> name:
4a41523d
PP
946+
947--
948[source,c]
949----
cd4aac1e 950#define BT_LOG_TAG "SUBSYS/MY-MODULE/MY-FILE"
4a41523d
PP
951----
952--
cd4aac1e
PP
953
954. Below the line above, define the source file's log level expression,
955 `BT_LOG_OUTPUT_LEVEL`. This expression is evaluated for each
956 <<gen-logging-statements,logging statement>> to know the current
957 <<run-time-log-level,run-time log level>>.
958+
959Examples:
960+
961[source,c]
962----
963/* Global log level variable */
964#define BT_LOG_OUTPUT_LEVEL module_global_log_level
965----
4a41523d 966+
cd4aac1e
PP
967[source,c]
968----
969/* Local log level variable; must exist where you use BT_LOG*() */
970#define BT_LOG_OUTPUT_LEVEL log_level
971----
972+
973[source,c]
974----
975/* Object's log level; `obj` must exist where you use BT_LOG*() */
976#define BT_LOG_OUTPUT_LEVEL (obj->log_level)
977----
4a41523d 978
cd4aac1e 979. Include `"logging/log.h"`:
4a41523d 980+
beb0fb75
PP
981[source,c]
982----
578e048b 983#include "logging/log.h"
cd4aac1e 984----
4a41523d 985
cd4aac1e
PP
986. In the file, instrument your code with the
987 <<gen-logging-statements,generic logging statement macros>>.
4a41523d 988
cd4aac1e
PP
989[[logging-instrument-h-file-gen]]
990==== Instrument a C header file (generic)
991
992To instrument a C header file (`.h`), if you have `static inline`
993functions in it:
994
995. Do not include `"logging/log.h"`!
996
997. Do one of:
998
999.. In the file, instrument your code with the
1000 <<gen-logging-statements,generic logging statement macros>>, making
1001 each of them conditional to the existence of the macro you're using:
4a41523d 1002+
cd4aac1e
PP
1003[source,c]
1004----
1005static inline
1006int some_function(int x)
1007{
1008 /* ... */
4a41523d 1009
ef267d12
PP
1010#ifdef BT_LOGT
1011 BT_LOGT(...);
cd4aac1e 1012#endif
beb0fb75 1013
cd4aac1e 1014 /* ... */
6470c171 1015
cd4aac1e
PP
1016#ifdef BT_LOGW_STR
1017 BT_LOGW_STR(...);
1018#endif
f9ded0e0 1019
cd4aac1e
PP
1020 /* ... */
1021}
1022----
1023+
1024The C source files which include this header file determine if logging
1025is enabled or not for them, and if so, what is their
1026<<choose-a-logging-tag,logging tag>> and <<run-time-log-level,run-time
1027log level>> expression.
f9ded0e0 1028
cd4aac1e
PP
1029.. Require that logging be enabled:
1030+
1031[source,c]
1032----
1033/* Protection: this file uses BT_LOG*() macros directly */
1034#ifndef BT_LOG_SUPPORTED
1035# error Please include "logging/log.h" before including this file.
1036#endif
1037----
1038+
1039Then, in the file, instrument your code with the
1040<<gen-logging-statements,generic logging statement macros>>.
1041
cd4aac1e
PP
1042[[logging-instrument-c-file-lib]]
1043==== Instrument a library C source file
1044
1045To instrument a library C source file (`.c`):
4a41523d
PP
1046
1047. At the top of the file, before the first `#include` line (if any),
cd4aac1e
PP
1048 define your file's <<choose-a-logging-tag,logging tag>> name (this
1049 tag must start with `LIB/`):
4a41523d
PP
1050+
1051--
1052[source,c]
1053----
cd4aac1e 1054#define BT_LOG_TAG "LIB/THE-FILE"
4a41523d
PP
1055----
1056--
cd4aac1e
PP
1057
1058. Include `"lib/logging.h"`:
4a41523d 1059+
cd4aac1e
PP
1060[source,c]
1061----
1062#include "lib/logging.h"
1063----
4a41523d 1064
cd4aac1e
PP
1065. In the file, instrument your code with the
1066 <<lib-logging-statements,library logging statement macros>> or with
1067 the <<gen-logging-statements,generic logging statement macros>>.
4a41523d 1068
cd4aac1e
PP
1069[[logging-instrument-h-file-lib]]
1070==== Instrument a library C header file
1071
1072To instrument a library C header file (`.h`), if you have `static
1073inline` functions in it:
1074
1075. Do not include `"lib/logging.h"`!
1076
1077. Require that library logging be enabled:
1078+
1079[source,c]
1080----
1081/* Protection: this file uses BT_LIB_LOG*() macros directly */
1082#ifndef BT_LIB_LOG_SUPPORTED
1083# error Please include "lib/logging.h" before including this file.
1084#endif
1085----
1086
1087. In the file, instrument your code with the
1088 <<lib-logging-statements,library logging statement macros>> or with
1089 the <<gen-logging-statements,generic logging statement macros>>.
f9ded0e0 1090
cd4aac1e
PP
1091[[logging-instrument-c-file-compcls]]
1092==== Instrument a component class C source file
1093
1094To instrument a component class C source file (`.c`):
1095
1096. At the top of the file, before the first `#include` line (if any),
1097 define your file's <<choose-a-logging-tag,logging tag>> name (this tag
1098 must start with `PLUGIN/` followed by the component class identifier):
4a41523d
PP
1099+
1100--
1101[source,c]
1102----
cd4aac1e
PP
1103#define BT_LOG_TAG "PLUGIN/SRC.MY-PLUGIN.MY-SRC"
1104----
1105--
4a41523d 1106
cd4aac1e
PP
1107. Below the line above, define the source file's log level expression,
1108 `BT_LOG_OUTPUT_LEVEL`. This expression is evaluated for each
1109 <<comp-logging-statements,logging statement>> to know the current
1110 <<run-time-log-level,run-time log level>>.
1111+
1112For a component class file, it is usually a member of a local component
1113private structure variable:
1114+
1115[source,c]
1116----
1117#define BT_LOG_OUTPUT_LEVEL (my_comp->log_level)
1118----
4a41523d 1119
cd4aac1e
PP
1120. Below the line above, define `BT_COMP_LOG_SELF_COMP` to an expression
1121 which, evaluated in the context of the
1122 <<comp-logging-statements,logging statements>>, evaluates to the self
1123 component address (`+bt_self_component *+`) of the component.
1124+
1125This is usually a member of a local component private structure
1126variable:
1127+
1128[source,c]
1129----
1130#define BT_COMP_LOG_SELF_COMP (my_comp->self_comp)
1131----
1132
d9c39b0a 1133. Include `"logging/comp-logging.h"`:
cd4aac1e
PP
1134+
1135[source,c]
1136----
d9c39b0a 1137#include "logging/comp-logging.h"
cd4aac1e
PP
1138----
1139
1140. In the component initialization method, make sure to set the
1141 component private structure's log level member to the initial
1142 component's log level:
1143+
1144[source,c]
1145----
1146struct my_comp {
1147 bt_logging_level log_level;
4a41523d 1148 /* ... */
cd4aac1e 1149};
4a41523d 1150
cd4aac1e
PP
1151BT_HIDDEN
1152bt_self_component_status my_comp_init(
1153 bt_self_component_source *self_comp_src,
1154 bt_value *params, void *init_method_data)
1155{
1156 struct my_comp *my_comp = g_new0(struct my_comp, 1);
1157 bt_self_component *self_comp =
1158 bt_self_component_source_as_self_component(self_comp_src);
1159 const bt_component *comp = bt_self_component_as_component(self_comp);
1160
1161 BT_ASSERT(my_comp);
1162 my_comp->log_level = bt_component_get_logging_level(comp);
4a41523d
PP
1163
1164 /* ... */
1165}
1166----
cd4aac1e
PP
1167
1168. In the file, instrument your code with the
1169 <<comp-logging-statements,component logging statement macros>>.
1170
cd4aac1e
PP
1171[[logging-instrument-h-file-compcls]]
1172==== Instrument a component class C header file
1173
1174To instrument a component class C header file (`.h`), if you have
1175`static inline` functions in it:
1176
d9c39b0a 1177. Do not include `"logging/comp-logging.h"`!
cd4aac1e
PP
1178
1179. Require that component logging be enabled:
4a41523d 1180+
cd4aac1e
PP
1181[source,c]
1182----
1183/* Protection: this file uses BT_COMP_LOG*() macros directly */
1184#ifndef BT_COMP_LOG_SUPPORTED
d9c39b0a 1185# error Please include "logging/comp-logging.h" before including this file.
cd4aac1e
PP
1186#endif
1187----
1188
1189. In the file, instrument your code with the
1190 <<comp-logging-statements,component logging statement macros>>.
1191
cd4aac1e
PP
1192[[choose-a-logging-tag]]
1193==== Choose a logging tag
beb0fb75 1194
cd4aac1e
PP
1195Each logging-enabled C source file must define `BT_LOG_TAG` to a logging
1196tag. A logging tag is a namespace to identify the logging messages of
1197this specific source file.
beb0fb75 1198
cd4aac1e
PP
1199In general, a logging tag name _must_ be only uppercase letters, digits,
1200and the `-`, `.`, and `/` characters.
beb0fb75 1201
cd4aac1e
PP
1202Use `/` to show the subsystem to source file hierarchy.
1203
28b765ac 1204For the {bt2} library, start with `LIB/`.
cd4aac1e
PP
1205
1206For the CTF writer library, start with `CTF-WRITER/`.
1207
1208For component classes, use:
beb0fb75 1209
93841983 1210[verse]
cd4aac1e
PP
1211`PLUGIN/__CCTYPE__.__PNAME__.__CCNAME__[/__FILE__]`
1212
1213With:
1214
1215`__CCTYPE__`::
1216 Component class's type (`SRC`, `FLT`, or `SINK`).
beb0fb75 1217
cd4aac1e 1218`__PNAME__`::
93841983 1219 Plugin's name.
beb0fb75 1220
cd4aac1e 1221`__CCNAME__`::
93841983 1222 Component class's name.
beb0fb75 1223
cd4aac1e 1224`__FILE__`::
93841983 1225 Additional information to specify the source file name or module.
beb0fb75 1226
cd4aac1e 1227For plugins (files common to many component classes), use:
beb0fb75 1228
cd4aac1e
PP
1229[verse]
1230`PLUGIN/__PNAME__[/__FILE__]`
beb0fb75 1231
cd4aac1e 1232With:
beb0fb75 1233
cd4aac1e
PP
1234`__PNAME__`::
1235 Plugin's name.
1236
1237`__FILE__`::
1238 Additional information to specify the source file name or module.
1239
cd4aac1e
PP
1240[[choose-a-log-level]]
1241==== Choose a log level
beb0fb75
PP
1242
1243Choosing the appropriate level for your logging statement is very
1244important.
1245
cd4aac1e 1246[options="header,autowidth",cols="1,2,3a,4"]
beb0fb75 1247|===
cd4aac1e 1248|Log level |Description |Use cases |Expected impact on performance
beb0fb75
PP
1249
1250|_FATAL_
cd4aac1e
PP
1251|
1252The program, library, or plugin cannot continue to work in this
beb0fb75
PP
1253condition: it must be terminated immediately.
1254
b4b9064d 1255A _FATAL_-level logging statement should always be followed by
4a41523d 1256`abort()`.
beb0fb75
PP
1257|
1258* Unexpected return values from system calls.
4a41523d
PP
1259* Logic error in internal code, for example an unexpected value in a
1260 `switch` statement.
cd4aac1e 1261* Failed assertion (within `BT_ASSERT()`).
bdb288b3
PP
1262* Unsatisfied library precondition (within `BT_ASSERT_PRE()` or
1263 `BT_ASSERT_PRE_DEV()`).
1264* Unsatisfied library postcondition (within `BT_ASSERT_POST()` or
1265 `BT_ASSERT_POST_DEV()`).
dd22a91f 1266|Almost none: always enabled.
beb0fb75
PP
1267
1268|_ERROR_
cd4aac1e
PP
1269|
1270An important error which is somewhat not fatal, that is, the program,
beb0fb75
PP
1271library, or plugin can continue to work after this, but you judge that
1272it should be reported to the user.
1273
1274Usually, the program cannot recover from such an error, but it can at
1275least exit cleanly.
1276|
1277* Memory allocation errors.
cd4aac1e
PP
1278* Wrong component initialization parameters.
1279* Corrupted, unrecoverable trace data.
beb0fb75
PP
1280* Failed to perform an operation which should work considering the
1281 implementation and the satisfied preconditions. For example, the
1282 failure to create an empty object (no parameters): most probably
1283 failed internally because of an allocation error.
93841983 1284* Almost any error in terminal elements: CLI and plugins.
dd22a91f 1285|Almost none: always enabled.
beb0fb75 1286
770538dd 1287|_WARNING_
cd4aac1e
PP
1288|
1289An error which still allows the execution to continue, but you judge
1290that it should be reported to the user.
6c1b33a8 1291
770538dd
PP
1292_WARNING_-level logging statements are for any error or weird action
1293that is directly or indirectly caused by the user, often through some
1294bad input data. For example, not having enough memory is considered
1295beyond the user's control, so we always log memory errors with an
1296_ERROR_ level (not _FATAL_ because we usually don't abort in this
1297condition).
beb0fb75 1298|
cd4aac1e
PP
1299* Missing data within something that is expected to have it, but there's
1300 an alternative.
1301* Invalid file, but recoverable/fixable.
dd22a91f 1302|Almost none: always enabled.
beb0fb75
PP
1303
1304|_INFO_
beb0fb75 1305|
cd4aac1e
PP
1306Any useful information which a non-developer user would possibly
1307understand.
1308
1309Anything logged with this level must _not_ happen repetitively on the
1310fast path, that is, nothing related to each message, for example. This
1311level is used for sporadic and one-shot events.
1312|
1313* CLI or component configuration report.
1314* Successful plugin, component, or message iterator initialization.
1315* In the library: anything related to plugins, graphs, component
1316 classes, components, message iterators, connections, and ports which
1317 is not on the fast path.
beb0fb75 1318* Successful connection to or disconnection from another system.
4a41523d 1319* An _optional_ subsystem cannot be loaded.
cd4aac1e
PP
1320* An _optional_ field/datum cannot be found.
1321|
dd22a91f 1322Very little: always enabled.
beb0fb75
PP
1323
1324|_DEBUG_
beb0fb75 1325|
28b765ac 1326Something that only {bt2} developers would be interested into,
cd4aac1e
PP
1327which can occur on the fast path, but not more often than once per
1328message.
1329
1330The _DEBUG_ level is the default <<build-time-log-level,build-time log
1331level>> as, since it's not _too_ verbose, the performance is similar to
1332an _INFO_ build.
1333|
1334* Object construction and destruction.
1335* Object recycling (except fields).
1336* Object copying (except fields and values).
1337* Object freezing (whatever the type, as freezing only occurs in
1338 developer mode).
9b4f9b42 1339* Object interruption.
cd4aac1e
PP
1340* Calling user methods and logging the result.
1341* Setting object properties (except fields and values).
1342|
ef267d12 1343Noticeable, but not as much as the _TRACE_ level: could be executed
cd4aac1e
PP
1344in production if you're going to need a thorough log for support
1345tickets without having to rebuild the project.
beb0fb75 1346
ef267d12 1347|_TRACE_
cd4aac1e
PP
1348|
1349Low-level debugging context information (anything that does not fit the
1350other log levels). More appropriate for tracing in general.
beb0fb75
PP
1351|
1352* Reference count change.
cd4aac1e 1353* Fast path, low level state machine's state change.
beb0fb75
PP
1354* Get or set an object's property.
1355* Object comparison's intermediate results.
1356|Huge: not executed in production.
1357|===
1358
cd4aac1e
PP
1359[IMPORTANT]
1360--
770538dd 1361Make sure not to use a _WARNING_ (or higher) log level when the
cd4aac1e
PP
1362condition leading to the logging statement can occur under normal
1363circumstances.
1364
1365For example, a public function to get some object or
1366property from an object by name or key that fails to find the value is
1367not a warning scenario: the user could legitimately use this function to
1368check if the name/key exists in the object. In this case, use the
ef267d12 1369_TRACE_ level (or do not log at all).
cd4aac1e 1370--
b4b9064d 1371
b4b9064d 1372[[message]]
cd4aac1e 1373==== Write an appropriate message
beb0fb75
PP
1374
1375Follow those rules when you write a logging statement's message:
1376
cd4aac1e
PP
1377* Use an English sentence which starts with a capital letter.
1378
1379* Start the sentence with the appropriate verb tense depending on the
1380 context. For example:
beb0fb75
PP
1381+
1382--
b4b9064d
PP
1383** Beginning of operation (present continuous): _Creating ..._,
1384 _Copying ..._, _Serializing ..._, _Freezing ..._, _Destroying ..._
1385** End of operation (simple past): _Created ..._, _Successfully created ..._,
1386 _Failed to create ..._, _Set ..._ (simple past of _to set_ which is
1387 also _set_)
beb0fb75
PP
1388--
1389+
1390For warning and error messages, you can start the message with _Cannot_
b4b9064d 1391or _Failed to_ followed by a verb if it's appropriate.
beb0fb75
PP
1392
1393* Do not include the log level in the message itself. For example,
1394 do not start the message with _Error while_ or _Warning:_.
1395
cd4aac1e
PP
1396* Do not put newlines, tabs, or other special characters in the message,
1397 unless you want to log a string with such characters. Note that
1398 multiline logging messages can be hard to parse, analyze, and filter,
1399 however, so prefer multiple logging statements over a single statement
1400 with newlines.
beb0fb75
PP
1401
1402* **If there are fields that your logging statement must record**,
1403 follow the message with `:` followed by a space, then with the list of
1404 fields (more about this below). If there are no fields, end the
1405 sentence with a period.
1406
1407The statement's fields _must_ be a comma-separated list of
cd4aac1e
PP
1408`__name__=__value__` tokens. Keep `__name__` as simple as possible; use
1409kebab case if possible. If `__value__` is a non-alphanumeric string, put
1410it between double quotes (`"%s"` specifier). Always use the `PRId64` and
1411`PRIu64` specifiers to log an `int64_t` or an `uint64_t` value. Use `%d`
1412to log a boolean value.
beb0fb75
PP
1413
1414Example:
1415
cd4aac1e
PP
1416 "Cannot read stream data for indexing: path=\"%s\", name=\"%s\", "
1417 "stream-id=%" PRIu64 ", stream-fd=%d, "
1418 "index=%" PRIu64 ", status=%s, is-mapped=%d"
beb0fb75 1419
cd4aac1e
PP
1420By following a standard format for the statement fields, it is easier to
1421use tools like https://www.elastic.co/products/logstash[Logstash] or
1422even https://www.splunk.com/[Splunk] to split fields and analyze logs.
beb0fb75
PP
1423
1424Prefer the following suffixes in field names:
1425
1426[options="header,autowidth"]
1427|===
1428|Field name suffix |Description |Format specifier
1429
1430|`-addr` |Memory address |`%p`
1431|`-fd` |File descriptor |`%d`
cd4aac1e
PP
1432|`-fp` |File stream (`+FILE *+`) |`%p`
1433|`-id` |Object's ID |`%" PRIu64 "`
1434|`-index` |Index |`%" PRIu64 "`
beb0fb75 1435|`-name` |Object's name |`\"%s\"`
beb0fb75
PP
1436|===
1437
f9ded0e0 1438=== Output
beb0fb75
PP
1439
1440The log is printed to the standard error stream. A log line contains the
cd4aac1e
PP
1441time, the process and thread IDs, the <<log-levels,log level>>, the
1442<<choose-a-logging-tag,logging tag>>, the source's function name, file
1443name and line number, and the <<message,message>>.
beb0fb75 1444
28b765ac 1445When {bt2} supports terminal color codes (depends on the
f9ded0e0
PP
1446`BABELTRACE_TERM_COLOR` environment variable's value and what the
1447standard output and error streams are plugged into), _INFO_-level lines
770538dd 1448are blue, _WARNING_-level lines are yellow, and _ERROR_-level and
f9ded0e0
PP
1449_FATAL_-level lines are red.
1450
1451Log line example:
beb0fb75 1452
cd4aac1e
PP
1453----
145405-11 00:58:03.691 23402 23402 D VALUES bt_value_destroy@values.c:498 Destroying value: addr=0xb9c3eb0
1455----
beb0fb75 1456
cd4aac1e
PP
1457You can easily filter the log with `grep` or `ag`. For example, to keep
1458only the _DEBUG_-level logging messages that the `FIELD-CLASS` module
beb0fb75
PP
1459generates:
1460
cd4aac1e
PP
1461----
1462$ babeltrace2 --log-level=D /path/to/trace |& ag 'D FIELD-CLASS'
1463----
1464
cd4aac1e
PP
1465== Valgrind
1466
1467To use Valgrind on an application (for example, the CLI or a test) which
1468loads libbabeltrace2, use:
1469
1470----
1471$ G_SLICE=always-malloc G_DEBUG=gc-friendly PYTHONMALLOC=malloc \
916d0d91 1472 LIBBABELTRACE2_NO_DLCLOSE=1 valgrind --leak-check=full app
cd4aac1e
PP
1473----
1474
1475`G_SLICE=always-malloc` and `G_DEBUG=gc-friendly` is for GLib and
1476`PYTHONMALLOC=malloc` is for the Python interpreter, if it is used by
1477the Python plugin provider (Valgrind will probably show a lot of errors
1478which originate from the Python interpreter anyway).
1479
16b7b5e7 1480`LIBBABELTRACE2_NO_DLCLOSE=1` makes libbabeltrace2 not close the shared
cd4aac1e
PP
1481libraries (plugins) which it loads. You need this to see the appropriate
1482backtrace when Valgrind shows errors.
c637d729
FD
1483
1484== Testing
1485
800400d8
PP
1486[[test-env]]
1487=== Environment
c637d729 1488
28b765ac 1489`tests/utils/utils.sh` sets the environment variables for any {bt2}
800400d8
PP
1490test script.
1491
1492`utils.sh` only needs to know the path to the `tests` directory within
1493the source and the build directories. By default, `utils.sh` assumes the
1494build is in tree, that is, you ran `./configure` from the source's root
1495directory, and sets the `BT_TESTS_SRCDIR` and `BT_TESTS_BUILDDIR`
1496environment variables accordingly. You can override those variables, for
1497example if you build out of tree.
1498
1499All test scripts eventually do something like this to source `utils.sh`,
1500according to where they are located relative to the `tests` directory:
1501
1502[source,bash]
1503----
1504if [ "x${BT_TESTS_SRCDIR:-}" != "x" ]; then
1505 UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh"
1506else
1507 UTILSSH="$(dirname "$0")/../utils/utils.sh"
1508fi
1509----
1510
1511==== Python
1512
1513You can use the `tests/utils/run_python_bt2` script to run any command
1514within an environment making the build's `bt2` Python package available.
1515
1516`run_python_bt2` uses <<test-env,`utils.sh`>> which needs to know the
1517build directory, so make sure you set the `BT_TESTS_BUILDDIR`
1518environment variable correctly _if you build out of tree_, for example:
1519
1520----
1521$ export BT_TESTS_BUILDDIR=/path/to/build/babeltrace/tests
1522----
1523
1524You can run any command which needs the `bt2` Python package through
1525`run_python_bt2`, for example:
c637d729
FD
1526
1527----
800400d8 1528$ ./tests/utils/run_python_bt2 ipython3
c637d729
FD
1529----
1530
800400d8
PP
1531=== Report format
1532
1533All test scripts output the test results following the
1534https://testanything.org/[Test Anything Protocol] (TAP) format.
1535
1536The TAP format has two mechanisms to print additional information about
1537a test:
1538
1539* Print a line starting with `#` to the standard output.
1540+
1541This is usually done with the `diag()` C function or the `diag` shell
1542function.
1543
1544* Print to the standard error.
1545
800400d8
PP
1546=== Python bindings
1547
1548The `bt2` Python package tests are located in
1549`tests/bindings/python/bt2`.
1550
800400d8
PP
1551==== Python test runner
1552
28b765ac 1553`tests/utils/python/testrunner.py` is {bt2}'s Python test runner
800400d8
PP
1554which loads Python files containing unit tests, finds all the test
1555cases, and runs the tests, producing a TAP report.
1556
1557You can see the test runner command's help with:
c637d729
FD
1558
1559----
800400d8 1560$ python3 ./tests/utils/python/testrunner.py --help
c637d729
FD
1561----
1562
800400d8
PP
1563By default, the test runner reports failing tests (TAP's `not{nbsp}ok`
1564line), but continues to run other tests. You can use the `--failfast`
1565option to make the test runner fail as soon as a test fails.
c637d729 1566
800400d8
PP
1567==== Guides
1568
1569To run all the `bt2` Python package tests:
1570
1571* Run:
1572+
1573----
1574$ ./tests/utils/run_python_bt2 ./tests/bindings/python/bt2/test_python_bt2
1575----
1576+
1577or:
1578+
1579----
1580$ ./tests/utils/run_python_bt2 python3 ./tests/utils/python/testrunner.py \
1581 ./tests/bindings/python/bt2/ -p '*.py'
1582----
1583
1584To run **all the tests** in a test module (for example,
1585`test_value.py`):
1586
1587* Run:
1588+
1589----
1590$ ./tests/utils/run_python_bt2 python3 ./tests/utils/python/testrunner.py \
1591 ./tests/bindings/python/bt2 -t test_value
1592----
1593
1594To run a **specific test case** (for example, `RealValueTestCase` within
1595`test_value.py`):
1596
1597* Run:
1598+
1599----
1600$ ./tests/utils/run_python_bt2 python3 ./tests/utils/python/testrunner.py \
1601 ./tests/bindings/python/bt2/ -t test_value.RealValueTestCase
1602----
1603
1604To run a **specific test** (for example,
1605`RealValueTestCase.test_assign_pos_int` within `test_value.py`):
1606
1607* Run:
1608+
c637d729 1609----
800400d8
PP
1610$ ./tests/utils/run_python_bt2 python3 ./tests/utils/python/testrunner.py \
1611 ./tests/bindings/python/bt2/ -t test_value.RealValueTestCase.test_assign_pos_int
c637d729 1612----
This page took 0.126275 seconds and 4 git commands to generate.