[glib-networking] Add logging function



commit e9da33da2b2f98303fc61efc5abe14370befbede
Author: Frederic Martinsons <frederic martinsons sigfox com>
Date:   Fri Nov 1 11:20:19 2019 +0100

    Add logging function
    
    By using glib structured log API.
    Some helper macro have been added to avoid specifying log level (
    the logging level is in the name of the macro).
    A pointer can also be given for logging function to print if this
    for server or for client side (will do nothing if the pointer is NULL
    or if is not of type G_TLS_CONNECTION)
    
    Signed-off-by: Frederic Martinsons <frederic martinsons sigfox com>

 meson.build          |  1 +
 tls/base/gtlslog.c   | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 tls/base/gtlslog.h   | 55 +++++++++++++++++++++++++++++++++++++
 tls/base/meson.build |  1 +
 4 files changed, 134 insertions(+)
---
diff --git a/meson.build b/meson.build
index 4c60691..d16cb8e 100644
--- a/meson.build
+++ b/meson.build
@@ -26,6 +26,7 @@ config_h.set_quoted('GETTEXT_PACKAGE', meson.project_name())
 common_flags = [
   '-DHAVE_CONFIG_H',
   '-DG_LOG_DOMAIN="GLib-Net"',
+  '-DG_LOG_USE_STRUCTURED',
   '-DLOCALE_DIR="@0@"'.format(localedir),
   '-DG_DISABLE_DEPRECATED',
   '-DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_56'
diff --git a/tls/base/gtlslog.c b/tls/base/gtlslog.c
new file mode 100644
index 0000000..1dbb699
--- /dev/null
+++ b/tls/base/gtlslog.c
@@ -0,0 +1,77 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2009 Red Hat, Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * In addition, when the library is used with OpenSSL, a special
+ * exception applies. Refer to the LICENSE_EXCEPTION file for details.
+ */
+
+#include "config.h"
+
+#include <gio/gio.h>
+#include <glib.h>
+#include <glib/gprintf.h>
+#include <stdarg.h>
+
+#include "gtlslog.h"
+
+void g_tls_log (GLogLevelFlags  level,
+                gpointer        conn,
+                const gchar    *file,
+                const gchar    *line,
+                const gchar    *func,
+                const gchar    *format,
+                ...)
+{
+  gchar *header = NULL;
+  gchar *message = NULL;
+  gchar *thread = NULL;
+  va_list args;
+  int ret;
+
+  va_start (args, format);
+  ret = g_vasprintf (&message, format, args);
+  va_end (args);
+
+  if (ret <= 0)
+    return;
+
+  if (conn && G_IS_TLS_CONNECTION (conn)) {
+    if (G_IS_TLS_CLIENT_CONNECTION (conn))
+      header = g_strdup_printf ("CLIENT[%p]: ", conn);
+    else if (G_IS_TLS_SERVER_CONNECTION (conn))
+      header = g_strdup_printf ("SERVER[%p]: ", conn);
+    else
+      g_assert_not_reached ();
+  } else {
+    header = g_strdup ("");
+  }
+
+  thread = g_strdup_printf ("%p", g_thread_self ());
+  g_log_structured (G_LOG_DOMAIN, level,
+                    "GLIB_NET_THREAD", thread,
+                    "CODE_FILE", file,
+                    "CODE_LINE", line,
+                    "CODE_FUNC", func,
+                    "MESSAGE", "%s%s", header, message);
+
+  g_free (header);
+  g_free (message);
+  g_free (thread);
+}
diff --git a/tls/base/gtlslog.h b/tls/base/gtlslog.h
new file mode 100644
index 0000000..ffb8b36
--- /dev/null
+++ b/tls/base/gtlslog.h
@@ -0,0 +1,55 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright 2010 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * In addition, when the library is used with OpenSSL, a special
+ * exception applies. Refer to the LICENSE_EXCEPTION file for details.
+ */
+
+#ifndef __G_TLS_LOG_H__
+#define __G_TLS_LOG_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void g_tls_log (GLogLevelFlags  level,
+                gpointer        conn,
+                const gchar    *file,
+                const gchar    *line,
+                const gchar    *func,
+                const gchar    *format,
+                ...) G_GNUC_PRINTF (6, 7);
+
+#define g_tls_log_debug(_conn, _format, _args...)   g_tls_log (G_LOG_LEVEL_DEBUG, _conn, \
+                                                               __FILE__, G_STRINGIFY (__LINE__), \
+                                                               G_STRFUNC, _format, ## _args)
+#define g_tls_log_info(_conn, _format, _args...)    g_tls_log (G_LOG_LEVEL_INFO, _conn, \
+                                                               __FILE__, G_STRINGIFY (__LINE__), \
+                                                               G_STRFUNC, _format, ## _args)
+#define g_tls_log_warning(_conn, _format, _args...) g_tls_log (G_LOG_LEVEL_WARNING, _conn, \
+                                                               __FILE__, G_STRINGIFY (__LINE__), \
+                                                               G_STRFUNC, _format, ## _args)
+#define g_tls_log_error(_conn, _format, _args...)   g_tls_log (G_LOG_LEVEL_ERROR, _conn, \
+                                                               __FILE__, G_STRINGIFY (__LINE__), \
+                                                               G_STRFUNC, _format, ## _args)
+
+G_END_DECLS
+
+#endif /* __G_TLS_LOG_H__ */
diff --git a/tls/base/meson.build b/tls/base/meson.build
index 0fac433..ca7d5a3 100644
--- a/tls/base/meson.build
+++ b/tls/base/meson.build
@@ -1,6 +1,7 @@
 tlsbase_sources = files(
   'gtlsconnection-base.c',
   'gtlsinputstream.c',
+  'gtlslog.c',
   'gtlsoutputstream.c',
 )
 


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