diff mbox series

teamd: add an option to force log output to stdout, stderr or syslog

Message ID 1537504862-1904-1-git-send-email-liuhangbin@gmail.com (mailing list archive)
State New
Headers show
Series teamd: add an option to force log output to stdout, stderr or syslog | expand

Commit Message

Hangbin Liu Sept. 21, 2018, 4:41 a.m. UTC
By default, libdaemon prints the logs to stderr, and switches to syslog
if the precess daemonizes.

When some users, e.g. NetworkManager, run teamd foreground, the logs printed
in syslog will as coming from NetworkManager, with mixed NetworkManager's
own debug logs, which makes people feel a mess and hard to debug.

Add option -l to support force teamd log output to stdout, stderr or
syslog. Also add a global env TEAM_LOG_OUTPUT, which could be used for
old version compatibility.

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
---
 man/teamd.8   |  3 +++
 teamd/teamd.c | 18 +++++++++++++++++-
 teamd/teamd.h |  1 +
 3 files changed, 21 insertions(+), 1 deletion(-)

Comments

Jiri Pirko Nov. 5, 2018, 4:07 p.m. UTC | #1
Fri, Sep 21, 2018 at 06:41:02AM CEST, liuhangbin@gmail.com wrote:
>By default, libdaemon prints the logs to stderr, and switches to syslog
>if the precess daemonizes.
>
>When some users, e.g. NetworkManager, run teamd foreground, the logs printed
>in syslog will as coming from NetworkManager, with mixed NetworkManager's
>own debug logs, which makes people feel a mess and hard to debug.
>
>Add option -l to support force teamd log output to stdout, stderr or
>syslog. Also add a global env TEAM_LOG_OUTPUT, which could be used for
>old version compatibility.
>
>Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>

applied, thanks!
diff mbox series

Patch

diff --git a/man/teamd.8 b/man/teamd.8
index 03b525c..3d27eae 100644
--- a/man/teamd.8
+++ b/man/teamd.8
@@ -69,6 +69,9 @@  Use the specified PID file.
 .B "\-g, \-\-debug"
 Turns on debugging messages. Repeating the option increases verbosity.
 .TP
+.B "\-l, \-\-log-output"
+Force teamd log output to stdout, stderr or syslog.
+.TP
 .B "\-r, \-\-force-recreate"
 Force team device recreation in case it already exists.
 .TP
diff --git a/teamd/teamd.c b/teamd/teamd.c
index aac2511..6c47312 100644
--- a/teamd/teamd.c
+++ b/teamd/teamd.c
@@ -107,6 +107,7 @@  static void print_help(const struct teamd_context *ctx) {
             "                             file will be ignored)\n"
             "    -p --pid-file=FILE       Use the specified PID file\n"
             "    -g --debug               Increase verbosity\n"
+            "    -l --log-output          Force teamd log output to stdout, stderr or syslog\n"
             "    -r --force-recreate      Force team device recreation in case it\n"
             "                             already exists\n"
             "    -o --take-over           Take over the device if it already exists\n"
@@ -140,6 +141,7 @@  static int parse_command_line(struct teamd_context *ctx,
 		{ "config",		required_argument,	NULL, 'c' },
 		{ "pid-file",		required_argument,	NULL, 'p' },
 		{ "debug",		no_argument,		NULL, 'g' },
+		{ "log-output",		required_argument,	NULL, 'l' },
 		{ "force-recreate",	no_argument,		NULL, 'r' },
 		{ "take-over",		no_argument,		NULL, 'o' },
 		{ "no-quit-destroy",	no_argument,		NULL, 'N' },
@@ -152,7 +154,7 @@  static int parse_command_line(struct teamd_context *ctx,
 		{ NULL, 0, NULL, 0 }
 	};
 
-	while ((opt = getopt_long(argc, argv, "hdkevf:c:p:groNt:nDZ:Uu",
+	while ((opt = getopt_long(argc, argv, "hdkevf:c:p:gl:roNt:nDZ:Uu",
 				  long_options, NULL)) >= 0) {
 
 		switch(opt) {
@@ -191,6 +193,10 @@  static int parse_command_line(struct teamd_context *ctx,
 		case 'g':
 			ctx->debug++;
 			break;
+		case 'l':
+			free(ctx->log_output);
+			ctx->log_output = strdup(optarg);
+			break;
 		case 'r':
 			ctx->force_recreate = true;
 			break;
@@ -1494,6 +1500,16 @@  static int teamd_start(struct teamd_context *ctx, enum teamd_exit_code *p_ret)
 	/* Child */
 	}
 
+	ctx->log_output = ctx->log_output ? : getenv("TEAM_LOG_OUTPUT");
+	if (ctx->log_output) {
+		if (strcmp(ctx->log_output, "stdout") == 0)
+			daemon_log_use = DAEMON_LOG_STDOUT;
+		else if (strcmp(ctx->log_output, "stderr") == 0)
+			daemon_log_use = DAEMON_LOG_STDERR;
+		else if (strcmp(ctx->log_output, "syslog") == 0)
+			daemon_log_use = DAEMON_LOG_SYSLOG;
+	}
+
 	if (daemon_close_all(-1) < 0) {
 		teamd_log_err("Failed to close all file descriptors.");
 		daemon_retval_send(errno);
diff --git a/teamd/teamd.h b/teamd/teamd.h
index 3934fc2..01bd022 100644
--- a/teamd/teamd.h
+++ b/teamd/teamd.h
@@ -99,6 +99,7 @@  struct teamd_context {
 	enum teamd_command		cmd;
 	bool				daemonize;
 	unsigned int			debug;
+	char *				log_output;
 	bool				force_recreate;
 	bool				take_over;
 	bool				no_quit_destroy;