commit 10dcd62: [Feature] Add logging of groups to the log_format

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Aug 30 18:42:06 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-08-30 19:39:42 +0100
URL: https://github.com/rspamd/rspamd/commit/10dcd6236aa3a7576bc81885cbcf668cdca0b293 (HEAD -> master)

[Feature] Add logging of groups to the log_format

---
 src/libserver/cfg_file.h  |  2 ++
 src/libserver/cfg_utils.c |  6 +++++
 src/libserver/task.c      | 56 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+)

diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index cffe997e4..4faca7b56 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -258,6 +258,8 @@ enum rspamd_log_format_type {
 	RSPAMD_LOG_FILENAME,
 	RSPAMD_LOG_FORCED_ACTION,
 	RSPAMD_LOG_SETTINGS_ID,
+	RSPAMD_LOG_GROUPS,
+	RSPAMD_LOG_PUBLIC_GROUPS,
 };
 
 enum rspamd_log_format_flags {
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index 8bbfd43e0..619e8784e 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -451,6 +451,12 @@ rspamd_config_process_var (struct rspamd_config *cfg, const rspamd_ftok_t *var,
 		type = RSPAMD_LOG_SYMBOLS;
 		flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS|RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES;
 	}
+	else if (rspamd_ftok_cstr_equal (&tok, "groups", TRUE)) {
+		type = RSPAMD_LOG_GROUPS;
+	}
+	else if (rspamd_ftok_cstr_equal (&tok, "public_groups", TRUE)) {
+		type = RSPAMD_LOG_PUBLIC_GROUPS;
+	}
 	else if (rspamd_ftok_cstr_equal (&tok, "ip", TRUE)) {
 		type = RSPAMD_LOG_IP;
 	}
diff --git a/src/libserver/task.c b/src/libserver/task.c
index b8e178a7b..6b5f85381 100644
--- a/src/libserver/task.c
+++ b/src/libserver/task.c
@@ -1035,6 +1035,16 @@ rspamd_task_compare_log_sym (gconstpointer a, gconstpointer b)
 	return (w2 - w1) * 1000.0;
 }
 
+static gint
+rspamd_task_compare_log_group (gconstpointer a, gconstpointer b)
+{
+	const struct rspamd_symbols_group *s1 = *(const struct rspamd_symbols_group **)a,
+			*s2 = *(const struct rspamd_symbols_group **)b;
+
+	return strcmp (s1->name, s2->name);
+}
+
+
 static rspamd_ftok_t
 rspamd_task_log_metric_res (struct rspamd_task *task,
 		struct rspamd_log_format *lf)
@@ -1047,7 +1057,10 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
 	struct rspamd_symbol_result *sym;
 	GPtrArray *sorted_symbols;
 	struct rspamd_action *act;
+	struct rspamd_symbols_group *gr;
+	gdouble gr_score;
 	guint i, j;
+	khiter_t k;
 
 	mres = task->result;
 	act = rspamd_check_action_metric (task);
@@ -1136,6 +1149,49 @@ rspamd_task_log_metric_res (struct rspamd_task *task,
 			res.begin = symbuf->str;
 			res.len = symbuf->len;
 			break;
+
+		case RSPAMD_LOG_GROUPS:
+		case RSPAMD_LOG_PUBLIC_GROUPS:
+
+			symbuf = rspamd_fstring_sized_new (128);
+			sorted_symbols = g_ptr_array_sized_new (kh_size (mres->sym_groups));
+
+			kh_foreach (mres->sym_groups, gr, gr_score,{
+				if (!(gr->flags & RSPAMD_SYMBOL_GROUP_PUBLIC)) {
+					if (lf->type == RSPAMD_LOG_PUBLIC_GROUPS) {
+						continue;
+					}
+				}
+			});
+
+			g_ptr_array_sort (sorted_symbols, rspamd_task_compare_log_group);
+
+			for (i = 0; i < sorted_symbols->len; i++) {
+				gr = g_ptr_array_index (sorted_symbols, i);
+
+				if (first) {
+					rspamd_printf_fstring (&symbuf, "%s", gr->name);
+				}
+				else {
+					rspamd_printf_fstring (&symbuf, ",%s", gr->name);
+				}
+
+				k = kh_get (rspamd_symbols_group_hash, mres->sym_groups, gr);
+
+				rspamd_printf_fstring (&symbuf, "(%.2f)",
+						kh_value (mres->sym_groups, k));
+
+				first = FALSE;
+			}
+
+			g_ptr_array_free (sorted_symbols, TRUE);
+
+			rspamd_mempool_add_destructor (task->task_pool,
+					(rspamd_mempool_destruct_t) rspamd_fstring_free,
+					symbuf);
+			res.begin = symbuf->str;
+			res.len = symbuf->len;
+			break;
 		default:
 			break;
 		}


More information about the Commits mailing list