[recipes] Move logging functions to their own file



commit 3eef856cf9f4abcdecad9a48d4e5b4e0589ca045
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Mar 14 22:28:26 2017 -0400

    Move logging functions to their own file
    
    Just a minor cleanup.

 src/Makefile.am  |    2 +
 src/gr-app.c     |   64 +++---------------------------------
 src/gr-logging.c |   93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gr-logging.h |   32 ++++++++++++++++++
 src/meson.build  |    1 +
 5 files changed, 134 insertions(+), 58 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 44d90e2..25ac6cf 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -74,6 +74,8 @@ gnome_recipes_SOURCES = \
        gr-ingredients-list.c   \
        gr-list-page.h          \
        gr-list-page.c          \
+       gr-logging.h            \
+       gr-logging.c            \
        gr-mail.h               \
        gr-mail.c               \
        gr-meal.h               \
diff --git a/src/gr-app.c b/src/gr-app.c
index 1b8c162..dbf7b78 100644
--- a/src/gr-app.c
+++ b/src/gr-app.c
@@ -32,6 +32,7 @@
 #include "gr-cuisine.h"
 #include "gr-shell-search-provider.h"
 #include "gr-utils.h"
+#include "gr-logging.h"
 #include "gr-recipe-exporter.h"
 
 
@@ -207,68 +208,15 @@ search_activated (GSimpleAction *action,
         gr_window_show_search (GR_WINDOW (win), search);
 }
 
-#define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE)
-#define INFO_LEVELS (G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG)
-
-static gboolean verbose_logging;
-
-/* verbose_logging turns enables DEBUG and INFO messages just for our log domain.
- * We also respect the G_MESSAGES_DEBUG environment variable.
- */
-static GLogWriterOutput
-log_writer (GLogLevelFlags   log_level,
-            const GLogField *fields,
-            gsize            n_fields,
-            gpointer         user_data)
-{
-        if (!(log_level & DEFAULT_LEVELS)) {
-                const gchar *domains, *log_domain = NULL;
-                gsize i;
-
-                domains = g_getenv ("G_MESSAGES_DEBUG");
-
-                if (verbose_logging && domains == NULL)
-                        domains = G_LOG_DOMAIN;
-
-                if ((log_level & INFO_LEVELS) == 0 || domains == NULL)
-                        return G_LOG_WRITER_HANDLED;
-
-                for (i = 0; i < n_fields; i++) {
-                        if (g_strcmp0 (fields[i].key, "GLIB_DOMAIN") == 0) {
-                                log_domain = fields[i].value;
-                                break;
-                        }
-                }
-
-                if (!verbose_logging || strcmp (log_domain, G_LOG_DOMAIN) != 0) {
-                        if (strcmp (domains, "all") != 0 &&
-                            (log_domain == NULL || !strstr (domains, log_domain)))
-                                return G_LOG_WRITER_HANDLED;
-                }
-        }
-
-        if (g_log_writer_is_journald (fileno (stderr)) &&
-            g_log_writer_journald (log_level, fields, n_fields, user_data) == G_LOG_WRITER_HANDLED)
-                goto handled;
-
-        if (g_log_writer_standard_streams (log_level, fields, n_fields, user_data) == G_LOG_WRITER_HANDLED)
-                goto handled;
-
-        return G_LOG_WRITER_UNHANDLED;
-
-handled:
-        if (log_level & G_LOG_LEVEL_ERROR)
-                g_abort ();
-
-        return G_LOG_WRITER_HANDLED;
-}
-
 static void
 verbose_logging_activated (GSimpleAction *action,
                            GVariant      *parameter,
                            gpointer       application)
 {
-        g_variant_get (parameter, "b", &verbose_logging);
+        gboolean verbose;
+
+        g_variant_get (parameter, "b", &verbose);
+        gr_set_verbose_logging (verbose);
 }
 
 static GActionEntry app_entries[] =
@@ -443,7 +391,7 @@ gr_app_init (GrApp *self)
                                        G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE,
                                        _("Turn on verbose logging"), NULL);
 
