diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
commit | 537a7180a0d5132c11636c4fd8b1450cd99d352c (patch) | |
tree | fb9f8c84955a411bdffbd6371ea32f2716fb3687 /src/libserver/logger | |
parent | 5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff) | |
download | rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip |
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'src/libserver/logger')
-rw-r--r-- | src/libserver/logger/logger.c | 731 | ||||
-rw-r--r-- | src/libserver/logger/logger_console.c | 179 | ||||
-rw-r--r-- | src/libserver/logger/logger_file.c | 372 | ||||
-rw-r--r-- | src/libserver/logger/logger_private.h | 96 | ||||
-rw-r--r-- | src/libserver/logger/logger_syslog.c | 91 |
5 files changed, 717 insertions, 752 deletions
diff --git a/src/libserver/logger/logger.c b/src/libserver/logger/logger.c index d44e81aa8..b62048c86 100644 --- a/src/libserver/logger/logger.c +++ b/src/libserver/logger/logger.c @@ -27,26 +27,25 @@ static rspamd_logger_t *default_logger = NULL; static rspamd_logger_t *emergency_logger = NULL; static struct rspamd_log_modules *log_modules = NULL; -guint rspamd_task_log_id = (guint)-1; +guint rspamd_task_log_id = (guint) -1; RSPAMD_CONSTRUCTOR(rspamd_task_log_init) { rspamd_task_log_id = rspamd_logger_add_debug_module("task"); } rspamd_logger_t * -rspamd_log_default_logger (void) +rspamd_log_default_logger(void) { return default_logger; } rspamd_logger_t * -rspamd_log_emergency_logger (void) +rspamd_log_emergency_logger(void) { return emergency_logger; } -void -rspamd_log_set_log_level (rspamd_logger_t *logger, gint level) +void rspamd_log_set_log_level(rspamd_logger_t *logger, gint level) { if (logger == NULL) { logger = default_logger; @@ -55,8 +54,7 @@ rspamd_log_set_log_level (rspamd_logger_t *logger, gint level) logger->log_level = level; } -gint -rspamd_log_get_log_level (rspamd_logger_t *logger) +gint rspamd_log_get_log_level(rspamd_logger_t *logger) { if (logger == NULL) { logger = default_logger; @@ -65,18 +63,16 @@ rspamd_log_get_log_level (rspamd_logger_t *logger) return logger->log_level; } -void -rspamd_log_set_log_flags (rspamd_logger_t *logger, gint flags) +void rspamd_log_set_log_flags(rspamd_logger_t *logger, gint flags) { - g_assert (logger != NULL); + g_assert(logger != NULL); logger->flags = flags; } -void -rspamd_log_close (rspamd_logger_t *logger) +void rspamd_log_close(rspamd_logger_t *logger) { - g_assert (logger != NULL); + g_assert(logger != NULL); if (logger->closed) { return; @@ -85,18 +81,18 @@ rspamd_log_close (rspamd_logger_t *logger) logger->closed = TRUE; if (logger->debug_ip) { - rspamd_map_helper_destroy_radix (logger->debug_ip); + rspamd_map_helper_destroy_radix(logger->debug_ip); } if (logger->pk) { - rspamd_pubkey_unref (logger->pk); + rspamd_pubkey_unref(logger->pk); } if (logger->keypair) { - rspamd_keypair_unref (logger->keypair); + rspamd_keypair_unref(logger->keypair); } - logger->ops.dtor (logger, logger->ops.specific); + logger->ops.dtor(logger, logger->ops.specific); /* TODO: Do we really need that ? */ if (logger == default_logger) { @@ -108,113 +104,111 @@ rspamd_log_close (rspamd_logger_t *logger) } if (!logger->pool) { - g_free (logger); + g_free(logger); } } -bool -rspamd_log_reopen (rspamd_logger_t *rspamd_log, struct rspamd_config *cfg, - uid_t uid, gid_t gid) +bool rspamd_log_reopen(rspamd_logger_t *rspamd_log, struct rspamd_config *cfg, + uid_t uid, gid_t gid) { void *nspec; GError *err = NULL; - g_assert (rspamd_log != NULL); + g_assert(rspamd_log != NULL); - nspec = rspamd_log->ops.reload (rspamd_log, cfg, rspamd_log->ops.specific, - uid, gid, &err); + nspec = rspamd_log->ops.reload(rspamd_log, cfg, rspamd_log->ops.specific, + uid, gid, &err); if (nspec != NULL) { rspamd_log->ops.specific = nspec; } else { - } return nspec != NULL; } static void -rspamd_emergency_logger_dtor (gpointer d) +rspamd_emergency_logger_dtor(gpointer d) { - rspamd_logger_t *logger = (rspamd_logger_t *)d; + rspamd_logger_t *logger = (rspamd_logger_t *) d; - rspamd_log_close (logger); + rspamd_log_close(logger); } rspamd_logger_t * -rspamd_log_open_emergency (rspamd_mempool_t *pool, gint flags) +rspamd_log_open_emergency(rspamd_mempool_t *pool, gint flags) { rspamd_logger_t *logger; GError *err = NULL; - g_assert (default_logger == NULL); - g_assert (emergency_logger == NULL); + g_assert(default_logger == NULL); + g_assert(emergency_logger == NULL); if (pool) { - logger = rspamd_mempool_alloc0 (pool, sizeof (rspamd_logger_t)); - logger->mtx = rspamd_mempool_get_mutex (pool); + logger = rspamd_mempool_alloc0(pool, sizeof(rspamd_logger_t)); + logger->mtx = rspamd_mempool_get_mutex(pool); } else { - logger = g_malloc0 (sizeof (rspamd_logger_t)); + logger = g_malloc0(sizeof(rspamd_logger_t)); } logger->flags = flags; logger->pool = pool; logger->process_type = "main"; - logger->pid = getpid (); + logger->pid = getpid(); const struct rspamd_logger_funcs *funcs = &console_log_funcs; - memcpy (&logger->ops, funcs, sizeof (*funcs)); + memcpy(&logger->ops, funcs, sizeof(*funcs)); - logger->ops.specific = logger->ops.init (logger, NULL, -1, -1, &err); + logger->ops.specific = logger->ops.init(logger, NULL, -1, -1, &err); if (logger->ops.specific == NULL) { - rspamd_fprintf (stderr, "fatal error: cannot init console logging: %e\n", - err); - g_error_free (err); + rspamd_fprintf(stderr, "fatal error: cannot init console logging: %e\n", + err); + g_error_free(err); - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } default_logger = logger; emergency_logger = logger; - rspamd_mempool_add_destructor (pool, rspamd_emergency_logger_dtor, - emergency_logger); + rspamd_mempool_add_destructor(pool, rspamd_emergency_logger_dtor, + emergency_logger); return logger; } rspamd_logger_t * -rspamd_log_open_specific (rspamd_mempool_t *pool, - struct rspamd_config *cfg, - const gchar *ptype, - uid_t uid, gid_t gid) +rspamd_log_open_specific(rspamd_mempool_t *pool, + struct rspamd_config *cfg, + const gchar *ptype, + uid_t uid, gid_t gid) { rspamd_logger_t *logger; GError *err = NULL; if (pool) { - logger = rspamd_mempool_alloc0 (pool, sizeof (rspamd_logger_t)); - logger->mtx = rspamd_mempool_get_mutex (pool); + logger = rspamd_mempool_alloc0(pool, sizeof(rspamd_logger_t)); + logger->mtx = rspamd_mempool_get_mutex(pool); } else { - logger = g_malloc0 (sizeof (rspamd_logger_t)); + logger = g_malloc0(sizeof(rspamd_logger_t)); } logger->pool = pool; if (cfg) { if (cfg->log_error_elts > 0 && pool) { - logger->errlog = rspamd_mempool_alloc0_shared (pool, - sizeof (*logger->errlog)); + 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->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; @@ -244,21 +238,21 @@ rspamd_log_open_specific (rspamd_mempool_t *pool, funcs = &console_log_funcs; } - g_assert (funcs != NULL); - memcpy (&logger->ops, funcs, sizeof (*funcs)); + g_assert(funcs != NULL); + memcpy(&logger->ops, funcs, sizeof(*funcs)); - logger->ops.specific = logger->ops.init (logger, cfg, uid, gid, &err); + logger->ops.specific = logger->ops.init(logger, cfg, uid, gid, &err); if (emergency_logger && logger->ops.specific == NULL) { - rspamd_common_log_function (emergency_logger, G_LOG_LEVEL_CRITICAL, - "logger", NULL, G_STRFUNC, - "cannot open specific logger: %e", err); - g_error_free (err); + rspamd_common_log_function(emergency_logger, G_LOG_LEVEL_CRITICAL, + "logger", NULL, G_STRFUNC, + "cannot open specific logger: %e", err); + g_error_free(err); return NULL; } - logger->pid = getpid (); + logger->pid = getpid(); logger->process_type = ptype; logger->enabled = TRUE; @@ -267,23 +261,23 @@ rspamd_log_open_specific (rspamd_mempool_t *pool, 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); + 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, "debug ip"); + rspamd_config_radix_from_ucl(cfg, + cfg->debug_ip_map, + "IP addresses for which debug logs are enabled", + &logger->debug_ip, + NULL, + NULL, "debug ip"); } 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); + 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); } } @@ -296,39 +290,38 @@ rspamd_log_open_specific (rspamd_mempool_t *pool, /** * Used after fork() for updating structure params */ -void -rspamd_log_on_fork (GQuark ptype, struct rspamd_config *cfg, - rspamd_logger_t *logger) +void rspamd_log_on_fork(GQuark ptype, struct rspamd_config *cfg, + rspamd_logger_t *logger) { - logger->pid = getpid (); - logger->process_type = g_quark_to_string (ptype); + logger->pid = getpid(); + logger->process_type = g_quark_to_string(ptype); if (logger->ops.on_fork) { GError *err = NULL; - bool ret = logger->ops.on_fork (logger, cfg, logger->ops.specific, &err); + bool ret = logger->ops.on_fork(logger, cfg, logger->ops.specific, &err); if (!ret && emergency_logger) { - rspamd_common_log_function (emergency_logger, G_LOG_LEVEL_CRITICAL, - "logger", NULL, G_STRFUNC, - "cannot update logging on fork: %e", err); - g_error_free (err); + rspamd_common_log_function(emergency_logger, G_LOG_LEVEL_CRITICAL, + "logger", NULL, G_STRFUNC, + "cannot update logging on fork: %e", err); + g_error_free(err); } } } inline gboolean -rspamd_logger_need_log (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, - gint module_id) +rspamd_logger_need_log(rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, + gint module_id) { - g_assert (rspamd_log != NULL); + g_assert(rspamd_log != NULL); if ((log_level & RSPAMD_LOG_FORCED) || - (log_level & (RSPAMD_LOG_LEVEL_MASK & G_LOG_LEVEL_MASK)) <= rspamd_log->log_level) { + (log_level & (RSPAMD_LOG_LEVEL_MASK & G_LOG_LEVEL_MASK)) <= rspamd_log->log_level) { return TRUE; } - if (module_id != -1 && isset (log_modules->bitset, module_id)) { + if (module_id != -1 && isset(log_modules->bitset, module_id)) { return TRUE; } @@ -336,8 +329,8 @@ rspamd_logger_need_log (rspamd_logger_t *rspamd_log, GLogLevelFlags log_level, } static gchar * -rspamd_log_encrypt_message (const gchar *begin, const gchar *end, gsize *enc_len, - rspamd_logger_t *rspamd_log) +rspamd_log_encrypt_message(const gchar *begin, const gchar *end, gsize *enc_len, + rspamd_logger_t *rspamd_log) { guchar *out; gchar *b64; @@ -345,38 +338,38 @@ rspamd_log_encrypt_message (const gchar *begin, const gchar *end, gsize *enc_len const guchar *comp; guint len, inlen; - g_assert (end > begin); + g_assert(end > begin); /* base64 (pubkey | nonce | message) */ - inlen = rspamd_cryptobox_nonce_bytes (RSPAMD_CRYPTOBOX_MODE_25519) + - rspamd_cryptobox_pk_bytes (RSPAMD_CRYPTOBOX_MODE_25519) + - rspamd_cryptobox_mac_bytes (RSPAMD_CRYPTOBOX_MODE_25519) + + inlen = rspamd_cryptobox_nonce_bytes(RSPAMD_CRYPTOBOX_MODE_25519) + + rspamd_cryptobox_pk_bytes(RSPAMD_CRYPTOBOX_MODE_25519) + + rspamd_cryptobox_mac_bytes(RSPAMD_CRYPTOBOX_MODE_25519) + (end - begin); - out = g_malloc (inlen); + out = g_malloc(inlen); p = out; - comp = rspamd_pubkey_get_pk (rspamd_log->pk, &len); - memcpy (p, comp, len); + comp = rspamd_pubkey_get_pk(rspamd_log->pk, &len); + memcpy(p, comp, len); p += len; - ottery_rand_bytes (p, rspamd_cryptobox_nonce_bytes (RSPAMD_CRYPTOBOX_MODE_25519)); + ottery_rand_bytes(p, rspamd_cryptobox_nonce_bytes(RSPAMD_CRYPTOBOX_MODE_25519)); nonce = p; - p += rspamd_cryptobox_nonce_bytes (RSPAMD_CRYPTOBOX_MODE_25519); + p += rspamd_cryptobox_nonce_bytes(RSPAMD_CRYPTOBOX_MODE_25519); mac = p; - p += rspamd_cryptobox_mac_bytes (RSPAMD_CRYPTOBOX_MODE_25519); - memcpy (p, begin, end - begin); - comp = rspamd_pubkey_get_nm (rspamd_log->pk, rspamd_log->keypair); - g_assert (comp != NULL); - rspamd_cryptobox_encrypt_nm_inplace (p, end - begin, nonce, comp, mac, - RSPAMD_CRYPTOBOX_MODE_25519); - b64 = rspamd_encode_base64 (out, inlen, 0, enc_len); - g_free (out); + p += rspamd_cryptobox_mac_bytes(RSPAMD_CRYPTOBOX_MODE_25519); + memcpy(p, begin, end - begin); + comp = rspamd_pubkey_get_nm(rspamd_log->pk, rspamd_log->keypair); + g_assert(comp != NULL); + rspamd_cryptobox_encrypt_nm_inplace(p, end - begin, nonce, comp, mac, + RSPAMD_CRYPTOBOX_MODE_25519); + b64 = rspamd_encode_base64(out, inlen, 0, enc_len); + g_free(out); return b64; } static void -rspamd_log_write_ringbuffer (rspamd_logger_t *rspamd_log, - const gchar *module, const gchar *id, - const gchar *data, glong len) +rspamd_log_write_ringbuffer(rspamd_logger_t *rspamd_log, + const gchar *module, const gchar *id, + const gchar *data, glong len) { guint32 row_num; struct rspamd_logger_error_log *elog; @@ -388,17 +381,17 @@ rspamd_log_write_ringbuffer (rspamd_logger_t *rspamd_log, elog = rspamd_log->errlog; - g_atomic_int_compare_and_exchange (&elog->cur_row, elog->max_elts, 0); + g_atomic_int_compare_and_exchange(&elog->cur_row, elog->max_elts, 0); #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30)) - row_num = g_atomic_int_add (&elog->cur_row, 1); + row_num = g_atomic_int_add(&elog->cur_row, 1); #else - row_num = g_atomic_int_exchange_and_add (&elog->cur_row, 1); + row_num = g_atomic_int_exchange_and_add(&elog->cur_row, 1); #endif if (row_num < elog->max_elts) { - elt = (struct rspamd_logger_error_elt *)(((guchar *)elog->elts) + - (sizeof (*elt) + elog->elt_len) * row_num); - g_atomic_int_set (&elt->completed, 0); + elt = (struct rspamd_logger_error_elt *) (((guchar *) elog->elts) + + (sizeof(*elt) + elog->elt_len) * row_num); + g_atomic_int_set(&elt->completed, 0); } else { /* Race condition */ @@ -407,31 +400,30 @@ rspamd_log_write_ringbuffer (rspamd_logger_t *rspamd_log, } elt->pid = rspamd_log->pid; - elt->ptype = g_quark_from_string (rspamd_log->process_type); - elt->ts = rspamd_get_calendar_ticks (); + elt->ptype = g_quark_from_string(rspamd_log->process_type); + elt->ts = rspamd_get_calendar_ticks(); if (id) { - rspamd_strlcpy (elt->id, id, sizeof (elt->id)); + rspamd_strlcpy(elt->id, id, sizeof(elt->id)); } else { - rspamd_strlcpy (elt->id, "", sizeof (elt->id)); + rspamd_strlcpy(elt->id, "", sizeof(elt->id)); } if (module) { - rspamd_strlcpy (elt->module, module, sizeof (elt->module)); + rspamd_strlcpy(elt->module, module, sizeof(elt->module)); } else { - rspamd_strlcpy (elt->module, "", sizeof (elt->module)); + rspamd_strlcpy(elt->module, "", sizeof(elt->module)); } - rspamd_strlcpy (elt->message, data, MIN (len + 1, elog->elt_len)); - g_atomic_int_set (&elt->completed, 1); + rspamd_strlcpy(elt->message, data, MIN(len + 1, elog->elt_len)); + g_atomic_int_set(&elt->completed, 1); } -bool -rspamd_common_logv (rspamd_logger_t *rspamd_log, gint level_flags, - const gchar *module, const gchar *id, const gchar *function, - const gchar *fmt, va_list args) +bool rspamd_common_logv(rspamd_logger_t *rspamd_log, gint level_flags, + const gchar *module, const gchar *id, const gchar *function, + const gchar *fmt, va_list args) { gchar *end; gint level = level_flags & (RSPAMD_LOG_LEVEL_MASK & G_LOG_LEVEL_MASK), mod_id; @@ -439,39 +431,39 @@ rspamd_common_logv (rspamd_logger_t *rspamd_log, gint level_flags, gchar logbuf[RSPAMD_LOGBUF_SIZE], *log_line; gsize nescaped; - if (G_UNLIKELY (rspamd_log == NULL)) { + if (G_UNLIKELY(rspamd_log == NULL)) { rspamd_log = default_logger; } log_line = logbuf; - if (G_UNLIKELY (rspamd_log == NULL)) { + if (G_UNLIKELY(rspamd_log == NULL)) { /* Just fprintf message to stderr */ if (level >= G_LOG_LEVEL_INFO) { - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, args); - rspamd_fprintf (stderr, "%*s\n", (gint)(end - log_line), - log_line); + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, args); + rspamd_fprintf(stderr, "%*s\n", (gint) (end - log_line), + log_line); } } else { if (level == G_LOG_LEVEL_DEBUG) { - mod_id = rspamd_logger_add_debug_module (module); + mod_id = rspamd_logger_add_debug_module(module); } else { mod_id = -1; } - if (rspamd_logger_need_log (rspamd_log, level_flags, mod_id)) { - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, args); + if (rspamd_logger_need_log(rspamd_log, level_flags, mod_id)) { + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, args); if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_RSPAMADM)) { - if ((nescaped = rspamd_log_line_need_escape (logbuf, end - logbuf)) != 0) { + if ((nescaped = rspamd_log_line_need_escape(logbuf, end - logbuf)) != 0) { gsize unescaped_len = end - logbuf; - gchar *logbuf_escaped = g_alloca (unescaped_len + nescaped * 4); + gchar *logbuf_escaped = g_alloca(unescaped_len + nescaped * 4); log_line = logbuf_escaped; - end = rspamd_log_line_hex_escape (logbuf, unescaped_len, - logbuf_escaped, unescaped_len + nescaped * 4); + end = rspamd_log_line_hex_escape(logbuf, unescaped_len, + logbuf_escaped, unescaped_len + nescaped * 4); } } @@ -479,32 +471,32 @@ rspamd_common_logv (rspamd_logger_t *rspamd_log, gint level_flags, gchar *encrypted; gsize enc_len; - encrypted = rspamd_log_encrypt_message (log_line, end, &enc_len, - rspamd_log); - ret = rspamd_log->ops.log (module, id, - function, - level_flags, - encrypted, - enc_len, - rspamd_log, - rspamd_log->ops.specific); - g_free (encrypted); + encrypted = rspamd_log_encrypt_message(log_line, end, &enc_len, + rspamd_log); + ret = rspamd_log->ops.log(module, id, + function, + level_flags, + encrypted, + enc_len, + rspamd_log, + rspamd_log->ops.specific); + g_free(encrypted); } else { - ret = rspamd_log->ops.log (module, id, - function, - level_flags, - log_line, - end - log_line, - rspamd_log, - rspamd_log->ops.specific); + ret = rspamd_log->ops.log(module, id, + function, + level_flags, + log_line, + end - log_line, + rspamd_log, + rspamd_log->ops.specific); } switch (level) { case G_LOG_LEVEL_CRITICAL: - rspamd_log->log_cnt[0] ++; - rspamd_log_write_ringbuffer (rspamd_log, module, id, log_line, - end - log_line); + rspamd_log->log_cnt[0]++; + rspamd_log_write_ringbuffer(rspamd_log, module, id, log_line, + end - log_line); break; case G_LOG_LEVEL_WARNING: rspamd_log->log_cnt[1]++; @@ -527,42 +519,39 @@ rspamd_common_logv (rspamd_logger_t *rspamd_log, gint level_flags, /** * This log functions select real logger and write message if level is less or equal to configured log level */ -bool -rspamd_common_log_function (rspamd_logger_t *rspamd_log, - gint level_flags, - const gchar *module, const gchar *id, - const gchar *function, - const gchar *fmt, - ...) +bool rspamd_common_log_function(rspamd_logger_t *rspamd_log, + gint level_flags, + const gchar *module, const gchar *id, + const gchar *function, + const gchar *fmt, + ...) { va_list vp; - va_start (vp, fmt); - bool ret = rspamd_common_logv (rspamd_log, level_flags, module, id, function, fmt, vp); - va_end (vp); + va_start(vp, fmt); + bool ret = rspamd_common_logv(rspamd_log, level_flags, module, id, function, fmt, vp); + va_end(vp); return ret; } -bool -rspamd_default_logv (gint level_flags, const gchar *module, const gchar *id, - const gchar *function, - const gchar *fmt, va_list args) +bool rspamd_default_logv(gint level_flags, const gchar *module, const gchar *id, + const gchar *function, + const gchar *fmt, va_list args) { - return rspamd_common_logv (NULL, level_flags, module, id, function, fmt, args); + return rspamd_common_logv(NULL, level_flags, module, id, function, fmt, args); } -bool -rspamd_default_log_function (gint level_flags, - const gchar *module, const gchar *id, - const gchar *function, const gchar *fmt, ...) +bool rspamd_default_log_function(gint level_flags, + const gchar *module, const gchar *id, + const gchar *function, const gchar *fmt, ...) { va_list vp; - va_start (vp, fmt); - bool ret = rspamd_default_logv (level_flags, module, id, function, fmt, vp); - va_end (vp); + va_start(vp, fmt); + bool ret = rspamd_default_logv(level_flags, module, id, function, fmt, vp); + va_end(vp); return ret; } @@ -574,10 +563,9 @@ rspamd_default_log_function (gint level_flags, /** * Write log line depending on ip */ -bool -rspamd_conditional_debug (rspamd_logger_t *rspamd_log, - rspamd_inet_addr_t *addr, const gchar *module, const gchar *id, - const gchar *function, const gchar *fmt, ...) +bool rspamd_conditional_debug(rspamd_logger_t *rspamd_log, + rspamd_inet_addr_t *addr, const gchar *module, const gchar *id, + const gchar *function, const gchar *fmt, ...) { static gchar logbuf[LOGBUF_LEN]; va_list vp; @@ -588,38 +576,37 @@ rspamd_conditional_debug (rspamd_logger_t *rspamd_log, rspamd_log = default_logger; } - mod_id = rspamd_logger_add_debug_module (module); + mod_id = rspamd_logger_add_debug_module(module); - if (rspamd_logger_need_log (rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || + if (rspamd_logger_need_log(rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || rspamd_log->is_debug) { if (rspamd_log->debug_ip && addr != NULL) { - if (rspamd_match_radix_map_addr (rspamd_log->debug_ip, - addr) == NULL) { + if (rspamd_match_radix_map_addr(rspamd_log->debug_ip, + addr) == NULL) { return false; } } - va_start (vp, fmt); - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp); + va_start(vp, fmt); + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, vp); *end = '\0'; - va_end (vp); - return rspamd_log->ops.log (module, id, - function, - G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, - logbuf, - end - logbuf, - rspamd_log, - rspamd_log->ops.specific); + va_end(vp); + return rspamd_log->ops.log(module, id, + function, + G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, + logbuf, + end - logbuf, + rspamd_log, + rspamd_log->ops.specific); } return false; } -bool -rspamd_conditional_debug_fast (rspamd_logger_t *rspamd_log, - rspamd_inet_addr_t *addr, - gint mod_id, const gchar *module, const gchar *id, - const gchar *function, const gchar *fmt, ...) +bool rspamd_conditional_debug_fast(rspamd_logger_t *rspamd_log, + rspamd_inet_addr_t *addr, + gint mod_id, const gchar *module, const gchar *id, + const gchar *function, const gchar *fmt, ...) { static gchar logbuf[LOGBUF_LEN]; va_list vp; @@ -629,36 +616,34 @@ rspamd_conditional_debug_fast (rspamd_logger_t *rspamd_log, rspamd_log = default_logger; } - if (rspamd_logger_need_log (rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || - rspamd_log->is_debug) { + if (rspamd_logger_need_log(rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || + rspamd_log->is_debug) { if (rspamd_log->debug_ip && addr != NULL) { - if (rspamd_match_radix_map_addr (rspamd_log->debug_ip, addr) - == NULL) { + if (rspamd_match_radix_map_addr(rspamd_log->debug_ip, addr) == NULL) { return false; } } - va_start (vp, fmt); - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp); + va_start(vp, fmt); + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, vp); *end = '\0'; - va_end (vp); - return rspamd_log->ops.log (module, id, - function, - G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, - logbuf, - end - logbuf, - rspamd_log, - rspamd_log->ops.specific); + va_end(vp); + return rspamd_log->ops.log(module, id, + function, + G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, + logbuf, + end - logbuf, + rspamd_log, + rspamd_log->ops.specific); } return false; } -bool -rspamd_conditional_debug_fast_num_id (rspamd_logger_t *rspamd_log, - rspamd_inet_addr_t *addr, - gint mod_id, const gchar *module, guint64 id, - const gchar *function, const gchar *fmt, ...) +bool rspamd_conditional_debug_fast_num_id(rspamd_logger_t *rspamd_log, + rspamd_inet_addr_t *addr, + gint mod_id, const gchar *module, guint64 id, + const gchar *function, const gchar *fmt, ...) { static gchar logbuf[LOGBUF_LEN], idbuf[64]; va_list vp; @@ -668,27 +653,26 @@ rspamd_conditional_debug_fast_num_id (rspamd_logger_t *rspamd_log, rspamd_log = default_logger; } - if (rspamd_logger_need_log (rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || + if (rspamd_logger_need_log(rspamd_log, G_LOG_LEVEL_DEBUG, mod_id) || rspamd_log->is_debug) { if (rspamd_log->debug_ip && addr != NULL) { - if (rspamd_match_radix_map_addr (rspamd_log->debug_ip, addr) - == NULL) { + if (rspamd_match_radix_map_addr(rspamd_log->debug_ip, addr) == NULL) { return false; } } - rspamd_snprintf (idbuf, sizeof (idbuf), "%XuL", id); - va_start (vp, fmt); - end = rspamd_vsnprintf (logbuf, sizeof (logbuf), fmt, vp); + rspamd_snprintf(idbuf, sizeof(idbuf), "%XuL", id); + va_start(vp, fmt); + end = rspamd_vsnprintf(logbuf, sizeof(logbuf), fmt, vp); *end = '\0'; - va_end (vp); - return rspamd_log->ops.log (module, idbuf, - function, - G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, - logbuf, - end - logbuf, - rspamd_log, - rspamd_log->ops.specific); + va_end(vp); + return rspamd_log->ops.log(module, idbuf, + function, + G_LOG_LEVEL_DEBUG | RSPAMD_LOG_FORCED, + logbuf, + end - logbuf, + rspamd_log, + rspamd_log->ops.specific); } return false; @@ -697,39 +681,36 @@ rspamd_conditional_debug_fast_num_id (rspamd_logger_t *rspamd_log, /** * Wrapper for glib logger */ -void -rspamd_glib_log_function (const gchar *log_domain, - GLogLevelFlags log_level, - const gchar *message, - gpointer arg) +void rspamd_glib_log_function(const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer arg) { - rspamd_logger_t *rspamd_log = (rspamd_logger_t *)arg; + rspamd_logger_t *rspamd_log = (rspamd_logger_t *) arg; if (rspamd_log->enabled && - rspamd_logger_need_log (rspamd_log, log_level, -1)) { - rspamd_log->ops.log ("glib", NULL, - NULL, - log_level, - message, - strlen (message), - rspamd_log, - rspamd_log->ops.specific); + rspamd_logger_need_log(rspamd_log, log_level, -1)) { + rspamd_log->ops.log("glib", NULL, + NULL, + log_level, + message, + strlen(message), + rspamd_log, + rspamd_log->ops.specific); } } -void -rspamd_glib_printerr_function (const gchar *message) +void rspamd_glib_printerr_function(const gchar *message) { - rspamd_common_log_function (NULL, G_LOG_LEVEL_CRITICAL, "glib", - NULL, G_STRFUNC, - "%s", message); + rspamd_common_log_function(NULL, G_LOG_LEVEL_CRITICAL, "glib", + NULL, G_STRFUNC, + "%s", message); } /** * Temporary turn on debugging */ -void -rspamd_log_debug (rspamd_logger_t *rspamd_log) +void rspamd_log_debug(rspamd_logger_t *rspamd_log) { rspamd_log->is_debug = TRUE; } @@ -737,14 +718,13 @@ rspamd_log_debug (rspamd_logger_t *rspamd_log) /** * Turn off temporary debugging */ -void -rspamd_log_nodebug (rspamd_logger_t *rspamd_log) +void rspamd_log_nodebug(rspamd_logger_t *rspamd_log) { rspamd_log->is_debug = FALSE; } const guint64 * -rspamd_log_counters (rspamd_logger_t *logger) +rspamd_log_counters(rspamd_logger_t *logger) { if (logger) { return logger->log_cnt; @@ -754,15 +734,15 @@ rspamd_log_counters (rspamd_logger_t *logger) } static gint -rspamd_log_errlog_cmp (const ucl_object_t **o1, const ucl_object_t **o2) +rspamd_log_errlog_cmp(const ucl_object_t **o1, const ucl_object_t **o2) { const ucl_object_t *ts1, *ts2; - ts1 = ucl_object_lookup (*o1, "ts"); - ts2 = ucl_object_lookup (*o2, "ts"); + ts1 = ucl_object_lookup(*o1, "ts"); + ts2 = ucl_object_lookup(*o2, "ts"); if (ts1 && ts2) { - gdouble t1 = ucl_object_todouble (ts1), t2 = ucl_object_todouble (ts2); + gdouble t1 = ucl_object_todouble(ts1), t2 = ucl_object_todouble(ts2); if (t1 > t2) { return -1; @@ -776,79 +756,77 @@ rspamd_log_errlog_cmp (const ucl_object_t **o1, const ucl_object_t **o2) } ucl_object_t * -rspamd_log_errorbuf_export (const rspamd_logger_t *logger) +rspamd_log_errorbuf_export(const rspamd_logger_t *logger) { struct rspamd_logger_error_elt *cpy, *cur; - ucl_object_t *top = ucl_object_typed_new (UCL_ARRAY); + ucl_object_t *top = ucl_object_typed_new(UCL_ARRAY); guint i; if (logger->errlog == NULL) { return top; } - cpy = g_malloc0_n (logger->errlog->max_elts, - sizeof (*cpy) + logger->errlog->elt_len); - memcpy (cpy, logger->errlog->elts, logger->errlog->max_elts * - (sizeof (*cpy) + logger->errlog->elt_len)); + cpy = g_malloc0_n(logger->errlog->max_elts, + sizeof(*cpy) + logger->errlog->elt_len); + memcpy(cpy, logger->errlog->elts, logger->errlog->max_elts * (sizeof(*cpy) + logger->errlog->elt_len)); - for (i = 0; i < logger->errlog->max_elts; i ++) { - cur = (struct rspamd_logger_error_elt *)((guchar *)cpy + - i * ((sizeof (*cpy) + logger->errlog->elt_len))); + for (i = 0; i < logger->errlog->max_elts; i++) { + cur = (struct rspamd_logger_error_elt *) ((guchar *) cpy + + i * ((sizeof(*cpy) + logger->errlog->elt_len))); if (cur->completed) { - ucl_object_t *obj = ucl_object_typed_new (UCL_OBJECT); - - ucl_object_insert_key (obj, ucl_object_fromdouble (cur->ts), - "ts", 0, false); - ucl_object_insert_key (obj, ucl_object_fromint (cur->pid), - "pid", 0, false); - ucl_object_insert_key (obj, - ucl_object_fromstring (g_quark_to_string (cur->ptype)), - "type", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring (cur->id), - "id", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring (cur->module), - "module", 0, false); - ucl_object_insert_key (obj, ucl_object_fromstring (cur->message), - "message", 0, false); - - ucl_array_append (top, obj); + ucl_object_t *obj = ucl_object_typed_new(UCL_OBJECT); + + ucl_object_insert_key(obj, ucl_object_fromdouble(cur->ts), + "ts", 0, false); + ucl_object_insert_key(obj, ucl_object_fromint(cur->pid), + "pid", 0, false); + ucl_object_insert_key(obj, + ucl_object_fromstring(g_quark_to_string(cur->ptype)), + "type", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(cur->id), + "id", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(cur->module), + "module", 0, false); + ucl_object_insert_key(obj, ucl_object_fromstring(cur->message), + "message", 0, false); + + ucl_array_append(top, obj); } } - ucl_object_array_sort (top, rspamd_log_errlog_cmp); - g_free (cpy); + ucl_object_array_sort(top, rspamd_log_errlog_cmp); + g_free(cpy); return top; } static guint -rspamd_logger_allocate_mod_bit (void) +rspamd_logger_allocate_mod_bit(void) { if (log_modules->bitset_allocated * NBBY > log_modules->bitset_len + 1) { - log_modules->bitset_len ++; + log_modules->bitset_len++; return log_modules->bitset_len - 1; } else { /* Need to expand */ log_modules->bitset_allocated *= 2; - log_modules->bitset = g_realloc (log_modules->bitset, - log_modules->bitset_allocated); + log_modules->bitset = g_realloc(log_modules->bitset, + log_modules->bitset_allocated); - return rspamd_logger_allocate_mod_bit (); + return rspamd_logger_allocate_mod_bit(); } } -RSPAMD_DESTRUCTOR (rspamd_debug_modules_dtor) +RSPAMD_DESTRUCTOR(rspamd_debug_modules_dtor) { if (log_modules) { - g_hash_table_unref (log_modules->modules); - g_free (log_modules->bitset); - g_free (log_modules); + g_hash_table_unref(log_modules->modules); + g_free(log_modules->bitset); + g_free(log_modules); } } -gint -rspamd_logger_add_debug_module (const gchar *mname) +gint rspamd_logger_add_debug_module(const gchar *mname) { struct rspamd_log_module *m; @@ -861,57 +839,56 @@ rspamd_logger_add_debug_module (const gchar *mname) * This is usually called from constructors, so we call init check * each time to avoid dependency issues between ctors calls */ - log_modules = g_malloc0 (sizeof (*log_modules)); - log_modules->modules = g_hash_table_new_full (rspamd_strcase_hash, - rspamd_strcase_equal, g_free, g_free); + log_modules = g_malloc0(sizeof(*log_modules)); + log_modules->modules = g_hash_table_new_full(rspamd_strcase_hash, + rspamd_strcase_equal, g_free, g_free); log_modules->bitset_allocated = 16; log_modules->bitset_len = 0; - log_modules->bitset = g_malloc0 (log_modules->bitset_allocated); + log_modules->bitset = g_malloc0(log_modules->bitset_allocated); } - if ((m = g_hash_table_lookup (log_modules->modules, mname)) == NULL) { - m = g_malloc0 (sizeof (*m)); - m->mname = g_strdup (mname); - m->id = rspamd_logger_allocate_mod_bit (); - clrbit (log_modules->bitset, m->id); - g_hash_table_insert (log_modules->modules, m->mname, m); + if ((m = g_hash_table_lookup(log_modules->modules, mname)) == NULL) { + m = g_malloc0(sizeof(*m)); + m->mname = g_strdup(mname); + m->id = rspamd_logger_allocate_mod_bit(); + clrbit(log_modules->bitset, m->id); + g_hash_table_insert(log_modules->modules, m->mname, m); } return m->id; } -void -rspamd_logger_configure_modules (GHashTable *mods_enabled) +void rspamd_logger_configure_modules(GHashTable *mods_enabled) { GHashTableIter it; gpointer k, v; guint id; /* Clear all in bitset_allocated -> this are bytes not bits */ - memset (log_modules->bitset, 0, log_modules->bitset_allocated); + memset(log_modules->bitset, 0, log_modules->bitset_allocated); /* On first iteration, we go through all modules enabled and add missing ones */ - g_hash_table_iter_init (&it, mods_enabled); + g_hash_table_iter_init(&it, mods_enabled); - while (g_hash_table_iter_next (&it, &k, &v)) { - rspamd_logger_add_debug_module ((const gchar *)k); + while (g_hash_table_iter_next(&it, &k, &v)) { + rspamd_logger_add_debug_module((const gchar *) k); } - g_hash_table_iter_init (&it, mods_enabled); + g_hash_table_iter_init(&it, mods_enabled); - while (g_hash_table_iter_next (&it, &k, &v)) { - id = rspamd_logger_add_debug_module ((const gchar *)k); + while (g_hash_table_iter_next(&it, &k, &v)) { + id = rspamd_logger_add_debug_module((const gchar *) k); - if (isclr (log_modules->bitset, id)) { - msg_info ("enable debugging for module %s (%d)", (const gchar *) k, - id); - setbit (log_modules->bitset, id); + if (isclr(log_modules->bitset, id)) { + msg_info("enable debugging for module %s (%d)", (const gchar *) k, + id); + setbit(log_modules->bitset, id); } } } -struct rspamd_logger_funcs* -rspamd_logger_set_log_function (rspamd_logger_t *logger, - struct rspamd_logger_funcs *nfuncs) +struct rspamd_logger_funcs * +rspamd_logger_set_log_function(rspamd_logger_t *logger, + struct rspamd_logger_funcs *nfuncs) { /* TODO: write this */ @@ -919,31 +896,30 @@ rspamd_logger_set_log_function (rspamd_logger_t *logger, } - gchar * -rspamd_log_line_hex_escape (const guchar *src, gsize srclen, - gchar *dst, gsize dstlen) +rspamd_log_line_hex_escape(const guchar *src, gsize srclen, + gchar *dst, gsize dstlen) { static const gchar hexdigests[16] = "0123456789ABCDEF"; gchar *d = dst; static guint32 escape[] = { - 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ - /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ - 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0100 */ + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0100 */ - /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ - 0x00000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */ + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x00000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */ - /* ~}| {zyx wvut srqp onml kjih gfed cba` */ - 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ - /* Allow all 8bit characters (assuming they are valid utf8) */ - 0x00000000, - 0x00000000, - 0x00000000, - 0x00000000, + /* Allow all 8bit characters (assuming they are valid utf8) */ + 0x00000000, + 0x00000000, + 0x00000000, + 0x00000000, }; while (srclen && dstlen) { @@ -960,9 +936,10 @@ rspamd_log_line_hex_escape (const guchar *src, gsize srclen, /* Overflow */ break; } - } else { + } + else { *d++ = *src++; - dstlen --; + dstlen--; } srclen--; @@ -971,26 +948,25 @@ rspamd_log_line_hex_escape (const guchar *src, gsize srclen, return d; } -gsize -rspamd_log_line_need_escape (const guchar *src, gsize srclen) +gsize rspamd_log_line_need_escape(const guchar *src, gsize srclen) { static guint32 escape[] = { - 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ + 0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */ - /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ - 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0100 */ + /* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */ + 0x00000000, /* 0000 0000 0000 0000 0000 0000 0000 0100 */ - /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ - 0x00000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */ + /* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */ + 0x00000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */ - /* ~}| {zyx wvut srqp onml kjih gfed cba` */ - 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ + /* ~}| {zyx wvut srqp onml kjih gfed cba` */ + 0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */ - /* Allow all 8bit characters (assuming they are valid utf8) */ - 0x00000000, - 0x00000000, - 0x00000000, - 0x00000000, + /* Allow all 8bit characters (assuming they are valid utf8) */ + 0x00000000, + 0x00000000, + 0x00000000, + 0x00000000, }; gsize n = 0; @@ -999,33 +975,32 @@ rspamd_log_line_need_escape (const guchar *src, gsize srclen) n++; } - src ++; - srclen --; + src++; + srclen--; } return n; } const gchar * -rspamd_get_log_severity_string (gint level_flags) +rspamd_get_log_severity_string(gint level_flags) { unsigned int bitnum; static const char *level_strs[G_LOG_LEVEL_USER_SHIFT] = { - "", /* G_LOG_FLAG_RECURSION */ - "", /* G_LOG_FLAG_FATAL */ - "crit", - "error", - "warn", - "notice", - "info", - "debug" - }; - level_flags &= ((1u << G_LOG_LEVEL_USER_SHIFT) - 1u) & ~(G_LOG_FLAG_RECURSION|G_LOG_FLAG_FATAL); + "", /* G_LOG_FLAG_RECURSION */ + "", /* G_LOG_FLAG_FATAL */ + "crit", + "error", + "warn", + "notice", + "info", + "debug"}; + level_flags &= ((1u << G_LOG_LEVEL_USER_SHIFT) - 1u) & ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL); #ifdef __GNUC__ /* We assume gcc >= 3 and clang >= 5 anyway */ - bitnum = __builtin_ffs (level_flags) - 1; + bitnum = __builtin_ffs(level_flags) - 1; #else - bitnum = ffs (level_flags) - 1; + bitnum = ffs(level_flags) - 1; #endif return level_strs[bitnum]; }
\ No newline at end of file diff --git a/src/libserver/logger/logger_console.c b/src/libserver/logger/logger_console.c index 2c1c535d5..b5250f657 100644 --- a/src/libserver/logger/logger_console.c +++ b/src/libserver/logger/logger_console.c @@ -22,7 +22,7 @@ #include "logger_private.h" -#define CONSOLE_LOG_QUARK g_quark_from_static_string ("console_logger") +#define CONSOLE_LOG_QUARK g_quark_from_static_string("console_logger") static const gchar lf_chr = '\n'; struct rspamd_console_logger_priv { @@ -36,56 +36,56 @@ struct rspamd_console_logger_priv { /* Copy & paste :( */ static inline void -log_time (gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, - size_t len) +log_time(gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, + size_t len) { - time_t sec = (time_t)now; + time_t sec = (time_t) now; gsize r; struct tm tms; - rspamd_localtime (sec, &tms); - r = strftime (timebuf, len, "%F %H:%M:%S", &tms); + rspamd_localtime(sec, &tms); + r = strftime(timebuf, len, "%F %H:%M:%S", &tms); if (rspamd_log->flags & RSPAMD_LOG_FLAG_USEC) { gchar usec_buf[16]; - rspamd_snprintf (usec_buf, sizeof (usec_buf), "%.5f", - now - (gdouble)sec); - rspamd_snprintf (timebuf + r, len - r, - "%s", usec_buf + 1); + rspamd_snprintf(usec_buf, sizeof(usec_buf), "%.5f", + now - (gdouble) sec); + rspamd_snprintf(timebuf + r, len - r, + "%s", usec_buf + 1); } } void * -rspamd_log_console_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err) +rspamd_log_console_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err) { struct rspamd_console_logger_priv *priv; - priv = g_malloc0 (sizeof (*priv)); + priv = g_malloc0(sizeof(*priv)); priv->log_color = (logger->flags & RSPAMD_LOG_FLAG_COLOR); priv->log_severity = (logger->flags & RSPAMD_LOG_FLAG_SEVERITY); priv->log_rspamadm = (logger->flags & RSPAMD_LOG_FLAG_RSPAMADM); if (priv->log_rspamadm) { - priv->fd = dup (STDOUT_FILENO); - priv->crit_fd = dup (STDERR_FILENO); + priv->fd = dup(STDOUT_FILENO); + priv->crit_fd = dup(STDERR_FILENO); } else { - priv->fd = dup (STDERR_FILENO); + priv->fd = dup(STDERR_FILENO); priv->crit_fd = priv->fd; } if (priv->fd == -1) { - g_set_error (err, CONSOLE_LOG_QUARK, errno, - "open_log: cannot dup console fd: %s\n", - strerror (errno)); - rspamd_log_console_dtor (logger, priv); + g_set_error(err, CONSOLE_LOG_QUARK, errno, + "open_log: cannot dup console fd: %s\n", + strerror(errno)); + rspamd_log_console_dtor(logger, priv); return NULL; } - if (isatty (priv->fd)) { + if (isatty(priv->fd)) { priv->log_tty = true; } else if (priv->log_color) { @@ -97,38 +97,37 @@ rspamd_log_console_init (rspamd_logger_t *logger, struct rspamd_config *cfg, } void * -rspamd_log_console_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err) +rspamd_log_console_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err) { struct rspamd_console_logger_priv *npriv; - npriv = rspamd_log_console_init (logger, cfg, uid, gid, err); + npriv = rspamd_log_console_init(logger, cfg, uid, gid, err); if (npriv) { /* Close old */ - rspamd_log_console_dtor (logger, arg); + rspamd_log_console_dtor(logger, arg); } return npriv; } -void -rspamd_log_console_dtor (rspamd_logger_t *logger, gpointer arg) +void rspamd_log_console_dtor(rspamd_logger_t *logger, gpointer arg) { - struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *)arg; + struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *) arg; if (priv->fd != -1) { if (priv->fd != priv->crit_fd) { /* Two different FD case */ - if (close (priv->crit_fd) == -1) { - rspamd_fprintf (stderr, "cannot close log crit_fd %d: %s\n", - priv->crit_fd, strerror (errno)); + if (close(priv->crit_fd) == -1) { + rspamd_fprintf(stderr, "cannot close log crit_fd %d: %s\n", + priv->crit_fd, strerror(errno)); } } - if (close (priv->fd) == -1) { - rspamd_fprintf (stderr, "cannot close log fd %d: %s\n", - priv->fd, strerror (errno)); + if (close(priv->fd) == -1) { + rspamd_fprintf(stderr, "cannot close log fd %d: %s\n", + priv->fd, strerror(errno)); } /* Avoid the next if to be executed as crit_fd is equal to fd */ @@ -136,25 +135,24 @@ rspamd_log_console_dtor (rspamd_logger_t *logger, gpointer arg) } if (priv->crit_fd != -1) { - if (close (priv->crit_fd) == -1) { - rspamd_fprintf (stderr, "cannot close log crit_fd %d: %s\n", - priv->crit_fd, strerror (errno)); + if (close(priv->crit_fd) == -1) { + rspamd_fprintf(stderr, "cannot close log crit_fd %d: %s\n", + priv->crit_fd, strerror(errno)); } } - g_free (priv); + g_free(priv); } -bool -rspamd_log_console_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg) +bool rspamd_log_console_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg) { - struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *)arg; + struct rspamd_console_logger_priv *priv = (struct rspamd_console_logger_priv *) arg; static gchar timebuf[64], modulebuf[64]; gchar tmpbuf[256]; gchar *m; @@ -177,32 +175,32 @@ rspamd_log_console_log (const gchar *module, const gchar *id, #ifndef DISABLE_PTHREAD_MUTEX if (rspamd_log->mtx) { - rspamd_mempool_lock_mutex (rspamd_log->mtx); + rspamd_mempool_lock_mutex(rspamd_log->mtx); } else { - rspamd_file_lock (fd, FALSE); + rspamd_file_lock(fd, FALSE); } #else - rspamd_file_lock (fd, FALSE); + rspamd_file_lock(fd, FALSE); #endif if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { - log_time (rspamd_get_calendar_ticks (), - rspamd_log, timebuf, sizeof (timebuf)); + log_time(rspamd_get_calendar_ticks(), + rspamd_log, timebuf, sizeof(timebuf)); } if (priv->log_color) { - if (level_flags & (G_LOG_LEVEL_INFO|G_LOG_LEVEL_MESSAGE)) { + if (level_flags & (G_LOG_LEVEL_INFO | G_LOG_LEVEL_MESSAGE)) { /* White */ - r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "\033[0;37m"); + r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "\033[0;37m"); } else if (level_flags & G_LOG_LEVEL_WARNING) { /* Magenta */ - r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "\033[0;32m"); + r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "\033[0;32m"); } else if (level_flags & G_LOG_LEVEL_CRITICAL) { /* Red */ - r = rspamd_snprintf (tmpbuf, sizeof (tmpbuf), "\033[1;31m"); + r = rspamd_snprintf(tmpbuf, sizeof(tmpbuf), "\033[1;31m"); } } else { @@ -211,10 +209,10 @@ rspamd_log_console_log (const gchar *module, const gchar *id, if (priv->log_rspamadm) { if (rspamd_log->log_level == G_LOG_LEVEL_DEBUG) { - log_time (rspamd_get_calendar_ticks (), - rspamd_log, timebuf, sizeof (timebuf)); + log_time(rspamd_get_calendar_ticks(), + rspamd_log, timebuf, sizeof(timebuf)); iov[niov].iov_base = (void *) timebuf; - iov[niov++].iov_len = strlen (timebuf); + iov[niov++].iov_len = strlen(timebuf); iov[niov].iov_base = (void *) " "; iov[niov++].iov_len = 1; } @@ -228,53 +226,54 @@ rspamd_log_console_log (const gchar *module, const gchar *id, if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { if (priv->log_severity) { r += rspamd_snprintf(tmpbuf + r, - sizeof(tmpbuf) - r, - "%s [%s] #%P(%s) ", - timebuf, - rspamd_get_log_severity_string (level_flags), - rspamd_log->pid, - rspamd_log->process_type); + sizeof(tmpbuf) - r, + "%s [%s] #%P(%s) ", + timebuf, + rspamd_get_log_severity_string(level_flags), + rspamd_log->pid, + rspamd_log->process_type); } else { r += rspamd_snprintf(tmpbuf + r, - sizeof(tmpbuf) - r, - "%s #%P(%s) ", - timebuf, - rspamd_log->pid, - rspamd_log->process_type); + sizeof(tmpbuf) - r, + "%s #%P(%s) ", + timebuf, + rspamd_log->pid, + rspamd_log->process_type); } - } else { - r += rspamd_snprintf (tmpbuf + r, - sizeof (tmpbuf) - r, - "#%P(%s) ", - rspamd_log->pid, - rspamd_log->process_type); + } + else { + r += rspamd_snprintf(tmpbuf + r, + sizeof(tmpbuf) - r, + "#%P(%s) ", + rspamd_log->pid, + rspamd_log->process_type); } modulebuf[0] = '\0'; - mremain = sizeof (modulebuf); + mremain = sizeof(modulebuf); m = modulebuf; if (id != NULL) { - guint slen = strlen (id); - slen = MIN (RSPAMD_LOG_ID_LEN, slen); - mr = rspamd_snprintf (m, mremain, "<%*.s>; ", slen, - id); + guint slen = strlen(id); + slen = MIN(RSPAMD_LOG_ID_LEN, slen); + mr = rspamd_snprintf(m, mremain, "<%*.s>; ", slen, + id); m += mr; mremain -= mr; } if (module != NULL) { - mr = rspamd_snprintf (m, mremain, "%s; ", module); + mr = rspamd_snprintf(m, mremain, "%s; ", module); m += mr; mremain -= mr; } if (function != NULL) { - mr = rspamd_snprintf (m, mremain, "%s: ", function); + mr = rspamd_snprintf(m, mremain, "%s: ", function); m += mr; mremain -= mr; } else { - mr = rspamd_snprintf (m, mremain, ": "); + mr = rspamd_snprintf(m, mremain, ": "); m += mr; mremain -= mr; } @@ -296,11 +295,11 @@ rspamd_log_console_log (const gchar *module, const gchar *id, if (priv->log_color) { iov[niov].iov_base = "\033[0m"; - iov[niov++].iov_len = sizeof ("\033[0m") - 1; + iov[niov++].iov_len = sizeof("\033[0m") - 1; } again: - r = writev (fd, iov, niov); + r = writev(fd, iov, niov); if (r == -1) { if (errno == EAGAIN || errno == EINTR) { @@ -308,20 +307,20 @@ again: } if (rspamd_log->mtx) { - rspamd_mempool_unlock_mutex (rspamd_log->mtx); + rspamd_mempool_unlock_mutex(rspamd_log->mtx); } else { - rspamd_file_unlock (fd, FALSE); + rspamd_file_unlock(fd, FALSE); } return false; } if (rspamd_log->mtx) { - rspamd_mempool_unlock_mutex (rspamd_log->mtx); + rspamd_mempool_unlock_mutex(rspamd_log->mtx); } else { - rspamd_file_unlock (fd, FALSE); + rspamd_file_unlock(fd, FALSE); } return true; diff --git a/src/libserver/logger/logger_file.c b/src/libserver/logger/logger_file.c index 6531f9916..91ae1956c 100644 --- a/src/libserver/logger/logger_file.c +++ b/src/libserver/logger/logger_file.c @@ -22,7 +22,7 @@ #include "logger_private.h" -#define FILE_LOG_QUARK g_quark_from_static_string ("file_logger") +#define FILE_LOG_QUARK g_quark_from_static_string("file_logger") static const gchar lf_chr = '\n'; @@ -52,29 +52,29 @@ struct rspamd_file_logger_priv { * Calculate checksum for log line (used for repeating logic) */ static inline guint64 -rspamd_log_calculate_cksum (const gchar *message, size_t mlen) +rspamd_log_calculate_cksum(const gchar *message, size_t mlen) { - return rspamd_cryptobox_fast_hash (message, mlen, rspamd_hash_seed ()); + return rspamd_cryptobox_fast_hash(message, mlen, rspamd_hash_seed()); } static inline void -log_time (gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, - size_t len) +log_time(gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, + size_t len) { - time_t sec = (time_t)now; + time_t sec = (time_t) now; gsize r; struct tm tms; - rspamd_localtime (sec, &tms); - r = strftime (timebuf, len, "%F %H:%M:%S", &tms); + rspamd_localtime(sec, &tms); + r = strftime(timebuf, len, "%F %H:%M:%S", &tms); if (rspamd_log->flags & RSPAMD_LOG_FLAG_USEC) { gchar usec_buf[16]; - rspamd_snprintf (usec_buf, sizeof (usec_buf), "%.5f", - now - (gdouble)sec); - rspamd_snprintf (timebuf + r, len - r, - "%s", usec_buf + 1); + rspamd_snprintf(usec_buf, sizeof(usec_buf), "%.5f", + now - (gdouble) sec); + rspamd_snprintf(timebuf + r, len - r, + "%s", usec_buf + 1); } } @@ -83,12 +83,12 @@ log_time (gdouble now, rspamd_logger_t *rspamd_log, gchar *timebuf, * Write a line to log file (unbuffered) */ static bool -direct_write_log_line (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv, - void *data, - gsize count, - gboolean is_iov, - gint level_flags) +direct_write_log_line(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv, + void *data, + gsize count, + gboolean is_iov, + gint level_flags) { struct iovec *iov; const gchar *line; @@ -105,7 +105,7 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, if (is_iov) { tlen = 0; - for (guint i = 0; i < count; i ++) { + for (guint i = 0; i < count; i++) { tlen += iov[i].iov_len; } } @@ -118,35 +118,35 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, #ifndef DISABLE_PTHREAD_MUTEX if (rspamd_log->mtx) { - rspamd_mempool_lock_mutex (rspamd_log->mtx); + rspamd_mempool_lock_mutex(rspamd_log->mtx); } else { - rspamd_file_lock (fd, FALSE); + rspamd_file_lock(fd, FALSE); } #else - rspamd_file_lock (fd, FALSE); + rspamd_file_lock(fd, FALSE); #endif } } if (is_iov) { - r = writev (fd, iov, count); + r = writev(fd, iov, count); } else { line = (const gchar *) data; - r = write (fd, line, count); + r = write(fd, line, count); } if (locked) { #ifndef DISABLE_PTHREAD_MUTEX if (rspamd_log->mtx) { - rspamd_mempool_unlock_mutex (rspamd_log->mtx); + rspamd_mempool_unlock_mutex(rspamd_log->mtx); } else { - rspamd_file_unlock (fd, FALSE); + rspamd_file_unlock(fd, FALSE); } #else - rspamd_file_unlock (fd, FALSE); + rspamd_file_unlock(fd, FALSE); #endif } @@ -154,14 +154,14 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, /* We cannot write message to file, so we need to detect error and make decision */ if (errno == EINTR) { /* Try again */ - return direct_write_log_line (rspamd_log, priv, data, count, is_iov, level_flags); + return direct_write_log_line(rspamd_log, priv, data, count, is_iov, level_flags); } if (errno == EFAULT || errno == EINVAL || errno == EFBIG || errno == ENOSPC) { /* Rare case */ priv->throttling = TRUE; - priv->throttling_time = time (NULL); + priv->throttling_time = time(NULL); } else if (errno == EPIPE || errno == EBADF) { /* We write to some pipe and it disappears, disable logging or we has opened bad file descriptor */ @@ -181,31 +181,30 @@ direct_write_log_line (rspamd_logger_t *rspamd_log, * Fill buffer with message (limits must be checked BEFORE this call) */ static void -fill_buffer (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv, - const struct iovec *iov, gint iovcnt) +fill_buffer(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv, + const struct iovec *iov, gint iovcnt) { gint i; for (i = 0; i < iovcnt; i++) { - memcpy (priv->io_buf.buf + priv->io_buf.used, - iov[i].iov_base, - iov[i].iov_len); + memcpy(priv->io_buf.buf + priv->io_buf.used, + iov[i].iov_base, + iov[i].iov_len); priv->io_buf.used += iov[i].iov_len; } - } static void -rspamd_log_flush (rspamd_logger_t *rspamd_log, struct rspamd_file_logger_priv *priv) +rspamd_log_flush(rspamd_logger_t *rspamd_log, struct rspamd_file_logger_priv *priv) { if (priv->is_buffered) { - direct_write_log_line (rspamd_log, - priv, - priv->io_buf.buf, - priv->io_buf.used, - FALSE, - rspamd_log->log_level); + direct_write_log_line(rspamd_log, + priv, + priv->io_buf.buf, + priv->io_buf.used, + FALSE, + rspamd_log->log_level); priv->io_buf.used = 0; } } @@ -214,19 +213,19 @@ rspamd_log_flush (rspamd_logger_t *rspamd_log, struct rspamd_file_logger_priv *p * Write message to buffer or to file (using direct_write_log_line function) */ static bool -file_log_helper (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv, - const struct iovec *iov, - guint iovcnt, - gint level_flags) +file_log_helper(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv, + const struct iovec *iov, + guint iovcnt, + gint level_flags) { size_t len = 0; guint i; if (!priv->is_buffered) { /* Write string directly */ - return direct_write_log_line (rspamd_log, priv, (void *) iov, iovcnt, - TRUE, level_flags); + return direct_write_log_line(rspamd_log, priv, (void *) iov, iovcnt, + TRUE, level_flags); } else { /* Calculate total length */ @@ -236,18 +235,18 @@ file_log_helper (rspamd_logger_t *rspamd_log, /* Fill buffer */ if (priv->io_buf.size < len) { /* Buffer is too small to hold this string, so write it directly */ - rspamd_log_flush (rspamd_log, priv); - return direct_write_log_line (rspamd_log, priv, (void *) iov, iovcnt, - TRUE, level_flags); + rspamd_log_flush(rspamd_log, priv); + return direct_write_log_line(rspamd_log, priv, (void *) iov, iovcnt, + TRUE, level_flags); } else if (priv->io_buf.used + len >= priv->io_buf.size) { /* Buffer is full, try to write it directly */ - rspamd_log_flush (rspamd_log, priv); - fill_buffer (rspamd_log, priv, iov, iovcnt); + rspamd_log_flush(rspamd_log, priv); + fill_buffer(rspamd_log, priv, iov, iovcnt); } else { /* Copy incoming string to buffer */ - fill_buffer (rspamd_log, priv, iov, iovcnt); + fill_buffer(rspamd_log, priv, iov, iovcnt); } } @@ -255,33 +254,33 @@ file_log_helper (rspamd_logger_t *rspamd_log, } static void -rspamd_log_reset_repeated (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv) +rspamd_log_reset_repeated(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv) { gchar tmpbuf[256]; gssize r; if (priv->repeats > REPEATS_MIN) { - r = rspamd_snprintf (tmpbuf, - sizeof (tmpbuf), - "Last message repeated %ud times", - priv->repeats - REPEATS_MIN); + r = rspamd_snprintf(tmpbuf, + sizeof(tmpbuf), + "Last message repeated %ud times", + priv->repeats - REPEATS_MIN); priv->repeats = 0; if (priv->saved_message) { - rspamd_log_file_log (priv->saved_module, - priv->saved_id, - priv->saved_function, - priv->saved_loglevel | RSPAMD_LOG_FORCED, - priv->saved_message, - priv->saved_mlen, - rspamd_log, - priv); - - g_free (priv->saved_message); - g_free (priv->saved_function); - g_free (priv->saved_module); - g_free (priv->saved_id); + rspamd_log_file_log(priv->saved_module, + priv->saved_id, + priv->saved_function, + priv->saved_loglevel | RSPAMD_LOG_FORCED, + priv->saved_message, + priv->saved_mlen, + rspamd_log, + priv); + + g_free(priv->saved_message); + g_free(priv->saved_function); + g_free(priv->saved_module); + g_free(priv->saved_id); priv->saved_message = NULL; priv->saved_function = NULL; priv->saved_module = NULL; @@ -289,41 +288,41 @@ rspamd_log_reset_repeated (rspamd_logger_t *rspamd_log, } /* It is safe to use temporary buffer here as it is not static */ - rspamd_log_file_log (NULL, NULL, - G_STRFUNC, - priv->saved_loglevel | RSPAMD_LOG_FORCED, - tmpbuf, - r, - rspamd_log, - priv); - rspamd_log_flush (rspamd_log, priv); + rspamd_log_file_log(NULL, NULL, + G_STRFUNC, + priv->saved_loglevel | RSPAMD_LOG_FORCED, + tmpbuf, + r, + rspamd_log, + priv); + rspamd_log_flush(rspamd_log, priv); } } static gint -rspamd_try_open_log_fd (rspamd_logger_t *rspamd_log, - struct rspamd_file_logger_priv *priv, - uid_t uid, gid_t gid, - GError **err) +rspamd_try_open_log_fd(rspamd_logger_t *rspamd_log, + struct rspamd_file_logger_priv *priv, + uid_t uid, gid_t gid, + GError **err) { gint fd; - fd = open (priv->log_file, - O_CREAT | O_WRONLY | O_APPEND, - S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); + fd = open(priv->log_file, + O_CREAT | O_WRONLY | O_APPEND, + S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); if (fd == -1) { - g_set_error (err, FILE_LOG_QUARK, errno, - "open_log: cannot open desired log file: %s, %s\n", - priv->log_file, strerror (errno)); + g_set_error(err, FILE_LOG_QUARK, errno, + "open_log: cannot open desired log file: %s, %s\n", + priv->log_file, strerror(errno)); return -1; } if (uid != -1 || gid != -1) { - if (fchown (fd, uid, gid) == -1) { - g_set_error (err, FILE_LOG_QUARK, errno, - "open_log: cannot chown desired log file: %s, %s\n", - priv->log_file, strerror (errno)); - close (fd); + if (fchown(fd, uid, gid) == -1) { + g_set_error(err, FILE_LOG_QUARK, errno, + "open_log: cannot chown desired log file: %s, %s\n", + priv->log_file, strerror(errno)); + close(fd); return -1; } @@ -333,18 +332,18 @@ rspamd_try_open_log_fd (rspamd_logger_t *rspamd_log, } void * -rspamd_log_file_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err) +rspamd_log_file_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err) { struct rspamd_file_logger_priv *priv; if (!cfg || !cfg->cfg_name) { - g_set_error (err, FILE_LOG_QUARK, EINVAL, - "no log file specified"); + g_set_error(err, FILE_LOG_QUARK, EINVAL, + "no log file specified"); return NULL; } - priv = g_malloc0 (sizeof (*priv)); + priv = g_malloc0(sizeof(*priv)); if (cfg->log_buffered) { if (cfg->log_buf_size != 0) { @@ -354,18 +353,18 @@ rspamd_log_file_init (rspamd_logger_t *logger, struct rspamd_config *cfg, priv->io_buf.size = LOGBUF_LEN; } priv->is_buffered = TRUE; - priv->io_buf.buf = g_malloc (priv->io_buf.size); + priv->io_buf.buf = g_malloc(priv->io_buf.size); } if (cfg->log_file) { - priv->log_file = g_strdup (cfg->log_file); + priv->log_file = g_strdup(cfg->log_file); } priv->log_severity = (logger->flags & RSPAMD_LOG_FLAG_SEVERITY); - priv->fd = rspamd_try_open_log_fd (logger, priv, uid, gid, err); + priv->fd = rspamd_try_open_log_fd(logger, priv, uid, gid, err); if (priv->fd == -1) { - rspamd_log_file_dtor (logger, priv); + rspamd_log_file_dtor(logger, priv); return NULL; } @@ -373,35 +372,33 @@ rspamd_log_file_init (rspamd_logger_t *logger, struct rspamd_config *cfg, return priv; } -void -rspamd_log_file_dtor (rspamd_logger_t *logger, gpointer arg) +void rspamd_log_file_dtor(rspamd_logger_t *logger, gpointer arg) { - struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *)arg; + struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *) arg; - rspamd_log_reset_repeated (logger, priv); - rspamd_log_flush (logger, priv); + rspamd_log_reset_repeated(logger, priv); + rspamd_log_flush(logger, priv); if (priv->fd != -1) { - if (close (priv->fd) == -1) { - rspamd_fprintf (stderr, "cannot close log fd %d: %s; log file = %s\n", - priv->fd, strerror (errno), priv->log_file); + if (close(priv->fd) == -1) { + rspamd_fprintf(stderr, "cannot close log fd %d: %s; log file = %s\n", + priv->fd, strerror(errno), priv->log_file); } } - g_free (priv->log_file); - g_free (priv); + g_free(priv->log_file); + g_free(priv); } -bool -rspamd_log_file_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg) +bool rspamd_log_file_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg) { - struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *)arg; + struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *) arg; static gchar timebuf[64], modulebuf[64]; gchar tmpbuf[256]; gchar *m; @@ -420,7 +417,7 @@ rspamd_log_file_log (const gchar *module, const gchar *id, /* Check throttling due to write errors */ if (!(level_flags & RSPAMD_LOG_FORCED) && priv->throttling) { - now = rspamd_get_calendar_ticks (); + now = rspamd_get_calendar_ticks(); if (priv->throttling_time != now) { priv->throttling_time = now; @@ -433,26 +430,26 @@ rspamd_log_file_log (const gchar *module, const gchar *id, } /* Check repeats */ - cksum = rspamd_log_calculate_cksum (message, mlen); + cksum = rspamd_log_calculate_cksum(message, mlen); if (cksum == priv->last_line_cksum) { priv->repeats++; if (priv->repeats > REPEATS_MIN && priv->repeats < - REPEATS_MAX) { + REPEATS_MAX) { /* Do not log anything but save message for future */ if (priv->saved_message == NULL) { - priv->saved_function = g_strdup (function); + priv->saved_function = g_strdup(function); priv->saved_mlen = mlen; - priv->saved_message = g_malloc (mlen); - memcpy (priv->saved_message, message, mlen); + priv->saved_message = g_malloc(mlen); + memcpy(priv->saved_message, message, mlen); if (module) { - priv->saved_module = g_strdup (module); + priv->saved_module = g_strdup(module); } if (id) { - priv->saved_id = g_strdup (id); + priv->saved_id = g_strdup(id); } priv->saved_loglevel = level_flags; @@ -461,15 +458,15 @@ rspamd_log_file_log (const gchar *module, const gchar *id, return true; } else if (priv->repeats > REPEATS_MAX) { - rspamd_log_reset_repeated (rspamd_log, priv); + rspamd_log_reset_repeated(rspamd_log, priv); - bool ret = rspamd_log_file_log (module, id, - function, - level_flags, - message, - mlen, - rspamd_log, - priv); + bool ret = rspamd_log_file_log(module, id, + function, + level_flags, + message, + mlen, + rspamd_log, + priv); /* Probably we have more repeats in future */ priv->repeats = REPEATS_MIN + 1; @@ -482,26 +479,26 @@ rspamd_log_file_log (const gchar *module, const gchar *id, priv->last_line_cksum = cksum; if (priv->repeats > REPEATS_MIN) { - rspamd_log_reset_repeated (rspamd_log, priv); - return rspamd_log_file_log (module, id, - function, - level_flags, - message, - mlen, - rspamd_log, - arg); + rspamd_log_reset_repeated(rspamd_log, priv); + return rspamd_log_file_log(module, id, + function, + level_flags, + message, + mlen, + rspamd_log, + arg); } else { priv->repeats = 0; } } if (!got_time) { - now = rspamd_get_calendar_ticks (); + now = rspamd_get_calendar_ticks(); } /* Format time */ if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { - log_time (now, rspamd_log, timebuf, sizeof (timebuf)); + log_time(now, rspamd_log, timebuf, sizeof(timebuf)); } cptype = rspamd_log->process_type; @@ -510,53 +507,53 @@ rspamd_log_file_log (const gchar *module, const gchar *id, if (!(rspamd_log->flags & RSPAMD_LOG_FLAG_SYSTEMD)) { if (priv->log_severity) { r += rspamd_snprintf(tmpbuf + r, - sizeof(tmpbuf) - r, - "%s [%s] #%P(%s) ", - timebuf, - rspamd_get_log_severity_string (level_flags), - rspamd_log->pid, - cptype); + sizeof(tmpbuf) - r, + "%s [%s] #%P(%s) ", + timebuf, + rspamd_get_log_severity_string(level_flags), + rspamd_log->pid, + cptype); } else { r += rspamd_snprintf(tmpbuf + r, - sizeof(tmpbuf) - r, - "%s #%P(%s) ", - timebuf, - rspamd_log->pid, - cptype); + sizeof(tmpbuf) - r, + "%s #%P(%s) ", + timebuf, + rspamd_log->pid, + cptype); } } else { - r += rspamd_snprintf (tmpbuf + r, - sizeof (tmpbuf) - r, - "(%s) ", - cptype); + r += rspamd_snprintf(tmpbuf + r, + sizeof(tmpbuf) - r, + "(%s) ", + cptype); } modulebuf[0] = '\0'; - mremain = sizeof (modulebuf); + mremain = sizeof(modulebuf); m = modulebuf; if (id != NULL) { - guint slen = strlen (id); - slen = MIN (RSPAMD_LOG_ID_LEN, slen); - mr = rspamd_snprintf (m, mremain, "<%*.s>; ", slen, - id); + guint slen = strlen(id); + slen = MIN(RSPAMD_LOG_ID_LEN, slen); + mr = rspamd_snprintf(m, mremain, "<%*.s>; ", slen, + id); m += mr; mremain -= mr; } if (module != NULL) { - mr = rspamd_snprintf (m, mremain, "%s; ", module); + mr = rspamd_snprintf(m, mremain, "%s; ", module); m += mr; mremain -= mr; } if (function != NULL) { - mr = rspamd_snprintf (m, mremain, "%s: ", function); + mr = rspamd_snprintf(m, mremain, "%s: ", function); m += mr; mremain -= mr; } else { - mr = rspamd_snprintf (m, mremain, ": "); + mr = rspamd_snprintf(m, mremain, ": "); m += mr; mremain -= mr; } @@ -576,39 +573,38 @@ rspamd_log_file_log (const gchar *module, const gchar *id, iov[3].iov_base = (void *) &lf_chr; iov[3].iov_len = 1; - return file_log_helper (rspamd_log, priv, iov, 4, level_flags); + return file_log_helper(rspamd_log, priv, iov, 4, level_flags); } void * -rspamd_log_file_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err) +rspamd_log_file_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err) { struct rspamd_file_logger_priv *npriv; if (!cfg->cfg_name) { - g_set_error (err, FILE_LOG_QUARK, EINVAL, - "no log file specified"); + g_set_error(err, FILE_LOG_QUARK, EINVAL, + "no log file specified"); return NULL; } - npriv = rspamd_log_file_init (logger, cfg, uid, gid, err); + npriv = rspamd_log_file_init(logger, cfg, uid, gid, err); if (npriv) { /* Close old */ - rspamd_log_file_dtor (logger, arg); + rspamd_log_file_dtor(logger, arg); } return npriv; } -bool -rspamd_log_file_on_fork (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, GError **err) +bool rspamd_log_file_on_fork(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, GError **err) { - struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *)arg; + struct rspamd_file_logger_priv *priv = (struct rspamd_file_logger_priv *) arg; - rspamd_log_reset_repeated (logger, priv); - rspamd_log_flush (logger, priv); + rspamd_log_reset_repeated(logger, priv); + rspamd_log_flush(logger, priv); return true; }
\ No newline at end of file diff --git a/src/libserver/logger/logger_private.h b/src/libserver/logger/logger_private.h index cb06abe7c..a5ce1cdc3 100644 --- a/src/libserver/logger/logger_private.h +++ b/src/libserver/logger/logger_private.h @@ -30,7 +30,7 @@ struct rspamd_log_module { struct rspamd_log_modules { guchar *bitset; - guint bitset_len; /* Number of BITS used in bitset */ + guint bitset_len; /* Number of BITS used in bitset */ guint bitset_allocated; /* Size of bitset allocated in BYTES */ GHashTable *modules; }; @@ -88,20 +88,20 @@ struct rspamd_logger_s { /* * File logging */ -void * rspamd_log_file_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err); -void * rspamd_log_file_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err); -void rspamd_log_file_dtor (rspamd_logger_t *logger, gpointer arg); -bool rspamd_log_file_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg); -bool rspamd_log_file_on_fork (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, GError **err); +void *rspamd_log_file_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err); +void *rspamd_log_file_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err); +void rspamd_log_file_dtor(rspamd_logger_t *logger, gpointer arg); +bool rspamd_log_file_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg); +bool rspamd_log_file_on_fork(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, GError **err); /** * Escape log line by replacing unprintable characters to hex escapes like \xNN * @param src @@ -110,7 +110,7 @@ bool rspamd_log_file_on_fork (rspamd_logger_t *logger, struct rspamd_config *cfg * @param dstlen * @return end of the escaped buffer */ -gchar* rspamd_log_line_hex_escape (const guchar *src, gsize srclen, +gchar *rspamd_log_line_hex_escape(const guchar *src, gsize srclen, gchar *dst, gsize dstlen); /** * Returns number of characters to be escaped, e.g. a caller can allocate a new buffer @@ -119,49 +119,49 @@ gchar* rspamd_log_line_hex_escape (const guchar *src, gsize srclen, * @param srclen * @return number of characters to be escaped */ -gsize rspamd_log_line_need_escape (const guchar *src, gsize srclen); +gsize rspamd_log_line_need_escape(const guchar *src, gsize srclen); static const struct rspamd_logger_funcs file_log_funcs = { - .init = rspamd_log_file_init, - .dtor = rspamd_log_file_dtor, - .reload = rspamd_log_file_reload, - .log = rspamd_log_file_log, - .on_fork = rspamd_log_file_on_fork, + .init = rspamd_log_file_init, + .dtor = rspamd_log_file_dtor, + .reload = rspamd_log_file_reload, + .log = rspamd_log_file_log, + .on_fork = rspamd_log_file_on_fork, }; /* * Syslog logging */ -void * rspamd_log_syslog_init (rspamd_logger_t *logger, struct rspamd_config *cfg, +void *rspamd_log_syslog_init(rspamd_logger_t *logger, struct rspamd_config *cfg, uid_t uid, gid_t gid, GError **err); -void * rspamd_log_syslog_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, +void *rspamd_log_syslog_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, gpointer arg, uid_t uid, gid_t gid, GError **err); -void rspamd_log_syslog_dtor (rspamd_logger_t *logger, gpointer arg); -bool rspamd_log_syslog_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg); +void rspamd_log_syslog_dtor(rspamd_logger_t *logger, gpointer arg); +bool rspamd_log_syslog_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg); static const struct rspamd_logger_funcs syslog_log_funcs = { - .init = rspamd_log_syslog_init, - .dtor = rspamd_log_syslog_dtor, - .reload = rspamd_log_syslog_reload, - .log = rspamd_log_syslog_log, - .on_fork = NULL, + .init = rspamd_log_syslog_init, + .dtor = rspamd_log_syslog_dtor, + .reload = rspamd_log_syslog_reload, + .log = rspamd_log_syslog_log, + .on_fork = NULL, }; /* * Console logging */ -void * rspamd_log_console_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err); -void * rspamd_log_console_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err); -void rspamd_log_console_dtor (rspamd_logger_t *logger, gpointer arg); -bool rspamd_log_console_log (const gchar *module, const gchar *id, +void *rspamd_log_console_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err); +void *rspamd_log_console_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err); +void rspamd_log_console_dtor(rspamd_logger_t *logger, gpointer arg); +bool rspamd_log_console_log(const gchar *module, const gchar *id, const gchar *function, gint level_flags, const gchar *message, @@ -170,11 +170,11 @@ bool rspamd_log_console_log (const gchar *module, const gchar *id, gpointer arg); static const struct rspamd_logger_funcs console_log_funcs = { - .init = rspamd_log_console_init, - .dtor = rspamd_log_console_dtor, - .reload = rspamd_log_console_reload, - .log = rspamd_log_console_log, - .on_fork = NULL, + .init = rspamd_log_console_init, + .dtor = rspamd_log_console_dtor, + .reload = rspamd_log_console_reload, + .log = rspamd_log_console_log, + .on_fork = NULL, }; #endif diff --git a/src/libserver/logger/logger_syslog.c b/src/libserver/logger/logger_syslog.c index ad10b7c13..3c4f7f7fd 100644 --- a/src/libserver/logger/logger_syslog.c +++ b/src/libserver/logger/logger_syslog.c @@ -19,7 +19,7 @@ #include "libserver/cfg_file.h" #include "logger_private.h" -#define SYSLOG_LOG_QUARK g_quark_from_static_string ("syslog_logger") +#define SYSLOG_LOG_QUARK g_quark_from_static_string("syslog_logger") struct rspamd_syslog_logger_priv { gint log_facility; @@ -29,51 +29,48 @@ struct rspamd_syslog_logger_priv { #include <syslog.h> void * -rspamd_log_syslog_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err) +rspamd_log_syslog_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err) { struct rspamd_syslog_logger_priv *priv; if (!cfg) { - g_set_error (err, SYSLOG_LOG_QUARK, EINVAL, - "no log config specified"); + g_set_error(err, SYSLOG_LOG_QUARK, EINVAL, + "no log config specified"); return NULL; } - priv = g_malloc0 (sizeof (*priv)); + priv = g_malloc0(sizeof(*priv)); priv->log_facility = cfg->log_facility; - openlog ("rspamd", LOG_NDELAY | LOG_PID, priv->log_facility); + openlog("rspamd", LOG_NDELAY | LOG_PID, priv->log_facility); return priv; } -void -rspamd_log_syslog_dtor (rspamd_logger_t *logger, gpointer arg) +void rspamd_log_syslog_dtor(rspamd_logger_t *logger, gpointer arg) { - struct rspamd_syslog_logger_priv *priv = (struct rspamd_syslog_logger_priv *)arg; + struct rspamd_syslog_logger_priv *priv = (struct rspamd_syslog_logger_priv *) arg; - closelog (); - g_free (priv); + closelog(); + g_free(priv); } -bool -rspamd_log_syslog_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg) +bool rspamd_log_syslog_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg) { static const struct { GLogLevelFlags glib_level; gint syslog_level; } levels_match[] = { - {G_LOG_LEVEL_DEBUG, LOG_DEBUG}, - {G_LOG_LEVEL_INFO, LOG_INFO}, - {G_LOG_LEVEL_WARNING, LOG_WARNING}, - {G_LOG_LEVEL_CRITICAL, LOG_ERR} - }; + {G_LOG_LEVEL_DEBUG, LOG_DEBUG}, + {G_LOG_LEVEL_INFO, LOG_INFO}, + {G_LOG_LEVEL_WARNING, LOG_WARNING}, + {G_LOG_LEVEL_CRITICAL, LOG_ERR}}; unsigned i; gint syslog_level; @@ -84,18 +81,18 @@ rspamd_log_syslog_log (const gchar *module, const gchar *id, /* Detect level */ syslog_level = LOG_DEBUG; - for (i = 0; i < G_N_ELEMENTS (levels_match); i ++) { + for (i = 0; i < G_N_ELEMENTS(levels_match); i++) { if (level_flags & levels_match[i].glib_level) { syslog_level = levels_match[i].syslog_level; break; } } - syslog (syslog_level, "<%.*s>; %s; %s: %.*s", - RSPAMD_LOG_ID_LEN, id != NULL ? id : "", - module != NULL ? module : "", - function != NULL ? function : "", - (gint)mlen, message); + syslog(syslog_level, "<%.*s>; %s; %s: %.*s", + RSPAMD_LOG_ID_LEN, id != NULL ? id : "", + module != NULL ? module : "", + function != NULL ? function : "", + (gint) mlen, message); return true; } @@ -103,28 +100,26 @@ rspamd_log_syslog_log (const gchar *module, const gchar *id, #else void * -rspamd_log_syslog_init (rspamd_logger_t *logger, struct rspamd_config *cfg, - uid_t uid, gid_t gid, GError **err) +rspamd_log_syslog_init(rspamd_logger_t *logger, struct rspamd_config *cfg, + uid_t uid, gid_t gid, GError **err) { - g_set_error (err, SYSLOG_LOG_QUARK, EINVAL, "syslog support is not compiled in"); + g_set_error(err, SYSLOG_LOG_QUARK, EINVAL, "syslog support is not compiled in"); return NULL; } -bool -rspamd_log_syslog_log (const gchar *module, const gchar *id, - const gchar *function, - gint level_flags, - const gchar *message, - gsize mlen, - rspamd_logger_t *rspamd_log, - gpointer arg) +bool rspamd_log_syslog_log(const gchar *module, const gchar *id, + const gchar *function, + gint level_flags, + const gchar *message, + gsize mlen, + rspamd_logger_t *rspamd_log, + gpointer arg) { return false; } -void -rspamd_log_syslog_dtor (rspamd_logger_t *logger, gpointer arg) +void rspamd_log_syslog_dtor(rspamd_logger_t *logger, gpointer arg) { /* Left blank intentionally */ } @@ -132,16 +127,16 @@ rspamd_log_syslog_dtor (rspamd_logger_t *logger, gpointer arg) #endif void * -rspamd_log_syslog_reload (rspamd_logger_t *logger, struct rspamd_config *cfg, - gpointer arg, uid_t uid, gid_t gid, GError **err) +rspamd_log_syslog_reload(rspamd_logger_t *logger, struct rspamd_config *cfg, + gpointer arg, uid_t uid, gid_t gid, GError **err) { struct rspamd_syslog_logger_priv *npriv; - npriv = rspamd_log_syslog_init (logger, cfg, uid, gid, err); + npriv = rspamd_log_syslog_init(logger, cfg, uid, gid, err); if (npriv) { /* Close old */ - rspamd_log_syslog_dtor (logger, arg); + rspamd_log_syslog_dtor(logger, arg); } return npriv; |