From: Lennart Poettering Date: Tue, 21 Apr 2015 15:26:04 +0000 (+0200) Subject: log: move log_syntax() into src/shared/log.c, and make it more similar to the other... X-Git-Tag: v220~373 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=158350e86923cb1d878d3bf7d928fb1c30a3da76;p=platform%2Fupstream%2Fsystemd.git log: move log_syntax() into src/shared/log.c, and make it more similar to the other log functions --- diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index aa6a4a6..2c85515 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -34,50 +34,6 @@ #include "path-util.h" #include "sd-messages.h" -int log_syntax_internal( - const char *unit, - int level, - const char *file, - int line, - const char *func, - const char *config_file, - unsigned config_line, - int error, - const char *format, ...) { - - _cleanup_free_ char *msg = NULL; - int r; - va_list ap; - - va_start(ap, format); - r = vasprintf(&msg, format, ap); - va_end(ap); - if (r < 0) - return log_oom(); - - if (unit) - r = log_struct_internal(level, - error, - file, line, func, - getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s", unit, - LOG_MESSAGE_ID(SD_MESSAGE_CONFIG_ERROR), - "CONFIG_FILE=%s", config_file, - "CONFIG_LINE=%u", config_line, - LOG_MESSAGE("[%s:%u] %s", config_file, config_line, msg), - NULL); - else - r = log_struct_internal(level, - error, - file, line, func, - LOG_MESSAGE_ID(SD_MESSAGE_CONFIG_ERROR), - "CONFIG_FILE=%s", config_file, - "CONFIG_LINE=%u", config_line, - LOG_MESSAGE("[%s:%u] %s", config_file, config_line, msg), - NULL); - - return r; -} - int config_item_table_lookup( const void *table, const char *section, diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h index 7a2f855..6152ee3 100644 --- a/src/shared/conf-parser.h +++ b/src/shared/conf-parser.h @@ -119,23 +119,6 @@ int config_parse_mode(const char *unit, const char *filename, unsigned line, con int config_parse_log_facility(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); int config_parse_log_level(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); -int log_syntax_internal( - const char *unit, - int level, - const char *file, - int line, - const char *func, - const char *config_file, - unsigned config_line, - int error, - const char *format, ...) _printf_(9, 10); - -#define log_syntax(unit, level, config_file, config_line, error, ...) \ - log_syntax_internal(unit, level, \ - __FILE__, __LINE__, __func__, \ - config_file, config_line, \ - error, __VA_ARGS__) - #define log_invalid_utf8(unit, level, config_file, config_line, error, rvalue) \ do { \ _cleanup_free_ char *_p = utf8_escape_invalid(rvalue); \ diff --git a/src/shared/log.c b/src/shared/log.c index 85c0605..e8b63a3 100644 --- a/src/shared/log.c +++ b/src/shared/log.c @@ -29,6 +29,7 @@ #include #include +#include "sd-messages.h" #include "log.h" #include "util.h" #include "missing.h" @@ -1064,3 +1065,58 @@ void log_received_signal(int level, const struct signalfd_siginfo *si) { void log_set_upgrade_syslog_to_journal(bool b) { upgrade_syslog_to_journal = b; } + +int log_syntax_internal( + const char *unit, + int level, + const char *config_file, + unsigned config_line, + int error, + const char *file, + int line, + const char *func, + const char *format, ...) { + + PROTECT_ERRNO; + char buffer[LINE_MAX]; + int r; + va_list ap; + + if (error < 0) + error = -error; + + if (_likely_(LOG_PRI(level) > log_max_level)) + return -error; + + if (log_target == LOG_TARGET_NULL) + return -error; + + if (error != 0) + errno = error; + + va_start(ap, format); + vsnprintf(buffer, sizeof(buffer), format, ap); + va_end(ap); + + if (unit) + r = log_struct_internal( + level, error, + file, line, func, + getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s", unit, + LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION), + "CONFIG_FILE=%s", config_file, + "CONFIG_LINE=%u", config_line, + LOG_MESSAGE("[%s:%u] %s", config_file, config_line, buffer), + NULL); + else + r = log_struct_internal( + level, error, + file, line, func, + LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION), + "CONFIG_FILE=%s", config_file, + "CONFIG_LINE=%u", config_line, + LOG_MESSAGE("[%s:%u] %s", config_file, config_line, buffer), + NULL); + + return r; +} diff --git a/src/shared/log.h b/src/shared/log.h index d6061c0..09d2fc7 100644 --- a/src/shared/log.h +++ b/src/shared/log.h @@ -28,8 +28,8 @@ #include #include -#include "macro.h" #include "sd-id128.h" +#include "macro.h" typedef enum LogTarget{ LOG_TARGET_CONSOLE, @@ -209,3 +209,22 @@ LogTarget log_target_from_string(const char *s) _pure_; void log_received_signal(int level, const struct signalfd_siginfo *si); void log_set_upgrade_syslog_to_journal(bool b); + +int log_syntax_internal( + const char *unit, + int level, + const char *config_file, + unsigned config_line, + int error, + const char *file, + int line, + const char *func, + const char *format, ...) _printf_(9, 10); + +#define log_syntax(unit, level, config_file, config_line, error, ...) \ + ({ \ + int _level = (level), _e = (error); \ + (log_get_max_level() >= LOG_PRI(_level)) \ + ? log_syntax_internal(unit, _level, config_file, config_line, _e, __FILE__, __LINE__, __func__, __VA_ARGS__) \ + : -abs(_e); \ + }) diff --git a/src/systemd/sd-messages.h b/src/systemd/sd-messages.h index a8379e0..8aedaec 100644 --- a/src/systemd/sd-messages.h +++ b/src/systemd/sd-messages.h @@ -81,7 +81,7 @@ _SD_BEGIN_DECLARATIONS; #define SD_MESSAGE_SUSPEND_KEY SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,72) #define SD_MESSAGE_HIBERNATE_KEY SD_ID128_MAKE(b7,2e,a4,a2,88,15,45,a0,b5,0e,20,0e,55,b9,b0,73) -#define SD_MESSAGE_CONFIG_ERROR SD_ID128_MAKE(c7,72,d2,4e,9a,88,4c,be,b9,ea,12,62,5c,30,6c,01) +#define SD_MESSAGE_INVALID_CONFIGURATION SD_ID128_MAKE(c7,72,d2,4e,9a,88,4c,be,b9,ea,12,62,5c,30,6c,01) #define SD_MESSAGE_BOOTCHART SD_ID128_MAKE(9f,26,aa,56,2c,f4,40,c2,b1,6c,77,3d,04,79,b5,18)