[Spice-devel] [PATCH spice-common v2] log: Use GLib logging levels directly

Frediano Ziglio fziglio at redhat.com
Thu Jun 8 13:05:18 UTC 2017


As we moved toward GLib logging instead of having to convert
every time the log level from the old system to GLib use
directly GLib constants.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 common/log.c | 35 +++++++++++++++++++++--------------
 common/log.h | 26 +++++++++-----------------
 2 files changed, 30 insertions(+), 31 deletions(-)

Changes since v1:
- some suggested variable rename and test optimization

diff --git a/common/log.c b/common/log.c
index 7437d3e..61a27ac 100644
--- a/common/log.c
+++ b/common/log.c
@@ -31,16 +31,24 @@
 #include "backtrace.h"
 
 static int glib_debug_level = INT_MAX;
-static int abort_level = -1;
+static int abort_mask = 0;
 
-#ifndef SPICE_ABORT_LEVEL_DEFAULT
+#ifndef SPICE_ABORT_MASK_DEFAULT
 #ifdef SPICE_DISABLE_ABORT
-#define SPICE_ABORT_LEVEL_DEFAULT -1
+#define SPICE_ABORT_MASK_DEFAULT 0
 #else
-#define SPICE_ABORT_LEVEL_DEFAULT SPICE_LOG_LEVEL_CRITICAL
+#define SPICE_ABORT_MASK_DEFAULT (G_LOG_LEVEL_CRITICAL|G_LOG_LEVEL_ERROR)
 #endif
 #endif
 
