[geary/mjog/mutiple-main-windows: 9/14] Improve logging initialisation and startup



commit f4f9ff45895b5c58767b59e7a8d18f560d47cfe8
Author: Michael Gratton <mike vee net>
Date:   Mon Nov 18 11:53:02 2019 +1100

    Improve logging initialisation and startup
    
    Initialise logging as early as possible to capture as many logging
    messages as possible. Don't log to stderr by default on startup to
    avoid printing unwanted messages, rather output any captured but not
    printed messages when enabling debug output for the first time.

 src/client/application/application-client.vala | 25 +++++++--------------
 src/client/application/main.vala               |  6 +++++
 src/engine/api/geary-logging.vala              | 31 ++++++++++++++++++++------
 3 files changed, 38 insertions(+), 24 deletions(-)
---
diff --git a/src/client/application/application-client.vala b/src/client/application/application-client.vala
index 822799bd..ac386e05 100644
--- a/src/client/application/application-client.vala
+++ b/src/client/application/application-client.vala
@@ -366,25 +366,25 @@ public class Application.Client : Gtk.Application {
     }
 
     public override int handle_local_options(GLib.VariantDict options) {
+        int ret = -1;
+        if (options.contains(OPTION_DEBUG)) {
+            Geary.Logging.log_to(GLib.stdout);
+        }
         if (options.contains(OPTION_VERSION)) {
             GLib.stdout.printf(
                 "%s: %s\n", this.binary, Client.VERSION
             );
-            return 0;
+            ret = 0;
         }
-        return -1;
+        return ret;
     }
 
     public override void startup() {
         Environment.set_application_name(NAME);
         Util.International.init(GETTEXT_PACKAGE, this.binary);
+        Util.Date.init();
 
         Configuration.init(this.is_installed, GSETTINGS_DIR);
-        Geary.Logging.init();
-        Geary.Logging.log_to(stderr);
-        GLib.Log.set_writer_func(Geary.Logging.default_log_writer);
-
-        Util.Date.init();
 
         // Add application's actions before chaining up so they are
         // present when the application is first registered on the
@@ -428,7 +428,6 @@ public class Application.Client : Gtk.Application {
             // Since command_line won't be called below if running as
             // a DBus service, disable logging spew and start the
             // controller running.
-            Geary.Logging.log_to(null);
             this.create_controller.begin();
         }
     }
@@ -817,14 +816,6 @@ public class Application.Client : Gtk.Application {
             return 0;
         }
 
-        bool enable_debug = options.contains(OPTION_DEBUG);
-        // Will be logging to stderr until this point
-        if (enable_debug) {
-            Geary.Logging.log_to(GLib.stdout);
-        } else {
-            Geary.Logging.log_to(null);
-        }
-
         bool activated = false;
 
         // Logging flags
@@ -885,7 +876,7 @@ public class Application.Client : Gtk.Application {
             }
         }
 
-        this.config.enable_debug = enable_debug;
+        this.config.enable_debug = options.contains(OPTION_DEBUG);
         this.config.enable_inspector = options.contains(OPTION_INSPECTOR);
         this.config.revoke_certs = options.contains(OPTION_REVOKE_CERTS);
 
diff --git a/src/client/application/main.vala b/src/client/application/main.vala
index c95a3a4c..0df87838 100644
--- a/src/client/application/main.vala
+++ b/src/client/application/main.vala
@@ -25,6 +25,12 @@ int main(string[] args) {
     // proper fix lands. See GNOME/geary#558.
     Environment.set_variable("WEBKIT_USE_SINGLE_WEB_PROCESS", "1", true);
 
+
+    // Init logging right up front so as to capture as many log
+    // messages as possible
+    Geary.Logging.init();
+    GLib.Log.set_writer_func(Geary.Logging.default_log_writer);
+
     Application.Client app = new Application.Client();
 
     int ec = app.run(args);
diff --git a/src/engine/api/geary-logging.vala b/src/engine/api/geary-logging.vala
index 560daf00..bdbe8744 100644
--- a/src/engine/api/geary-logging.vala
+++ b/src/engine/api/geary-logging.vala
@@ -497,15 +497,27 @@ public Record? get_latest_record() {
 }
 
 /**
- * Registers a FileStream to receive all log output from the Engine, be it via the specialized
- * Logging calls (which use the topic-based {@link Flag} or GLib's standard issue
- * debug/message/error/etc. calls ... thus, calling this will also affect the Engine user's calls
- * to those functions.
+ * Registers a FileStream to receive all log output from the engine.
  *
- * If stream is null, no logging occurs.  This is default.
+ * This may be via the specialized Logging calls (which use the
+ * topic-based {@link Flag} or GLib's standard issue
+ * debug/message/error/etc. calls ... thus, calling this will also
+ * affect the Engine user's calls to those functions.
+ *
+ * If stream is null, no logging occurs (the default). If non-null and
+ * the stream was previously null, all pending log records will be
+ * output before proceeding.
  */
 public void log_to(FileStream? stream) {
+    bool catch_up = (stream != null && Logging.stream == null);
     Logging.stream = stream;
+    if (catch_up) {
+        Record? record = Logging.first_record;
+        while (record != null) {
+            write_record(record, record.levels);
+            record = record.next;
+        }
+    }
 }
 
 
@@ -550,6 +562,13 @@ public GLib.LogWriterOutput default_log_writer(GLib.LogLevelFlags levels,
             });
     }
 
+    write_record(record, levels);
+
+    return GLib.LogWriterOutput.HANDLED;
+}
+
+private inline void write_record(Record record,
+                                 GLib.LogLevelFlags levels) {
     // Print a log message to the stream if configured, or if the
     // priority is high enough.
     unowned FileStream? out = stream;
@@ -569,8 +588,6 @@ public GLib.LogWriterOutput default_log_writer(GLib.LogLevelFlags levels,
         out.putc('\n');
         writer_lock.unlock();
     }
-
-    return GLib.LogWriterOutput.HANDLED;
 }
 
 


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