From: Marco Bettini Date: Tue, 19 Jul 2022 10:31:53 +0000 (+0000) Subject: lib-storage: Add prefix to storage event X-Git-Tag: 2.4.0~3717 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f042aa5ba8b16fbca850eb26e5f8bfd06bb50441;p=thirdparty%2Fdovecot%2Fcore.git lib-storage: Add prefix to storage event --- diff --git a/src/lib-storage/fail-mailbox.c b/src/lib-storage/fail-mailbox.c index 2ba1b7a604..0c97f15907 100644 --- a/src/lib-storage/fail-mailbox.c +++ b/src/lib-storage/fail-mailbox.c @@ -331,12 +331,8 @@ fail_mailbox_alloc(struct mail_storage *storage, struct mailbox_list *list, box->pool = pool; box->flags = flags; - box->event = event_create(box->storage->event); - event_add_category(box->event, &event_category_mailbox); - event_add_str(box->event, "mailbox", box->vname); - event_set_append_log_prefix(box->event, - t_strdup_printf("Mailbox %s: ", str_sanitize(box->vname, 128))); - + box->event = mail_storage_mailbox_create_event(box->storage->event, + box->vname); p_array_init(&box->search_results, pool, 16); p_array_init(&box->module_contexts, pool, 5); return box; diff --git a/src/lib-storage/index/index-storage.c b/src/lib-storage/index/index-storage.c index 31ce6bb730..c3bcc1ba72 100644 --- a/src/lib-storage/index/index-storage.c +++ b/src/lib-storage/index/index-storage.c @@ -402,11 +402,8 @@ void index_storage_mailbox_alloc(struct mailbox *box, const char *vname, mailbox_list_get_storage_name(box->list, vname)); box->flags = flags; box->index_prefix = p_strdup(box->pool, index_prefix); - box->event = event_create(box->storage->event); - event_add_category(box->event, &event_category_mailbox); - event_add_str(box->event, "mailbox", box->vname); - event_set_append_log_prefix(box->event, - t_strdup_printf("Mailbox %s: ", str_sanitize(box->vname, 128))); + box->event = mail_storage_mailbox_create_event(box->storage->event, + box->vname); p_array_init(&box->search_results, box->pool, 16); array_create(&box->module_contexts, diff --git a/src/lib-storage/mail-storage-private.h b/src/lib-storage/mail-storage-private.h index c4b567aedc..79ba6465c3 100644 --- a/src/lib-storage/mail-storage-private.h +++ b/src/lib-storage/mail-storage-private.h @@ -912,6 +912,9 @@ static inline const char *mailbox_name_sanitize(const char *name) return str_sanitize(name, 128); } +struct event * +mail_storage_mailbox_create_event(struct event *parent, const char *vname); + /* for unit testing */ int mailbox_verify_name(struct mailbox *box); diff --git a/src/lib-storage/mail-storage.c b/src/lib-storage/mail-storage.c index d3d46afbe7..b1514f640a 100644 --- a/src/lib-storage/mail-storage.c +++ b/src/lib-storage/mail-storage.c @@ -430,6 +430,8 @@ int mail_storage_create_full(struct mail_namespace *ns, const char *driver, storage->event = event_create(ns->user->event); if (storage_class->event_category != NULL) event_add_category(storage->event, storage_class->event_category); + event_set_append_log_prefix( + storage->event, t_strdup_printf("%s: ", storage_class->name)); p_array_init(&storage->module_contexts, storage->pool, 5); if (storage->v.create != NULL && @@ -573,12 +575,11 @@ void mail_storage_set_internal_error(struct mail_storage *storage) i_free(storage->last_internal_error); } -void mail_storage_set_critical(struct mail_storage *storage, - const char *fmt, ...) +static void +mail_storage_set_critical_error(struct mail_storage *storage, const char *str) { char *old_error = storage->error_string; char *old_internal_error = storage->last_internal_error; - va_list va; storage->error_string = NULL; storage->last_internal_error = NULL; @@ -587,11 +588,8 @@ void mail_storage_set_critical(struct mail_storage *storage, easier to look from log files the actual error message. */ mail_storage_set_internal_error(storage); - va_start(va, fmt); - storage->last_internal_error = i_strdup_vprintf(fmt, va); - va_end(va); + storage->last_internal_error = i_strdup(str); storage->last_error_is_internal = TRUE; - e_error(storage->event, "%s", storage->last_internal_error); /* free the old_error and old_internal_error only after the new error is generated, because they may be one of the parameters. */ @@ -599,15 +597,31 @@ void mail_storage_set_critical(struct mail_storage *storage, i_free(old_internal_error); } +void mail_storage_set_critical(struct mail_storage *storage, + const char *fmt, ...) +{ + va_list va; + + va_start(va, fmt); + T_BEGIN { + const char *str = t_strdup_vprintf(fmt, va); + mail_storage_set_critical_error(storage, str); + e_error(storage->event, "%s", str); + } T_END; + va_end(va); +} + void mailbox_set_critical(struct mailbox *box, const char *fmt, ...) { va_list va; va_start(va, fmt); T_BEGIN { - mail_storage_set_critical(box->storage, "Mailbox %s: %s", - mailbox_name_sanitize(box->vname), - t_strdup_vprintf(fmt, va)); + const char *str = t_strdup_vprintf(fmt, va); + mail_storage_set_critical_error(box->storage, + t_strdup_printf("Mailbox %s: %s", + mailbox_name_sanitize(box->vname), str)); + e_error(box->event, "%s", str); } T_END; va_end(va); } @@ -3141,6 +3155,19 @@ mail_storage_settings_to_index_flags(const struct mail_storage_settings *set) return index_flags; } + +struct event * +mail_storage_mailbox_create_event(struct event *parent, const char* vname) +{ + struct event *event = event_create(parent); + event_add_category(event, &event_category_mailbox); + event_add_str(event, "mailbox", vname); + event_drop_parent_log_prefixes(event, 1); + event_set_append_log_prefix(event, t_strdup_printf( + "Mailbox %s: ", mailbox_name_sanitize(vname))); + return event; +} + int mail_parse_human_timestamp(const char *str, time_t *timestamp_r, bool *utc_r) {