From: Timo Sirainen Date: Fri, 17 Nov 2017 10:37:15 +0000 (+0200) Subject: lib: Add failure_context.log_prefix to override the default X-Git-Tag: 2.3.0.rc1~102 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=982c30abd049087ce8b2fd44769561441e074961;p=thirdparty%2Fdovecot%2Fcore.git lib: Add failure_context.log_prefix to override the default --- diff --git a/src/lib/failures.c b/src/lib/failures.c index 10ed8e95f7..c7ad8578cb 100644 --- a/src/lib/failures.c +++ b/src/lib/failures.c @@ -97,7 +97,9 @@ static void log_prefix_add(const struct failure_context *ctx, string_t *str) get_log_stamp_format("unused", now.tv_usec), tm) > 0) str_append(str, buf); } - if (log_prefix != NULL) + if (ctx->log_prefix != NULL) + str_append(str, ctx->log_prefix); + else if (log_prefix != NULL) str_append(str, log_prefix); } @@ -414,8 +416,9 @@ void i_get_failure_handlers(failure_callback_t **fatal_callback_r, *debug_callback_r = debug_handler; } -static int ATTR_FORMAT(3, 0) -syslog_handler(int level, enum log_type type, const char *format, va_list args) +static int ATTR_FORMAT(4, 0) +syslog_handler(const struct failure_context *ctx, + int level, enum log_type type, const char *format, va_list args) { static int recursed = 0; @@ -426,8 +429,10 @@ syslog_handler(int level, enum log_type type, const char *format, va_list args) /* syslogs don't generally bother to log the level in any way, so make sure errors are shown clearly */ T_BEGIN { + const char *cur_log_prefix = ctx->log_prefix != NULL ? + ctx->log_prefix : log_prefix; syslog(level, "%s%s%s", - log_prefix == NULL ? "" : log_prefix, + cur_log_prefix == NULL ? "" : cur_log_prefix, type != LOG_TYPE_INFO ? failure_log_type_prefixes[type] : "", t_strdup_vprintf(format, args)); @@ -441,7 +446,7 @@ void i_syslog_fatal_handler(const struct failure_context *ctx, { int status = ctx->exit_status; - if (syslog_handler(LOG_CRIT, ctx->type, format, args) < 0 && + if (syslog_handler(ctx, LOG_CRIT, ctx->type, format, args) < 0 && status == FATAL_DEFAULT) status = FATAL_LOGERROR; @@ -475,7 +480,7 @@ void i_syslog_error_handler(const struct failure_context *ctx, i_unreached(); } - if (syslog_handler(level, ctx->type, format, args) < 0) + if (syslog_handler(ctx, level, ctx->type, format, args) < 0) failure_exit(FATAL_LOGERROR); } diff --git a/src/lib/failures.h b/src/lib/failures.h index e2bd20f2ad..3b4a53c8d6 100644 --- a/src/lib/failures.h +++ b/src/lib/failures.h @@ -37,6 +37,7 @@ struct failure_context { int exit_status; /* for LOG_TYPE_FATAL */ const struct tm *timestamp; /* NULL = use time() + localtime() */ unsigned int timestamp_usecs; + const char *log_prefix; /* override the default log prefix */ }; #define DEFAULT_FAILURE_STAMP_FORMAT "%b %d %H:%M:%S "