]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Improve logging to console
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 4 Nov 2019 16:52:15 +0000 (16:52 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 4 Nov 2019 16:52:15 +0000 (16:52 +0000)
src/libserver/cfg_file.h
src/libutil/logger.c

index 0aa8df36c34df92bd1d916fd17120d750bd2a827..1cf4a1fafb1ceaf29321fbeb97b55cbb88df1858 100644 (file)
@@ -76,6 +76,7 @@ enum rspamd_log_cfg_flags {
        RSPAMD_LOG_FLAG_USEC = (1 << 3),
        RSPAMD_LOG_FLAG_RSPAMADM = (1 << 4),
        RSPAMD_LOG_FLAG_ENFORCED = (1 << 5),
+       RSPAMD_LOG_FLAG_TTY = (1 << 6),
 };
 
 struct rspamd_worker_log_pipe {
index cb57b6825086aec61e49ed646e27ef9ce31d4495..31d018533999e33fa998f05e6e4e3101cbf939a9 100644 (file)
@@ -168,7 +168,7 @@ direct_write_log_line (rspamd_logger_t *rspamd_log,
                        }
                }
                else {
-                       fd = STDERR_FILENO;
+                       fd = rspamd_log->fd;
                }
        }
        else {
@@ -189,7 +189,7 @@ direct_write_log_line (rspamd_logger_t *rspamd_log,
                        tlen = count;
                }
 
-               if (tlen > PIPE_BUF) {
+               if (tlen > PIPE_BUF || rspamd_log->flags & RSPAMD_LOG_FLAG_TTY) {
                        locked = TRUE;
 
 #ifndef DISABLE_PTHREAD_MUTEX
@@ -306,8 +306,11 @@ rspamd_log_open_priv (rspamd_logger_t *rspamd_log, uid_t uid, gid_t gid)
        if (!rspamd_log->opened) {
                switch (rspamd_log->log_type) {
                case RSPAMD_LOG_CONSOLE:
-                       /* Do nothing with console */
-                       rspamd_log->fd = -1;
+                       /* Dup stderr fd to simplify processing */
+                       rspamd_log->fd = dup (STDERR_FILENO);
+                       if (isatty (STDERR_FILENO)) {
+                               rspamd_log->flags |= RSPAMD_LOG_FLAG_TTY;
+                       }
                        break;
                case RSPAMD_LOG_SYSLOG:
 #ifdef HAVE_SYSLOG_H
@@ -385,9 +388,6 @@ rspamd_log_close_priv (rspamd_logger_t *rspamd_log, gboolean termination, uid_t
 
        if (rspamd_log->opened) {
                switch (rspamd_log->type) {
-               case RSPAMD_LOG_CONSOLE:
-                       /* Do nothing special */
-                       break;
                case RSPAMD_LOG_SYSLOG:
 #ifdef HAVE_SYSLOG_H
                        closelog ();
@@ -407,6 +407,11 @@ rspamd_log_close_priv (rspamd_logger_t *rspamd_log, gboolean termination, uid_t
                                close (rspamd_log->fd);
                        }
                        break;
+               case RSPAMD_LOG_CONSOLE:
+                       if (rspamd_log->fd != -1) {
+                               close (rspamd_log->fd);
+                       }
+                       break;
                }
 
                rspamd_log->enabled = FALSE;
@@ -522,7 +527,6 @@ rspamd_set_logger (struct rspamd_config *cfg,
        switch (cfg->log_type) {
                case RSPAMD_LOG_CONSOLE:
                        logger->log_func = file_log_function;
-                       logger->fd = -1;
                        break;
                case RSPAMD_LOG_SYSLOG:
                        logger->log_func = syslog_log_function;