From dc48e79c1507e1235a271b4a3763e9701651d2ab Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 16 Jun 2009 15:39:09 +0400 Subject: [PATCH] * Use own logging system to use static logging buffer instead of dynamically allocated one --- src/main.c | 4 ++++ src/main.h | 9 +++++---- src/message.h | 3 +++ src/util.c | 28 ++++++++++++++++++++++++++++ src/util.h | 6 +++++- 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main.c b/src/main.c index 03a2c01b9..9b8992d8b 100644 --- a/src/main.c +++ b/src/main.c @@ -193,6 +193,7 @@ config_logger (struct rspamd_main *rspamd, gboolean is_fatal) else { rspamd->cfg->log_fd = 2; } + rspamd_set_logger (file_log_function, rspamd->cfg); g_log_set_default_handler (file_log_function, rspamd->cfg); break; case RSPAMD_LOG_FILE: @@ -206,6 +207,7 @@ config_logger (struct rspamd_main *rspamd, gboolean is_fatal) } } else { + rspamd_set_logger (file_log_function, rspamd->cfg); g_log_set_default_handler (file_log_function, rspamd->cfg); } break; @@ -220,6 +222,7 @@ config_logger (struct rspamd_main *rspamd, gboolean is_fatal) } } else { + rspamd_set_logger (syslog_log_function, rspamd->cfg); g_log_set_default_handler (syslog_log_function, rspamd->cfg); } break; @@ -446,6 +449,7 @@ main (int argc, char **argv, char **env) /* First set logger to console logger */ cfg->log_fd = STDERR_FILENO; + rspamd_set_logger (file_log_function, rspamd->cfg); g_log_set_default_handler (file_log_function, cfg); #ifndef HAVE_SETPROCTITLE diff --git a/src/main.h b/src/main.h index c80efc494..c356823b8 100644 --- a/src/main.h +++ b/src/main.h @@ -16,6 +16,7 @@ #include "filter.h" #include "buffer.h" #include "hash.h" +#include "util.h" /* Default values */ #define FIXED_CONFIG_FILE CMAKE_PREFIX "/etc/rspamd.conf" @@ -27,10 +28,10 @@ #define WORKER_IO_TIMEOUT 60 /* Logging in postfix style */ -#define msg_err g_critical -#define msg_warn g_warning -#define msg_info g_message -#define msg_debug g_debug +#define msg_err(args...) rspamd_log_function(G_LOG_LEVEL_CRITICAL, ##args) +#define msg_warn(args...) rspamd_log_function(G_LOG_LEVEL_WARNING, ##args) +#define msg_info(args...) rspamd_log_function(G_LOG_LEVEL_INFO, ##args) +#define msg_debug(args...) rspamd_log_function(G_LOG_LEVEL_DEBUG, ##args) #ifdef CRLF #undef CRLF diff --git a/src/message.h b/src/message.h index 9a63b0824..d32c78d7f 100644 --- a/src/message.h +++ b/src/message.h @@ -9,6 +9,9 @@ #include "config.h" #include "fuzzy.h" +struct worker_task; +struct controller_session; + struct mime_part { GMimeContentType *type; GByteArray *content; diff --git a/src/util.c b/src/util.c index cb2d948ec..c90427935 100644 --- a/src/util.c +++ b/src/util.c @@ -40,7 +40,14 @@ struct list_file { struct stat st; }; +struct logger_params { + GLogFunc log_func; + struct config_file *cfg; +}; + + static GHashTable *listfiles = NULL; +static struct logger_params log_params; int make_socket_nonblocking (int fd) @@ -662,6 +669,13 @@ close_log (struct config_file *cfg) } +void +rspamd_set_logger (GLogFunc func, struct config_file *cfg) +{ + log_params.log_func = func; + log_params.cfg = cfg; +} + int reopen_log (struct config_file *cfg) { @@ -670,6 +684,20 @@ reopen_log (struct config_file *cfg) return open_log (cfg); } +void +rspamd_log_function (GLogLevelFlags log_level, const char *fmt, ...) +{ + static char logbuf[BUFSIZ]; + va_list vp; + + if (log_level <= log_params.cfg->log_level) { + va_start (vp, fmt); + vsnprintf (logbuf, sizeof (logbuf), fmt, vp); + va_end (vp); + log_params.log_func (NULL, log_level, logbuf, log_params.cfg); + } +} + void syslog_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer arg) { diff --git a/src/util.h b/src/util.h index c6ca2c25e..4bea3d08e 100644 --- a/src/util.h +++ b/src/util.h @@ -2,9 +2,11 @@ #define RSPAMD_UTIL_H #include "config.h" -#include "main.h" +#include "mem_pool.h" struct config_file; +struct rspamd_main; +struct workq; /* Create socket and bind or connect it to specified address and port */ int make_tcp_socket (struct in_addr *, u_short, gboolean is_server); @@ -47,9 +49,11 @@ int pidfile_close(struct pidfh *pfh); int pidfile_remove(struct pidfh *pfh); #endif +void rspamd_set_logger (GLogFunc func, struct config_file *cfg); int open_log (struct config_file *cfg); void close_log (struct config_file *cfg); int reopen_log (struct config_file *cfg); +void rspamd_log_function (GLogLevelFlags log_level, const char *fmt, ...); void syslog_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer arg); void file_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer arg); -- 2.39.5