mirror of
https://gitlab.uni-freiburg.de/opensourcevdi/spice-common
synced 2025-12-26 05:26:09 +00:00
log: Add spice_extra_assert
This macro was suggested to simplify hot path expensive checks which should be disable in production environments. Signed-off-by: Frediano Ziglio <fziglio@redhat.com> Acked-by: Francesco Giudici <fgiudici@redhat.com>
This commit is contained in:
parent
151c23d235
commit
e761c2d4de
@ -99,6 +99,12 @@ enum { spice_extra_checks = 1 };
|
||||
enum { spice_extra_checks = 0 };
|
||||
#endif
|
||||
|
||||
#define spice_extra_assert(x) G_STMT_START { \
|
||||
if (!spice_extra_checks || G_LIKELY(x)) { } else { \
|
||||
spice_error("assertion `%s' failed", #x); \
|
||||
} \
|
||||
} G_STMT_END
|
||||
|
||||
SPICE_END_DECLS
|
||||
|
||||
#endif // H_SPICE_COMMON_LOG
|
||||
|
||||
@ -267,6 +267,30 @@ static void test_spice_g_messages_debug_all(void)
|
||||
g_test_trap_assert_stderr("*g_message\n*other_message\n");
|
||||
}
|
||||
|
||||
/* Checks that spice_assert() aborts if condition fails */
|
||||
static void test_spice_assert(void)
|
||||
{
|
||||
if (g_test_subprocess()) {
|
||||
spice_assert(1 == 2);
|
||||
return;
|
||||
}
|
||||
g_test_trap_subprocess(NULL, 0, 0);
|
||||
g_test_trap_assert_failed();
|
||||
g_test_trap_assert_stderr("*assertion `1 == 2' failed*");
|
||||
}
|
||||
|
||||
/* Checks that spice_extra_assert() aborts if condition fails */
|
||||
static void test_spice_extra_assert(void)
|
||||
{
|
||||
if (g_test_subprocess()) {
|
||||
spice_extra_assert(1 == 2);
|
||||
return;
|
||||
}
|
||||
g_test_trap_subprocess(NULL, 0, 0);
|
||||
g_test_trap_assert_failed();
|
||||
g_test_trap_assert_stderr("*assertion `1 == 2' failed*");
|
||||
}
|
||||
|
||||
static void handle_sigabrt(int sig G_GNUC_UNUSED)
|
||||
{
|
||||
_Exit(1);
|
||||
@ -298,6 +322,10 @@ int main(int argc, char **argv)
|
||||
g_test_add_func("/spice-common/spice-fatal-return-if-fail", test_spice_fatal_return_if_fail);
|
||||
g_test_add_func("/spice-common/spice-non-fatal-greturn-if-fail", test_spice_non_fatal_g_return_if_fail);
|
||||
g_test_add_func("/spice-common/spice-fatal-warning", test_spice_fatal_warning);
|
||||
g_test_add_func("/spice-common/spice-assert", test_spice_assert);
|
||||
if (spice_extra_checks) {
|
||||
g_test_add_func("/spice-common/spice-extra-assert", test_spice_extra_assert);
|
||||
}
|
||||
|
||||
return g_test_run();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user