[geary/mjog/921-db-locked: 2/3] Geary.Db: Update logging implementation




commit 470e069b8199f8d25d140460043f4121ce867af2
Author: Michael Gratton <mike vee net>
Date:   Fri Sep 4 00:33:36 2020 +1000

    Geary.Db: Update logging implementation
    
    Fix infinite recursion logging from DatabaseConnection caused by
    `to_string` and Context implementing `to_logging_state` that calls that.
    Make individual objects implement `Logging.Source` instead and have
    them return appropriate, custom logging state individually.

 src/engine/db/db-context.vala                | 12 ++----------
 src/engine/db/db-database-connection.vala    | 13 ++++++++-----
 src/engine/db/db-database.vala               | 19 +++++++++++++++----
 src/engine/db/db-statement.vala              |  9 +++++++--
 src/engine/db/db-transaction-connection.vala |  4 ----
 5 files changed, 32 insertions(+), 25 deletions(-)
---
diff --git a/src/engine/db/db-context.vala b/src/engine/db/db-context.vala
index 8f3b64d61..8125c6108 100644
--- a/src/engine/db/db-context.vala
+++ b/src/engine/db/db-context.vala
@@ -13,7 +13,7 @@
  *
  * Geary.Db's major classes (Database, Connection, Statement, and Result) inherit from Context.
  */
-public abstract class Geary.Db.Context : BaseObject, Logging.Source {
+public abstract class Geary.Db.Context : BaseObject {
 
 
     /**
@@ -28,7 +28,7 @@ public abstract class Geary.Db.Context : BaseObject, Logging.Source {
     public const string LOGGING_DOMAIN = Logging.DOMAIN + ".Db";
 
     /** {@inheritDoc} */
-    public override string logging_domain {
+    public string logging_domain {
         get { return LOGGING_DOMAIN; }
     }
 
@@ -54,14 +54,6 @@ public abstract class Geary.Db.Context : BaseObject, Logging.Source {
     }
 
     /** {@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;
     }
diff --git a/src/engine/db/db-database-connection.vala b/src/engine/db/db-database-connection.vala
index da9f7e7fb..1e5dc41a8 100644
--- a/src/engine/db/db-database-connection.vala
+++ b/src/engine/db/db-database-connection.vala
@@ -9,7 +9,7 @@
 /**
  * A primary connection to the database.
  */
-public class Geary.Db.DatabaseConnection : Context, Connection {
+public class Geary.Db.DatabaseConnection : Context, Connection, Logging.Source {
 
 
     /**
@@ -118,13 +118,15 @@ public class Geary.Db.DatabaseConnection : Context, Connection {
 
     /** {@inheritDoc} */
     public Statement prepare(string sql) throws DatabaseError {
-        return new Statement(this, sql);
+        var prepared = new Statement(this, sql);
+        prepared.set_logging_parent(this);
+        return prepared;
     }
 
     /** {@inheritDoc} */
     public Result query(string sql, GLib.Cancellable? cancellable = null)
         throws GLib.Error {
-        return (new Statement(this, sql)).exec(cancellable);
+        return prepare(sql).exec(cancellable);
     }
 
     /** {@inheritDoc} */
@@ -240,8 +242,9 @@ public class Geary.Db.DatabaseConnection : Context, Connection {
         return this;
     }
 
-    public string to_string() {
-        return "[%u] %s".printf(this.cx_number, this._database.path);
+    /** {@inheritDoc} */
+    public Logging.State to_logging_state() {
+        return new Logging.State(this, "[%u]", this.cx_number);
     }
 
 }
diff --git a/src/engine/db/db-database.vala b/src/engine/db/db-database.vala
index ac201ef03..3da010e44 100644
--- a/src/engine/db/db-database.vala
+++ b/src/engine/db/db-database.vala
@@ -15,7 +15,7 @@
  * thread pool, as well as allowing multiple connections to be opened
  * for fully concurrent access.
  */
-public class Geary.Db.Database : Geary.Db.Context {
+public class Geary.Db.Database : Context, Logging.Source {
 
 
     /** The path passed to SQLite to open a transient database. */
@@ -143,6 +143,7 @@ public class Geary.Db.Database : Geary.Db.Context {
             var cx = new DatabaseConnection(
                 this, Sqlite.OPEN_READWRITE, cancellable
             );
+            cx.set_logging_parent(this);
 
             try {
                 // drop existing test table (in case created in prior failed open)
@@ -232,6 +233,7 @@ public class Geary.Db.Database : Geary.Db.Context {
         DatabaseConnection cx = new DatabaseConnection(
             this, sqlite_flags, cancellable
         );
+        cx.set_logging_parent(this);
         prepare_connection(cx);
         return cx;
     }
@@ -355,6 +357,18 @@ public class Geary.Db.Database : Geary.Db.Context {
         return yield job.wait_for_completion_async();
     }
 
+
+    public override Database? get_database() {
+        return this;
+    }
+
+    /** {@inheritDoc} */
+    public Logging.State to_logging_state() {
+        return new Logging.State(
+            this, "%s, is_open: %s", this.path, this.is_open.to_string()
+        );
+    }
+
     /** Adds the given job to the thread pool. */
     internal void add_async_job(TransactionAsyncJob new_job) throws Error {
         check_open();
@@ -411,7 +425,4 @@ public class Geary.Db.Database : Geary.Db.Context {
         }
     }
 
-    public override Database? get_database() {
-        return this;
-    }
 }
diff --git a/src/engine/db/db-statement.vala b/src/engine/db/db-statement.vala
index 38c04d737..f2715c9bf 100644
--- a/src/engine/db/db-statement.vala
+++ b/src/engine/db/db-statement.vala
@@ -7,7 +7,7 @@
 private extern string? sqlite3_expanded_sql(Sqlite.Statement stmt);
 
 
-public class Geary.Db.Statement : Geary.Db.Context {
+public class Geary.Db.Statement : Context, Logging.Source {
 
     public string sql {
         get { return this.stmt.sql(); }
@@ -274,5 +274,10 @@ public class Geary.Db.Statement : Geary.Db.Context {
     public override Statement? get_statement() {
         return this;
     }
-}
 
+    /** {@inheritDoc} */
+    public Logging.State to_logging_state() {
+        return new Logging.State(this, this.sql);
+    }
+
+}
diff --git a/src/engine/db/db-transaction-connection.vala b/src/engine/db/db-transaction-connection.vala
index 737828de3..e3f9a0cb4 100644
--- a/src/engine/db/db-transaction-connection.vala
+++ b/src/engine/db/db-transaction-connection.vala
@@ -58,8 +58,4 @@ internal class Geary.Db.TransactionConnection : Context, Connection {
         return this;
     }
 
-    public string to_string() {
-        return this.db_cx.to_string();
-    }
-
 }


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