diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-11-27 19:48:02 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-11-27 22:05:23 +0100 |
commit | 086891e5c119abb9854237fc32e736fe2d67234c (patch) | |
tree | 4fc81d73413d1f1aa351e0de248307180c8893de /src/shared/log.h | |
parent | fb6d9b77a71a5f007392b754bf7d8e06a6bed69a (diff) |
log: add an "error" parameter to all low-level logging calls and intrdouce log_error_errno() as log calls that take error numbers
This change has two benefits:
- The format string %m will now resolve to the specified error (or to
errno if the specified error is 0. This allows getting rid of a ton of
strerror() invocations, a function that is not thread-safe.
- The specified error can be passed to the journal in the ERRNO= field.
Now of course, we just need somebody to convert all cases of this:
log_error("Something happened: %s", strerror(-r));
into thus:
log_error_errno(-r, "Something happened: %m");
Diffstat (limited to 'src/shared/log.h')
-rw-r--r-- | src/shared/log.h | 49 |
1 files changed, 35 insertions, 14 deletions
diff --git a/src/shared/log.h b/src/shared/log.h index 8141e9dbb0..9dd68df500 100644 --- a/src/shared/log.h +++ b/src/shared/log.h @@ -77,44 +77,49 @@ void log_parse_environment(void); int log_meta( int level, + int error, const char*file, int line, const char *func, - const char *format, ...) _printf_(5,6); + const char *format, ...) _printf_(6,7); int log_metav( int level, + int error, const char*file, int line, const char *func, const char *format, - va_list ap) _printf_(5,0); + va_list ap) _printf_(6,0); int log_meta_object( int level, + int error, const char*file, int line, const char *func, const char *object_name, const char *object, - const char *format, ...) _printf_(7,8); + const char *format, ...) _printf_(8,9); int log_metav_object( int level, + int error, const char*file, int line, const char *func, const char *object_name, const char *object, const char *format, - va_list ap) _printf_(7,0); + va_list ap) _printf_(8,0); int log_struct_internal( int level, + int error, const char *file, int line, const char *func, - const char *format, ...) _printf_(5,0) _sentinel_; + const char *format, ...) _printf_(6,0) _sentinel_; int log_oom_internal( const char *file, @@ -124,11 +129,13 @@ int log_oom_internal( /* This modifies the buffer passed! */ int log_dump_internal( int level, + int error, const char*file, int line, const char *func, char *buffer); +/* Logging for various assertions */ noreturn void log_assert_failed( const char *text, const char *file, @@ -147,12 +154,16 @@ void log_assert_failed_return( int line, const char *func); -#define log_full(level, ...) \ -do { \ - if (log_get_max_level() >= (level)) \ - log_meta((level), __FILE__, __LINE__, __func__, __VA_ARGS__); \ -} while (0) +/* Logging with level */ +#define log_full_errno(level, error, ...) \ + do { \ + if (log_get_max_level() >= (level)) \ + log_meta((level), error, __FILE__, __LINE__, __func__, __VA_ARGS__); \ + } while (false) +#define log_full(level, ...) log_full_errno(level, 0, __VA_ARGS__) + +/* Normal logging */ #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__) @@ -160,18 +171,28 @@ do { \ #define log_error(...) log_full(LOG_ERR, __VA_ARGS__) #define log_emergency(...) log_full(getpid() == 1 ? LOG_EMERG : LOG_ERR, __VA_ARGS__) +/* Logging triggered by an errno-like error */ +#define log_debug_errno(error, ...) log_full_errno(LOG_DEBUG, error, __VA_ARGS__) +#define log_info_errno(error, ...) log_full_errno(LOG_INFO, error, __VA_ARGS__) +#define log_notice_errno(error, ...) log_full_errno(LOG_NOTICE, error, __VA_ARGS__) +#define log_warning_errno(error, ...) log_full_errno(LOG_WARNING, error, __VA_ARGS__) +#define log_error_errno(error, ...) log_full_errno(LOG_ERR, error, __VA_ARGS__) +#define log_emergency_errno(error, ...) log_full_errno(getpid() == 1 ? LOG_EMERG : LOG_ERR, __VA_ARGS__) + #ifdef LOG_TRACE # define log_trace(...) log_debug(__VA_ARGS__) #else # define log_trace(...) do {} while(0) #endif -#define log_struct(level, ...) log_struct_internal(level, __FILE__, __LINE__, __func__, __VA_ARGS__) - -#define log_oom() log_oom_internal(__FILE__, __LINE__, __func__) +/* Structured logging */ +#define log_struct(level, ...) log_struct_internal(level, 0, __FILE__, __LINE__, __func__, __VA_ARGS__) +#define log_struct_errno(level, error, ...) log_struct_internal(level, error, __FILE__, __LINE__, __func__, __VA_ARGS__) /* This modifies the buffer passed! */ -#define log_dump(level, buffer) log_dump_internal(level, __FILE__, __LINE__, __func__, buffer) +#define log_dump(level, buffer) log_dump_internal(level, 0, __FILE__, __LINE__, __func__, buffer) + +#define log_oom() log_oom_internal(__FILE__, __LINE__, __func__) bool log_on_console(void) _pure_; |