xref: /dpdk/doc/guides/prog_guide/log_lib.rst (revision 985130369be32dd68ca104c1ccc86716f6e2bb7b)
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.
8*98513036SStephen HemmingerBy default, logs are sent only to standard error output of the process.
9*98513036SStephen HemmingerThe syslog EAL option can be used to redirect to the stystem logger on Linux and FreeBSD.
10*98513036SStephen 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
6209ce4131SBruce RichardsonUsing Logging APIs to Generate Log Messages
6309ce4131SBruce Richardson-------------------------------------------
6409ce4131SBruce Richardson
6509ce4131SBruce RichardsonTo output log messages, ``rte_log()`` API function should be used.
6609ce4131SBruce RichardsonAs well as the log message, ``rte_log()`` takes two additional parameters:
6709ce4131SBruce Richardson
6809ce4131SBruce Richardson* The log level
6909ce4131SBruce Richardson* The log component type
7009ce4131SBruce Richardson
7109ce4131SBruce RichardsonThe log level is a numeric value as discussed above.
7209ce4131SBruce RichardsonThe component type is a unique id that identifies the particular DPDK component to the logging system.
7309ce4131SBruce RichardsonTo get this id, each component needs to register itself at startup,
7409ce4131SBruce Richardsonusing the macro ``RTE_LOG_REGISTER_DEFAULT``.
7509ce4131SBruce RichardsonThis macro takes two parameters, with the second being the default log level for the component.
7609ce4131SBruce RichardsonThe first parameter, called "type", the name of the "logtype", or "component type" variable used in the component.
7709ce4131SBruce RichardsonThis variable will be defined by the macro, and should be passed as the second parameter in calls to ``rte_log()``.
7809ce4131SBruce RichardsonIn general, most DPDK components define their own logging macros to simplify the calls to the log APIs.
7909ce4131SBruce RichardsonThey do this by:
8009ce4131SBruce Richardson
8109ce4131SBruce Richardson* Hiding the component type parameter inside the macro so it never needs to be passed explicitly.
8209ce4131SBruce Richardson* Using the log-level definitions given in ``rte_log.h`` to allow short textual names to be used in
8309ce4131SBruce Richardson  place of the numeric log levels.
8409ce4131SBruce Richardson
85523bf2d9SDavid MarchandThe following code is taken from ``rte_cfgfile.c`` and shows the log registration,
8609ce4131SBruce Richardsonand subsequent definition of a shortcut logging macro.
8709ce4131SBruce RichardsonIt can be used as a template for any new components using DPDK logging.
8809ce4131SBruce Richardson
89523bf2d9SDavid Marchand.. literalinclude:: ../../../lib/cfgfile/rte_cfgfile.c
90523bf2d9SDavid Marchand   :language: c
91523bf2d9SDavid Marchand   :start-after: Setting up dynamic logging 8<
92523bf2d9SDavid Marchand   :end-before: >8 End of setting up dynamic logging
9309ce4131SBruce Richardson
9409ce4131SBruce Richardson.. note::
9509ce4131SBruce Richardson
9609ce4131SBruce Richardson	Because the log registration macro provides the logtype variable definition,
9709ce4131SBruce Richardson	it should be placed near the top of the C file using it.
9809ce4131SBruce Richardson	If not, the logtype variable should be defined as an "extern int" near the top of the file.
9909ce4131SBruce Richardson
10009ce4131SBruce Richardson	Similarly, if logging is to be done by multiple files in a component,
10109ce4131SBruce Richardson	only one file should register the logtype via the macro,
10209ce4131SBruce Richardson	and the logtype should be defined as an "extern int" in a common header file.
10309ce4131SBruce Richardson	Any component-specific logging macro should similarly be defined in that header.
10409ce4131SBruce Richardson
105523bf2d9SDavid MarchandThroughout the cfgfile library, all logging calls are therefore of the form:
10609ce4131SBruce Richardson
10709ce4131SBruce Richardson.. code:: C
10809ce4131SBruce Richardson
109523bf2d9SDavid Marchand	CFG_LOG(ERR, "missing cfgfile parameters");
11009ce4131SBruce Richardson
111523bf2d9SDavid Marchand	CFG_LOG(ERR, "invalid comment characters %c",
112523bf2d9SDavid Marchand	       params->comment_character);
113