logging: ignore unrecognized domains on startup

If the command line or NetworkManager.conf mentions a non-existent
domain, just print a warning and ignore it. That way if you switch to
using an older NM that doesn't have that domain, it will still work.
This commit is contained in:
Dan Winship 2013-11-26 11:33:42 -05:00
parent fc2a14d0f9
commit 111603212c
7 changed files with 42 additions and 10 deletions

View file

@ -151,8 +151,12 @@ match_log_level (const char *level,
}
gboolean
nm_logging_setup (const char *level, const char *domains, GError **error)
nm_logging_setup (const char *level,
const char *domains,
char **bad_domains,
GError **error)
{
GString *unrecognized = NULL;
guint64 new_logging[LOGL_MAX];
guint32 new_log_level = log_level;
int i;
@ -219,9 +223,18 @@ nm_logging_setup (const char *level, const char *domains, GError **error)
}
if (!bits) {
g_set_error (error, NM_LOGGING_ERROR, NM_LOGGING_ERROR_UNKNOWN_DOMAIN,
_("Unknown log domain '%s'"), *iter);
return FALSE;
if (!bad_domains) {
g_set_error (error, NM_LOGGING_ERROR, NM_LOGGING_ERROR_UNKNOWN_DOMAIN,
_("Unknown log domain '%s'"), *iter);
return FALSE;
}
if (unrecognized)
g_string_append (unrecognized, ", ");
else
unrecognized = g_string_new (NULL);
g_string_append (unrecognized, *iter);
continue;
}
for (i = 0; i < domain_log_level; i++)
@ -242,6 +255,9 @@ nm_logging_setup (const char *level, const char *domains, GError **error)
log_level = new_log_level;
if (unrecognized)
*bad_domains = g_string_free (unrecognized, FALSE);
return TRUE;
}

View file

@ -127,7 +127,10 @@ const char *nm_logging_all_domains_to_string (void);
#undef nm_error
#undef nm_error_str
gboolean nm_logging_setup (const char *level, const char *domains, GError **error);
gboolean nm_logging_setup (const char *level,
const char *domains,
char **bad_domains,
GError **error);
void nm_logging_syslog_openlog (gboolean debug);
void nm_logging_syslog_closelog (void);

View file

@ -323,6 +323,7 @@ main (int argc, char *argv[])
gs_unref_object NMSessionMonitor *session_monitor = NULL;
GError *error = NULL;
gboolean wrote_pidfile = FALSE;
char *bad_domains = NULL;
GOptionEntry options[] = {
{ "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Print NetworkManager version and exit"), NULL },
@ -405,11 +406,17 @@ main (int argc, char *argv[])
if (!nm_logging_setup (opt_log_level,
opt_log_domains,
&bad_domains,
&error)) {
fprintf (stderr,
_("%s. Please use --help to see a list of valid options.\n"),
error->message);
exit (1);
} else if (bad_domains) {
fprintf (stderr,
_("Ignoring unrecognized log domain(s) '%s' passed on command line.\n"),
bad_domains);
g_clear_pointer (&bad_domains, g_free);
}
/* When running from the build directory, determine our build directory
@ -470,10 +477,16 @@ main (int argc, char *argv[])
if (opt_log_level == NULL && opt_log_domains == NULL) {
if (!nm_logging_setup (nm_config_get_log_level (config),
nm_config_get_log_domains (config),
&bad_domains,
&error)) {
fprintf (stderr, _("%s. Please use --help to see a list of valid options.\n"),
fprintf (stderr, _("Error in configuration file: %s.\n"),
error->message);
exit (1);
} else if (bad_domains) {
fprintf (stderr,
_("Ignoring unrecognized log domain(s) '%s' from config files.\n"),
bad_domains);
g_clear_pointer (&bad_domains, g_free);
}
}

View file

@ -3981,7 +3981,7 @@ impl_manager_set_logging (NMManager *manager,
const char *domains,
GError **error)
{
if (nm_logging_setup (level, domains, error)) {
if (nm_logging_setup (level, domains, NULL, error)) {
char *new_domains = nm_logging_domains_to_string ();
nm_log_info (LOGD_CORE, "logging: level '%s' domains '%s'",

View file

@ -12,7 +12,7 @@ main (int argc, char **argv)
g_type_init ();
loop = g_main_loop_new (NULL, FALSE);
nm_logging_setup ("debug", NULL, NULL);
nm_logging_setup ("debug", NULL, NULL, NULL);
openlog (G_LOG_DOMAIN, LOG_CONS | LOG_PERROR, LOG_DAEMON);
g_assert (argc <= 2);

View file

@ -120,7 +120,7 @@ main (int argc, char **argv)
/* Enable debug messages if called with --debug */
for (; *argv; argv++) {
if (!g_strcmp0 (*argv, "--debug")) {
nm_logging_setup ("debug", NULL, NULL);
nm_logging_setup ("debug", NULL, NULL, NULL);
}
}

View file

@ -21,7 +21,7 @@ main (int argc, char **argv)
g_type_init ();
loop = g_main_loop_new (NULL, FALSE);
nm_logging_setup ("debug", NULL, NULL);
nm_logging_setup ("debug", NULL, NULL, NULL);
openlog (G_LOG_DOMAIN, LOG_CONS | LOG_PERROR, LOG_DAEMON);
argv++;