From: Daan De Meyer Date: Sat, 12 Apr 2025 09:16:49 +0000 (+0200) Subject: basic: Move ratelimit logging functions to ratelimit.h X-Git-Tag: v258-rc1~780^2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=40d60725ec0b479c4d4e8cadcfe26578db75bc64;p=thirdparty%2Fsystemd.git basic: Move ratelimit logging functions to ratelimit.h The ratelimit logging functions are only useful in a few scenarios so let's move them to ratelimit.h instead of keeping them in the generic log.h --- diff --git a/src/basic/lock-util.h b/src/basic/lock-util.h index a67d8b2c931..b327b8e5614 100644 --- a/src/basic/lock-util.h +++ b/src/basic/lock-util.h @@ -5,6 +5,8 @@ /* Include here so consumers have LOCK_{EX,SH,NB} available. */ #include +#include "time-util.h" + typedef struct LockFile { int dir_fd; char *path; diff --git a/src/basic/log.h b/src/basic/log.h index def54c81b13..3687c8e69bf 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -9,7 +9,6 @@ #include "list.h" #include "macro.h" -#include "ratelimit.h" #include "stdio-util.h" /* Some structures we reference but don't want to pull in headers for */ @@ -403,58 +402,6 @@ int log_syntax_parse_error_internal( void log_setup(void); -typedef struct LogRateLimit { - int error; - 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__) - const char* _log_set_prefix(const char *prefix, bool force); static inline const char* _log_unset_prefixp(const char **p) { assert(p); diff --git a/src/basic/ratelimit.h b/src/basic/ratelimit.h index 7801ef4270a..fd834263757 100644 --- a/src/basic/ratelimit.h +++ b/src/basic/ratelimit.h @@ -28,3 +28,55 @@ unsigned ratelimit_num_dropped(const RateLimit *rl); usec_t ratelimit_end(const RateLimit *rl); usec_t ratelimit_left(const RateLimit *rl); + +typedef struct LogRateLimit { + int error; + 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/core/clock-warp.c b/src/core/clock-warp.c index 3620c6942ef..e9a832a8e72 100644 --- a/src/core/clock-warp.c +++ b/src/core/clock-warp.c @@ -8,6 +8,7 @@ #include "clock-util.h" #include "clock-warp.h" #include "errno-util.h" +#include "time-util.h" int clock_reset_timewarp(void) { static const struct timezone tz = { diff --git a/src/core/execute.h b/src/core/execute.h index 78f04d51739..63d5b448def 100644 --- a/src/core/execute.h +++ b/src/core/execute.h @@ -28,6 +28,7 @@ typedef struct Manager Manager; #include "open-file.h" #include "ordered-set.h" #include "path-util.h" +#include "ratelimit.h" #include "rlimit-util.h" #include "runtime-scope.h" #include "set.h" diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index a0e618cf1dc..2f3e48c674c 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -32,6 +32,7 @@ #include "path-util.h" #include "prioq.h" #include "random-util.h" +#include "ratelimit.h" #include "set.h" #include "sort-util.h" #include "stat-util.h" diff --git a/src/libsystemd/sd-journal/journal-vacuum.c b/src/libsystemd/sd-journal/journal-vacuum.c index f7dee86209e..7537f5be49d 100644 --- a/src/libsystemd/sd-journal/journal-vacuum.c +++ b/src/libsystemd/sd-journal/journal-vacuum.c @@ -15,6 +15,7 @@ #include "journal-file.h" #include "journal-internal.h" #include "journal-vacuum.h" +#include "ratelimit.h" #include "sort-util.h" #include "string-util.h" #include "time-util.h" diff --git a/src/libsystemd/sd-json/sd-json.c b/src/libsystemd/sd-json/sd-json.c index 618ce93978c..f330045f5d5 100644 --- a/src/libsystemd/sd-json/sd-json.c +++ b/src/libsystemd/sd-json/sd-json.c @@ -31,6 +31,7 @@ #include "memstream-util.h" #include "path-util.h" #include "process-util.h" +#include "ratelimit.h" #include "set.h" #include "signal-util.h" #include "string-table.h" diff --git a/src/shared/watchdog.c b/src/shared/watchdog.c index 0da142b51ae..a6077d66540 100644 --- a/src/shared/watchdog.c +++ b/src/shared/watchdog.c @@ -15,6 +15,7 @@ #include "fileio.h" #include "log.h" #include "path-util.h" +#include "ratelimit.h" #include "string-util.h" #include "time-util.h" #include "watchdog.h" diff --git a/src/udev/scsi_id/scsi_serial.c b/src/udev/scsi_id/scsi_serial.c index d8655c08c3b..e83523fe251 100644 --- a/src/udev/scsi_id/scsi_serial.c +++ b/src/udev/scsi_id/scsi_serial.c @@ -26,6 +26,7 @@ #include "scsi.h" #include "scsi_id.h" #include "string-util.h" +#include "time-util.h" /* * A priority based list of id, naa, and binary/ascii for the identifier