All of lore.kernel.org
 help / color / mirror / Atom feed
* [conntrack-tools PATCH] log: print messages to stdout/sderr if running in console mode
@ 2016-10-27 12:01 Arturo Borrero Gonzalez
  2016-10-27 19:53 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 2+ messages in thread
From: Arturo Borrero Gonzalez @ 2016-10-27 12:01 UTC (permalink / raw)
  To: netfilter-devel

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>
---
 include/conntrackd.h |    1 +
 src/log.c            |   67 +++++++++++++++++++++++++++++++++++---------------
 src/main.c           |    1 +
 3 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/include/conntrackd.h b/include/conntrackd.h
index f8b11a7..8406c54 100644
--- a/include/conntrackd.h
+++ b/include/conntrackd.h
@@ -110,6 +110,7 @@ struct ct_conf {
 	int filter_from_kernelspace;
 	int event_iterations_limit;
 	int systemd;
+	int running_mode;
 	struct {
 		int error_queue_length;
 	} channelc;
diff --git a/src/log.c b/src/log.c
index d4de111..a3629db 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) {
diff --git a/src/main.c b/src/main.c
index 2be9b2f..0319b5c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -150,6 +150,7 @@ int main(int argc, char *argv[])
 		switch(argv[i][1]) {
 		case 'd':
 			set_operation_mode(&type, DAEMON, argv);
+			CONFIG(running_mode) = DAEMON;
 			break;
 		case 'c':
 			set_operation_mode(&type, REQUEST, argv);


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [conntrack-tools PATCH] log: print messages to stdout/sderr if running in console mode
  2016-10-27 12:01 [conntrack-tools PATCH] log: print messages to stdout/sderr if running in console mode Arturo Borrero Gonzalez
@ 2016-10-27 19:53 ` Pablo Neira Ayuso
  0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2016-10-27 19:53 UTC (permalink / raw)
  To: Arturo Borrero Gonzalez; +Cc: netfilter-devel

On Thu, Oct 27, 2016 at 02:01:40PM +0200, Arturo Borrero Gonzalez wrote:
> 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

I'm fine with this. But I'm hitting compilation warnings here:

log.c: In function `logline_put':
log.c:93:23: warning: passing argument 3 of `vfprintf' from incompatible pointer type
  vfprintf(fd, format, args);
                       ^
In file included from ../include/log.h:4:0,
                 from log.c:21:
/usr/include/stdio.h:371:12: note: expected `struct __va_list_tag *'
but argument is of type `struct __va_list_tag (*)[1]'
 extern int vfprintf (FILE *__restrict __s, const char *__restrict __format,
            ^
log.c: In function `dlog':
log.c:117:46: warning: passing argument 4 of `logline_put' from incompatible pointer type
   logline_put(console_out, priority, format, args);
                                              ^
log.c:63:13: note: expected `struct __va_list_tag (*)[1]' but argument is of type `struct __va_list_tag *'
 static void logline_put(FILE *fd, int priority, const char *format,
             ^
log.c:123:37: warning: passing argument 4 of `logline_put' from incompatible pointer type
   logline_put(fd, priority, format, args);
                                     ^
log.c:63:13: note: expected `struct __va_list_tag (*)[1]' but argument is of type `struct __va_list_tag *'
 static void logline_put(FILE *fd, int priority, const char *format,

$ gcc -v
[...]
gcc version 4.9.2 (Debian 4.9.2-10) 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-10-27 19:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-10-27 12:01 [conntrack-tools PATCH] log: print messages to stdout/sderr if running in console mode Arturo Borrero Gonzalez
2016-10-27 19:53 ` Pablo Neira Ayuso

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.