]> source.dussan.org Git - rspamd.git/commitdiff
* Use buffered IO for logging
authorVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 25 Aug 2009 12:47:55 +0000 (16:47 +0400)
committerVsevolod Stakhov <vsevolod@rambler-co.ru>
Tue, 25 Aug 2009 12:47:55 +0000 (16:47 +0400)
src/cfg_file.h
src/main.c
src/util.c

index 44e9421b03e9f2806f3c7d95e667c4f7bb26fb0b..6529fea5105d42a6fe2fa813909f4e933dcb0766 100644 (file)
@@ -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                                                  */
 
index 233b93eeb163b483c209b0360d25f7ec1fc25a41..b2de3bf981d87c960754090e5ba412df5ad751b9 100644 (file)
@@ -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);
 
index bbe7ff666c05b0db85c0ca265a6be3e639f3bdc9..3cd24a6ebc5b9298f8d62e1bc654f738b220447b 100644 (file)
@@ -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);
        }
 }