[network-manager-libreswan/th/vpn-plugin-debug-bgo766872: 17/22] service: pass the log-level and logging-prefix to the helper



commit 2dbcb72aa0b12f45957d5ebdfb74a9c46a37b169
Author: Thomas Haller <thaller redhat com>
Date:   Thu May 26 17:45:21 2016 +0200

    service: pass the log-level and logging-prefix to the helper
    
    and replace g_log() calls by _LOGW() macro (and g_print()).

 src/Makefile.am                   |    1 +
 src/nm-libreswan-service-helper.c |   63 +++++++++++++++++++++++++++++++-----
 src/nm-libreswan-service.c        |    6 +++-
 3 files changed, 60 insertions(+), 10 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index b72636a..a2cd632 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -56,6 +56,7 @@ nm_libreswan_service_helper_SOURCES = \
 
 nm_libreswan_service_helper_LDADD = \
     $(LIBNM_LIBS) \
+    $(top_builddir)/shared/libnm-vpn-plugin-libreswan-shared.la \
     libnm-libreswan-helper-service-dbus.la
 
 ###############################################################################
diff --git a/src/nm-libreswan-service-helper.c b/src/nm-libreswan-service-helper.c
index 75d55cb..d871fd2 100644
--- a/src/nm-libreswan-service-helper.c
+++ b/src/nm-libreswan-service-helper.c
@@ -26,6 +26,37 @@
 #include <string.h>
 
 #include "nm-libreswan-helper-service-dbus.h"
+#include "nm-utils/nm-shared-utils.h"
+#include "nm-vpn/nm-vpn-plugin-macros.h"
+
+extern char **environ;
+
+static struct {
+       int log_level;
+       const char *log_prefix_token;
+} gl/*obal*/ = {
+       .log_level = LOG_WARNING,
+       .log_prefix_token = "???",
+};
+
+/*****************************************************************************/
+
+#define _NMLOG(level, ...) \
+    G_STMT_START { \
+         if (gl.log_level >= (level)) { \
+              g_print ("nm-libreswan-helper[%s,%ld]: %-7s " _NM_UTILS_MACRO_FIRST (__VA_ARGS__) "\n", \
+                       gl.log_prefix_token, \
+                       (long) getpid (), \
+                       nm_utils_syslog_to_str (level) \
+                       _NM_UTILS_MACRO_REST (__VA_ARGS__)); \
+         } \
+    } G_STMT_END
+
+#define _LOGD(...) _NMLOG(LOG_INFO,    __VA_ARGS__)
+#define _LOGI(...) _NMLOG(LOG_NOTICE,  __VA_ARGS__)
+#define _LOGW(...) _NMLOG(LOG_WARNING, __VA_ARGS__)
+
+/*****************************************************************************/
 
 int
 main (int argc, char *argv[])
@@ -35,11 +66,23 @@ main (int argc, char *argv[])
        GError *err = NULL;
        gchar **env;
        gchar **p;
-       const char *bus_name;
+       const char *bus_name = NM_DBUS_SERVICE_LIBRESWAN;
+       char *str = NULL;
+       char **i_env;
 
+#if !GLIB_CHECK_VERSION (2, 35, 0)
+       g_type_init ();
+#endif
+
+       /* support old command line arguments. The only reason for that is to
+        * support update of the plugin while being connected. */
        switch (argc) {
        case 1:
-               bus_name = NM_DBUS_SERVICE_LIBRESWAN;
+               break;
+       case 4:
+               gl.log_level = _nm_utils_ascii_str_to_int64 (argv[1], 10, 0, LOG_DEBUG, 0);
+               gl.log_prefix_token = argv[2];
+               bus_name = argv[3];
                break;
        case 3:
                if (strcmp (argv[1], "--bus-name") == 0) {
@@ -48,18 +91,20 @@ main (int argc, char *argv[])
                }
                /* fallthrough */
        default:
-               g_warning ("Usage: %s [--bus-name <name>]", argv[0]);
+               g_printerr ("Usage: %s <LEVEL> <PREFIX_TOKEN> <BUS_NAME>\n", argv[0]);
                exit (1);
        }
 
        if (!g_dbus_is_name (bus_name)) {
-               g_warning ("Not a valid bus name: '%s'\n", bus_name);
+               g_printerr ("Not a valid bus name: '%s'\n", bus_name);
                exit (1);
        }
 
-#if !GLIB_CHECK_VERSION (2, 35, 0)
-       g_type_init ();
-#endif
+       _LOGD ("command line: %s", (str = g_strjoinv (" ", argv)));
+       g_clear_pointer (&str, g_free);
+
+       for (i_env = environ; i_env && *i_env; i_env++)
+               _LOGD ("environment: %s", *i_env);
 
        proxy = nmdbus_libreswan_helper_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                                                G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
@@ -67,7 +112,7 @@ main (int argc, char *argv[])
                                                                NM_DBUS_PATH_LIBRESWAN_HELPER,
                                                                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);
        }
@@ -84,7 +129,7 @@ main (int argc, char *argv[])
        if (!nmdbus_libreswan_helper_call_callback_sync (proxy,
                                                         g_variant_builder_end (&environment),
                                                         NULL, &err)) {
-               g_warning ("Could not call the plugin: %s", err->message);
+               _LOGW ("Could not call the plugin: %s", err->message);
                g_error_free (err);
                g_object_unref (proxy);
                exit (1);
diff --git a/src/nm-libreswan-service.c b/src/nm-libreswan-service.c
index db79891..c198ce8 100644
--- a/src/nm-libreswan-service.c
+++ b/src/nm-libreswan-service.c
@@ -1635,7 +1635,11 @@ connect_step (NMLibreswanPlugin *self, GError **error)
                 */
                trailing_newline = priv->openswan;
 
-               ifupdown_script = g_strdup_printf ("\"%s --bus-name %s\"", NM_LIBRESWAN_HELPER_PATH, 
bus_name);
+               ifupdown_script = g_strdup_printf ("\"%s %d %ld %s\"",
+                                                  NM_LIBRESWAN_HELPER_PATH,
+                                                  LOG_DEBUG,
+                                                  (long) getpid (),
+                                                  bus_name);
 
                if (!nm_libreswan_config_write (fd,
                                                priv->connection,


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