xref: /dpdk/doc/guides/prog_guide/log_lib.rst (revision 259f6f78094d0fa33ce2ffe298b8df526c535f3b)
109ce4131SBruce Richardson..  SPDX-License-Identifier: BSD-3-Clause
209ce4131SBruce Richardson    Copyright(c) 2023 Intel Corporation.
309ce4131SBruce Richardson
409ce4131SBruce RichardsonLog Library
509ce4131SBruce Richardson===========
609ce4131SBruce Richardson
709ce4131SBruce RichardsonThe DPDK Log library provides the logging functionality for other DPDK libraries and drivers.
898513036SStephen HemmingerBy default, logs are sent only to standard error output of the process.
998513036SStephen HemmingerThe syslog EAL option can be used to redirect to the stystem logger on Linux and FreeBSD.
1098513036SStephen HemmingerIn addition, the log can be redirected to a different stdio file stream.
1109ce4131SBruce Richardson
1209ce4131SBruce RichardsonLog Levels
1309ce4131SBruce Richardson----------
1409ce4131SBruce Richardson
1509ce4131SBruce RichardsonLog messages from apps and libraries are reported with a given level of severity.
1609ce4131SBruce RichardsonThese levels, specified in ``rte_log.h`` are (from most to least important):
1709ce4131SBruce Richardson
1809ce4131SBruce Richardson#. Emergency
1909ce4131SBruce Richardson#. Alert
2009ce4131SBruce Richardson#. Critical
2109ce4131SBruce Richardson#. Error
2209ce4131SBruce Richardson#. Warning
2309ce4131SBruce Richardson#. Notice
2409ce4131SBruce Richardson#. Information
2509ce4131SBruce Richardson#. Debug
2609ce4131SBruce Richardson
2709ce4131SBruce RichardsonAt runtime, only messages of a configured level or above (i.e. of higher importance)
2809ce4131SBruce Richardsonwill be emitted by the application to the log output.
2909ce4131SBruce RichardsonThat level can be configured either by the application calling the relevant APIs from the logging library,
3009ce4131SBruce Richardsonor by the user passing the ``--log-level`` parameter to the EAL via the application.
3109ce4131SBruce Richardson
3209ce4131SBruce RichardsonSetting Global Log Level
3309ce4131SBruce Richardson~~~~~~~~~~~~~~~~~~~~~~~~
3409ce4131SBruce Richardson
3509ce4131SBruce RichardsonTo adjust the global log level for an application,
3609ce4131SBruce Richardsonjust pass a numeric level or a level name to the ``--log-level`` EAL parameter.
3709ce4131SBruce RichardsonFor example::
3809ce4131SBruce Richardson
3909ce4131SBruce Richardson	/path/to/app --log-level=error
4009ce4131SBruce Richardson
4109ce4131SBruce Richardson	/path/to/app --log-level=debug
4209ce4131SBruce Richardson
4309ce4131SBruce Richardson	/path/to/app --log-level=5   # warning
4409ce4131SBruce Richardson
4509ce4131SBruce RichardsonWithin an application, the log level can be similarly set using the ``rte_log_set_global_level`` API.
4609ce4131SBruce Richardson
4709ce4131SBruce RichardsonSetting Log Level for a Component
4809ce4131SBruce Richardson~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4909ce4131SBruce Richardson
5009ce4131SBruce RichardsonIn some cases, for example, for debugging purposes,
5109ce4131SBruce Richardsonit may be desirable to increase or decrease the log level for only a specific component, or set of components.
5209ce4131SBruce RichardsonTo facilitate this, the ``--log-level`` argument also accepts an, optionally wildcarded, component name,
5309ce4131SBruce Richardsonalong with the desired level for that component.
5409ce4131SBruce RichardsonFor example::
5509ce4131SBruce Richardson
5609ce4131SBruce Richardson	/path/to/app --log-level=lib.eal:crit
5709ce4131SBruce Richardson
5809ce4131SBruce Richardson	/path/to/app --log-level=lib.*:warning
5909ce4131SBruce Richardson
6009ce4131SBruce RichardsonWithin an application, the same result can be got using the ``rte_log_set_level_pattern()`` or ``rte_log_set_level_regex()`` APIs.
6109ce4131SBruce Richardson
62*259f6f78SStephen Hemminger
6309ce4131SBruce RichardsonUsing Logging APIs to Generate Log Messages
6409ce4131SBruce Richardson-------------------------------------------
6509ce4131SBruce Richardson
6609ce4131SBruce RichardsonTo output log messages, ``rte_log()`` API function should be used.
6709ce4131SBruce RichardsonAs well as the log message, ``rte_log()`` takes two additional parameters:
6809ce4131SBruce Richardson
6909ce4131SBruce Richardson* The log level
7009ce4131SBruce Richardson* The log component type
7109ce4131SBruce Richardson
7209ce4131SBruce RichardsonThe log level is a numeric value as discussed above.
7309ce4131SBruce RichardsonThe component type is a unique id that identifies the particular DPDK component to the logging system.
7409ce4131SBruce RichardsonTo get this id, each component needs to register itself at startup,
7509ce4131SBruce Richardsonusing the macro ``RTE_LOG_REGISTER_DEFAULT``.
7609ce4131SBruce RichardsonThis macro takes two parameters, with the second being the default log level for the component.
7709ce4131SBruce RichardsonThe first parameter, called "type", the name of the "logtype", or "component type" variable used in the component.
7809ce4131SBruce RichardsonThis variable will be defined by the macro, and should be passed as the second parameter in calls to ``rte_log()``.
7909ce4131SBruce RichardsonIn general, most DPDK components define their own logging macros to simplify the calls to the log APIs.
8009ce4131SBruce RichardsonThey do this by:
8109ce4131SBruce Richardson
8209ce4131SBruce Richardson* Hiding the component type parameter inside the macro so it never needs to be passed explicitly.
8309ce4131SBruce Richardson* Using the log-level definitions given in ``rte_log.h`` to allow short textual names to be used in
8409ce4131SBruce Richardson  place of the numeric log levels.
8509ce4131SBruce Richardson
86523bf2d9SDavid MarchandThe following code is taken from ``rte_cfgfile.c`` and shows the log registration,
8709ce4131SBruce Richardsonand subsequent definition of a shortcut logging macro.
8809ce4131SBruce RichardsonIt can be used as a template for any new components using DPDK logging.
8909ce4131SBruce Richardson
90523bf2d9SDavid Marchand.. literalinclude:: ../../../lib/cfgfile/rte_cfgfile.c
91523bf2d9SDavid Marchand   :language: c
92523bf2d9SDavid Marchand   :start-after: Setting up dynamic logging 8<
93523bf2d9SDavid Marchand   :end-before: >8 End of setting up dynamic logging
9409ce4131SBruce Richardson
9509ce4131SBruce Richardson.. note::
9609ce4131SBruce Richardson
9709ce4131SBruce Richardson	Because the log registration macro provides the logtype variable definition,
9809ce4131SBruce Richardson	it should be placed near the top of the C file using it.
9909ce4131SBruce Richardson	If not, the logtype variable should be defined as an "extern int" near the top of the file.
10009ce4131SBruce Richardson
10109ce4131SBruce Richardson	Similarly, if logging is to be done by multiple files in a component,
10209ce4131SBruce Richardson	only one file should register the logtype via the macro,
10309ce4131SBruce Richardson	and the logtype should be defined as an "extern int" in a common header file.
10409ce4131SBruce Richardson	Any component-specific logging macro should similarly be defined in that header.
10509ce4131SBruce Richardson
106523bf2d9SDavid MarchandThroughout the cfgfile library, all logging calls are therefore of the form:
10709ce4131SBruce Richardson
10809ce4131SBruce Richardson.. code:: C
10909ce4131SBruce Richardson
110523bf2d9SDavid Marchand	CFG_LOG(ERR, "missing cfgfile parameters");
11109ce4131SBruce Richardson
112523bf2d9SDavid Marchand	CFG_LOG(ERR, "invalid comment characters %c",
113523bf2d9SDavid Marchand	       params->comment_character);
11462ae1149SStephen Hemminger
11562ae1149SStephen HemmingerLog timestamp
11662ae1149SStephen Hemminger~~~~~~~~~~~~~
11762ae1149SStephen Hemminger
11862ae1149SStephen HemmingerAn optional timestamp can be added before each message by adding the ``--log-timestamp`` option.
11962ae1149SStephen HemmingerFor example::
12062ae1149SStephen Hemminger
12162ae1149SStephen Hemminger	/path/to/app --log-level=lib.*:debug --log-timestamp
12262ae1149SStephen Hemminger
12362ae1149SStephen HemmingerMultiple alternative timestamp formats are available:
12462ae1149SStephen Hemminger
12562ae1149SStephen Hemminger.. csv-table:: Log time stamp format
12662ae1149SStephen Hemminger   :header: "Format", "Description", "Example"
12762ae1149SStephen Hemminger   :widths: 6, 30, 32
12862ae1149SStephen Hemminger
12962ae1149SStephen Hemminger   "ctime", "Unix ctime", "``[Wed Mar 20 07:26:12 2024]``"
13062ae1149SStephen Hemminger   "delta", "Offset since last", "``[<    3.162373>]``"
13162ae1149SStephen Hemminger   "reltime", "Seconds since last or time if minute changed", "``[  +3.001791]`` or ``[Mar20 07:26:12]``"
13262ae1149SStephen Hemminger   "iso", "ISO-8601", "``[2024-03-20T07:26:12−07:00]``"
13362ae1149SStephen Hemminger
13462ae1149SStephen HemmingerTo prefix all console messages with ISO format time the syntax is::
13562ae1149SStephen Hemminger
13662ae1149SStephen Hemminger	/path/to/app --log-timestamp=iso
13762ae1149SStephen Hemminger
13862ae1149SStephen Hemminger.. note::
13962ae1149SStephen Hemminger
14062ae1149SStephen Hemminger   Timestamp option has no effect if using syslog
14162ae1149SStephen Hemminger   because the ``syslog()`` service already does timestamping internally.
142*259f6f78SStephen Hemminger
143*259f6f78SStephen Hemminger
144*259f6f78SStephen HemmingerColor output
145*259f6f78SStephen Hemminger~~~~~~~~~~~~
146*259f6f78SStephen Hemminger
147*259f6f78SStephen HemmingerThe log library will highlight important messages.
148*259f6f78SStephen HemmingerThis is controlled by the ``--log-color`` option.
149*259f6f78SStephen HemmingerThe optional argument describes when color is enabled:
150*259f6f78SStephen Hemminger
151*259f6f78SStephen Hemminger:never: Do not enable color. This is the default behavior.
152*259f6f78SStephen Hemminger
153*259f6f78SStephen Hemminger:auto: Enable color only when printing to a terminal.
154*259f6f78SStephen Hemminger       This is the same as ``--log-color`` with no argument.
155*259f6f78SStephen Hemminger
156*259f6f78SStephen Hemminger:always: Always print color.
157*259f6f78SStephen Hemminger
158*259f6f78SStephen HemmingerFor example to enable color in logs if using terminal::
159*259f6f78SStephen Hemminger
160*259f6f78SStephen Hemminger	/path/to/app --log-color
161*259f6f78SStephen Hemminger
162*259f6f78SStephen Hemminger.. note::
163*259f6f78SStephen Hemminger
164*259f6f78SStephen Hemminger   Color output is never used for syslog or systemd journal logging.
165