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.
This commit is contained in:
Thomas Haller 2019-01-16 16:16:49 +01:00
parent 83338428d9
commit 729feb0a93
4 changed files with 44 additions and 45 deletions

View file

@ -354,7 +354,7 @@ main (int argc, char *argv[])
NM_CONFIG_KEYFILE_GROUP_LOGGING,
NM_CONFIG_KEYFILE_KEY_LOGGING_BACKEND,
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)",

View file

@ -397,11 +397,11 @@ main (int argc, char *argv[])
if (!do_early_setup (&argc, &argv))
return 1;
nm_logging_set_syslog_identifier ("nm-iface-helper");
nm_logging_set_prefix ("%s[%ld] (%s): ",
_NMLOG_PREFIX_NAME,
(long) getpid (),
global_opt.ifname ?: "???");
nm_logging_init_pre ("nm-iface-helper",
g_strdup_printf ("%s[%ld] (%s): ",
_NMLOG_PREFIX_NAME,
(long) getpid (),
global_opt.ifname ?: "???"));
if (global_opt.g_fatal_warnings) {
GLogLevelFlags fatal_mask;
@ -466,8 +466,8 @@ main (int argc, char *argv[])
gl.main_loop = g_main_loop_new (NULL, FALSE);
setup_signals ();
nm_logging_syslog_openlog (global_opt.logging_backend,
global_opt.debug);
nm_logging_init (global_opt.logging_backend,
global_opt.debug);
_LOGI (LOGD_CORE, "nm-iface-helper (version " NM_DIST_VERSION ") is starting...");

View file

@ -89,7 +89,8 @@ typedef struct {
typedef struct {
NMLogLevel log_level;
bool uses_syslog:1;
bool syslog_identifier_initialized:1;
bool init_pre_done:1;
bool init_done:1;
bool debug_stderr:1;
const char *prefix;
const char *syslog_identifier;
@ -242,23 +243,6 @@ syslog_identifier_full (const Global *g)
}
#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
@ -860,42 +844,56 @@ nm_logging_syslog_enabled (void)
}
void
nm_logging_set_prefix (const char *format, ...)
nm_logging_init_pre (const char *syslog_identifier,
char *prefix_take)
{
char *prefix;
va_list ap;
/* this function may be called zero or one times, and only
* - on the main thread
* - not after nm_logging_init(). */
/* prefix can only be set once, to a non-empty string. Also, after
* nm_logging_syslog_openlog() the prefix cannot be set either. */
if (gl.imm.log_backend != LOG_BACKEND_GLIB)
g_return_if_reached ();
if (gl.imm.prefix[0])
NM_ASSERT_ON_MAIN_THREAD ();
if (gl.imm.init_pre_done)
g_return_if_reached ();
va_start (ap, format);
prefix = g_strdup_vprintf (format, ap);
va_end (ap);
if (!prefix || !prefix[0])
if (gl.imm.init_done)
g_return_if_reached ();
if (!_syslog_identifier_valid_domain (syslog_identifier))
g_return_if_reached ();
if (!prefix_take || !prefix_take[0])
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. */
gl.mut.prefix = prefix;
gl.mut.prefix = prefix_take;
}
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 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_LOG_CONFIG_BACKEND_JOURNAL,
NM_LOG_CONFIG_BACKEND_SYSLOG));
if (gl.imm.log_backend != LOG_BACKEND_GLIB)
if (gl.imm.init_done)
g_return_if_reached ();
gl.mut.init_done = TRUE;
if (!logging_backend)
logging_backend = ""NM_CONFIG_DEFAULT_LOGGING_BACKEND;

View file

@ -157,10 +157,11 @@ gboolean nm_logging_setup (const char *level,
char **bad_domains,
GError **error);
void nm_logging_set_syslog_identifier (const char *domain);
void nm_logging_set_prefix (const char *format, ...) _nm_printf (1, 2);
void nm_logging_init_pre (const char *syslog_identifier,
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);
/*****************************************************************************/