+typedef enum {
+    SPICE_LOG_LEVEL_ERROR,
+    SPICE_LOG_LEVEL_CRITICAL,
+    SPICE_LOG_LEVEL_WARNING,
+    SPICE_LOG_LEVEL_INFO,
+    SPICE_LOG_LEVEL_DEBUG,
+} SpiceLogLevel;
+
 static GLogLevelFlags spice_log_level_to_glib(SpiceLogLevel level)
 {
     static const GLogLevelFlags glib_levels[] = {
@@ -95,23 +103,23 @@ static void spice_log_set_debug_level(void)
 
 static void spice_log_set_abort_level(void)
 {
-    if (abort_level == -1) {
+    if (abort_mask == 0) {
         const char *abort_str = g_getenv("SPICE_ABORT_LEVEL");
         if (abort_str != NULL) {
             GLogLevelFlags glib_abort_level;
 
             /* FIXME: To be removed after enough deprecation time */
             g_warning("Setting SPICE_ABORT_LEVEL is deprecated, use G_DEBUG instead");
-            abort_level = atoi(abort_str);
-            glib_abort_level = spice_log_level_to_glib(abort_level);
+            glib_abort_level = spice_log_level_to_glib(atoi(abort_str));
             unsigned int fatal_mask = G_LOG_FATAL_MASK;
             while (glib_abort_level >= G_LOG_LEVEL_ERROR) {
                 fatal_mask |= glib_abort_level;
                 glib_abort_level >>= 1;
             }
+            abort_mask = fatal_mask;
             g_log_set_fatal_mask(SPICE_LOG_DOMAIN, fatal_mask);
         } else {
-            abort_level = SPICE_ABORT_LEVEL_DEFAULT;
+            abort_mask = SPICE_ABORT_MASK_DEFAULT;
         }
     }
 }
@@ -146,16 +154,15 @@ SPICE_CONSTRUCTOR_FUNC(spice_log_init)
 }
 
 static void spice_logv(const char *log_domain,
-                       SpiceLogLevel log_level,
+                       GLogLevelFlags log_level,
                        const char *strloc,
                        const char *function,
                        const char *format,
                        va_list args)
 {
     GString *log_msg;
-    GLogLevelFlags glib_level = spice_log_level_to_glib(log_level);
 
-    if ((glib_level & G_LOG_LEVEL_MASK) > glib_debug_level) {
+    if ((log_level & G_LOG_LEVEL_MASK) > glib_debug_level) {
         return; // do not print anything
     }
 
@@ -166,17 +173,17 @@ static void spice_logv(const char *log_domain,
     if (format) {
         g_string_append_vprintf(log_msg, format, args);
     }
-    g_log(log_domain, glib_level, "%s", log_msg->str);
+    g_log(log_domain, log_level, "%s", log_msg->str);
     g_string_free(log_msg, TRUE);
 
-    if (abort_level != -1 && abort_level >= (int) log_level) {
+    if ((abort_mask & log_level) != 0) {
         spice_backtrace();
         abort();
     }
 }
 
 void spice_log(const char *log_domain,
-               SpiceLogLevel log_level,
+               GLogLevelFlags log_level,
                const char *strloc,
                const char *function,
                const char *format,
diff --git a/common/log.h b/common/log.h
index 73432d7..1ea2313 100644
--- a/common/log.h
+++ b/common/log.h
@@ -33,16 +33,8 @@ SPICE_BEGIN_DECLS
 
 #define SPICE_STRLOC  __FILE__ ":" G_STRINGIFY (__LINE__)
 
-typedef enum {
-    SPICE_LOG_LEVEL_ERROR,
-    SPICE_LOG_LEVEL_CRITICAL,
-    SPICE_LOG_LEVEL_WARNING,
-    SPICE_LOG_LEVEL_INFO,
-    SPICE_LOG_LEVEL_DEBUG,
-} SpiceLogLevel;
-
 void spice_log(const char *log_domain,
-               SpiceLogLevel log_level,
+               GLogLevelFlags log_level,
                const char *strloc,
                const char *function,
                const char *format,
@@ -50,20 +42,20 @@ void spice_log(const char *log_domain,
 
 #define spice_return_if_fail(x) G_STMT_START {                          \
     if G_LIKELY(x) { } else {                                           \
-        spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_CRITICAL, SPICE_STRLOC, G_STRFUNC, "condition `%s' failed", #x); \
+        spice_log(SPICE_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, SPICE_STRLOC, G_STRFUNC, "condition `%s' failed", #x); \
         return;                                                         \
     }                                                                   \
 } G_STMT_END
 
 #define spice_return_val_if_fail(x, val) G_STMT_START {                 \
     if G_LIKELY(x) { } else {                                           \
-        spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, "condition `%s' failed", #x); \
+        spice_log(SPICE_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, "condition `%s' failed", #x); \
         return (val);                                                   \
     }                                                                   \
 } G_STMT_END
 
 #define spice_warn_if_reached() G_STMT_START {                          \
-    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, "should not be reached"); \
+    spice_log(SPICE_LOG_DOMAIN, G_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, "should not be reached"); \
 } G_STMT_END
 
 #define spice_printerr(format, ...) G_STMT_START {                      \
@@ -71,23 +63,23 @@ void spice_log(const char *log_domain,
 } G_STMT_END
 
 #define spice_info(format, ...) G_STMT_START {                         \
-    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_INFO, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
+    spice_log(SPICE_LOG_DOMAIN, G_LOG_LEVEL_INFO, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
 } G_STMT_END
 
 #define spice_debug(format, ...) G_STMT_START {                         \
-    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_DEBUG, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
+    spice_log(SPICE_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
 } G_STMT_END
 
 #define spice_warning(format, ...) G_STMT_START {                       \
-    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
+    spice_log(SPICE_LOG_DOMAIN, G_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
 } G_STMT_END
 
 #define spice_critical(format, ...) G_STMT_START {                          \
-    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
+    spice_log(SPICE_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
 } G_STMT_END
 
 #define spice_error(format, ...) G_STMT_START {                         \
-    spice_log(SPICE_LOG_DOMAIN, SPICE_LOG_LEVEL_ERROR, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
+    spice_log(SPICE_LOG_DOMAIN, G_LOG_LEVEL_ERROR, SPICE_STRLOC, __FUNCTION__, format, ## __VA_ARGS__); \
 } G_STMT_END
 
 #define spice_warn_if_fail(x) G_STMT_START {            \
-- 
2.9.4



More information about the Spice-devel mailing list