]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
log: add log_once() and log_once_errno() macros
authorDan Streetman <ddstreet@canonical.com>
Wed, 19 May 2021 14:22:21 +0000 (10:22 -0400)
committerDan Streetman <ddstreet@canonical.com>
Thu, 20 May 2021 19:39:15 +0000 (15:39 -0400)
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.

src/basic/log.h

index 51ba3d8fde788e99b759c2b50d3e1adeda87551c..738c181070337cf20b3eff5d20cf8879b989afe9 100644 (file)
@@ -236,6 +236,29 @@ int log_emergency_level(void);
 #define log_error_errno(error, ...)     log_full_errno(LOG_ERR,     error, __VA_ARGS__)
 #define log_emergency_errno(error, ...) log_full_errno(log_emergency_level(), error, __VA_ARGS__)
 
+/* This logs at the specified level the first time it is called, and then
+ * logs at debug. If the specified level is debug, this logs only the first
+ * time it is called. */
+#define log_once(level, ...)                                             \
+        ({                                                               \
+                if (ONCE)                                                \
+                        log_full(level, __VA_ARGS__);                    \
+                else if (LOG_PRI(level) != LOG_DEBUG)                    \
+                        log_debug(__VA_ARGS__);                          \
+        })
+
+#define log_once_errno(level, error, ...)                                \
+        ({                                                               \
+                int _err = (error);                                      \
+                if (ONCE)                                                \
+                        _err = log_full_errno(level, _err, __VA_ARGS__); \
+                else if (LOG_PRI(level) != LOG_DEBUG)                    \
+                        _err = log_debug_errno(_err, __VA_ARGS__);       \
+                else                                                     \
+                        _err = -ERRNO_VALUE(_err);                       \
+                _err;                                                    \
+        })
+
 #if LOG_TRACE
 #  define log_trace(...) log_debug(__VA_ARGS__)
 #else