From: Aki Tuomi Date: Thu, 29 Nov 2018 11:12:28 +0000 (+0200) Subject: lib: event - Add event_set_log_prefix_callback X-Git-Tag: 2.3.5~297 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=facfea299c42ceb6edb473650a552b264f5b55db;p=thirdparty%2Fdovecot%2Fcore.git lib: event - Add event_set_log_prefix_callback Fetch prefix using a callback. --- diff --git a/src/lib/event-log.c b/src/lib/event-log.c index 2db02dea87..dd4c8953e4 100644 --- a/src/lib/event-log.c +++ b/src/lib/event-log.c @@ -81,12 +81,16 @@ static bool event_get_log_prefix(struct event *event, string_t *log_prefix, bool *replace_prefix, unsigned int *type_pos) { bool ret = FALSE; + const char *prefix = event->log_prefix; + + if (event->log_prefix_callback != NULL) + prefix = event->log_prefix_callback(event->log_prefix_callback_context); if (event->log_prefix_replace) { /* this event replaces all parent log prefixes */ *replace_prefix = TRUE; - *type_pos = event->log_prefix == NULL ? 0 : - strlen(event->log_prefix); + *type_pos = prefix == NULL ? 0 : + strlen(prefix); } else if (event->parent == NULL) { /* append to default log prefix, don't replace it */ } else { @@ -94,8 +98,8 @@ static bool event_get_log_prefix(struct event *event, string_t *log_prefix, replace_prefix, type_pos)) ret = TRUE; } - if (event->log_prefix != NULL) { - str_append(log_prefix, event->log_prefix); + if (prefix != NULL) { + str_append(log_prefix, prefix); ret = TRUE; } return ret; diff --git a/src/lib/lib-event-private.h b/src/lib/lib-event-private.h index a93945c04f..63c805af5e 100644 --- a/src/lib/lib-event-private.h +++ b/src/lib/lib-event-private.h @@ -12,6 +12,8 @@ struct event { uint64_t id; char *log_prefix; + event_log_prefix_callback_t *log_prefix_callback; + void *log_prefix_callback_context; bool log_prefix_from_system_pool:1; bool log_prefix_replace:1; bool passthrough:1; diff --git a/src/lib/lib-event.c b/src/lib/lib-event.c index ab6d1adaa4..228c582da8 100644 --- a/src/lib/lib-event.c +++ b/src/lib/lib-event.c @@ -279,6 +279,8 @@ struct event *event_get_global(void) static struct event * event_set_log_prefix(struct event *event, const char *prefix, bool append) { + event->log_prefix_callback = NULL; + event->log_prefix_callback_context = NULL; if (event->log_prefix == NULL) { /* allocate the first log prefix from the pool */ event->log_prefix = p_strdup(event->pool, prefix); @@ -306,6 +308,23 @@ struct event *event_replace_log_prefix(struct event *event, const char *prefix) return event_set_log_prefix(event, prefix, FALSE); } +#undef event_set_log_prefix_callback +struct event * +event_set_log_prefix_callback(struct event *event, + bool replace, + event_log_prefix_callback_t *callback, + void *context) +{ + if (event->log_prefix_from_system_pool) + i_free(event->log_prefix); + else + event->log_prefix = NULL; + event->log_prefix_replace = replace; + event->log_prefix_callback = callback; + event->log_prefix_callback_context = context; + return event; +} + struct event * event_set_name(struct event *event, const char *name) { diff --git a/src/lib/lib-event.h b/src/lib/lib-event.h index 391f5d7f20..e0c4143495 100644 --- a/src/lib/lib-event.h +++ b/src/lib/lib-event.h @@ -77,6 +77,8 @@ struct event_passthrough { struct event *(*event)(void); }; +typedef const char *event_log_prefix_callback_t(void *context); + /* Returns TRUE if the event has all the categories that the "other" event has (and maybe more). */ bool event_has_all_categories(struct event *event, const struct event *other); /* Returns TRUE if the event has all the fields that the "other" event has (and maybe more). @@ -144,13 +146,29 @@ struct event *event_get_global(void); /* Set the appended log prefix string for this event. All the parent events' log prefixes will be concatenated together when logging. The log type text (e.g. "Info: ") will be inserted before appended log prefixes (but - after replaced log prefix). */ + after replaced log prefix). + + Clears log_prefix callback. + */ struct event * event_set_append_log_prefix(struct event *event, const char *prefix); /* Replace the full log prefix string for this event. The parent events' log - prefixes won't be used. */ + prefixes won't be used. + + Clears log_prefix callback. +*/ struct event *event_replace_log_prefix(struct event *event, const char *prefix); + +/* Sets event prefix callback, sets log_prefix empty */ +struct event *event_set_log_prefix_callback(struct event *event, + bool replace, + event_log_prefix_callback_t *callback, + void *context); +#define event_set_log_prefix_callback(event, replace, callback, context) \ + event_set_log_prefix_callback(event, replace, (event_log_prefix_callback_t*)callback, \ + context + CALLBACK_TYPECHECK(callback, const char *(*)(typeof(context)))) + /* Set the event's name. The name is specific to a single sending of an event, and it'll be automatically cleared once the event is sent. This should typically be used only in a parameter to e_debug(), etc. */ diff --git a/src/lib/test-event-log.c b/src/lib/test-event-log.c index 622bf519df..c42e7defda 100644 --- a/src/lib/test-event-log.c +++ b/src/lib/test-event-log.c @@ -11,6 +11,8 @@ enum test_log_prefix_type { TYPE_END, TYPE_APPEND, TYPE_REPLACE, + TYPE_CALLBACK_APPEND, + TYPE_CALLBACK_REPLACE, TYPE_SKIP, }; @@ -43,6 +45,12 @@ info_handler(const struct failure_context *ctx, } T_END; } +static const char * +test_event_log_prefix_cb(char *prefix) +{ + return t_strdup_printf("callback(%s)", prefix); +} + static void test_event_log_prefix(void) { struct test_log tests[] = { @@ -131,6 +139,39 @@ static void test_event_log_prefix(void) }, .result = "replaced4;Info: appended5-TEXT", }, + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_CALLBACK_APPEND, "appended1-" }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: callback(appended1-)TEXT", + }, + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_APPEND, "appended1," }, + { TYPE_REPLACE, "replaced1." }, + { TYPE_CALLBACK_REPLACE, "replaced2-" }, + { .type = TYPE_END } + }, + .result = "callback(replaced2-)Info: TEXT", + }, + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_CALLBACK_REPLACE, "replaced1." }, + { TYPE_APPEND, "appended1," }, + { .type = TYPE_END } + }, + .result = "callback(replaced1.)Info: appended1,TEXT", + }, + { + .prefixes = (const struct test_log_prefix []) { + { TYPE_CALLBACK_REPLACE, "replaced1." }, + { TYPE_REPLACE, "replaced2-" }, + { .type = TYPE_END } + }, + .result = "replaced2-Info: TEXT", + }, }; const struct event_log_params params = { .log_type = LOG_TYPE_INFO, @@ -166,6 +207,16 @@ static void test_event_log_prefix(void) case TYPE_REPLACE: event_replace_log_prefix(event, test->prefixes[j].str); break; + case TYPE_CALLBACK_APPEND: + event_set_log_prefix_callback(event, FALSE, + test_event_log_prefix_cb, + (char*)test->prefixes[j].str); + break; + case TYPE_CALLBACK_REPLACE: + event_set_log_prefix_callback(event, TRUE, + test_event_log_prefix_cb, + (char*)test->prefixes[j].str); + break; case TYPE_SKIP: break; }