Commit 729feb0a authored by Thomas Haller's avatar Thomas Haller
Browse files

logging: refactor and merge early logging initialization

Instead of having two functions nm_logging_set_syslog_identifier()
and nm_logging_set_prefix(), merge them.

They must both be called at earliest point and together. No point
in giving them the appearance that they could be called any time.
parent 83338428
...@@ -354,7 +354,7 @@ main (int argc, char *argv[]) ...@@ -354,7 +354,7 @@ main (int argc, char *argv[])
NM_CONFIG_KEYFILE_GROUP_LOGGING, NM_CONFIG_KEYFILE_GROUP_LOGGING,
NM_CONFIG_KEYFILE_KEY_LOGGING_BACKEND, NM_CONFIG_KEYFILE_KEY_LOGGING_BACKEND,
NM_CONFIG_GET_VALUE_STRIP | NM_CONFIG_GET_VALUE_NO_EMPTY); NM_CONFIG_GET_VALUE_STRIP | NM_CONFIG_GET_VALUE_NO_EMPTY);
nm_logging_syslog_openlog (v, nm_config_get_is_debug (config)); nm_logging_init (v, nm_config_get_is_debug (config));
} }
nm_log_info (LOGD_CORE, "NetworkManager (version " NM_DIST_VERSION ") is starting... (%s)", nm_log_info (LOGD_CORE, "NetworkManager (version " NM_DIST_VERSION ") is starting... (%s)",
......
...@@ -397,11 +397,11 @@ main (int argc, char *argv[]) ...@@ -397,11 +397,11 @@ main (int argc, char *argv[])
if (!do_early_setup (&argc, &argv)) if (!do_early_setup (&argc, &argv))
return 1; return 1;
nm_logging_set_syslog_identifier ("nm-iface-helper"); nm_logging_init_pre ("nm-iface-helper",
nm_logging_set_prefix ("%s[%ld] (%s): ", g_strdup_printf ("%s[%ld] (%s): ",
_NMLOG_PREFIX_NAME, _NMLOG_PREFIX_NAME,
(long) getpid (), (long) getpid (),
global_opt.ifname ?: "???"); global_opt.ifname ?: "???"));
if (global_opt.g_fatal_warnings) { if (global_opt.g_fatal_warnings) {
GLogLevelFlags fatal_mask; GLogLevelFlags fatal_mask;
...@@ -466,8 +466,8 @@ main (int argc, char *argv[]) ...@@ -466,8 +466,8 @@ main (int argc, char *argv[])
gl.main_loop = g_main_loop_new (NULL, FALSE); gl.main_loop = g_main_loop_new (NULL, FALSE);
setup_signals (); setup_signals ();
nm_logging_syslog_openlog (global_opt.logging_backend, nm_logging_init (global_opt.logging_backend,
global_opt.debug); global_opt.debug);
_LOGI (LOGD_CORE, "nm-iface-helper (version " NM_DIST_VERSION ") is starting..."); _LOGI (LOGD_CORE, "nm-iface-helper (version " NM_DIST_VERSION ") is starting...");
......
...@@ -89,7 +89,8 @@ typedef struct { ...@@ -89,7 +89,8 @@ typedef struct {
typedef struct { typedef struct {
NMLogLevel log_level; NMLogLevel log_level;
bool uses_syslog:1; bool uses_syslog:1;
bool syslog_identifier_initialized:1; bool init_pre_done:1;
bool init_done:1;
bool debug_stderr:1; bool debug_stderr:1;
const char *prefix; const char *prefix;
const char *syslog_identifier; const char *syslog_identifier;
...@@ -242,23 +243,6 @@ syslog_identifier_full (const Global *g) ...@@ -242,23 +243,6 @@ syslog_identifier_full (const Global *g)
} }
#endif #endif
void
nm_logging_set_syslog_identifier (const char *domain)
{
if (gl.imm.log_backend != LOG_BACKEND_GLIB)
g_return_if_reached ();
if (!_syslog_identifier_valid_domain (domain))
g_return_if_reached ();
if (gl.imm.syslog_identifier_initialized)
g_return_if_reached ();
gl.mut.syslog_identifier_initialized = TRUE;
gl.mut.syslog_identifier = g_strdup_printf ("SYSLOG_IDENTIFIER=%s", domain);
nm_assert (_syslog_identifier_assert (&gl.imm));
}
/*****************************************************************************/ /*****************************************************************************/
static gboolean static gboolean
...@@ -860,42 +844,56 @@ nm_logging_syslog_enabled (void) ...@@ -860,42 +844,56 @@ nm_logging_syslog_enabled (void)
} }
void void
nm_logging_set_prefix (const char *format, ...) nm_logging_init_pre (const char *syslog_identifier,
char *prefix_take)
{ {
char *prefix; /* this function may be called zero or one times, and only
va_list ap; * - on the main thread
* - not after nm_logging_init(). */
/* prefix can only be set once, to a non-empty string. Also, after NM_ASSERT_ON_MAIN_THREAD ();
* nm_logging_syslog_openlog() the prefix cannot be set either. */
if (gl.imm.log_backend != LOG_BACKEND_GLIB) if (gl.imm.init_pre_done)
g_return_if_reached (); g_return_if_reached ();
if (gl.imm.prefix[0])
if (gl.imm.init_done)
g_return_if_reached (); g_return_if_reached ();
va_start (ap, format); if (!_syslog_identifier_valid_domain (syslog_identifier))
prefix = g_strdup_vprintf (format, ap); g_return_if_reached ();
va_end (ap);
if (!prefix || !prefix[0]) if (!prefix_take || !prefix_take[0])
g_return_if_reached (); g_return_if_reached ();
gl.mut.init_pre_done = TRUE;
gl.mut.syslog_identifier = g_strdup_printf ("SYSLOG_IDENTIFIER=%s", syslog_identifier);
nm_assert (_syslog_identifier_assert (&gl.imm));
/* we pass the allocated string on and never free it. */ /* we pass the allocated string on and never free it. */
gl.mut.prefix = prefix; gl.mut.prefix = prefix_take;
} }
void void
nm_logging_syslog_openlog (const char *logging_backend, gboolean debug) nm_logging_init (const char *logging_backend, gboolean debug)
{ {
gboolean fetch_monotonic_timestamp = FALSE; gboolean fetch_monotonic_timestamp = FALSE;
gboolean obsolete_debug_backend = FALSE; gboolean obsolete_debug_backend = FALSE;
/* this function may be called zero or one times, and only on the
* main thread. */
NM_ASSERT_ON_MAIN_THREAD ();
nm_assert (NM_IN_STRSET (""NM_CONFIG_DEFAULT_LOGGING_BACKEND, nm_assert (NM_IN_STRSET (""NM_CONFIG_DEFAULT_LOGGING_BACKEND,
NM_LOG_CONFIG_BACKEND_JOURNAL, NM_LOG_CONFIG_BACKEND_JOURNAL,
NM_LOG_CONFIG_BACKEND_SYSLOG)); NM_LOG_CONFIG_BACKEND_SYSLOG));
if (gl.imm.log_backend != LOG_BACKEND_GLIB) if (gl.imm.init_done)
g_return_if_reached (); g_return_if_reached ();
gl.mut.init_done = TRUE;
if (!logging_backend) if (!logging_backend)
logging_backend = ""NM_CONFIG_DEFAULT_LOGGING_BACKEND; logging_backend = ""NM_CONFIG_DEFAULT_LOGGING_BACKEND;
......
...@@ -157,10 +157,11 @@ gboolean nm_logging_setup (const char *level, ...@@ -157,10 +157,11 @@ gboolean nm_logging_setup (const char *level,
char **bad_domains, char **bad_domains,
GError **error); GError **error);
void nm_logging_set_syslog_identifier (const char *domain); void nm_logging_init_pre (const char *syslog_identifier,
void nm_logging_set_prefix (const char *format, ...) _nm_printf (1, 2); char *prefix_take);
void nm_logging_init (const char *logging_backend, gboolean debug);
void nm_logging_syslog_openlog (const char *logging_backend, gboolean debug);
gboolean nm_logging_syslog_enabled (void); gboolean nm_logging_syslog_enabled (void);
/*****************************************************************************/ /*****************************************************************************/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment