[network-manager-pptp: 10/12] src: use logging macros in pppd-plugin and make logging configurable



commit 1263f527a3faea32eec3cf885dc1966aa7ac0f8e
Author: Thomas Haller <thaller redhat com>
Date:   Mon Sep 19 16:51:31 2016 +0200

    src: use logging macros in pppd-plugin and make logging configurable

 src/Makefile.am           |    1 +
 src/nm-pptp-pppd-plugin.c |   82 ++++++++++++++++++++++++++++----------------
 src/nm-pptp-service.c     |    3 ++
 3 files changed, 56 insertions(+), 30 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 455ae44..972401a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -58,6 +58,7 @@ pppd_plugindir = $(PPPD_PLUGIN_DIR)
 pppd_plugin_LTLIBRARIES = nm-pptp-pppd-plugin.la
 
 nm_pptp_pppd_plugin_la_SOURCES = \
+    $(shared_sources) \
     nm-pptp-pppd-plugin.c \
     nm-ppp-status.h
 
diff --git a/src/nm-pptp-pppd-plugin.c b/src/nm-pptp-pppd-plugin.c
index 74ccb04..21c2442 100644
--- a/src/nm-pptp-pppd-plugin.c
+++ b/src/nm-pptp-pppd-plugin.c
@@ -40,6 +40,9 @@
 #include "nm-pptp-service.h"
 #include "nm-ppp-status.h"
 
+#include "nm-utils/nm-shared-utils.h"
+#include "nm-utils/nm-vpn-plugin-macros.h"
+
 int plugin_init (void);
 
 char pppd_version[] = VERSION;
@@ -47,11 +50,30 @@ char pppd_version[] = VERSION;
 /*****************************************************************************/
 
 struct {
+       int log_level;
+       const char *log_prefix_token;
        GDBusProxy *proxy;
 } gl/*lobal*/;
 
 /*****************************************************************************/
 
+#define _NMLOG(level, ...) \
+    G_STMT_START { \
+         if (gl.log_level >= (level)) { \
+             g_print ("nm-pptp[%s] %-7s [helper-%ld] " _NM_UTILS_MACRO_FIRST (__VA_ARGS__) "\n", \
+                      gl.log_prefix_token, \
+                      nm_utils_syslog_to_str (level), \
+                      (long) getpid () \
+                      _NM_UTILS_MACRO_REST (__VA_ARGS__)); \
+         } \
+    } G_STMT_END
+
+#define _LOGI(...) _NMLOG(LOG_NOTICE,  __VA_ARGS__)
+#define _LOGW(...) _NMLOG(LOG_WARNING, __VA_ARGS__)
+#define _LOGE(...) _NMLOG(LOG_ERR, __VA_ARGS__)
+
+/*****************************************************************************/
+
 static void
 nm_phasechange (void *data, int arg)
 {
@@ -119,10 +141,8 @@ nm_phasechange (void *data, int arg)
                break;
        }
 
