[network-manager-openvpn/th/vpn-plugin-debug-bgo766816: 3/4] service: refactor logging and don't use g_log()



commit 57ebf2c16c0661f3e0d425c9ec19d11c4396b743
Author: Thomas Haller <thaller redhat com>
Date:   Mon May 23 14:07:19 2016 +0200

    service: refactor logging and don't use g_log()
    
    g_log() prefixes every logging message with something like:
    
      (nm-openvpn-service:7563): nm-openvpn-WARNING **:
    
    We don't want these prefixes, but use our own style.
    
    Also, G_LOG_LEVEL_INFO and G_LOG_LEVEL_DEBUG are not shown by
    default, unless G_MESSAGES_DEBUG is defined. We already have our
    internal ways to enable/disable logging. We don't want to allow
    glib to swallow our logging statements.
    An alternative would be to define G_MESSAGES_DEBUG in the service
    itself.

 shared/utils.h           |   13 +++++++++++++
 src/nm-openvpn-service.c |   18 ++++++++++--------
 2 files changed, 23 insertions(+), 8 deletions(-)
---
diff --git a/shared/utils.h b/shared/utils.h
index 6370871..d900cc7 100644
--- a/shared/utils.h
+++ b/shared/utils.h
@@ -22,6 +22,8 @@
 #ifndef UTILS_H
 #define UTILS_H
 
+#include <syslog.h>
+
 #define NMV_OVPN_TAG_AUTH               "auth"
 #define NMV_OVPN_TAG_AUTH_NOCACHE       "auth-nocache"
 #define NMV_OVPN_TAG_AUTH_USER_PASS     "auth-user-pass"
@@ -81,5 +83,16 @@ const char *nmv_utils_str_utf8safe_escape_c   (const char *str, char **out_clone
 char *      nmv_utils_str_utf8safe_unescape   (const char *str);
 const char *nmv_utils_str_utf8safe_unescape_c (const char *str, char **str_free);
 
+static inline const char *
+nm_utils_syslog_to_str (int syslog_level)
+{
+       switch (syslog_level) {
+       case LOG_DEBUG:   return "<debug>";
+       case LOG_INFO:    return "<info>";
+       case LOG_WARNING: return "<warn>";
+       }
+       return "<unknown>";
+}
+
 #endif  /* UTILS_H */
 
diff --git a/src/nm-openvpn-service.c b/src/nm-openvpn-service.c
index 8c47c73..812dfbb 100644
--- a/src/nm-openvpn-service.c
+++ b/src/nm-openvpn-service.c
@@ -44,7 +44,6 @@
 #include <locale.h>
 #include <pwd.h>
 #include <grp.h>
-#include <syslog.h>
 #include <glib-unix.h>
 
 #include "utils.h"
@@ -161,10 +160,13 @@ static ValidProperty valid_secrets[] = {
 
 /*****************************************************************************/
 
-#define _LOG(log_always, level, ...) \
+#define _NMLOG(level, log_always, ...) \
        G_STMT_START { \
-               if ((log_always) || _LOGd_enabled ()) { \
-                       g_log (G_LOG_DOMAIN, level, __VA_ARGS__); \
+               if ((log_always) || gl.log_level >= (level)) { \
+                       g_print ("nm-openvpn[%ld] %-7s " _NM_UTILS_MACRO_FIRST (__VA_ARGS__) "\n", \
+                                (long) getpid (), \
+                                nm_utils_syslog_to_str (level) \
+                                _NM_UTILS_MACRO_REST (__VA_ARGS__)); \
                } \
        } G_STMT_END
 
@@ -174,10 +176,10 @@ _LOGd_enabled (void)
        return gl.log_level >= LOG_INFO;
 }
 
-#define _LOGd(...) _LOG(FALSE, G_LOG_LEVEL_INFO, __VA_ARGS__)
-#define _LOGD(...) _LOG(TRUE,  G_LOG_LEVEL_INFO, __VA_ARGS__)
-#define _LOGI(...) _LOG(TRUE,  G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
-#define _LOGW(...) _LOG(TRUE,  G_LOG_LEVEL_WARNING, __VA_ARGS__)
+#define _LOGd(...) _NMLOG(LOG_DEBUG,   FALSE, __VA_ARGS__)
+#define _LOGD(...) _NMLOG(LOG_DEBUG,   TRUE,  __VA_ARGS__)
+#define _LOGI(...) _NMLOG(LOG_INFO,    TRUE,  __VA_ARGS__)
+#define _LOGW(...) _NMLOG(LOG_WARNING, TRUE,  __VA_ARGS__)
 
 /*****************************************************************************/
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]