[network-manager-openconnect/th/logging-bgo767733: 5/6] src: use logging macros in service helper



commit c9dd6d2a48bb5fd2fd90829e3103603ef0a3f2c1
Author: Thomas Haller <thaller redhat com>
Date:   Thu Jun 16 18:41:52 2016 +0200

    src: use logging macros in service helper

 src/Makefile.am                                 |    2 +
 src/nm-openconnect-service-openconnect-helper.c |   56 ++++++++++++++++++-----
 src/nm-openconnect-service.c                    |    5 ++
 3 files changed, 51 insertions(+), 12 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 5fc94fa..dc9d45a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -32,6 +32,8 @@ nm_openconnect_service_LDADD = \
 ###############################################################################
 
 nm_openconnect_service_openconnect_helper_SOURCES = \
+       $(top_srcdir)/shared/nm-utils/nm-shared-utils.c \
+       $(top_srcdir)/shared/nm-utils/nm-shared-utils.h \
        nm-openconnect-service-openconnect-helper.c
 
 nm_openconnect_service_openconnect_helper_LDADD = \
diff --git a/src/nm-openconnect-service-openconnect-helper.c b/src/nm-openconnect-service-openconnect-helper.c
index f4ef10c..7cbad0e 100644
--- a/src/nm-openconnect-service-openconnect-helper.c
+++ b/src/nm-openconnect-service-openconnect-helper.c
@@ -32,19 +32,46 @@
 #include <arpa/inet.h>
 #include <errno.h>
 