-       g_message ("nm-pptp-ppp-plugin: (%s): status %d / phase '%s'",
-                  __func__,
-                  ppp_status,
-                  ppp_phase);
+       _LOGI ("phasechange: status %d / phase '%s'",
+              ppp_status, ppp_phase);
 
        if (ppp_status != NM_PPP_STATUS_UNKNOWN) {
                g_dbus_proxy_call (gl.proxy,
@@ -144,10 +164,10 @@ nm_ip_up (void *data, int arg)
 
        g_return_if_fail (G_IS_DBUS_PROXY (gl.proxy));
 
-       g_message ("nm-pptp-ppp-plugin: (%s): ip-up event", __func__);
+       _LOGI ("ip-up: event");
 
        if (!opts.ouraddr) {
-               g_warning ("nm-pptp-ppp-plugin: (%s): didn't receive an internal IP from pppd!", __func__);
+               _LOGW ("ip-up: didn't receive an internal IP from pppd!");
                nm_phasechange (NULL, PHASE_DEAD);
                return;
        }
@@ -205,7 +225,7 @@ nm_ip_up (void *data, int arg)
                               NM_VPN_PLUGIN_IP4_CONFIG_MTU,
                               g_variant_new_uint32 (1400));
 
-       g_message ("nm-pptp-ppp-plugin: (%s): sending Ip4Config to NetworkManager-pptp...", __func__);
+       _LOGI ("ip-up: sending Ip4Config to NetworkManager-pptp...");
 
        g_dbus_proxy_call (gl.proxy,
                           "SetIp4Config",
@@ -234,7 +254,7 @@ get_credentials (char *username, char *password)
        const char *my_password = NULL;
        size_t len;
        GVariant *ret;
-       GError *err = NULL;
+       GError *error = NULL;
 
        if (!password) {
                /* pppd is checking pap support; return 1 for supported */
@@ -245,23 +265,21 @@ get_credentials (char *username, char *password)
        g_return_val_if_fail (username, -1);
        g_return_val_if_fail (G_IS_DBUS_PROXY (gl.proxy), -1);
 
-       g_message ("nm-pptp-ppp-plugin: (%s): passwd-hook, requesting credentials...", __func__);
+       _LOGI ("passwd-hook: requesting credentials...");
 
        ret = g_dbus_proxy_call_sync (gl.proxy,
                                      "NeedSecrets",
                                      NULL,
                                      G_DBUS_CALL_FLAGS_NONE, -1,
-                                     NULL, &err);
+                                     NULL, &error);
        if (!ret) {
-               g_warning ("nm-pptp-ppp-plugin: (%s): could not get secrets: (%d) %s",
-                          __func__,
-                          err ? err->code : -1,
-                          err->message ? err->message : "(unknown)");
-               g_error_free (err);
+               _LOGW ("passwd-hook: could not get secrets: %s",
+                      error->message);
+               g_error_free (error);
                return -1;
        }
 
-       g_message ("nm-pptp-ppp-plugin: (%s): got credentials from NetworkManager-pptp", __func__);
+       _LOGI ("passwd-hook: got credentials from NetworkManager-pptp");
 
        g_variant_get (ret, "(&s&s)", &my_username, &my_password);
 
@@ -291,8 +309,7 @@ nm_exit_notify (void *data, int arg)
 {
        g_return_if_fail (G_IS_DBUS_PROXY (gl.proxy));
 
-       g_message ("nm-pptp-ppp-plugin: (%s): cleaning up", __func__);
-
+       _LOGI ("exit: cleaning up");
        g_clear_object (&gl.proxy);
 }
 
@@ -300,24 +317,29 @@ int
 plugin_init (void)
 {
        GDBusConnection *bus;
-       GError *err = NULL;
+       GError *error = NULL;
        const char *bus_name;
 
        nm_g_type_init ();
 
+       g_return_val_if_fail (!gl.proxy, -1);
+
        bus_name = getenv ("NM_DBUS_SERVICE_PPTP");
        if (!bus_name)
                bus_name = NM_DBUS_SERVICE_PPTP;
 
-       g_message ("nm-pptp-ppp-plugin: (%s): initializing", __func__);
+       gl.log_level = _nm_utils_ascii_str_to_int64 (getenv ("NM_VPN_LOG_LEVEL"),
+                                                    10, 0, LOG_DEBUG,
+                                                    LOG_NOTICE);
+       gl.log_prefix_token = getenv ("NM_VPN_LOG_PREFIX_TOKEN") ?: "???";
+
+       _LOGI ("initializing");
 
-       bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &err);
+       bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
        if (!bus) {
-               g_warning ("nm-pptp-pppd-plugin: (%s): couldn't connect to system bus: (%d) %s",
-                          __func__,
-                          err ? err->code : -1,
-                          err && err->message ? err->message : "(unknown)");
-               g_error_free (err);
+               _LOGE ("couldn't connect to system bus: %s",
+                      error->message);
+               g_error_free (error);
                return -1;
        }
 
@@ -327,13 +349,13 @@ plugin_init (void)
                                          bus_name,
                                          NM_DBUS_PATH_PPTP_PPP,
                                          NM_DBUS_INTERFACE_PPTP_PPP,
-                                         NULL, &err);
+                                         NULL, &error);
        g_object_unref (bus);
 
        if (!gl.proxy) {
-               g_warning ("nm-pptp-pppd-plugin: (%s): couldn't create D-Bus proxy: %s",
-                          __func__, err->message);
-               g_error_free (err);
+               _LOGE ("couldn't create D-Bus proxy: %s",
+                      error->message);
+               g_error_free (error);
                return -1;
        }
 
diff --git a/src/nm-pptp-service.c b/src/nm-pptp-service.c
index fb76c59..e358a48 100644
--- a/src/nm-pptp-service.c
+++ b/src/nm-pptp-service.c
@@ -1117,6 +1117,7 @@ main (int argc, char *argv[])
        GError *error = NULL;
        gs_free char *bus_name_free = NULL;
        const char *bus_name;
+       char sbuf[30];
 
        GOptionEntry options[] = {
                { "persist", 0, 0, G_OPTION_ARG_NONE, &persist, N_("Don't quit when VPN connection 
terminates"), NULL },
@@ -1164,6 +1165,8 @@ main (int argc, char *argv[])
        _LOGD ("nm-pptp-service (version " DIST_VERSION ") starting...");
        _LOGD ("   uses%s --bus-name \"%s\"", bus_name_free ? "" : " default", bus_name);
 
+       setenv ("NM_VPN_LOG_LEVEL", nm_sprintf_buf (sbuf, "%d", gl.log_level), TRUE);
+       setenv ("NM_VPN_LOG_PREFIX_TOKEN", nm_sprintf_buf (sbuf, "%ld", (long) getpid ()), TRUE);
        setenv ("NM_DBUS_SERVICE_PPTP", bus_name, 0);
 
        plugin = nm_pptp_plugin_new (bus_name);


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