From: Daan De Meyer Date: Sat, 24 May 2025 17:55:42 +0000 (+0200) Subject: basic: Move ratelimit logging functions to log-ratelimit.h X-Git-Tag: v258-rc1~502 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b416ac0e157b87c2bd9a7badd733c41cfa496d71;p=thirdparty%2Fsystemd.git basic: Move ratelimit logging functions to log-ratelimit.h --- diff --git a/src/basic/log-ratelimit.h b/src/basic/log-ratelimit.h new file mode 100644 index 00000000000..8e4a7fbf155 --- /dev/null +++ b/src/basic/log-ratelimit.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "log.h" +#include "ratelimit.h" + +#define log_ratelimit_internal(_level, _error, _ratelimit, _file, _line, _func, _format, ...) \ +({ \ + int _log_ratelimit_error = (_error); \ + int _log_ratelimit_level = (_level); \ + static LogRateLimit _log_ratelimit = { \ + .ratelimit = (_ratelimit), \ + }; \ + unsigned _num_dropped_errors = ratelimit_num_dropped(&_log_ratelimit.ratelimit); \ + if (_log_ratelimit_error != _log_ratelimit.error || _log_ratelimit_level != _log_ratelimit.level) { \ + ratelimit_reset(&_log_ratelimit.ratelimit); \ + _log_ratelimit.error = _log_ratelimit_error; \ + _log_ratelimit.level = _log_ratelimit_level; \ + } \ + if (log_get_max_level() == LOG_DEBUG || ratelimit_below(&_log_ratelimit.ratelimit)) \ + _log_ratelimit_error = _num_dropped_errors > 0 \ + ? log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format " (Dropped %u similar message(s))", ##__VA_ARGS__, _num_dropped_errors) \ + : log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format, ##__VA_ARGS__); \ + _log_ratelimit_error; \ +}) + +#define log_ratelimit_full_errno(level, error, _ratelimit, format, ...) \ + ({ \ + int _level = (level), _e = (error); \ + _e = (log_get_max_level() >= LOG_PRI(_level)) \ + ? log_ratelimit_internal(_level, _e, _ratelimit, PROJECT_FILE, __LINE__, __func__, format, ##__VA_ARGS__) \ + : -ERRNO_VALUE(_e); \ + _e < 0 ? _e : -ESTRPIPE; \ + }) + +#define log_ratelimit_full(level, _ratelimit, format, ...) \ + log_ratelimit_full_errno(level, 0, _ratelimit, format, ##__VA_ARGS__) + +/* Normal logging */ +#define log_ratelimit_info(...) log_ratelimit_full(LOG_INFO, __VA_ARGS__) +#define log_ratelimit_notice(...) log_ratelimit_full(LOG_NOTICE, __VA_ARGS__) +#define log_ratelimit_warning(...) log_ratelimit_full(LOG_WARNING, __VA_ARGS__) +#define log_ratelimit_error(...) log_ratelimit_full(LOG_ERR, __VA_ARGS__) +#define log_ratelimit_emergency(...) log_ratelimit_full(log_emergency_level(), __VA_ARGS__) + +/* Logging triggered by an errno-like error */ +#define log_ratelimit_info_errno(error, ...) log_ratelimit_full_errno(LOG_INFO, error, __VA_ARGS__) +#define log_ratelimit_notice_errno(error, ...) log_ratelimit_full_errno(LOG_NOTICE, error, __VA_ARGS__) +#define log_ratelimit_warning_errno(error, ...) log_ratelimit_full_errno(LOG_WARNING, error, __VA_ARGS__) +#define log_ratelimit_error_errno(error, ...) log_ratelimit_full_errno(LOG_ERR, error, __VA_ARGS__) +#define log_ratelimit_emergency_errno(error, ...) log_ratelimit_full_errno(log_emergency_level(), error, __VA_ARGS__) diff --git a/src/basic/ratelimit.h b/src/basic/ratelimit.h index fd834263757..ce18354be52 100644 --- a/src/basic/ratelimit.h +++ b/src/basic/ratelimit.h @@ -34,49 +34,3 @@ typedef struct LogRateLimit { int level; RateLimit ratelimit; } LogRateLimit; - -#define log_ratelimit_internal(_level, _error, _ratelimit, _file, _line, _func, _format, ...) \ -({ \ - int _log_ratelimit_error = (_error); \ - int _log_ratelimit_level = (_level); \ - static LogRateLimit _log_ratelimit = { \ - .ratelimit = (_ratelimit), \ - }; \ - unsigned _num_dropped_errors = ratelimit_num_dropped(&_log_ratelimit.ratelimit); \ - if (_log_ratelimit_error != _log_ratelimit.error || _log_ratelimit_level != _log_ratelimit.level) { \ - ratelimit_reset(&_log_ratelimit.ratelimit); \ - _log_ratelimit.error = _log_ratelimit_error; \ - _log_ratelimit.level = _log_ratelimit_level; \ - } \ - if (log_get_max_level() == LOG_DEBUG || ratelimit_below(&_log_ratelimit.ratelimit)) \ - _log_ratelimit_error = _num_dropped_errors > 0 \ - ? log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format " (Dropped %u similar message(s))", ##__VA_ARGS__, _num_dropped_errors) \ - : log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format, ##__VA_ARGS__); \ - _log_ratelimit_error; \ -}) - -#define log_ratelimit_full_errno(level, error, _ratelimit, format, ...) \ - ({ \ - int _level = (level), _e = (error); \ - _e = (log_get_max_level() >= LOG_PRI(_level)) \ - ? log_ratelimit_internal(_level, _e, _ratelimit, PROJECT_FILE, __LINE__, __func__, format, ##__VA_ARGS__) \ - : -ERRNO_VALUE(_e); \ - _e < 0 ? _e : -ESTRPIPE; \ - }) - -#define log_ratelimit_full(level, _ratelimit, format, ...) \ - log_ratelimit_full_errno(level, 0, _ratelimit, format, ##__VA_ARGS__) - -/* Normal logging */ -#define log_ratelimit_info(...) log_ratelimit_full(LOG_INFO, __VA_ARGS__) -#define log_ratelimit_notice(...) log_ratelimit_full(LOG_NOTICE, __VA_ARGS__) -#define log_ratelimit_warning(...) log_ratelimit_full(LOG_WARNING, __VA_ARGS__) -#define log_ratelimit_error(...) log_ratelimit_full(LOG_ERR, __VA_ARGS__) -#define log_ratelimit_emergency(...) log_ratelimit_full(log_emergency_level(), __VA_ARGS__) - -/* Logging triggered by an errno-like error */ -#define log_ratelimit_info_errno(error, ...) log_ratelimit_full_errno(LOG_INFO, error, __VA_ARGS__) -#define log_ratelimit_notice_errno(error, ...) log_ratelimit_full_errno(LOG_NOTICE, error, __VA_ARGS__) -#define log_ratelimit_warning_errno(error, ...) log_ratelimit_full_errno(LOG_WARNING, error, __VA_ARGS__) -#define log_ratelimit_error_errno(error, ...) log_ratelimit_full_errno(LOG_ERR, error, __VA_ARGS__) -#define log_ratelimit_emergency_errno(error, ...) log_ratelimit_full_errno(log_emergency_level(), error, __VA_ARGS__) diff --git a/src/journal/journald-audit.c b/src/journal/journald-audit.c index 41336321b7d..31474830194 100644 --- a/src/journal/journald-audit.c +++ b/src/journal/journald-audit.c @@ -12,6 +12,7 @@ #include "journald-audit.h" #include "journald-manager.h" #include "log.h" +#include "log-ratelimit.h" #include "missing_audit.h" #include "stdio-util.h" #include "string-util.h" diff --git a/src/journal/journald-context.c b/src/journal/journald-context.c index 70c0a2a1eed..258bccf6bdc 100644 --- a/src/journal/journald-context.c +++ b/src/journal/journald-context.c @@ -19,6 +19,7 @@ #include "journald-context.h" #include "journald-manager.h" #include "log.h" +#include "log-ratelimit.h" #include "parse-util.h" #include "path-util.h" #include "pidref.h" diff --git a/src/journal/journald-kmsg.c b/src/journal/journald-kmsg.c index fe69f77e017..f8185baefc1 100644 --- a/src/journal/journald-kmsg.c +++ b/src/journal/journald-kmsg.c @@ -22,6 +22,7 @@ #include "journald-sync.h" #include "journald-syslog.h" #include "log.h" +#include "log-ratelimit.h" #include "parse-util.h" #include "process-util.h" #include "stdio-util.h" diff --git a/src/journal/journald-manager.c b/src/journal/journald-manager.c index 8f239a14f56..55aa8bc3096 100644 --- a/src/journal/journald-manager.c +++ b/src/journal/journald-manager.c @@ -47,6 +47,7 @@ #include "journald-syslog.h" #include "journald-varlink.h" #include "log.h" +#include "log-ratelimit.h" #include "memory-util.h" #include "missing_audit.h" #include "mkdir.h" diff --git a/src/journal/journald-native.c b/src/journal/journald-native.c index 2ae755b7371..9a622d272cf 100644 --- a/src/journal/journald-native.c +++ b/src/journal/journald-native.c @@ -21,6 +21,7 @@ #include "journald-syslog.h" #include "journald-wall.h" #include "log.h" +#include "log-ratelimit.h" #include "memfd-util.h" #include "memory-util.h" #include "parse-util.h" diff --git a/src/journal/journald-stream.c b/src/journal/journald-stream.c index 88a6233bcb7..c7da314118a 100644 --- a/src/journal/journald-stream.c +++ b/src/journal/journald-stream.c @@ -26,6 +26,7 @@ #include "journald-syslog.h" #include "journald-wall.h" #include "log.h" +#include "log-ratelimit.h" #include "mkdir.h" #include "parse-util.h" #include "process-util.h" diff --git a/src/journal/journald-syslog.c b/src/journal/journald-syslog.c index 1aacf2fd19f..8493bc24bd5 100644 --- a/src/journal/journald-syslog.c +++ b/src/journal/journald-syslog.c @@ -19,6 +19,7 @@ #include "journald-syslog.h" #include "journald-wall.h" #include "log.h" +#include "log-ratelimit.h" #include "process-util.h" #include "selinux-util.h" #include "socket-util.h" diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 69453081874..dfc3e8aa913 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -27,6 +27,7 @@ #include "journal-file.h" #include "journal-internal.h" #include "log.h" +#include "log-ratelimit.h" #include "lookup3.h" #include "memory-util.h" #include "missing_fs.h" diff --git a/src/libsystemd/sd-journal/journal-vacuum.c b/src/libsystemd/sd-journal/journal-vacuum.c index e069594f6d3..b50a0179d0b 100644 --- a/src/libsystemd/sd-journal/journal-vacuum.c +++ b/src/libsystemd/sd-journal/journal-vacuum.c @@ -16,6 +16,7 @@ #include "journal-internal.h" #include "journal-vacuum.h" #include "log.h" +#include "log-ratelimit.h" #include "ratelimit.h" #include "sort-util.h" #include "string-util.h"