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.
This commit is contained in:
Michael Gratton 2019-11-18 11:53:02 +11:00
parent 3f6fe1f3f2
commit f4f9ff4589
3 changed files with 38 additions and 24 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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;
}