[geary/mjog/logging-improvements: 7/23] Geary.Db.Context: Convert to implement Logging.Source



commit 2f24974e7bb294b7a7902c0dd31668599272a202
Author: Michael Gratton <mike vee net>
Date:   Wed Apr 15 15:36:07 2020 +1000

    Geary.Db.Context: Convert to implement Logging.Source
    
    Use a custom logging sub-domain, update Connection to use debug
    method from Source.

 src/engine/db/db-connection.vala |  2 +-
 src/engine/db/db-context.vala    | 54 ++++++++++++++++++++++++++++++----------
 2 files changed, 42 insertions(+), 14 deletions(-)
---
diff --git a/src/engine/db/db-connection.vala b/src/engine/db/db-connection.vala
index 8949a80d..2a1424b9 100644
--- a/src/engine/db/db-connection.vala
+++ b/src/engine/db/db-connection.vala
@@ -111,7 +111,7 @@ public class Geary.Db.Connection : Geary.Db.Context {
         throw_on_error("Connection.exec", db.exec(sql), sql);
 
         // Don't use Context.log(), which is designed for logging Results and Statements
-        Logging.debug(Logging.Flag.SQL, "exec:\n\t%s", sql);
+        debug("exec:\n\t%s", sql);
     }
 
     /**
diff --git a/src/engine/db/db-context.vala b/src/engine/db/db-context.vala
index aac70215..e659d6d4 100644
--- a/src/engine/db/db-context.vala
+++ b/src/engine/db/db-context.vala
@@ -1,7 +1,9 @@
-/* Copyright 2016 Software Freedom Conservancy Inc.
+/*
+ * Copyright © 2016 Software Freedom Conservancy Inc.
+ * Copyright © 2020 Michael Gratton <mike vee net>
  *
  * This software is licensed under the GNU Lesser General Public License
- * (version 2.1 or later).  See the COPYING file in this distribution.
+ * (version 2.1 or later). See the COPYING file in this distribution.
  */
 
 /**
@@ -11,8 +13,26 @@
  *
  * Geary.Db's major classes (Database, Connection, Statement, and Result) inherit from Context.
  */
+public abstract class Geary.Db.Context : BaseObject, Logging.Source {
+
+    /** The GLib logging domain used by this class. */
+    public const string LOGGING_DOMAIN = Logging.DOMAIN + ".Db";
+
+    /** {@inheritDoc} */
+    public Logging.Flag logging_flags {
+        get; protected set; default = Logging.Flag.SQL;
+    }
+
+    /** {@inheritDoc} */
+    public override string logging_domain {
+        get { return LOGGING_DOMAIN; }
+    }
+
+    /** {@inheritDoc} */
+    public Logging.Source? logging_parent { get { return _logging_parent; } }
+    private weak Logging.Source? _logging_parent = null;
+
 
-public abstract class Geary.Db.Context : BaseObject {
     public virtual Database? get_database() {
         return get_connection() != null ? get_connection().database : null;
     }
@@ -29,6 +49,19 @@ public abstract class Geary.Db.Context : BaseObject {
         return null;
     }
 
+    /** {@inheritDoc} */
+    public Logging.State to_logging_state() {
+        Connection? cx = get_connection();
+        return new Logging.State(
+            this, (cx != null) ? cx.to_string() : "[no cx]"
+        );
+    }
+
+    /** Sets the connection's logging parent. */
+    public void set_logging_parent(Logging.Source parent) {
+        this._logging_parent = parent;
+    }
+
     protected inline int throw_on_error(string? method, int result, string? raw = null) throws DatabaseError 
{
         return Db.throw_on_error(this, method, result, raw);
     }
@@ -38,19 +71,14 @@ public abstract class Geary.Db.Context : BaseObject {
         if (!Logging.are_all_flags_set(Logging.Flag.SQL))
             return;
 
-        Connection? cx = get_connection();
         Statement? stmt = get_statement();
-
         if (stmt != null) {
-            Logging.debug(Logging.Flag.SQL, "%s %s\n\t<%s>",
-                (cx != null) ? cx.to_string() : "[no cx]",
-                fmt.vprintf(va_list()),
-                (stmt != null) ? "%.100s".printf(stmt.sql) : "no sql");
+            debug("%s\n\t<%s>",
+                  fmt.vprintf(va_list()),
+                  (stmt != null) ? "%.100s".printf(stmt.sql) : "no sql");
         } else {
-            Logging.debug(Logging.Flag.SQL, "%s %s",
-                (cx != null) ? cx.to_string() : "[no cx]",
-                fmt.vprintf(va_list()));
+            debug(fmt.vprintf(va_list()));
         }
     }
-}
 
+}


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