summaryrefslogtreecommitdiff
path: root/src/basic/log.h
Commit message (Collapse)AuthorAgeFilesLines
* log: Add LOG_SET_PREFIX() macroDaan De Meyer2023-03-291-0/+10
| | | | | | | | | | | | LOG_SET_PREFIX() sets a logging prefix for the current block. The prefix is prepended to every logging message in the block, followed by ": ". If a logging prefix is already configured, it is overridden for the duration of the block, after which it is restored. A use case for this macro is when we're operating on an image or directory (using --root or --image). We can use LOG_SET_PREFIX() to prefix all logging messages with the directory or image that we're operating on.
* Merge pull request #26916 from DaanDeMeyer/log-context-refDaan De Meyer2023-03-221-16/+21
|\ | | | | log: Avoid pushing the same fields more than once on the log context
| * log: Add key/value support to the log contextDaan De Meyer2023-03-211-9/+16
| | | | | | | | | | | | | | | | Now that we have reference counting, it's useful to be able to push single key values onto the log context separately, so that we don't have to allocate new storage to join the separate string together into a single field which means we won't be able to reuse a context containing the same field.
| * log: Avoid pushing the same fields more than once on the log contextDaan De Meyer2023-03-211-12/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | Let's try to optimize against pushing the same fields multiple times onto the log context. To achieve this we make the log context reference counted and return an existing context object if it's using the same fields. A consequence of this is that we have to make sure attaching/detaching is coupled to the lifetime of the context object, so we make the attach and detach functions private for now. If we need independent attach/detach in the future, we can make that work with some extra complexity but since we don't need it yet, let's not support it for now.
* | core: Settle log target if we're going to be closing all fdsDaan De Meyer2023-03-221-0/+1
|/ | | | | | | | | | | Whenever we're going to close all file descriptors, we tend to close the log and set it into open when needed mode. When this is done with the logging target set to LOG_TARGET_AUTO, we run into issues because for every logging call, we'll check if stderr is connected to the journal to determine where to send the logging message. This check obviously stops working when we close stderr, so we settle the log target before we do that so that we keep using the same logging target even after stderr is closed.
* log: add iov helpers for log_contextLuca Boccassi2023-03-141-3/+18
|
* log: add common helper log_set_target_and_open()Lennart Poettering2023-02-161-0/+1
| | | | | quite often we want to set a log target and immediately open it. Add a common helper for that.
* build: add some coloring to --version outputLennart Poettering2023-01-231-0/+1
| | | | Make it easier to discern enabled and disabled build options.
* basic: Add log contextDaan De Meyer2023-01-081-0/+82
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit adds support for attaching extra metadata to log messages written to the journal via log.h. We keep track of a thread local log context in log.c onto which we can push extra metadata fields that should be logged. Once a field is no longer relevant, it can be popped again from the log context. On top of this, we then add macros to allow pushing extra fields onto the log context. LOG_CONTEXT_PUSH() will push the provided field onto the log context and pop the last field from the log context when the current block ends. LOG_CONTEXT_PUSH_STRV() will do the same but for all fields in the given strv. Using the macros is as simple as putting them anywhere inside a block to add a field to all following log messages logged from inside that block. void myfunction(...) { ... LOG_CONTEXT_PUSH("MYMETADATA=abc"); // Every journal message logged will now have the MYMETADATA=abc // field included. } For convenience, there's also LOG_CONTEXT_PUSHF() to allow constructing the field to be logged using printf() syntax. log_context_new()/log_context_free() can be used to attach a log context to an async operation by storing it in the associated userdata struct.
* log: Remove log_ratelimit_debug() macrosDaan De Meyer2022-11-171-2/+0
| | | | | These don't make sense anymore since we disable log ratelimiting when logging at debug level.
* log: Disable log rate limiting if debug logging is enabledDaan De Meyer2022-11-161-1/+1
| | | | When debugging, it makes sense to not do any rate limiting at all.
* log: Add shorthands for log_ratelimit_full()Daan De Meyer2022-11-101-0/+16
|
* log: Add ratelimit argument to ratelimit log macrosDaan De Meyer2022-11-101-9/+6
| | | | Allows configuring the ratelimit per logging statement.
* log: Add log_ratelimit_full()Daan De Meyer2022-11-101-0/+3
|
* log: Use ##__VA_ARGS__ in ratelimit log macrosDaan De Meyer2022-11-101-3/+3
| | | | This allows us to call these without formatting arguments.
* journald: use rate-limited logging to log 'Failed to write entry' messageRichard Phibel2022-08-241-0/+39
|
* meson: turn on log-message-verification by default in developer buildsZbigniew Jędrzejewski-Szmek2022-05-111-3/+4
| | | | | I'm not _quite_ convinced that this a good idea… I'm at least keeping it separate to make it easy to revert ;)
* Do LOG_MESSAGE_VERIFICATION in coverity runsZbigniew Jędrzejewski-Szmek2022-05-111-1/+1
| | | | | This should be enough to get reports if we screw up anywhere, coverity does analysis of printf format strings.
* Optionally call printf on LOG_MESSAGE() argumentsZbigniew Jędrzejewski-Szmek2022-05-111-1/+7
| | | | | | | | | With an intentional mistake: ../src/login/logind-dbus.c: In function ‘bus_manager_log_shutdown’: ../src/login/logind-dbus.c:1542:39: error: format ‘%s’ expects a matching ‘char *’ argument [-Werror=format=] 1542 | LOG_MESSAGE("%s %s", message), | ^~~~~~~
* tree-wide: use LOG_MESSAGE() where possibleZbigniew Jędrzejewski-Szmek2022-05-111-1/+2
| | | | | | | Also break some long lines for more uniform formatting. No functional change. I went over all log_struct, log_struct_errno, log_unit_struct, log_unit_struct_errno calls, and they seem fine.
* log: introduce log_trace_errno()Yu Watanabe2022-01-281-2/+4
|
* nss: only read logging config from environment variablesZbigniew Jędrzejewski-Szmek2022-01-111-0/+1
| | | | | | | | | | | | | log_parse_environment() uses should_parse_proc_cmdline() to determine whether it should parse settings from the kernel command line. But the checks that should_parse_proc_cmdline() apply to the whole process, and we could get a positive answer also when log_parse_environment() was called from one of the nss modules. In case of nss-modules, we don't want to look at the kernel command line. log_parse_environment_variables() that only looks at the environment variables is split out and used in the nss modules. Fixes #22020.
* basic/log: allow errno values higher than 255Zbigniew Jędrzejewski-Szmek2022-01-031-2/+2
| | | | | | | | | | | | | | | | When the support for "synthetic errno" was added, we started truncating the errno value to just the least significant byte. This is generally OK, because errno values are defined up to ~130. The docs don't really say what the maximum value is. But at least in principle higher values could be added in the future. So let's stop truncating the values needlessly. The kernel (or libbpf?) have an error where they return 524 as an errno value (https://bugzilla.redhat.com/show_bug.cgi?id=2036145). We would confusingly truncate this to 12 (ENOMEM). It seems much nicer to let strerror() give us "Unknown error 524" rather than to print the bogus message about ENOMEM.
* journal: Don't discard kmsg messages coming from journald itselfDaan De Meyer2021-11-181-0/+4
| | | | | | | | | | | | | | | | | | Previously, we discarded any kmsg messages coming from journald itself to avoid infinite loops where potentially the processing of a kmsg message causes journald to log one or more messages to kmsg which then get read again by the kmsg handler, ... However, if we completely disable logging whenever we're processing a kmsg message coming from journald itself, we also prevent any infinite loops as we can be sure that journald won't accidentally generate logging messages while processing a kmsg log message. This change allows us to store all journald logs generated during the processing of log messages from other services in the system journal. Previously these could only be found in kmsg which has low retention, can't be queried using journalctl and whose logs don't survive reboots.
* systemd-analyze: option to exit with an error when 'verify' failsMaanya Goenka2021-08-121-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The commit introduces a callback invoked from log_syntax_internal. Use it from systemd-analyze to gather a list of units that contain syntax warnings. A new command line option is added to make use of this. The new option --recursive-errors takes in three possible modes: 1. yes - which is the default. systemd-analyze exits with an error when syntax warnings arise during verification of the specified units or any of their dependencies. 3. no - systemd-analyze exits with an error when syntax warnings arise during verification of only the selected unit. Analyzing and loading any dependencies will be skipped. 4. one - systemd-analyze exits with an error when syntax warnings arise during verification of only the selected units and their direct dependencies. Below are two service unit files that I created for the purposes of testing: 1. First, we run the commands on a unit that does not have dependencies but has a non-existing key-value setting (i.e. foo = bar). > cat <<EOF>testcase.service [Unit] foo = bar [Service] ExecStart = echo hello EOF OUTPUT: maanya-goenka@debian:~/systemd (log-error)$ sudo build/systemd-analyze verify testcase.service /home/maanya-goenka/systemd/testcase.service:2: Unknown key name 'foo' in section 'Unit', ignoring. /usr/lib/systemd/system/plymouth-start.service:15: Unit configured to use KillMode=none. This is unsafe, as it disables systemd's process lifecycle management for the service. Please update your service to use a safer KillMode=, such as 'mixed' or 'control-group'. Support for KillMode=none is deprecated and will eventually be removed. /usr/lib/systemd/system/dbus.socket:5: ListenStream= references a path below legacy directory /var/run/, updating /var/run/dbus/system_bus_socket → /run/dbus/system_bus_socket; please update the unit file accordingly. /usr/lib/systemd/system/gdm.service:30: Standard output type syslog is obsolete, automatically updating to journal. Please update your unit file, and consider removing the setting altogether. maanya-goenka@debian:~/systemd (log-error)$ echo $? 1 maanya-goenka@debian:~/systemd (log-error)$ sudo build/systemd-analyze verify --recursive-errors=yes testcase.service /home/maanya-goenka/systemd/testcase.service:2: Unknown key name 'foo' in section 'Unit', ignoring. /usr/lib/systemd/system/plymouth-start.service:15: Unit configured to use KillMode=none. This is unsafe, as it disables systemd's process lifecycle management for the service. Please update your service to use a safer KillMode=, such as 'mixed' or 'control-group'. Support for KillMode=none is deprecated and will eventually be removed. /usr/lib/systemd/system/dbus.socket:5: ListenStream= references a path below legacy directory /var/run/, updating /var/run/dbus/system_bus_socket → /run/dbus/system_bus_socket; please update the unit file accordingly. /usr/lib/systemd/system/gdm.service:30: Standard output type syslog is obsolete, automatically updating to journal. Please update your unit file, and consider removing the setting altogether. maanya-goenka@debian:~/systemd (log-error)$ echo $? 1 maanya-goenka@debian:~/systemd (log-error)$ sudo build/systemd-analyze verify --recursive-errors=no testcase.service /home/maanya-goenka/systemd/testcase.service:2: Unknown key name 'foo' in section 'Unit', ignoring. maanya-goenka@debian:~/systemd (log-error)$ echo $? 1 maanya-goenka@debian:~/systemd (log-error)$ sudo build/systemd-analyze verify --recursive-errors=one testcase.service /home/maanya-goenka/systemd/testcase.service:2: Unknown key name 'foo' in section 'Unit', ignoring. /usr/lib/systemd/system/plymouth-start.service:15: Unit configured to use KillMode=none. This is unsafe, as it disables systemd's process lifecycle management for the service. Please update your service to use a safer KillMode=, such as 'mixed' or 'control-group'. Support for KillMode=none is deprecated and will eventually be removed. /usr/lib/systemd/system/dbus.socket:5: ListenStream= references a path below legacy directory /var/run/, updating /var/run/dbus/system_bus_socket → /run/dbus/system_bus_socket; please update the unit file accordingly. /usr/lib/systemd/system/gdm.service:30: Standard output type syslog is obsolete, automatically updating to journal. Please update your unit file, and consider removing the setting altogether. maanya-goenka@debian:~/systemd (log-error)$ echo $? 1 2. Next, we run the commands on a unit that is syntactically valid but has a non-existing dependency (i.e. foo2.service) > cat <<EOF>foobar.service [Unit] Requires = foo2.service [Service] ExecStart = echo hello EOF OUTPUT: maanya-goenka@debian:~/systemd (log-error)$ sudo build/systemd-analyze verify foobar.service /usr/lib/systemd/system/plymouth-start.service:15: Unit configured to use KillMode=none. This is unsafe, as it disables systemd's process lifecycle management for the service. Please update your service to use a safer KillMode=, such as 'mixed' or 'control-group'. Support for KillMode=none is deprecated and will eventually be removed. /usr/lib/systemd/system/dbus.socket:5: ListenStream= references a path below legacy directory /var/run/, updating /var/run/dbus/system_bus_socket → /run/dbus/system_bus_socket; please update the unit file accordingly. /usr/lib/systemd/system/gdm.service:30: Standard output type syslog is obsolete, automatically updating to journal. Please update your unit file, and consider removing the setting altogether. foobar.service: Failed to create foobar.service/start: Unit foo2.service not found. maanya-goenka@debian:~/systemd (log-error)$ echo $? 1 maanya-goenka@debian:~/systemd (log-error)$ sudo build/systemd-analyze verify --recursive-errors=yes foobar.service /usr/lib/systemd/system/plymouth-start.service:15: Unit configured to use KillMode=none. This is unsafe, as it disables systemd's process lifecycle management for the service. Please update your service to use a safer KillMode=, such as 'mixed' or 'control-group'. Support for KillMode=none is deprecated and will eventually be removed. /usr/lib/systemd/system/dbus.socket:5: ListenStream= references a path below legacy directory /var/run/, updating /var/run/dbus/system_bus_socket → /run/dbus/system_bus_socket; please update the unit file accordingly. /usr/lib/systemd/system/gdm.service:30: Standard output type syslog is obsolete, automatically updating to journal. Please update your unit file, and consider removing the setting altogether. foobar.service: Failed to create foobar.service/start: Unit foo2.service not found. maanya-goenka@debian:~/systemd (log-error)$ echo $? 1 maanya-goenka@debian:~/systemd (log-error)$ sudo build/systemd-analyze verify --recursive-errors=no foobar.service maanya-goenka@debian:~/systemd (log-error)$ echo $? 0 maanya-goenka@debian:~/systemd (log-error)$ sudo build/systemd-analyze verify --recursive-errors=one foobar.service /usr/lib/systemd/system/plymouth-start.service:15: Unit configured to use KillMode=none. This is unsafe, as it disables systemd's process lifecycle management for the service. Please update your service to use a safer KillMode=, such as 'mixed' or 'control-group'. Support for KillMode=none is deprecated and will eventually be removed. /usr/lib/systemd/system/dbus.socket:5: ListenStream= references a path below legacy directory /var/run/, updating /var/run/dbus/system_bus_socket → /run/dbus/system_bus_socket; please update the unit file accordingly. /usr/lib/systemd/system/gdm.service:30: Standard output type syslog is obsolete, automatically updating to journal. Please update your unit file, and consider removing the setting altogether. foobar.service: Failed to create foobar.service/start: Unit foo2.service not found. maanya-goenka@debian:~/systemd (log-error)$ echo $? 1
* Drop the text argument from assert_not_reached()Zbigniew Jędrzejewski-Szmek2021-08-031-1/+0
| | | | | | | | | | | | | | | | | In general we almost never hit those asserts in production code, so users see them very rarely, if ever. But either way, we just need something that users can pass to the developers. We have quite a few of those asserts, and some have fairly nice messages, but many are like "WTF?" or "???" or "unexpected something". The error that is printed includes the file location, and function name. In almost all functions there's at most one assert, so the function name alone is enough to identify the failure for a developer. So we don't get much extra from the message, and we might just as well drop them. Dropping them makes our code a tiny bit smaller, and most importantly, improves development experience by making it easy to insert such an assert in the code without thinking how to phrase the argument.
* log: add log_once() and log_once_errno() macrosDan Streetman2021-05-201-0/+23
| | | | | | These macros will log a message at the specified level only the first time they are called. On all later calls, if the specified level is debug, the logs will be suppressed; otherwise the message will be logged at debug.
* basic/log: change error for unset errno to ESTRPIPEZbigniew Jędrzejewski-Szmek2021-04-191-1/+1
| | | | | | | | EIO is used in other contexts, so change ot something otherwise unused. ENOTRECOVERABLE was also suggested, but it's used in various other places in the code. https://github.com/systemd/systemd/pull/19317#discussion_r614887661
* Do not try to return 0 from log_debug()Zbigniew Jędrzejewski-Szmek2021-04-141-1/+1
| | | | | | | | | As @yuwata correctly points out, this became broken when log_debug() started returning -EIO. I wanted to preserve this pattern, but it turns out it is not very widely used, and preserving it would make the whole thing, already quite complicated, even more complex. log_debug() is made like log_info() and friends, and returns void.
* basic/log: assert that 0 is not passed as errno, except in test codeZbigniew Jędrzejewski-Szmek2021-04-141-3/+16
| | | | | | | | | | | | | | | | | | | | Let's assert if we ever happen to pass 0 to one of the log functions. With the preceding commit to return -EIO from log_*(), passing 0 wouldn't affect the return value any more, but it is still most likely an error. The unit test code is an exception: we fairly often pass the return value to print it, before checking what it is. So let's assert that we're not passing 0 in non-test code. As with the previous check for %m, this is only done in developer mode. We are depending on external code setting errno correctly for us, which might not always be true, and which we can't test, so we shouldn't assert, but just handle this gracefully. I did a bunch of greps to try to figure out if there are any places where we're passing 0 on purpose, and couldn't find any. The one place that failed in tests is adjusted. About "zerook" in the name: I wanted the suffix to be unambiguous. It's a single "word" because each of the words in log_full_errno is also meaningful, and having one term use two words would be confusing.
* basic/log: assert that %m is not used when error is not setZbigniew Jędrzejewski-Szmek2021-04-141-1/+7
| | | | | | This is only done in developer mode. It is a pretty rare occurence that we make this kind of mistake. And even if it happens, the result is just a misleading error message. So let's only do the check in non-release builds.
* basic/log: force log_*_errno() to return negativeZbigniew Jędrzejewski-Szmek2021-04-141-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This silences some warnigns where gcc thinks that some variables are unitialized. One particular case: ../src/journal/journald-server.c: In function 'ache_space_refresh': ../src/journal/journald-server.c:136:28: error: 'vfs_avail' may be used uninitialized in this function [-Werror=maybe-uninitialized] 136 | uint64_t vfs_used, vfs_avail, avail; | ^~~~~~~~~ ../src/journal/journald-server.c:136:18: error: 'vfs_used' may be used uninitialized in this function [-Werror=maybe-uninitialized] 136 | uint64_t vfs_used, vfs_avail, avail; | ^~~~~~~~ cc1: all warnings being treated as errors which is caused by d = opendir(path); if (!d) return log_full_errno(errno == ENOENT ? LOG_DEBUG : LOG_ERR, errno, "Failed to open %s: %m", path); if (fstatvfs(dirfd(d), &ss) < 0) return log_error_errno(errno, "Failed to fstatvfs(%s): %m", path); For some reason on aarch64 gcc thinks we might return non-negative here. In principle errno must be set in both cases, but it's hard to say for certain. So let's make sure that our code flow is correct, even if somebody forgot to set the global variable somewhere.
* basic/log: fix log_trace()Zbigniew Jędrzejewski-Szmek2021-03-261-1/+1
| | | | log_trace() was always on. It's supposed to be opt-in.
* tree-wide: use -EINVAL for enum invalid valuesZbigniew Jędrzejewski-Szmek2021-02-101-1/+1
| | | | | | | | | As suggested in https://github.com/systemd/systemd/pull/11484#issuecomment-775288617. This does not touch anything exposed in src/systemd. Changing the defines there would be a compatibility break. Note that tests are broken after this commit. They will be fixed in the next one.
* tree-wide: enable colorized logging for daemons when run in consoleYu Watanabe2021-02-011-2/+1
| | | | It may be useful when debugging daemons.
* log: make tools also read the kernel command line when run as a serviceYu Watanabe2021-02-011-1/+0
| | | | | | | | This effectively reverts 41d1f469cf10f5f3e9cb4f4853ace9b0cfe5beae. Before this, e.g., `networkctl reload` invoked by `systemctl reload systemd-networkd.service` does not produce debugging logs even if systemd.log-level=debug is set. This fixes the issue.
* log: drop unused LogRealmYu Watanabe2021-01-251-71/+22
| | | | | Already no binary is built with LOG_REALM= argument. Hence, we can safely drop LogRealm now.
* log: drop unused log_full_errno_realm() macroYu Watanabe2021-01-251-8/+4
|
* basic/log: add debug-level log_oom() variantZbigniew Jędrzejewski-Szmek2020-12-111-2/+3
| | | | This is useful for contexts where only debug-level messages are allowed.
* log: add helper set sets log level in all realmsLennart Poettering2020-12-091-0/+7
| | | | Prompted-by: #17903
* license: LGPL-2.1+ -> LGPL-2.1-or-laterYu Watanabe2020-11-091-1/+1
|
* log: update commentLennart Poettering2020-09-301-1/+1
| | | | | The logic was changed in bc694c06e60505efeb09e5278a7b22cdfa23975e, let's update the comment accordingly.
* log: optionally display thread ID in log output to ttyLennart Poettering2020-09-291-0/+3
| | | | | This is very useful when playing around with threaded tests, but should be useful in other cases too.
* basic/log: make log_{info,warning,...} return voidZbigniew Jędrzejewski-Szmek2020-09-081-2/+2
| | | | | | | | | | log_debug still returns 0. I think it is legitimate to use 'return log_debug()' to return 0. It is different than the other functions, since we often want to supress errors logged at debug level. This case is quite common in the codebase and we could use 'return log_debug_errno()' to make the code more consise. For all other variants, a separate return line is required. Previous commit changes all the non-conforming instances, now we can make it mandatory.
* log: introduce log_parse_environment_cli() and log_setup_cli()Filipe Brandenburger2020-06-241-0/+4
| | | | | | | | | | | | | | | | Presently, CLI utilities such as systemctl will check whether they have a tty attached or not to decide whether to parse /proc/cmdline or EFI variable SystemdOptions looking for systemd.log_* entries. But this check will be misleading if these tools are being launched by a daemon, such as a monitoring daemon or automation service that runs in background. Make log handling of CLI tools uniform by never checking /proc/cmdline or EFI variables to determine the logging level. Furthermore, introduce a new log_setup_cli() shortcut to set up common options used by most command-line utilities.
* Merge pull request #13084 from ddstreet/log_timeLennart Poettering2020-03-301-0/+3
|\ | | | | log: add support for prefixing console log messages with current timestamp
| * log: add support for prefixing console log messages with current timestampDan Streetman2020-02-101-0/+3
| |
* | Silence Wstring-plus-int warning when using clangd with GCC.Daan De Meyer2020-03-211-1/+1
|/
* Drop trailing slash from assert_cc() definitionZbigniew Jędrzejewski-Szmek2019-07-171-1/+1
| | | | | | We use assert_cc(...); almost everywhere. Let's always require that. https://github.com/systemd/systemd/issues/12997#issuecomment-510103988
* tree-wide: use PROJECT_FILE instead of __FILE__Zbigniew Jędrzejewski-Szmek2019-07-041-8/+8
| | | | This replaces the internal uses of __FILE__ with the new macro.