#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
+#include <string.h>
#include <syslog.h>
#include "macro.h"
log_dispatch_internal(level, error, PROJECT_FILE, __LINE__, __func__, NULL, NULL, NULL, NULL, buffer)
/* Logging with level */
-#define log_full_errno(level, error, ...) \
+#define log_full_errno_zerook(level, error, ...) \
({ \
int _level = (level), _e = (error); \
- (log_get_max_level() >= LOG_PRI(_level)) \
+ _e = (log_get_max_level() >= LOG_PRI(_level)) \
? log_internal(_level, _e, PROJECT_FILE, __LINE__, __func__, __VA_ARGS__) \
: -ERRNO_VALUE(_e); \
+ _e < 0 ? _e : -ESTRPIPE; \
+ })
+
+#if BUILD_MODE_DEVELOPER && !defined(TEST_CODE)
+# define ASSERT_NON_ZERO(x) assert((x) != 0)
+#else
+# define ASSERT_NON_ZERO(x)
+#endif
+
+#define log_full_errno(level, error, ...) \
+ ({ \
+ int _error = (error); \
+ ASSERT_NON_ZERO(_error); \
+ log_full_errno_zerook(level, _error, __VA_ARGS__); \
})
-#define log_full(level, ...) (void) log_full_errno((level), 0, __VA_ARGS__)
+#define log_full(level, fmt, ...) \
+ ({ \
+ if (BUILD_MODE_DEVELOPER) \
+ assert(!strstr(fmt, "%m")); \
+ (void) log_full_errno_zerook(level, 0, fmt, ##__VA_ARGS__); \
+ })
int log_emergency_level(void);
/* Normal logging */
-#define log_debug(...) log_full_errno(LOG_DEBUG, 0, __VA_ARGS__)
+#define log_debug(...) log_full(LOG_DEBUG, __VA_ARGS__)
#define log_info(...) log_full(LOG_INFO, __VA_ARGS__)
#define log_notice(...) log_full(LOG_NOTICE, __VA_ARGS__)
#define log_warning(...) log_full(LOG_WARNING, __VA_ARGS__)
#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