log: move log_syntax() into src/shared/log.c, and make it more similar to the other log functions

This commit is contained in:
Lennart Poettering 2015-04-21 17:26:04 +02:00
parent 200c7fa6fe
commit 158350e869
5 changed files with 77 additions and 63 deletions

View File

@ -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,

View File

@ -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); \

View File

@ -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;
}

View File

@ -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); \
})

View File

@ -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)