-        g_log_set_writer_func (log_writer, NULL, NULL);
+        g_log_set_writer_func (gr_log_writer, NULL, NULL);
 }
 
 static int
diff --git a/src/gr-logging.c b/src/gr-logging.c
new file mode 100644
index 0000000..d61e3c2
--- /dev/null
+++ b/src/gr-logging.c
@@ -0,0 +1,93 @@
+/* gr-logging.c:
+ *
+ * Copyright (C) 2017 Matthias Clasen <mclasen redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <gr-logging.h>
+
+/* verbose_logging turns enables DEBUG and INFO messages just for our log domain.
+ * We also respect the G_MESSAGES_DEBUG environment variable.
+ */
+
+static gboolean verbose_logging;
+
+void
+gr_set_verbose_logging (gboolean verbose)
+{
+        verbose_logging = verbose;
+}
+
+
+#define DEFAULT_LEVELS (G_LOG_LEVEL_ERROR |     \
+                        G_LOG_LEVEL_CRITICAL |  \
+                        G_LOG_LEVEL_WARNING |   \
+                        G_LOG_LEVEL_MESSAGE)
+#define INFO_LEVELS    (G_LOG_LEVEL_INFO |      \
+                        G_LOG_LEVEL_DEBUG)
+
+GLogWriterOutput
+gr_log_writer (GLogLevelFlags   log_level,
+               const GLogField *fields,
+               gsize            n_fields,
+               gpointer         user_data)
+{
+        if (!(log_level & DEFAULT_LEVELS)) {
+                const gchar *domains, *log_domain = NULL;
+                gsize i;
+
+                domains = g_getenv ("G_MESSAGES_DEBUG");
+
+                if (verbose_logging && domains == NULL)
+                        domains = G_LOG_DOMAIN;
+
+                if ((log_level & INFO_LEVELS) == 0 || domains == NULL)
+                        return G_LOG_WRITER_HANDLED;
+
+                for (i = 0; i < n_fields; i++) {
+                        if (g_strcmp0 (fields[i].key, "GLIB_DOMAIN") == 0) {
+                                log_domain = fields[i].value;
+                                break;
+                        }
+                }
+
+                if (!verbose_logging || strcmp (log_domain, G_LOG_DOMAIN) != 0) {
+                        if (strcmp (domains, "all") != 0 &&
+                            (log_domain == NULL || !strstr (domains, log_domain)))
+                                return G_LOG_WRITER_HANDLED;
+                }
+        }
+
+        if (g_log_writer_is_journald (fileno (stderr)) &&
+            g_log_writer_journald (log_level, fields, n_fields, user_data) == G_LOG_WRITER_HANDLED)
+                goto handled;
+
+        if (g_log_writer_standard_streams (log_level, fields, n_fields, user_data) == G_LOG_WRITER_HANDLED)
+                goto handled;
+
+        return G_LOG_WRITER_UNHANDLED;
+
+handled:
+        if (log_level & G_LOG_LEVEL_ERROR)
+                g_abort ();
+
+        return G_LOG_WRITER_HANDLED;
+}
diff --git a/src/gr-logging.h b/src/gr-logging.h
new file mode 100644
index 0000000..e5e20a9
--- /dev/null
+++ b/src/gr-logging.h
@@ -0,0 +1,32 @@
+/* gr-logging.h:
+ *
+ * Copyright (C) 2017 Matthias Clasen <mclasen redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+GLogWriterOutput        gr_log_writer          (GLogLevelFlags   log_level,
+                                                const GLogField *fields,
+                                                gsize            n_fields,
+                                                gpointer         user_data);
+
+void                    gr_set_verbose_logging (gboolean         verbose);
+
+G_END_DECLS
diff --git a/src/meson.build b/src/meson.build
index 3fe3e2c..a3fffc0 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -71,6 +71,7 @@ src += ['main.c',
        'gr-ingredient-row.c',
        'gr-ingredients-list.c',
        'gr-list-page.c',
+       'gr-logging.c',
        'gr-mail.c',
        'gr-meal.c',
        'gr-meal-row.c',


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