]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Add `logging`->`task_max_elts` option
authorVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 5 Sep 2023 12:28:49 +0000 (13:28 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Tue, 5 Sep 2023 12:28:49 +0000 (13:28 +0100)
Issue: #4583

src/libserver/cfg_file.h
src/libserver/cfg_rcl.cxx
src/libserver/cfg_utils.cxx
src/libserver/task.c

index 3f876ab233c1d30dfe6fa3e3c1fcfe55f2398e1c..1415fd8659994ad891c7dc0a5d761da84a9ae75f 100644 (file)
@@ -385,6 +385,7 @@ struct rspamd_config {
        guint log_flags;                                    /**< logging flags                                                                          */
        guint log_error_elts;                               /**< number of elements in error logbuf                                     */
        guint log_error_elt_maxlen;                         /**< maximum size of error log element                                      */
+       guint log_task_max_elts;                            /**< maximum number of elements in task logging                     */
        struct rspamd_worker_log_pipe *log_pipes;
 
        gboolean compat_messages; /**< use old messages in the protocol (array)                         */
index a20cd572da57bae711defbeeb64b9d0291ad293d..26b799cdf83df554ef122b2d23d16d9ac2610517 100644 (file)
@@ -1641,6 +1641,12 @@ rspamd_rcl_config_init(struct rspamd_config *cfg, GHashTable *skip_sections)
                                                                           G_STRUCT_OFFSET(struct rspamd_config, log_error_elt_maxlen),
                                                                           RSPAMD_CL_FLAG_UINT,
                                                                           "Size of each element in error log buffer (1000 by default)");
+               rspamd_rcl_add_default_handler(sub,
+                                                                          "task_max_elts",
+                                                                          rspamd_rcl_parse_struct_integer,
+                                                                          G_STRUCT_OFFSET(struct rspamd_config, log_task_max_elts),
+                                                                          RSPAMD_CL_FLAG_UINT,
+                                                                          "Maximum number of elements in task log entry (7 by default)");
 
                /* Documentation only options, handled in log_handler to map flags */
                rspamd_rcl_add_doc_by_path(cfg,
index 5c5a8a24baad80594345600025d6314310d0bb5f..12074a83a3374166d8abb931f6ba754b8c753855 100644 (file)
@@ -279,6 +279,7 @@ rspamd_config_new(enum rspamd_config_init_flags flags)
        cfg->history_rows = 200;
        cfg->log_error_elts = 10;
        cfg->log_error_elt_maxlen = 1000;
+       cfg->log_task_max_elts = 7;
        cfg->cache_reload_time = 30.0;
        cfg->max_lua_urls = 1024;
        cfg->max_urls = cfg->max_lua_urls * 10;
index 864bbedf41d5d5d6622a238a71dd610688dd95da..937dd8b054a4d9eeca45bce70ac6b8afbc249d4b 100644 (file)
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2016 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -50,11 +50,6 @@ __KHASH_IMPL(rspamd_req_headers_hash, static inline,
                         rspamd_ftok_t *, struct rspamd_request_header_chain *, 1,
                         rspamd_ftok_icase_hash, rspamd_ftok_icase_equal)
 
-/*
- * Do not print more than this amount of elts
- */
-static const int max_log_elts = 7;
-
 static GQuark
 rspamd_task_quark(void)
 {
@@ -669,10 +664,10 @@ rspamd_task_load_message(struct rspamd_task *task,
        return TRUE;
 }
 
-static gint
+static guint
 rspamd_task_select_processing_stage(struct rspamd_task *task, guint stages)
 {
-       gint st, mask;
+       guint st, mask;
 
        mask = task->processed_stages;
 
@@ -681,7 +676,7 @@ rspamd_task_select_processing_stage(struct rspamd_task *task, guint stages)
        }
        else {
                for (st = 1; mask != 1; st++) {
-                       mask = (unsigned int) mask >> 1;
+                       mask = mask >> 1u;
                }
        }
 
@@ -703,7 +698,7 @@ rspamd_task_select_processing_stage(struct rspamd_task *task, guint stages)
 gboolean
 rspamd_task_process(struct rspamd_task *task, guint stages)
 {
-       gint st;
+       guint st;
        gboolean ret = TRUE, all_done = TRUE;
        GError *stat_error = NULL;
 
@@ -1090,6 +1085,7 @@ rspamd_task_log_metric_res(struct rspamd_task *task,
        struct rspamd_symbols_group *gr;
        guint i, j;
        khiter_t k;
+       guint max_log_elts = task->cfg->log_task_max_elts;
 
        mres = task->result;
        act = rspamd_check_action_metric(task, NULL, NULL);
@@ -1157,7 +1153,7 @@ rspamd_task_log_metric_res(struct rspamd_task *task,
                                                        rspamd_printf_fstring(&symbuf, "%*s;",
                                                                                                  (gint) opt->optlen, opt->option);
 
-                                                       if (j >= max_log_elts) {
+                                                       if (j >= max_log_elts && opt->next) {
                                                                rspamd_printf_fstring(&symbuf, "...;");
                                                                break;
                                                        }
@@ -1286,6 +1282,7 @@ rspamd_task_write_ialist(struct rspamd_task *task,
        struct rspamd_email_address *addr;
        gint i, nchars = 0, wr = 0, cur_chars;
        gboolean has_orig = FALSE;
+       guint max_log_elts = task->cfg->log_task_max_elts;
 
        if (addrs && lim <= 0) {
                lim = addrs->len;
@@ -1314,19 +1311,19 @@ rspamd_task_write_ialist(struct rspamd_task *task,
                        }
                }
 
+               bool last = i == lim - 1;
+
                cur_chars = addr->addr_len;
                varbuf = rspamd_fstring_append(varbuf, addr->addr,
                                                                           cur_chars);
                nchars += cur_chars;
                wr++;
 
-               if (varbuf->len > 0) {
-                       if (i != lim - 1) {
-                               varbuf = rspamd_fstring_append(varbuf, ",", 1);
-                       }
+               if (varbuf->len > 0 && !last) {
+                       varbuf = rspamd_fstring_append(varbuf, ",", 1);
                }
 
-               if (wr >= max_log_elts || nchars >= max_log_elts * 10) {
+               if (!last && (wr >= max_log_elts || nchars >= max_log_elts * 16)) {
                        varbuf = rspamd_fstring_append(varbuf, "...", 3);
                        break;
                }
@@ -1353,7 +1350,8 @@ rspamd_task_write_addr_list(struct rspamd_task *task,
        rspamd_fstring_t *res = logbuf, *varbuf;
        rspamd_ftok_t var = {.begin = NULL, .len = 0};
        struct rspamd_email_address *addr;
-       gint i;
+       guint max_log_elts = task->cfg->log_task_max_elts;
+       guint i;
 
        if (lim <= 0) {
                lim = addrs->len;
@@ -1363,18 +1361,17 @@ rspamd_task_write_addr_list(struct rspamd_task *task,
 
        for (i = 0; i < lim; i++) {
                addr = g_ptr_array_index(addrs, i);
+               bool last = i == lim - 1;
 
                if (addr->addr) {
                        varbuf = rspamd_fstring_append(varbuf, addr->addr, addr->addr_len);
                }
 
-               if (varbuf->len > 0) {
-                       if (i != lim - 1) {
-                               varbuf = rspamd_fstring_append(varbuf, ",", 1);
-                       }
+               if (varbuf->len > 0 && !last) {
+                       varbuf = rspamd_fstring_append(varbuf, ",", 1);
                }
 
-               if (i >= max_log_elts) {
+               if (!last && i >= max_log_elts) {
                        varbuf = rspamd_fstring_append(varbuf, "...", 3);
                        break;
                }
@@ -1455,11 +1452,6 @@ rspamd_task_log_variable(struct rspamd_task *task,
                var.begin = numbuf;
                break;
        case RSPAMD_LOG_TIME_REAL:
-               var.begin = rspamd_log_check_time(task->task_timestamp,
-                                                                                 task->time_real_finish,
-                                                                                 task->cfg->clock_res);
-               var.len = strlen(var.begin);
-               break;
        case RSPAMD_LOG_TIME_VIRTUAL:
                var.begin = rspamd_log_check_time(task->task_timestamp,
                                                                                  task->time_real_finish,
@@ -1662,8 +1654,6 @@ void rspamd_task_write_log(struct rspamd_task *task)
 gdouble
 rspamd_task_get_required_score(struct rspamd_task *task, struct rspamd_scan_result *m)
 {
-       gint i;
-
        if (m == NULL) {
                m = task->result;
 
@@ -1672,7 +1662,7 @@ rspamd_task_get_required_score(struct rspamd_task *task, struct rspamd_scan_resu
                }
        }
 
-       for (i = m->nactions - 1; i >= 0; i--) {
+       for (guint i = m->nactions - 1; i >= 0; i--) {
                struct rspamd_action_config *action_lim = &m->actions_config[i];