summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-04-21 17:26:04 +0200
committerLennart Poettering <lennart@poettering.net>2015-04-21 17:43:55 +0200
commit158350e86923cb1d878d3bf7d928fb1c30a3da76 (patch)
tree3731231a28497ade4ef8605f893136563bccd3cf
parent200c7fa6fea5aea301e5b091338c5dc1f88f759c (diff)
log: move log_syntax() into src/shared/log.c, and make it more similar to the other log functions
-rw-r--r--src/shared/conf-parser.c44
-rw-r--r--src/shared/conf-parser.h17
-rw-r--r--src/shared/log.c56
-rw-r--r--src/shared/log.h21
-rw-r--r--src/systemd/sd-messages.h2
5 files changed, 77 insertions, 63 deletions
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index aa6a4a6395..2c855157a9 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 7a2f855f9f..6152ee33b9 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 85c0605bd5..e8b63a3ed9 100644
--- a/src/shared/log.c
+++ b/src/shared/log.c
@@ -29,6 +29,7 @@
#include <stddef.h>
#include <printf.h>
+#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 d6061c05ea..09d2fc767b 100644
--- a/src/shared/log.h
+++ b/src/shared/log.h
@@ -28,8 +28,8 @@
#include <sys/signalfd.h>
#include <errno.h>
-#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 a8379e098c..8aedaec6d1 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)