Skip to content

Commit 40d6072

Browse files
committed
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
1 parent 39dd06d commit 40d6072

File tree

10 files changed

+61
-53
lines changed

10 files changed

+61
-53
lines changed

src/basic/lock-util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
/* Include here so consumers have LOCK_{EX,SH,NB} available. */
66
#include <sys/file.h>
77

8+
#include "time-util.h"
9+
810
typedef struct LockFile {
911
int dir_fd;
1012
char *path;

src/basic/log.h

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include "list.h"
1111
#include "macro.h"
12-
#include "ratelimit.h"
1312
#include "stdio-util.h"
1413

1514
/* Some structures we reference but don't want to pull in headers for */
@@ -403,58 +402,6 @@ int log_syntax_parse_error_internal(
403402

404403
void log_setup(void);
405404

406-
typedef struct LogRateLimit {
407-
int error;
408-
int level;
409-
RateLimit ratelimit;
410-
} LogRateLimit;
411-
412-
#define log_ratelimit_internal(_level, _error, _ratelimit, _file, _line, _func, _format, ...) \
413-
({ \
414-
int _log_ratelimit_error = (_error); \
415-
int _log_ratelimit_level = (_level); \
416-
static LogRateLimit _log_ratelimit = { \
417-
.ratelimit = (_ratelimit), \
418-
}; \
419-
unsigned _num_dropped_errors = ratelimit_num_dropped(&_log_ratelimit.ratelimit); \
420-
if (_log_ratelimit_error != _log_ratelimit.error || _log_ratelimit_level != _log_ratelimit.level) { \
421-
ratelimit_reset(&_log_ratelimit.ratelimit); \
422-
_log_ratelimit.error = _log_ratelimit_error; \
423-
_log_ratelimit.level = _log_ratelimit_level; \
424-
} \
425-
if (log_get_max_level() == LOG_DEBUG || ratelimit_below(&_log_ratelimit.ratelimit)) \
426-
_log_ratelimit_error = _num_dropped_errors > 0 \
427-
? log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format " (Dropped %u similar message(s))", ##__VA_ARGS__, _num_dropped_errors) \
428-
: log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format, ##__VA_ARGS__); \
429-
_log_ratelimit_error; \
430-
})
431-
432-
#define log_ratelimit_full_errno(level, error, _ratelimit, format, ...) \
433-
({ \
434-
int _level = (level), _e = (error); \
435-
_e = (log_get_max_level() >= LOG_PRI(_level)) \
436-
? log_ratelimit_internal(_level, _e, _ratelimit, PROJECT_FILE, __LINE__, __func__, format, ##__VA_ARGS__) \
437-
: -ERRNO_VALUE(_e); \
438-
_e < 0 ? _e : -ESTRPIPE; \
439-
})
440-
441-
#define log_ratelimit_full(level, _ratelimit, format, ...) \
442-
log_ratelimit_full_errno(level, 0, _ratelimit, format, ##__VA_ARGS__)
443-
444-
/* Normal logging */
445-
#define log_ratelimit_info(...) log_ratelimit_full(LOG_INFO, __VA_ARGS__)
446-
#define log_ratelimit_notice(...) log_ratelimit_full(LOG_NOTICE, __VA_ARGS__)
447-
#define log_ratelimit_warning(...) log_ratelimit_full(LOG_WARNING, __VA_ARGS__)
448-
#define log_ratelimit_error(...) log_ratelimit_full(LOG_ERR, __VA_ARGS__)
449-
#define log_ratelimit_emergency(...) log_ratelimit_full(log_emergency_level(), __VA_ARGS__)
450-
451-
/* Logging triggered by an errno-like error */
452-
#define log_ratelimit_info_errno(error, ...) log_ratelimit_full_errno(LOG_INFO, error, __VA_ARGS__)
453-
#define log_ratelimit_notice_errno(error, ...) log_ratelimit_full_errno(LOG_NOTICE, error, __VA_ARGS__)
454-
#define log_ratelimit_warning_errno(error, ...) log_ratelimit_full_errno(LOG_WARNING, error, __VA_ARGS__)
455-
#define log_ratelimit_error_errno(error, ...) log_ratelimit_full_errno(LOG_ERR, error, __VA_ARGS__)
456-
#define log_ratelimit_emergency_errno(error, ...) log_ratelimit_full_errno(log_emergency_level(), error, __VA_ARGS__)
457-
458405
const char* _log_set_prefix(const char *prefix, bool force);
459406
static inline const char* _log_unset_prefixp(const char **p) {
460407
assert(p);

src/basic/ratelimit.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,55 @@ unsigned ratelimit_num_dropped(const RateLimit *rl);
2828

2929
usec_t ratelimit_end(const RateLimit *rl);
3030
usec_t ratelimit_left(const RateLimit *rl);
31+
32+
typedef struct LogRateLimit {
33+
int error;
34+
int level;
35+
RateLimit ratelimit;
36+
} LogRateLimit;
37+
38+
#define log_ratelimit_internal(_level, _error, _ratelimit, _file, _line, _func, _format, ...) \
39+
({ \
40+
int _log_ratelimit_error = (_error); \
41+
int _log_ratelimit_level = (_level); \
42+
static LogRateLimit _log_ratelimit = { \
43+
.ratelimit = (_ratelimit), \
44+
}; \
45+
unsigned _num_dropped_errors = ratelimit_num_dropped(&_log_ratelimit.ratelimit); \
46+
if (_log_ratelimit_error != _log_ratelimit.error || _log_ratelimit_level != _log_ratelimit.level) { \
47+
ratelimit_reset(&_log_ratelimit.ratelimit); \
48+
_log_ratelimit.error = _log_ratelimit_error; \
49+
_log_ratelimit.level = _log_ratelimit_level; \
50+
} \
51+
if (log_get_max_level() == LOG_DEBUG || ratelimit_below(&_log_ratelimit.ratelimit)) \
52+
_log_ratelimit_error = _num_dropped_errors > 0 \
53+
? log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format " (Dropped %u similar message(s))", ##__VA_ARGS__, _num_dropped_errors) \
54+
: log_internal(_log_ratelimit_level, _log_ratelimit_error, _file, _line, _func, _format, ##__VA_ARGS__); \
55+
_log_ratelimit_error; \
56+
})
57+
58+
#define log_ratelimit_full_errno(level, error, _ratelimit, format, ...) \
59+
({ \
60+
int _level = (level), _e = (error); \
61+
_e = (log_get_max_level() >= LOG_PRI(_level)) \
62+
? log_ratelimit_internal(_level, _e, _ratelimit, PROJECT_FILE, __LINE__, __func__, format, ##__VA_ARGS__) \
63+
: -ERRNO_VALUE(_e); \
64+
_e < 0 ? _e : -ESTRPIPE; \
65+
})
66+
67+
#define log_ratelimit_full(level, _ratelimit, format, ...) \
68+
log_ratelimit_full_errno(level, 0, _ratelimit, format, ##__VA_ARGS__)
69+
70+
/* Normal logging */
71+
#define log_ratelimit_info(...) log_ratelimit_full(LOG_INFO, __VA_ARGS__)
72+
#define log_ratelimit_notice(...) log_ratelimit_full(LOG_NOTICE, __VA_ARGS__)
73+
#define log_ratelimit_warning(...) log_ratelimit_full(LOG_WARNING, __VA_ARGS__)
74+
#define log_ratelimit_error(...) log_ratelimit_full(LOG_ERR, __VA_ARGS__)
75+
#define log_ratelimit_emergency(...) log_ratelimit_full(log_emergency_level(), __VA_ARGS__)
76+
77+
/* Logging triggered by an errno-like error */
78+
#define log_ratelimit_info_errno(error, ...) log_ratelimit_full_errno(LOG_INFO, error, __VA_ARGS__)
79+
#define log_ratelimit_notice_errno(error, ...) log_ratelimit_full_errno(LOG_NOTICE, error, __VA_ARGS__)
80+
#define log_ratelimit_warning_errno(error, ...) log_ratelimit_full_errno(LOG_WARNING, error, __VA_ARGS__)
81+
#define log_ratelimit_error_errno(error, ...) log_ratelimit_full_errno(LOG_ERR, error, __VA_ARGS__)
82+
#define log_ratelimit_emergency_errno(error, ...) log_ratelimit_full_errno(log_emergency_level(), error, __VA_ARGS__)

src/core/clock-warp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "clock-util.h"
99
#include "clock-warp.h"
1010
#include "errno-util.h"
11+
#include "time-util.h"
1112

1213
int clock_reset_timewarp(void) {
1314
static const struct timezone tz = {

src/core/execute.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct Manager Manager;
2828
#include "open-file.h"
2929
#include "ordered-set.h"
3030
#include "path-util.h"
31+
#include "ratelimit.h"
3132
#include "rlimit-util.h"
3233
#include "runtime-scope.h"
3334
#include "set.h"

src/libsystemd/sd-journal/journal-file.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "path-util.h"
3333
#include "prioq.h"
3434
#include "random-util.h"
35+
#include "ratelimit.h"
3536
#include "set.h"
3637
#include "sort-util.h"
3738
#include "stat-util.h"

src/libsystemd/sd-journal/journal-vacuum.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "journal-file.h"
1616
#include "journal-internal.h"
1717
#include "journal-vacuum.h"
18+
#include "ratelimit.h"
1819
#include "sort-util.h"
1920
#include "string-util.h"
2021
#include "time-util.h"

src/libsystemd/sd-json/sd-json.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "memstream-util.h"
3232
#include "path-util.h"
3333
#include "process-util.h"
34+
#include "ratelimit.h"
3435
#include "set.h"
3536
#include "signal-util.h"
3637
#include "string-table.h"

src/shared/watchdog.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "fileio.h"
1616
#include "log.h"
1717
#include "path-util.h"
18+
#include "ratelimit.h"
1819
#include "string-util.h"
1920
#include "time-util.h"
2021
#include "watchdog.h"

src/udev/scsi_id/scsi_serial.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "scsi.h"
2727
#include "scsi_id.h"
2828
#include "string-util.h"
29+
#include "time-util.h"
2930

3031
/*
3132
* A priority based list of id, naa, and binary/ascii for the identifier

0 commit comments

Comments
 (0)