+#include "nm-utils/nm-shared-utils.h"
+#include "nm-utils/nm-vpn-plugin-macros.h"
+
+/*****************************************************************************/
+
+static struct {
+       int log_level;
+       const char *log_prefix_token;
+} gl/*obal*/;
+
+/*****************************************************************************/
+
+#define _NMLOG(level, ...) \
+       G_STMT_START { \
+               if (gl.log_level >= (level)) { \
+                       g_print ("nm-openconnect[%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 _LOGW(...) _NMLOG(LOG_WARNING, __VA_ARGS__)
+
+/*****************************************************************************/
+
 static void
 helper_failed (GDBusProxy *proxy, const char *reason)
 {
        GError *err = NULL;
 
-       g_warning ("nm-nopenconnect-service-openconnect-helper did not receive a valid %s from openconnect", 
reason);
+       _LOGW ("nm-nopenconnect-service-openconnect-helper did not receive a valid %s from openconnect", 
reason);
 
        if (!g_dbus_proxy_call_sync (proxy, "SetFailure",
                                     g_variant_new ("(s)", reason),
                                     G_DBUS_CALL_FLAGS_NONE, -1,
                                     NULL,
                                     &err)) {
-               g_warning ("Could not send failure information: %s", err->message);
+               _LOGW ("Could not send failure information: %s", err->message);
                g_error_free (err);
        }
 
@@ -84,7 +111,7 @@ send_config (GDBusProxy *proxy, GVariant *config,
 
        return;
 error:
-       g_warning ("Could not send configuration information: %s", err->message);
+       _LOGW ("Could not send configuration information: %s", err->message);
        g_error_free (err);
 }
 
@@ -257,7 +284,7 @@ get_ip4_routes (void)
                snprintf (buf, BUFLEN, "CISCO_SPLIT_INC_%d_ADDR", i);
                tmp = getenv (buf);
                if (!tmp || inet_pton (AF_INET, tmp, &network) <= 0) {
-                       g_warning ("Ignoring invalid static route address '%s'", tmp ? tmp : "NULL");
+                       _LOGW ("Ignoring invalid static route address '%s'", tmp ? tmp : "NULL");
                        continue;
                }
 
@@ -269,7 +296,7 @@ get_ip4_routes (void)
                        errno = 0;
                        tmp_prefix = strtol (tmp, NULL, 10);
                        if (errno || tmp_prefix <= 0 || tmp_prefix > 32) {
-                               g_warning ("Ignoring invalid static route prefix '%s'", tmp ? tmp : "NULL");
+                               _LOGW ("Ignoring invalid static route prefix '%s'", tmp ? tmp : "NULL");
                                continue;
                        }
                        prefix = (guint32) tmp_prefix;
@@ -279,7 +306,7 @@ get_ip4_routes (void)
                        snprintf (buf, BUFLEN, "CISCO_SPLIT_INC_%d_MASK", i);
                        tmp = getenv (buf);
                        if (!tmp || inet_pton (AF_INET, tmp, &netmask) <= 0) {
-                               g_warning ("Ignoring invalid static route netmask '%s'", tmp ? tmp : "NULL");
+                               _LOGW ("Ignoring invalid static route netmask '%s'", tmp ? tmp : "NULL");
                                continue;
                        }
                        prefix = nm_utils_ip4_netmask_to_prefix (netmask.s_addr);
@@ -331,7 +358,7 @@ get_ip6_routes (void)
                snprintf (buf, BUFLEN, "CISCO_IPV6_SPLIT_INC_%d_ADDR", i);
                network = getenv (buf);
                if (!network) {
-                       g_warning ("Ignoring invalid static route address '%s'", network ? network : "NULL");
+                       _LOGW ("Ignoring invalid static route address '%s'", network ? network : "NULL");
                        continue;
                }
 
@@ -343,18 +370,18 @@ get_ip6_routes (void)
                        errno = 0;
                        tmp_prefix = strtol (tmp, NULL, 10);
                        if (errno || tmp_prefix <= 0 || tmp_prefix > 128) {
-                               g_warning ("Ignoring invalid static route prefix '%s'", tmp ? tmp : "NULL");
+                               _LOGW ("Ignoring invalid static route prefix '%s'", tmp ? tmp : "NULL");
                                continue;
                        }
                        prefix = (guint32) tmp_prefix;
                } else {
-                       g_warning ("Ignoring static route %d with no prefix length", i);
+                       _LOGW ("Ignoring static route %d with no prefix length", i);
                        continue;
                }
 
                route = nm_ip_route_new (AF_INET6, network, prefix, NULL, -1, &error);
                if (!route) {
-                       g_warning ("Ignoring a route: %s", error->message);
+                       _LOGW ("Ignoring a route: %s", error->message);
                        g_error_free (error);
                        continue;
                }
@@ -398,6 +425,11 @@ main (int argc, char *argv[])
        g_type_init ();
 #endif
 
+       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") ?: "???";
+
        /* openconnect gives us a "reason" code.  If we are given one,
         * don't proceed unless its "connect".
         */
@@ -417,7 +449,7 @@ main (int argc, char *argv[])
                                               NM_VPN_DBUS_PLUGIN_INTERFACE,
                                               NULL, &err);
        if (!proxy) {
-               g_warning ("Could not create a D-Bus proxy: %s", err->message);
+               _LOGW ("Could not create a D-Bus proxy: %s", err->message);
                g_error_free (err);
                exit (1);
        }
@@ -460,7 +492,7 @@ main (int argc, char *argv[])
                errno = 0;
                mtu = strtol (tmp, NULL, 10);
                if (errno || mtu < 0 || mtu > 20000) {
-                       g_warning ("Ignoring invalid tunnel MTU '%s'", tmp);
+                       _LOGW ("Ignoring invalid tunnel MTU '%s'", tmp);
                } else {
                        val = g_variant_new_uint32 ((guint32) mtu);
                        g_variant_builder_add (&builder, "{sv}", NM_VPN_PLUGIN_CONFIG_MTU, val);
diff --git a/src/nm-openconnect-service.c b/src/nm-openconnect-service.c
index 2acdf23..cbcf82c 100644
--- a/src/nm-openconnect-service.c
+++ b/src/nm-openconnect-service.c
@@ -684,6 +684,7 @@ int main (int argc, char *argv[])
        gboolean persist = FALSE;
        GOptionContext *opt_ctx = NULL;
        gchar *bus_name = NM_DBUS_SERVICE_OPENCONNECT;
+       char sbuf[30];
 
        GOptionEntry options[] = {
                { "persist", 0, 0, G_OPTION_ARG_NONE, &persist, N_("Don't quit when VPN connection 
terminates"), NULL },
@@ -724,6 +725,10 @@ int main (int argc, char *argv[])
                                                     10, 0, LOG_DEBUG,
                                                     gl.debug ? LOG_INFO : LOG_NOTICE);
 
+       /* set logging options for helper script. */
+       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);
+
        _LOGD ("nm-openconnect-service (version " DIST_VERSION ") starting...");
 
        if (system ("/sbin/modprobe tun") == -1)


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