[network-manager-openvpn: 4/7] service: refactor logging and don't use g_log()



commit ae5195770bee4fa474156beeac069fe720329128
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 and don't want g_log()
    interfere with this. Otherwise, we would have to set G_MESSAGES_DEBUG
    internally.
    
    Also, we use it for regular loggings. Even a warning, is something that
    can happen under regular conditions. These are not assertions.
    On the other hand, g_warning() dumps core with G_DEBUG=fatal-warnings.
    That is great for libraries and assertions, but not for regular logging.

 shared/utils.h           |   17 +++++++++++++++++
 src/nm-openvpn-service.c |   16 +++++++++-------
 2 files changed, 26 insertions(+), 7 deletions(-)
---
diff --git a/shared/utils.h b/shared/utils.h
index 6370871..48653d1 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,20 @@ 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)
+{
+       /* Maps the levels the same way as NetworkManager's nm-logging.c does */
+       if (syslog_level >= LOG_DEBUG)
+               return "<trace>";
+       if (syslog_level >= LOG_INFO)
+               return "<debug>";
+       if (syslog_level >= LOG_NOTICE)
+               return "<info>";
+       if (syslog_level >= LOG_WARNING)
+               return "<warn>";
+       return "<error>";
+}
+
 #endif  /* UTILS_H */
 
diff --git a/src/nm-openvpn-service.c b/src/nm-openvpn-service.c
index 63aa868..92be446 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,9 +176,9 @@ _LOGD_enabled (void)
        return gl.log_level >= LOG_INFO;
 }
 
-#define _LOGD(...) _LOG(FALSE, 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_INFO,    FALSE, __VA_ARGS__)
+#define _LOGI(...) _NMLOG(LOG_NOTICE,  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]