From: Stephan Bosch Date: Thu, 25 Jul 2019 23:57:44 +0000 (+0200) Subject: lib: lib-event - Add support for dropping log prefixes introduced by parent events. X-Git-Tag: 2.3.9~359 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=704880b6dcae6ebdc22a81ef1e8a5540d4480560;p=thirdparty%2Fdovecot%2Fcore.git lib: lib-event - Add support for dropping log prefixes introduced by parent events. This allows dropping a specific number prefixes from parents when this event is used for logging. This does not affect the parent events. This only counts actual prefixes and not parents. If the count is higher than the actual number of prefixes added by parents, all will be dropped. --- diff --git a/src/lib/event-log.c b/src/lib/event-log.c index 1a4387bb20..396d13b930 100644 --- a/src/lib/event-log.c +++ b/src/lib/event-log.c @@ -139,9 +139,10 @@ event_get_log_message_str_out(struct event_get_log_message_context *glmctx, glmctx->str_out_done = TRUE; } -static bool ATTR_FORMAT(3, 0) +static bool ATTR_FORMAT(4, 0) event_get_log_message(struct event *event, struct event_get_log_message_context *glmctx, + unsigned int prefixes_dropped, const char *fmt, va_list args) { const struct event_log_params *params = glmctx->params; @@ -199,8 +200,12 @@ event_get_log_message(struct event *event, event_get_log_message_str_out(glmctx, fmt, args); } if (prefix != NULL) { - str_insert(glmctx->log_prefix, 0, prefix); - ret = TRUE; + if (event->log_prefix_replace || prefixes_dropped == 0) { + str_insert(glmctx->log_prefix, 0, prefix); + ret = TRUE; + } else if (prefixes_dropped > 0) { + prefixes_dropped--; + } } if (event->parent == NULL) { event_get_log_message_str_out(glmctx, fmt, args); @@ -213,7 +218,10 @@ event_get_log_message(struct event *event, } } else if (!event->log_prefix_replace && (!params->no_send || !glmctx->str_out_done)) { - if (event_get_log_message(event->parent, glmctx, fmt, args)) + if (event->log_prefixes_dropped > prefixes_dropped) + prefixes_dropped = event->log_prefixes_dropped; + if (event_get_log_message(event->parent, glmctx, + prefixes_dropped, fmt, args)) ret = TRUE; } return ret; @@ -302,7 +310,7 @@ event_logv_params(struct event *event, const struct event_log_params *params, i_zero(&glmctx); glmctx.params = params; glmctx.log_prefix = t_str_new(64); - if (!event_get_log_message(event, &glmctx, fmt, args)) { + if (!event_get_log_message(event, &glmctx, 0, fmt, args)) { /* keep log prefix as it is */ if (params->base_str_out != NULL && !glmctx.str_out_done) { va_list args_copy; diff --git a/src/lib/lib-event-private.h b/src/lib/lib-event-private.h index 7adc1de6f6..c81761f3e5 100644 --- a/src/lib/lib-event-private.h +++ b/src/lib/lib-event-private.h @@ -12,6 +12,7 @@ struct event { uint64_t id; char *log_prefix; + unsigned int log_prefixes_dropped; event_log_prefix_callback_t *log_prefix_callback; void *log_prefix_callback_context; event_log_message_callback_t *log_message_callback; diff --git a/src/lib/lib-event.c b/src/lib/lib-event.c index 7ea2acf799..34c546dc21 100644 --- a/src/lib/lib-event.c +++ b/src/lib/lib-event.c @@ -468,6 +468,13 @@ struct event *event_replace_log_prefix(struct event *event, const char *prefix) return event_set_log_prefix(event, prefix, FALSE); } +struct event * +event_drop_parent_log_prefixes(struct event *event, unsigned int count) +{ + event->log_prefixes_dropped = count; + return event; +} + #undef event_set_log_prefix_callback struct event * event_set_log_prefix_callback(struct event *event, diff --git a/src/lib/lib-event.h b/src/lib/lib-event.h index 4a62e1a00c..7104e60485 100644 --- a/src/lib/lib-event.h +++ b/src/lib/lib-event.h @@ -181,6 +181,12 @@ event_set_append_log_prefix(struct event *event, const char *prefix); */ struct event *event_replace_log_prefix(struct event *event, const char *prefix); +/* Drop count prefixes from parents when this event is used for logging. This + does not affect the parent events. This only counts actual prefixes and not + parents. If the count is higher than the actual number of prefixes added by + parents, all will be dropped. */ +struct event * +event_drop_parent_log_prefixes(struct event *event, unsigned int count); /* Sets event prefix callback, sets log_prefix empty */ struct event *event_set_log_prefix_callback(struct event *event, diff --git a/src/lib/test-event-log.c b/src/lib/test-event-log.c index 9299bdce7c..acb755a45c 100644 --- a/src/lib/test-event-log.c +++ b/src/lib/test-event-log.c @@ -19,6 +19,9 @@ enum test_log_event_type { enum test_log_event_flag { FLAG_BASE_EVENT = BIT(0), + FLAG_DROP_PREFIXES_1 = BIT(1), + FLAG_DROP_PREFIXES_2 = BIT(2), + FLAG_DROP_PREFIXES_4 = BIT(3), }; enum test_log_flag { @@ -2183,6 +2186,189 @@ static void test_event_log_message(void) .result = "replaced2,Info: [amended2-PREFIX: TEXT]", .result_str_out = "STR_PREFIX: TEXT", }, + /* Tests in which parent log prefixes are dropped by an event + lower in the hierarchy. */ + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { TYPE_PREFIX_APPEND, "appended2.", 0 }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { TYPE_PREFIX_APPEND, "appended5.", 0 }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: " + "appended1,appended2.appended3." + "appended4.appended5.TEXT", + .result_str_out = "appended1,appended2.appended3." + "appended4.appended5.TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { TYPE_PREFIX_APPEND, "appended2.", 0 }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { TYPE_PREFIX_APPEND, "appended5.", + FLAG_DROP_PREFIXES_1 }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: " + "appended1,appended2.appended3." + "appended5.TEXT", + .result_str_out = "appended1,appended2.appended3." + "appended5.TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { TYPE_PREFIX_APPEND, "appended2.", 0 }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { TYPE_PREFIX_APPEND, "appended5.", 0 }, + { TYPE_SKIP, NULL, FLAG_DROP_PREFIXES_1 }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: " + "appended1,appended2.appended3." + "appended4.TEXT", + .result_str_out = "appended1,appended2.appended3." + "appended4.TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { TYPE_PREFIX_APPEND, "appended2.", 0 }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { TYPE_PREFIX_APPEND, "appended5.", + FLAG_DROP_PREFIXES_2 }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: " + "appended1,appended2.appended5.TEXT", + .result_str_out = "appended1,appended2.appended5.TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { TYPE_PREFIX_APPEND, "appended2.", 0 }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { TYPE_PREFIX_APPEND, "appended5.", + (FLAG_DROP_PREFIXES_1 | + FLAG_DROP_PREFIXES_2) }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: appended1,appended5.TEXT", + .result_str_out = "appended1,appended5.TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { TYPE_PREFIX_APPEND, "appended2.", 0 }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { TYPE_PREFIX_APPEND, "appended5.", + FLAG_DROP_PREFIXES_4 }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: appended5.TEXT", + .result_str_out = "appended5.TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { TYPE_PREFIX_APPEND, "appended2.", 0 }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { TYPE_PREFIX_APPEND, "appended5.", + (FLAG_DROP_PREFIXES_1 | + FLAG_DROP_PREFIXES_2 | + FLAG_DROP_PREFIXES_4) }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: appended5.TEXT", + .result_str_out = "appended5.TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { TYPE_PREFIX_APPEND, "appended2.", 0 }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { TYPE_PREFIX_APPEND, "appended5.", 0 }, + { TYPE_SKIP, NULL, (FLAG_DROP_PREFIXES_1 | + FLAG_DROP_PREFIXES_2 | + FLAG_DROP_PREFIXES_4) }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: TEXT", + .result_str_out = "TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { TYPE_PREFIX_APPEND, "appended2.", + FLAG_DROP_PREFIXES_1 }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { TYPE_PREFIX_APPEND, "appended5.", + FLAG_DROP_PREFIXES_1 }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: " + "appended2.appended3.appended5.TEXT", + .result_str_out = "appended2.appended3.appended5.TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", + FLAG_DROP_PREFIXES_1 }, + { TYPE_PREFIX_APPEND, "appended2.", + FLAG_DROP_PREFIXES_1 }, + { TYPE_PREFIX_APPEND, "appended3.", + FLAG_DROP_PREFIXES_1 }, + { TYPE_PREFIX_APPEND, "appended4.", + FLAG_DROP_PREFIXES_1 }, + { TYPE_PREFIX_APPEND, "appended5.", + FLAG_DROP_PREFIXES_1 }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: appended5.TEXT", + .result_str_out = "appended5.TEXT", + }, + { + .prefixes = (const struct test_log_event []) { + { TYPE_PREFIX_APPEND, "appended1,", 0 }, + { .type = TYPE_SKIP }, + { TYPE_PREFIX_APPEND, "appended2.", + FLAG_DROP_PREFIXES_1 }, + { .type = TYPE_SKIP }, + { TYPE_PREFIX_APPEND, "appended3.", 0 }, + { .type = TYPE_SKIP }, + { TYPE_PREFIX_APPEND, "appended4.", 0 }, + { .type = TYPE_SKIP }, + { TYPE_PREFIX_APPEND, "appended5.", + FLAG_DROP_PREFIXES_1 }, + { .type = TYPE_SKIP }, + { .type = TYPE_END } + }, + .global_log_prefix = "global3.", + .result = "global3.Info: " + "appended2.appended3.appended5.TEXT", + .result_str_out = "appended2.appended3.appended5.TEXT", + }, }; test_begin("event log message"); @@ -2208,6 +2394,8 @@ static void test_event_log_message(void) struct event *event, *parent; event = parent = event_create(NULL); for (unsigned int j = 0; test->prefixes[j].type != TYPE_END; j++) { + unsigned int drop_prefixes = 0; + if (event == NULL) { struct event *child = event_create(parent); event_unref(&parent); @@ -2217,6 +2405,17 @@ static void test_event_log_message(void) i_assert(params.base_event == NULL); params.base_event = event; } + if ((test->prefixes[j].flags & + FLAG_DROP_PREFIXES_1) != 0) + drop_prefixes += 1; + if ((test->prefixes[j].flags & + FLAG_DROP_PREFIXES_2) != 0) + drop_prefixes += 2; + if ((test->prefixes[j].flags & + FLAG_DROP_PREFIXES_4) != 0) + drop_prefixes += 4; + event_drop_parent_log_prefixes(event, drop_prefixes); + switch (test->prefixes[j].type) { case TYPE_END: i_unreached();