lib: Add new cli to specify where to output logs on startup

When we are starting a daemon, allow the user to specify:

--log <stdout|syslog|file:file_name>

This can be used on early startup to put the log files
where the end user wants them to show up.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
This commit is contained in:
Donald Sharp 2018-06-15 13:38:46 -04:00
parent 1f14323649
commit f8507817cf
4 changed files with 34 additions and 2 deletions

View File

@ -2429,6 +2429,7 @@ static int set_log_file(struct vty *vty, const char *fname, int loglevel)
XFREE(MTYPE_TMP, p);
if (!ret) {
if (vty)
vty_out(vty, "can't open logfile %s\n", fname);
return CMD_WARNING_CONFIG_FAILED;
}
@ -2445,6 +2446,26 @@ static int set_log_file(struct vty *vty, const char *fname, int loglevel)
return CMD_SUCCESS;
}
void command_setup_early_logging(const char *option)
{
char *token;
if (strcmp(option, "stdout") == 0) {
zlog_set_level(ZLOG_DEST_STDOUT, zlog_default->default_lvl);
return;
}
if (strcmp(option, "syslog") == 0) {
zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
return;
}
token = strstr(option, ":");
token++;
set_log_file(NULL, token, zlog_default->default_lvl);
}
DEFUN (config_log_file,
config_log_file_cmd,
"log file FILENAME [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",

View File

@ -479,4 +479,5 @@ extern void
cmd_variable_handler_register(const struct cmd_variable_handler *cvh);
extern char *cmd_variable_comp2str(vector comps, unsigned short cols);
extern void command_setup_early_logging(const char *option);
#endif /* _ZEBRA_COMMAND_H */

View File

@ -78,6 +78,7 @@ static void opt_extend(const struct optspec *os)
#define OPTION_VTYSOCK 1000
#define OPTION_MODULEDIR 1002
#define OPTION_LOG 1003
static const struct option lo_always[] = {
{"help", no_argument, NULL, 'h'},
@ -86,6 +87,7 @@ static const struct option lo_always[] = {
{"module", no_argument, NULL, 'M'},
{"vty_socket", required_argument, NULL, OPTION_VTYSOCK},
{"moduledir", required_argument, NULL, OPTION_MODULEDIR},
{"log", required_argument, NULL, OPTION_LOG},
{NULL}};
static const struct optspec os_always = {
"hvdM:",
@ -94,7 +96,8 @@ static const struct optspec os_always = {
" -d, --daemon Runs in daemon mode\n"
" -M, --module Load specified module\n"
" --vty_socket Override vty socket path\n"
" --moduledir Override modules directory\n",
" --moduledir Override modules directory\n"
" --log Set Logging to stdout, syslog, or file:<name>\n",
lo_always};
@ -444,6 +447,9 @@ static int frr_opt(int opt)
return 1;
di->privs->group = optarg;
break;
case OPTION_LOG:
di->early_logging = optarg;
break;
default:
return 1;
}
@ -543,6 +549,9 @@ struct thread_master *frr_init(void)
openzlog(di->progname, di->logname, di->instance,
LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
if (di->early_logging)
command_setup_early_logging(di->early_logging);
#if defined(HAVE_CUMULUS)
zlog_set_level(ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
#endif

View File

@ -58,6 +58,7 @@ struct frr_daemon_info {
const char *vty_path;
const char *module_path;
const char *pathspace;
const char *early_logging;
const char *proghelp;
void (*printhelp)(FILE *target);