162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci===========================
462306a36Sopenharmony_ciMessage logging with printk
562306a36Sopenharmony_ci===========================
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciprintk() is one of the most widely known functions in the Linux kernel. It's the
862306a36Sopenharmony_cistandard tool we have for printing messages and usually the most basic way of
962306a36Sopenharmony_citracing and debugging. If you're familiar with printf(3) you can tell printk()
1062306a36Sopenharmony_ciis based on it, although it has some functional differences:
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci  - printk() messages can specify a log level.
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci  - the format string, while largely compatible with C99, doesn't follow the
1562306a36Sopenharmony_ci    exact same specification. It has some extensions and a few limitations
1662306a36Sopenharmony_ci    (no ``%n`` or floating point conversion specifiers). See :ref:`How to get
1762306a36Sopenharmony_ci    printk format specifiers right <printk-specifiers>`.
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ciAll printk() messages are printed to the kernel log buffer, which is a ring
2062306a36Sopenharmony_cibuffer exported to userspace through /dev/kmsg. The usual way to read it is
2162306a36Sopenharmony_ciusing ``dmesg``.
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciprintk() is typically used like this::
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci  printk(KERN_INFO "Message: %s\n", arg);
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ciwhere ``KERN_INFO`` is the log level (note that it's concatenated to the format
2862306a36Sopenharmony_cistring, the log level is not a separate argument). The available log levels are:
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
3162306a36Sopenharmony_ci| Name           | String |  Alias function                               |
3262306a36Sopenharmony_ci+================+========+===============================================+
3362306a36Sopenharmony_ci| KERN_EMERG     | "0"    | pr_emerg()                                    |
3462306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
3562306a36Sopenharmony_ci| KERN_ALERT     | "1"    | pr_alert()                                    |
3662306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
3762306a36Sopenharmony_ci| KERN_CRIT      | "2"    | pr_crit()                                     |
3862306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
3962306a36Sopenharmony_ci| KERN_ERR       | "3"    | pr_err()                                      |
4062306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
4162306a36Sopenharmony_ci| KERN_WARNING   | "4"    | pr_warn()                                     |
4262306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
4362306a36Sopenharmony_ci| KERN_NOTICE    | "5"    | pr_notice()                                   |
4462306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
4562306a36Sopenharmony_ci| KERN_INFO      | "6"    | pr_info()                                     |
4662306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
4762306a36Sopenharmony_ci| KERN_DEBUG     | "7"    | pr_debug() and pr_devel() if DEBUG is defined |
4862306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
4962306a36Sopenharmony_ci| KERN_DEFAULT   | ""     |                                               |
5062306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
5162306a36Sopenharmony_ci| KERN_CONT      | "c"    | pr_cont()                                     |
5262306a36Sopenharmony_ci+----------------+--------+-----------------------------------------------+
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciThe log level specifies the importance of a message. The kernel decides whether
5662306a36Sopenharmony_cito show the message immediately (printing it to the current console) depending
5762306a36Sopenharmony_cion its log level and the current *console_loglevel* (a kernel variable). If the
5862306a36Sopenharmony_cimessage priority is higher (lower log level value) than the *console_loglevel*
5962306a36Sopenharmony_cithe message will be printed to the console.
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciIf the log level is omitted, the message is printed with ``KERN_DEFAULT``
6262306a36Sopenharmony_cilevel.
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ciYou can check the current *console_loglevel* with::
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci  $ cat /proc/sys/kernel/printk
6762306a36Sopenharmony_ci  4        4        1        7
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciThe result shows the *current*, *default*, *minimum* and *boot-time-default* log
7062306a36Sopenharmony_cilevels.
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciTo change the current console_loglevel simply write the desired level to
7362306a36Sopenharmony_ci``/proc/sys/kernel/printk``. For example, to print all messages to the console::
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci  # echo 8 > /proc/sys/kernel/printk
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciAnother way, using ``dmesg``::
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci  # dmesg -n 5
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_cisets the console_loglevel to print KERN_WARNING (4) or more severe messages to
8262306a36Sopenharmony_ciconsole. See ``dmesg(1)`` for more information.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciAs an alternative to printk() you can use the ``pr_*()`` aliases for
8562306a36Sopenharmony_cilogging. This family of macros embed the log level in the macro names. For
8662306a36Sopenharmony_ciexample::
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci  pr_info("Info message no. %d\n", msg_num);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ciprints a ``KERN_INFO`` message.
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciBesides being more concise than the equivalent printk() calls, they can use a
9362306a36Sopenharmony_cicommon definition for the format string through the pr_fmt() macro. For
9462306a36Sopenharmony_ciinstance, defining this at the top of a source file (before any ``#include``
9562306a36Sopenharmony_cidirective)::
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci  #define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciwould prefix every pr_*() message in that file with the module and function name
10062306a36Sopenharmony_cithat originated the message.
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciFor debugging purposes there are also two conditionally-compiled macros:
10362306a36Sopenharmony_cipr_debug() and pr_devel(), which are compiled-out unless ``DEBUG`` (or
10462306a36Sopenharmony_cialso ``CONFIG_DYNAMIC_DEBUG`` in the case of pr_debug()) is defined.
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciFunction reference
10862306a36Sopenharmony_ci==================
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci.. kernel-doc:: include/linux/printk.h
11162306a36Sopenharmony_ci   :functions: printk pr_emerg pr_alert pr_crit pr_err pr_warn pr_notice pr_info
11262306a36Sopenharmony_ci      pr_fmt pr_debug pr_devel pr_cont
113