Skip to content

Commit

Permalink
lib: Add failure_context.log_prefix_type_pos
Browse files Browse the repository at this point in the history
If non-zero, this specifies where the log type (e.g. "Info: ") is written
within the log prefix. By default it's appended.
  • Loading branch information
sirainen authored and cmouse committed Sep 3, 2018
1 parent fcd9bcd commit 36d7bd3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/lib/failures.c
Expand Up @@ -187,13 +187,17 @@ static string_t * ATTR_FORMAT(3, 0) internal_format(const struct failure_context

if (ctx->log_prefix != NULL) {
log_type |= LOG_TYPE_FLAG_DISABLE_LOG_PREFIX;
if (ctx->log_prefix_type_pos != 0)
log_type |= LOG_TYPE_FLAG_PREFIX_LEN;
} else if (!log_prefix_sent && log_prefix != NULL) {
log_prefix_sent = TRUE;
i_failure_send_option("prefix", log_prefix);
}

str = t_str_new(128);
str_printfa(str, "\001%c%s ", log_type, my_pid);
if ((log_type & LOG_TYPE_FLAG_PREFIX_LEN) != 0)
str_printfa(str, "%u ", ctx->log_prefix_type_pos);
if (ctx->log_prefix != NULL)
str_append(str, ctx->log_prefix);
*prefix_len_r = str_len(str);
Expand Down Expand Up @@ -335,10 +339,17 @@ static void log_prefix_add(const struct failure_context *ctx, string_t *str)
/* use global log prefix */
if (log_prefix != NULL)
str_append(str, log_prefix);
} else {
str_append(str, failure_log_type_prefixes[ctx->type]);
} else if (ctx->log_prefix_type_pos == 0) {
str_append(str, ctx->log_prefix);
str_append(str, failure_log_type_prefixes[ctx->type]);
} else {
i_assert(ctx->log_prefix_type_pos <= strlen(ctx->log_prefix));
str_append_data(str, ctx->log_prefix, ctx->log_prefix_type_pos);
str_insert(str, ctx->log_prefix_type_pos,
failure_log_type_prefixes[ctx->type]);
str_append(str, ctx->log_prefix + ctx->log_prefix_type_pos);
}
str_append(str, failure_log_type_prefixes[ctx->type]);
}

static void fd_wait_writable(int fd)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/failures.h
Expand Up @@ -43,6 +43,9 @@ struct failure_context {
const struct tm *timestamp; /* NULL = use time() + localtime() */
unsigned int timestamp_usecs;
const char *log_prefix; /* override the default log prefix */
/* If non-0, insert the log type text (e.g. "Info: ") at this position
in the log_prefix instead of appending it. */
unsigned int log_prefix_type_pos;
};

#define DEFAULT_FAILURE_STAMP_FORMAT "%b %d %H:%M:%S "
Expand Down

0 comments on commit 36d7bd3

Please sign in to comment.