commit 03a195e: [Rework] Logger: Fix logic of initialisation

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Feb 10 21:28:13 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-02-10 20:56:55 +0000
URL: https://github.com/rspamd/rspamd/commit/03a195e7b35f75b5f0ef81a5dc1580f2962cd5ab

[Rework] Logger: Fix logic of initialisation

---
 src/libutil/logger.c         | 72 ++++++++++++++++++++++++--------------------
 src/libutil/logger_console.c |  8 ++---
 2 files changed, 43 insertions(+), 37 deletions(-)

diff --git a/src/libutil/logger.c b/src/libutil/logger.c
index 4fed80d3e..568c0b8e9 100644
--- a/src/libutil/logger.c
+++ b/src/libutil/logger.c
@@ -194,15 +194,24 @@ rspamd_log_open_specific (rspamd_mempool_t *pool,
 
 	logger->pool = pool;
 
-	if (cfg->log_error_elts > 0 && pool) {
-		logger->errlog = rspamd_mempool_alloc0_shared (pool,
-				sizeof (*logger->errlog));
-		logger->errlog->pool = pool;
-		logger->errlog->max_elts = cfg->log_error_elts;
-		logger->errlog->elt_len = cfg->log_error_elt_maxlen;
-		logger->errlog->elts = rspamd_mempool_alloc0_shared (pool,
-				sizeof (struct rspamd_logger_error_elt) * cfg->log_error_elts +
-				cfg->log_error_elt_maxlen * cfg->log_error_elts);
+	if (cfg) {
+		if (cfg->log_error_elts > 0 && pool) {
+			logger->errlog = rspamd_mempool_alloc0_shared (pool,
+					sizeof (*logger->errlog));
+			logger->errlog->pool = pool;
+			logger->errlog->max_elts = cfg->log_error_elts;
+			logger->errlog->elt_len = cfg->log_error_elt_maxlen;
+			logger->errlog->elts = rspamd_mempool_alloc0_shared (pool,
+					sizeof (struct rspamd_logger_error_elt) * cfg->log_error_elts +
+					cfg->log_error_elt_maxlen * cfg->log_error_elts);
+		}
+
+		logger->log_level = cfg->log_level;
+		logger->flags = cfg->log_flags;
+
+		if (!(logger->flags & RSPAMD_LOG_FLAG_ENFORCED)) {
+			logger->log_level = cfg->log_level;
+		}
 	}
 
 	const struct rspamd_logger_funcs *funcs = NULL;
@@ -235,34 +244,31 @@ rspamd_log_open_specific (rspamd_mempool_t *pool,
 
 	logger->pid = getpid ();
 	logger->process_type = ptype;
-
-	if (!(logger->flags & RSPAMD_LOG_FLAG_ENFORCED)) {
-		logger->log_level = cfg->log_level;
-	}
-
-	logger->flags = cfg->log_flags;
+	logger->enabled = TRUE;
 
 	/* Set up conditional logging */
-	if (cfg->debug_ip_map != NULL) {
-		/* Try to add it as map first of all */
-		if (logger->debug_ip) {
-			rspamd_map_helper_destroy_radix (logger->debug_ip);
-		}
+	if (cfg) {
+		if (cfg->debug_ip_map != NULL) {
+			/* Try to add it as map first of all */
+			if (logger->debug_ip) {
+				rspamd_map_helper_destroy_radix (logger->debug_ip);
+			}
 
-		logger->debug_ip = NULL;
-		rspamd_config_radix_from_ucl (cfg,
-				cfg->debug_ip_map,
-				"IP addresses for which debug logs are enabled",
-				&logger->debug_ip,
-				NULL,
-				NULL);
-	}
+			logger->debug_ip = NULL;
+			rspamd_config_radix_from_ucl (cfg,
+					cfg->debug_ip_map,
+					"IP addresses for which debug logs are enabled",
+					&logger->debug_ip,
+					NULL,
+					NULL);
+		}
 
-	if (cfg->log_encryption_key) {
-		logger->pk = rspamd_pubkey_ref (cfg->log_encryption_key);
-		logger->keypair = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
-				RSPAMD_CRYPTOBOX_MODE_25519);
-		rspamd_pubkey_calculate_nm (logger->pk, logger->keypair);
+		if (cfg->log_encryption_key) {
+			logger->pk = rspamd_pubkey_ref (cfg->log_encryption_key);
+			logger->keypair = rspamd_keypair_new (RSPAMD_KEYPAIR_KEX,
+					RSPAMD_CRYPTOBOX_MODE_25519);
+			rspamd_pubkey_calculate_nm (logger->pk, logger->keypair);
+		}
 	}
 
 	default_logger = logger;
diff --git a/src/libutil/logger_console.c b/src/libutil/logger_console.c
index 05bb67f21..1bc91007a 100644
--- a/src/libutil/logger_console.c
+++ b/src/libutil/logger_console.c
@@ -85,10 +85,10 @@ rspamd_log_console_init (rspamd_logger_t *logger, struct rspamd_config *cfg,
 
 	if (isatty (priv->fd)) {
 		priv->log_tty = true;
-
-		if (priv->log_color) {
-			priv->log_color = false;
-		}
+	}
+	else if (priv->log_color) {
+		/* Disable colors for not a tty */
+		priv->log_color = false;
 	}
 
 	return priv;


More information about the Commits mailing list