From: Vsevolod Stakhov Date: Tue, 25 Aug 2009 12:47:55 +0000 (+0400) Subject: * Use buffered IO for logging X-Git-Tag: 0.2.7~36 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=52eacd589704ae63a3794a1ed111de06add196f3;p=rspamd.git * Use buffered IO for logging --- diff --git a/src/cfg_file.h b/src/cfg_file.h index 44e9421b0..6529fea51 100644 --- a/src/cfg_file.h +++ b/src/cfg_file.h @@ -190,6 +190,7 @@ struct config_file { int log_level; /**< log level trigger */ char *log_file; /**< path to logfile in case of file logging */ int log_fd; /**< log descriptor in case of file logging */ + FILE *logf; size_t max_statfile_size; /**< maximum size for statfile */ diff --git a/src/main.c b/src/main.c index 233b93eeb..b2de3bf98 100644 --- a/src/main.c +++ b/src/main.c @@ -195,9 +195,11 @@ config_logger (struct rspamd_main *rspamd, gboolean is_fatal) fprintf (stderr, "Cannot log to console while daemonized, disable logging\n"); } rspamd->cfg->log_fd = -1; + rspamd->cfg->logf = NULL; } else { - rspamd->cfg->log_fd = 2; + rspamd->cfg->logf = stderr; + rspamd->cfg->log_fd = STDERR_FILENO; } rspamd_set_logger (file_log_function, rspamd->cfg); g_log_set_default_handler (file_log_function, rspamd->cfg); @@ -507,6 +509,7 @@ main (int argc, char **argv, char **env) /* First set logger to console logger */ cfg->log_fd = STDERR_FILENO; + cfg->logf = stderr; rspamd_set_logger (file_log_function, rspamd->cfg); g_log_set_default_handler (file_log_function, cfg); diff --git a/src/util.c b/src/util.c index bbe7ff666..3cd24a6eb 100644 --- a/src/util.c +++ b/src/util.c @@ -650,6 +650,7 @@ open_log (struct config_file *cfg) fprintf (stderr, "open_log: cannot open desired log file: %s, %s", cfg->log_file, strerror (errno)); return -1; } + cfg->logf = fdopen (cfg->log_fd, "w"); return 0; } return -1; @@ -666,7 +667,9 @@ close_log (struct config_file *cfg) closelog (); break; case RSPAMD_LOG_FILE: - close (cfg->log_fd); + if (cfg->logf != NULL) { + fclose (cfg->logf); + } break; } @@ -734,12 +737,10 @@ file_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gcha { struct config_file *cfg = (struct config_file *)arg; char tmpbuf[128], timebuf[32]; - int r; - struct iovec out[3]; time_t now; struct tm *tms; - if (cfg->log_fd == -1) { + if (cfg->log_fd == -1 || cfg->logf == NULL) { return; } #ifdef RSPAMD_MAIN @@ -752,15 +753,8 @@ file_log_function (const gchar *log_domain, GLogLevelFlags log_level, const gcha now = time (NULL); tms = localtime (&now); strftime (timebuf, sizeof (timebuf), "%b %d %H:%M:%S", tms); - r = snprintf (tmpbuf, sizeof (tmpbuf), "#%d: %s rspamd ", (int)getpid (), timebuf); - out[0].iov_base = tmpbuf; - out[0].iov_len = r; - out[1].iov_base = (char *)message; - out[1].iov_len = strlen (message); - out[2].iov_base = "\r\n"; - out[2].iov_len = 2; - - writev (cfg->log_fd, out, sizeof (out) / sizeof (out[0])); + snprintf (tmpbuf, sizeof (tmpbuf), "#%d: %s rspamd ", (int)getpid (), timebuf); + fprintf (cfg->logf, "%s%s" CRLF, tmpbuf, message); } }