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




commit 51ad8458a95fdc6923a5196280781d21f78edc56
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                | 13 ++++---------
 src/engine/db/db-database-connection.vala    | 11 +++++++----
 src/engine/db/db-database.vala               | 19 +++++++++++++++----
 src/engine/db/db-result.vala                 |  6 ++++++
 src/engine/db/db-statement.vala              |  9 +++++++--
 src/engine/db/db-transaction-connection.vala |  5 +++--
 6 files changed, 42 insertions(+), 21 deletions(-)
---
diff --git a/src/engine/db/db-context.vala b/src/engine/db/db-context.vala
index 8f3b64d61..9bbb85038 100644
--- a/src/engine/db/db-context.vala
+++ b/src/engine/db/db-context.vala
@@ -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,18 +54,13 @@ 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;
     }
 
+    /** {@inheritDoc} */
+    public abstract Logging.State to_logging_state();
+
     protected inline int throw_on_error(string? method, int result, string? raw = null) throws DatabaseError 
{
         return Db.throw_on_error(this, method, result, raw);
     }
diff --git a/src/engine/db/db-database-connection.vala b/src/engine/db/db-database-connection.vala
index da9f7e7fb..a4114ad96 100644
--- a/src/engine/db/db-database-connection.vala
+++ b/src/engine/db/db-database-connection.vala
@@ -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 override 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..592bd3066 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 {
 
 
     /** 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 override 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-result.vala b/src/engine/db/db-result.vala
index d1eacf70e..1ec3ed551 100644
--- a/src/engine/db/db-result.vala
+++ b/src/engine/db/db-result.vala
@@ -14,6 +14,7 @@ public class Geary.Db.Result : Geary.Db.Context {
     // This results in an automatic first next().
     internal Result(Statement statement, Cancellable? cancellable) throws Error {
         this.statement = statement;
+        set_logging_parent(statement);
 
         statement.was_reset.connect(on_query_finished);
         statement.bindings_cleared.connect(on_query_finished);
@@ -297,6 +298,11 @@ public class Geary.Db.Result : Geary.Db.Context {
         return this;
     }
 
+    /** {@inheritDoc} */
+    public override Logging.State to_logging_state() {
+        return new Logging.State(this, this.finished ? "finished" : "not finished");
+    }
+
     [PrintfFormat]
     private void log_result(string fmt, ...) {
         if (Db.Context.enable_sql_logging) {
diff --git a/src/engine/db/db-statement.vala b/src/engine/db/db-statement.vala
index 38c04d737..0a36dfb1e 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 {
 
     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 override 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..48244dbc7 100644
--- a/src/engine/db/db-transaction-connection.vala
+++ b/src/engine/db/db-transaction-connection.vala
@@ -58,8 +58,9 @@ internal class Geary.Db.TransactionConnection : Context, Connection {
         return this;
     }
 
-    public string to_string() {
-        return this.db_cx.to_string();
+    /** {@inheritDoc} */
+    public override Logging.State to_logging_state() {
+        return new Logging.State(this, "");
     }
 
 }


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