summaryrefslogtreecommitdiffstats
path: root/src/log.c
diff options
context:
space:
mode:
authorArturo Borrero Gonzalez <arturo@debian.org>2016-10-28 08:57:08 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2016-10-28 10:06:19 +0200
commitab81c355fa292b7d71253a4b8d3a3daf31a78ed1 (patch)
tree0580d8442d53081ac8767090b3eb994821df6380 /src/log.c
parent9e94e85e10757aedd7f8dfdbea2ecafcbbb658fd (diff)
log: print messages to stdout/sderr if running in console mode
If conntrackd is running in console mode (i.e. in foreground) then we can print the log messages to stdout/stderr. This eases the workflow for admins, since we condensate more info into the same terminal output. Example: % sudo conntrackd -C /etc/conntrackd.conf WARNING: XXXX is an invalid interface [Thu Oct 27 13:57:09 2016] (pid=7581) [notice] disabling internal cache [Thu Oct 27 13:57:09 2016] (pid=7581) [notice] disabling external cache [Thu Oct 27 13:57:09 2016] (pid=7581) [ERROR] can't open channel socket: No such device [Thu Oct 27 13:57:09 2016] (pid=7581) [ERROR] initialization failed ERROR: conntrackd cannot start, please check the logfile for more info Signed-off-by: Arturo Borrero Gonzalez <arturo@debian.org> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Diffstat (limited to 'src/log.c')
-rw-r--r--src/log.c67
1 files changed, 47 insertions, 20 deletions
diff --git a/src/log.c b/src/log.c
index d4de111..0796ba9 100644
--- a/src/log.c
+++ b/src/log.c
@@ -60,41 +60,68 @@ int init_log(void)
return 0;
}
-void dlog(int priority, const char *format, ...)
- {
- FILE *fd = STATE(log);
+static void logline_put(FILE *fd, int priority, const char *format,
+ va_list *args)
+{
time_t t;
char *buf;
const char *prio;
+
+ t = time(NULL);
+ buf = ctime(&t);
+ buf[strlen(buf)-1]='\0';
+
+ switch (priority) {
+ case LOG_INFO:
+ prio = "info";
+ break;
+ case LOG_NOTICE:
+ prio = "notice";
+ break;
+ case LOG_WARNING:
+ prio = "warning";
+ break;
+ case LOG_ERR:
+ prio = "ERROR";
+ break;
+ default:
+ prio = "?";
+ break;
+ }
+
+ fprintf(fd, "[%s] (pid=%d) [%s] ", buf, getpid(), prio);
+ vfprintf(fd, format, *args);
+ fprintf(fd, "\n");
+ fflush(fd);
+}
+
+void dlog(int priority, const char *format, ...)
+{
+ FILE *fd = STATE(log);
+ FILE *console_out;
va_list args;
-
- if (fd) {
- t = time(NULL);
- buf = ctime(&t);
- buf[strlen(buf)-1]='\0';
+
+ if (CONFIG(running_mode) != DAEMON) {
switch (priority) {
case LOG_INFO:
- prio = "info";
- break;
case LOG_NOTICE:
- prio = "notice";
+ console_out = stdout;
break;
case LOG_WARNING:
- prio = "warning";
- break;
case LOG_ERR:
- prio = "ERROR";
- break;
default:
- prio = "?";
+ console_out = stderr;
break;
}
va_start(args, format);
- fprintf(fd, "[%s] (pid=%d) [%s] ", buf, getpid(), prio);
- vfprintf(fd, format, args);
+ logline_put(console_out, priority, format, &args);
+ va_end(args);
+ }
+
+ if (fd) {
+ va_start(args, format);
+ logline_put(fd, priority, format, &args);
va_end(args);
- fprintf(fd, "\n");
- fflush(fd);
}
if (CONFIG(syslog_facility) != -1) {