log: Use SPICE_CONSTRUCTOR_FUNC

This was done through a GOnce called every time spice_log() is called,
now it will always be called at spice-server startup.

This means the unit test needs to be updated as SPICE_DEBUG/ABORT_LEVEL
must now be set before the process starts up rather than before the
first spice_log call, and the deprecation warning these environment
variables trigger cannot be caught using g_test_expect_message() as
they are output before g_test_init() is called.
This commit is contained in:
Christophe Fergeau 2016-04-04 17:54:33 +02:00
parent 6a65f470d1
commit 8473d0ae71
2 changed files with 23 additions and 45 deletions

View File

@ -130,18 +130,14 @@ static void spice_logger(const gchar *log_domain,
g_log_default_handler(log_domain, log_level, message, NULL);
}
static inline void spice_log_init_once(void)
SPICE_CONSTRUCTOR_FUNC(spice_log_init)
{
static gsize logging_initialized = FALSE;
if (g_once_init_enter(&logging_initialized)) {
spice_log_set_debug_level();
spice_log_set_abort_level();
g_once_init_leave (&logging_initialized, TRUE);
g_log_set_handler(SPICE_LOG_DOMAIN,
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
spice_logger, NULL);
}
spice_log_set_debug_level();
spice_log_set_abort_level();
g_log_set_handler(SPICE_LOG_DOMAIN,
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION,
spice_logger, NULL);
}
static void spice_logv(const char *log_domain,
@ -153,8 +149,6 @@ static void spice_logv(const char *log_domain,
{
GString *log_msg;
spice_log_init_once();
g_return_if_fail(spice_log_level_to_glib(log_level) != 0);
log_msg = g_string_new(NULL);

View File

@ -48,18 +48,15 @@ LOG_OTHER_HELPER(critical, CRITICAL)
static void test_spice_abort_level(void)
{
if (g_test_subprocess()) {
/* 2 = SPICE_LOG_LEVEL_WARNING */
g_setenv("SPICE_ABORT_LEVEL", "2", TRUE);
g_test_expect_message(NULL,
G_LOG_LEVEL_WARNING,
"*SPICE_ABORT_LEVEL*deprecated*");
spice_debug("trigger deprecation warning");
g_test_assert_expected_messages();
spice_warning("spice_warning");
return;
}
/* 2 = SPICE_LOG_LEVEL_WARNING */
g_setenv("SPICE_ABORT_LEVEL", "2", TRUE);
g_test_trap_subprocess(NULL, 0, 0);
g_unsetenv("SPICE_ABORT_LEVEL");
g_test_trap_assert_failed();
g_test_trap_assert_stderr("*SPICE_ABORT_LEVEL*deprecated*");
g_test_trap_assert_stderr("*spice_warning*");
}
@ -67,18 +64,14 @@ static void test_spice_abort_level(void)
static void test_spice_abort_level_g_warning(void)
{
if (g_test_subprocess()) {
/* 2 = SPICE_LOG_LEVEL_WARNING */
g_setenv("SPICE_ABORT_LEVEL", "2", TRUE);
g_test_expect_message(NULL,
G_LOG_LEVEL_WARNING,
"*SPICE_ABORT_LEVEL*deprecated*");
spice_debug("trigger deprecation warning");
g_test_assert_expected_messages();
g_warning("g_warning");
return;
}
g_setenv("SPICE_ABORT_LEVEL", "2", TRUE);
g_test_trap_subprocess(NULL, 0, 0);
g_unsetenv("SPICE_ABORT_LEVEL");
g_test_trap_assert_failed();
g_test_trap_assert_stderr("*SPICE_ABORT_LEVEL*deprecated*");
g_test_trap_assert_stderr("*g_warning*");
}
@ -263,14 +256,6 @@ static void test_log_levels(void)
static void test_spice_debug_level(void)
{
if (g_test_subprocess()) {
g_unsetenv("G_MESSAGES_DEBUG");
g_setenv("SPICE_DEBUG_LEVEL", "5", TRUE);
g_test_expect_message(NULL,
G_LOG_LEVEL_WARNING,
"*SPICE_DEBUG_LEVEL*deprecated*");
spice_debug("trigger deprecation warning");
g_test_assert_expected_messages();
/* g_test_expected_message only checks whether the appropriate messages got up to g_log()
* The following calls will be caught by the parent process to check what was (not) printed
* to stdout/stderr
@ -283,8 +268,12 @@ static void test_spice_debug_level(void)
return;
}
g_unsetenv("G_MESSAGES_DEBUG");
g_setenv("SPICE_DEBUG_LEVEL", "5", TRUE);
g_test_trap_subprocess(NULL, 0, 0);
g_unsetenv("SPICE_DEBUG_LEVEL");
g_test_trap_assert_passed();
g_test_trap_assert_stderr("*SPICE_DEBUG_LEVEL*deprecated*");
g_test_trap_assert_stdout("*spice_info\n*g_debug\n*spice_debug\n");
}
@ -294,17 +283,6 @@ static void test_spice_debug_level(void)
static void test_spice_debug_level_warning(void)
{
if (g_test_subprocess()) {
g_setenv("SPICE_ABORT_LEVEL", "0", TRUE);
g_setenv("SPICE_DEBUG_LEVEL", "1", TRUE);
g_test_expect_message(NULL,
G_LOG_LEVEL_WARNING,
"*SPICE_DEBUG_LEVEL*deprecated*");
g_test_expect_message(NULL,
G_LOG_LEVEL_WARNING,
"*SPICE_ABORT_LEVEL*deprecated*");
spice_debug("trigger deprecation warning");
g_test_assert_expected_messages();
spice_info("spice_info");
spice_debug("spice_debug");
spice_warning("spice_warning");
@ -323,8 +301,14 @@ static void test_spice_debug_level_warning(void)
return;
}
g_setenv("SPICE_ABORT_LEVEL", "0", TRUE);
g_setenv("SPICE_DEBUG_LEVEL", "1", TRUE);
g_test_trap_subprocess(NULL, 0, 0);
g_unsetenv("SPICE_ABORT_LEVEL");
g_unsetenv("SPICE_DEBUG_LEVEL");
g_test_trap_assert_passed();
g_test_trap_assert_stderr("*SPICE_DEBUG_LEVEL*deprecated*");
g_test_trap_assert_stderr("*SPICE_ABORT_LEVEL*deprecated*");
g_test_trap_assert_stderr("*spice_critical\n*g_critical\n*other_message\n*other_warning\n*other_critical\n");
}