From 3be9164b4ef280f25b996238a135b2f29e95e8fb Mon Sep 17 00:00:00 2001 From: Dan Breslau Date: Tue, 19 Apr 2016 16:16:24 -0400 Subject: [PATCH] Added the MoonshotLogger class, which by default, does nothing. But if the --enable-log4vala argument is passed on the command line to ../configure, this class wraps the Log4Vala logger (which you must already have installed.) --- Makefile.am | 27 +++-- configure.ac | 11 ++ src/moonshot-id.vala | 12 +- src/moonshot-identity-management-view.vala | 19 ++-- src/moonshot-identity-manager-app.vala | 118 +++++++++++++++----- src/moonshot-logger.vala | 172 +++++++++++++++++++++++++++++ src/moonshot-server.vala | 25 +++++ 7 files changed, 331 insertions(+), 53 deletions(-) create mode 100644 src/moonshot-logger.vala diff --git a/Makefile.am b/Makefile.am index 7cd9a6a..f480c26 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,11 +24,21 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/libmoonshot \ -I$(top_builddir)/libmoonshot + +if LOG4VALA +MOONSHOT_LOG_PKG= --pkg log4vala-0.1 -X -llog4vala-0.1 --define=USE_LOG4VALA +MOONSHOT_LOG_LIBS= -llog4vala-0.1 +else +MOONSHOT_LOG_PKG= +MOONSHOT_LOG_LIBS= +endif + + AM_VALAFLAGS = -g \ config.vapi \ - --vapidir=$(top_srcdir)/vapi \ - --pkg gio-2.0 - + --vapidir=$(top_srcdir)/vapi \ + --pkg gio-2.0 \ + $(MOONSHOT_LOG_PKG) libmoonshot_libmoonshot_la_CPPFLAGS = \ $(libmoonshot_CFLAGS) \ @@ -58,17 +68,18 @@ src_moonshot_SOURCES = \ src/moonshot-password-dialog.vala \ src/moonshot-provisioning-common.vala \ src/moonshot-utils.vala \ - src/moonshot-futils.c + src/moonshot-futils.c \ + src/moonshot-logger.vala src_moonshot_webp_SOURCES = \ src/moonshot-webp-parser.vala \ src/moonshot-provisioning-common.vala \ src/moonshot-id.vala -src_moonshot_VALAFLAGS = --pkg gdk-2.0 --pkg gtk+-2.0 --pkg gee-1.0 $(AM_VALAFLAGS) +src_moonshot_VALAFLAGS = --pkg gdk-2.0 --pkg gtk+-2.0 --pkg gee-1.0 $(AM_VALAFLAGS) src_moonshot_CPPFLAGS = $(moonshot_CFLAGS) $(AM_CPPFLAGS) -src_moonshot_LDADD = $(moonshot_LIBS) -src_moonshot_LDFLAGS = -g -O0 +src_moonshot_LDADD = $(moonshot_LIBS) +src_moonshot_LDFLAGS = -g -O0 $(MOONSHOT_LOG_LIBS) src_moonshot_webp_VALAFLAGS = --vapidir=$(top_srcdir)/libmoonshot --pkg gtk+-2.0 --pkg gdk-2.0 --pkg libmoonshot $(AM_VALAFLAGS) src_moonshot_webp_CPPFLAGS = $(moonshot_CFLAGS) $(AM_CPPFLAGS) @@ -168,7 +179,7 @@ $(dbusservice_DATA): $(dbusservice_in_files) Makefile libmoonshot_libmoonshot_la_SOURCES += libmoonshot/libmoonshot-dbus.c -CLEANFILES = $(dbusservice_DATA) +CLEANFILES = $(dbusservice_DATA) src_moonshot_vala.stamp src_moonshot_vala.stamp-t if IPC_DBUS_GLIB AM_VALAFLAGS += \ diff --git a/configure.ac b/configure.ac index c9f63b4..5ff6644 100644 --- a/configure.ac +++ b/configure.ac @@ -94,6 +94,17 @@ case "$host" in ;; esac +#enable the optional use of Log4Vala (Must have the package installed!) +AC_ARG_ENABLE([log4vala], +[ --enable-log4vala Enable use of log4vala for logging], +[case "${enableval}" in + yes) log4vala=true ;; + no) log4vala=false ;; + *) AC_MSG_ERROR([bad value ${enableval} for --enable-log4vala]) ;; +esac],[log4vala=false]) +AM_CONDITIONAL([LOG4VALA], [test x$log4vala = xtrue]) + + AM_CONDITIONAL([OS_LINUX], [test "$linux" = "yes"]) AM_CONDITIONAL([OS_WIN32], [test "$win32" = "yes"]) AM_CONDITIONAL([OS_MACOS], [test "$macos" = "yes"]) diff --git a/src/moonshot-id.vala b/src/moonshot-id.vala index 27d1c8b..e578257 100644 --- a/src/moonshot-id.vala +++ b/src/moonshot-id.vala @@ -71,8 +71,8 @@ public class IdCard : Object public string display_name { get; set; default = ""; } public string username { get; set; default = ""; } - #if GNOME_KEYRING - private unowned string _password; +#if GNOME_KEYRING + private unowned string _password; public string password { get { return (_password!=null) ? _password : ""; @@ -86,11 +86,11 @@ public class IdCard : Object _password = GnomeKeyring.memory_strdup(value); } } - #else - public string password { get; set; default = null; } - #endif +#else + public string password { get; set; default = null; } +#endif - public string issuer { get; set; default = ""; } + public string issuer { get; set; default = ""; } public Rule[] rules {get; set; default = {};} public string[] services { get; set; default = {}; } diff --git a/src/moonshot-identity-management-view.vala b/src/moonshot-identity-management-view.vala index 289c847..8717438 100644 --- a/src/moonshot-identity-management-view.vala +++ b/src/moonshot-identity-management-view.vala @@ -33,6 +33,8 @@ using Gee; using Gtk; public class IdentityManagerView : Window { + static MoonshotLogger logger = get_logger("IdentityManagerView"); + private const int WINDOW_WIDTH = 400; private const int WINDOW_HEIGHT = 500; protected IdentityManagerApp parent_app; @@ -627,11 +629,11 @@ public class IdentityManagerView : Window { { var label = new Label(service); label.set_alignment(0, (float) 0.5); - #if VALA_0_12 - var remove_button = new Button.from_stock(Stock.REMOVE); - #else - var remove_button = new Button.from_stock(STOCK_REMOVE); - #endif +#if VALA_0_12 + var remove_button = new Button.from_stock(Stock.REMOVE); +#else + var remove_button = new Button.from_stock(STOCK_REMOVE); +#endif service_button_map.insert(remove_button, service); @@ -803,6 +805,7 @@ SUCH DAMAGE. catch (Error e) { stderr.printf("%s\n", e.message); + logger.error("create_ui_manager: Caught error: " + e.message); } ui_manager.ensure_update(); } @@ -934,10 +937,10 @@ SUCH DAMAGE. osxApp.set_use_quartz_accelerators(true); osxApp.sync_menu_bar(); osxApp.ready(); - #else +#else var menubar = this.ui_manager.get_widget("/MenuBar"); main_vbox.pack_start(menubar, false, false, 0); - #endif +#endif main_vbox.pack_start(hbox, true, true, 0); add(main_vbox); main_vbox.show_all(); @@ -966,5 +969,3 @@ SUCH DAMAGE. atk_widget.add_relationship(relationship, atk_target_widget); } } - - diff --git a/src/moonshot-identity-manager-app.vala b/src/moonshot-identity-manager-app.vala index 2b66d7f..b1e2400 100644 --- a/src/moonshot-identity-manager-app.vala +++ b/src/moonshot-identity-manager-app.vala @@ -43,7 +43,10 @@ interface IIdentityManager : GLib.Object { } #endif + public class IdentityManagerApp { + public static MoonshotLogger logger = get_logger("IdentityManagerApp"); + public IdentityManagerModel model; public IdCard default_id_card; public bool explicitly_launched; @@ -52,7 +55,7 @@ public class IdentityManagerApp { private bool name_is_owned; private bool show_requested; - #if OS_MACOS +#if OS_MACOS public OSXApplication osxApp; // the signal handler function. @@ -64,13 +67,13 @@ public class IdentityManagerApp { int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name); return true; } - #endif +#endif private const int WINDOW_WIDTH = 400; private const int WINDOW_HEIGHT = 500; - /** If we're successfully registered with DBus, then show the UI. Otherwise, wait until we're registered. */ + /** If we're successfully registered with DBus, then show the UI. Otherwise, wait until we're registered. */ public void show() { if (name_is_owned) { if (view != null) { @@ -81,15 +84,20 @@ public class IdentityManagerApp { show_requested = true; } } + +#if LOG4VALA + // Call this from main() to ensure that the logger is initialized + internal IdentityManagerApp.dummy() {} +#endif public IdentityManagerApp(bool headless, bool use_flat_file_store) { use_flat_file_store |= UserForcesFlatFileStore(); - #if GNOME_KEYRING - bool keyring_available = (!use_flat_file_store) && GnomeKeyring.is_available(); - #else - bool keyring_available = false; - #endif +#if GNOME_KEYRING + bool keyring_available = (!use_flat_file_store) && GnomeKeyring.is_available(); +#else + bool keyring_available = false; +#endif IIdentityCardStore.StoreType store_type; if (headless || use_flat_file_store || !keyring_available) @@ -111,13 +119,13 @@ public class IdentityManagerApp { init_ipc_server(); - #if OS_MACOS +#if OS_MACOS osxApp = OSXApplication.get_instance(); // The 'correct' way of connecting won't work in Mac OS with Vala 0.12; e.g. // osxApp.ns_application_open_file.connect(install_from_file); // so we have to use this old way Signal.connect(osxApp, "NSApplicationOpenFile", (GLib.Callback)(on_osx_open_files), this); - #endif +#endif } public bool add_identity(IdCard id, bool force_flat_file_store) { @@ -127,6 +135,7 @@ public class IdentityManagerApp { } public void select_identity(IdentityRequest request) { + logger.trace("select_identity"); IdCard identity = null; @@ -234,6 +243,7 @@ public class IdentityManagerApp { } if (identity == null) { if (request.candidates.length() != 1) { + logger.trace("select_identity: Have %u candidates; user must make selection.".printf(request.candidates.length())); confirm = true; } else { identity = request.candidates.nth_data(0); @@ -253,6 +263,7 @@ public class IdentityManagerApp { GLib.Idle.add( () => { if (view != null) { + logger.trace("select_identity (Idle handler): calling check_add_password"); identity = view.check_add_password(identity, request, model); } request.return_identity(identity); @@ -266,13 +277,12 @@ public class IdentityManagerApp { return; } - private bool match_service_pattern(string service, string pattern) - { - var pspec = new PatternSpec(pattern); - return pspec.match_string(service); - } + private bool match_service_pattern(string service, string pattern) { + var pspec = new PatternSpec(pattern); + return pspec.match_string(service); + } - #if IPC_MSRPC +#if IPC_MSRPC private void init_ipc_server() { // Errors will currently be sent via g_log - ie. to an // obtrusive message box, on Windows @@ -280,7 +290,7 @@ public class IdentityManagerApp { this.ipc_server = MoonshotServer.get_instance(); MoonshotServer.start(this); } - #elif IPC_DBUS_GLIB +#elif IPC_DBUS_GLIB private void init_ipc_server() { try { var conn = DBus.Bus.get(DBus.BusType.SESSION); @@ -293,8 +303,10 @@ public class IdentityManagerApp { if (reply == DBus.RequestNameReply.PRIMARY_OWNER) { this.ipc_server = new MoonshotServer(this); + logger.trace("init_ipc_server(IPC_DBUS_GLIB) : Constructed new MoonshotServer"); conn.register_object("/org/janet/moonshot", ipc_server); } else { + logger.trace("init_ipc_server: reply != PRIMARY_OWNER"); bool shown = false; GLib.Error e; DBus.Object manager_proxy = conn.get_object("org.janet.Moonshot", @@ -313,17 +325,50 @@ public class IdentityManagerApp { } catch (DBus.Error e) { - stderr.printf("%s\n", e.message); + logger.trace("bus_acquired_cb"); + try { + conn.register_object ("/org/janet/moonshot", ipc_server); + } + catch (Error e) + { + stderr.printf ("%s\n", e.message); + logger.error("bus_acquired_cb: Caught error: " + e.message); + } } } - #else +#else private void bus_acquired_cb(DBusConnection conn) { + logger.trace("bus_acquired_cb"); try { conn.register_object("/org/janet/moonshot", ipc_server); } catch (Error e) { - stderr.printf("%s\n", e.message); + this.ipc_server = new MoonshotServer (this); + logger.trace("init_ipc_server: Constructed new MoonshotServer"); + GLib.Bus.own_name (GLib.BusType.SESSION, + "org.janet.Moonshot", + GLib.BusNameOwnerFlags.NONE, + bus_acquired_cb, + (conn, name) => {logger.trace("init_ipc_server: name_acquired_closure");}, + (conn, name) => { + logger.trace("init_ipc_server: name_lost_closure"); + bool shown=false; + try { + IIdentityManager manager = Bus.get_proxy_sync (BusType.SESSION, name, "/org/janet/moonshot"); + shown = manager.show_ui(); + } catch (IOError e) { + logger.error("init_ipc_server.name_lost_closure: Caught error: "); + } + if (!shown) { + GLib.error ("Couldn't own name %s on dbus or show previously launched identity manager.", name); + logger.error("init_ipc_server.name_lost_closure: Couldn't own name %s on dbus or show previously launched identity manager".printf(name)); + } else { + logger.trace("init_ipc_server.name_lost_closure: Showed previously launched identity manager."); + stdout.printf("Showed previously launched identity manager.\n"); + GLib.Process.exit(0); + } + }); } } @@ -337,6 +382,7 @@ public class IdentityManagerApp { // Name acquired callback: (conn, name) => { + logger.trace(@"init_ipc_server: name_acquired_closure; show_requested=$show_requested"); name_is_owned = true; @@ -350,6 +396,7 @@ public class IdentityManagerApp { // Name lost callback: (conn, name) => { + logger.trace("init_ipc_server: name_lost_closure"); // This callback usually means that another moonshot is already running. // But it *might* mean that we lost the name for some other reason @@ -364,16 +411,19 @@ public class IdentityManagerApp { shown = manager.show_ui(); } } catch (IOError e) { + logger.error("init_ipc_server.name_lost_closure: Caught IOError: " + e.message); } if (!shown) { + logger.error("init_ipc_server.name_lost_closure: Couldn't own name %s on dbus or show previously launched identity manager".printf(name)); GLib.error("Couldn't own name %s on dbus or show previously launched identity manager.", name); } else { + logger.trace("init_ipc_server.name_lost_closure: Showed previously launched identity manager."); stdout.printf("Showed previously launched identity manager.\n"); GLib.Process.exit(0); } }); } - #endif +#endif } static bool explicitly_launched = true; @@ -388,11 +438,16 @@ const GLib.OptionEntry[] options = { public static int main(string[] args) { - #if IPC_MSRPC - bool headless = false; - #else - bool headless = (GLib.Environment.get_variable("DISPLAY") == null); - #endif + +#if LOG4VALA + new IdentityManagerApp.dummy(); +#endif + +#if IPC_MSRPC + bool headless = false; +#else + bool headless = GLib.Environment.get_variable("DISPLAY") == null; +#endif if (headless) { try { @@ -420,29 +475,32 @@ public static int main(string[] args) { gtk_available = true; } - #if OS_WIN32 +#if OS_WIN32 // Force specific theme settings on Windows without requiring a gtkrc file Gtk.Settings settings = Gtk.Settings.get_default(); settings.set_string_property("gtk-theme-name", "ms-windows", "moonshot"); settings.set_long_property("gtk-menu-images", 0, "moonshot"); - #endif +#endif Intl.bindtextdomain(Config.GETTEXT_PACKAGE, Config.LOCALEDIR); Intl.bind_textdomain_codeset(Config.GETTEXT_PACKAGE, "UTF-8"); Intl.textdomain(Config.GETTEXT_PACKAGE); + var app = new IdentityManagerApp(headless, use_flat_file_store); + IdentityManagerApp.logger.trace("Hi again from main()!"); app.explicitly_launched = explicitly_launched; + IdentityManagerApp.logger.trace(@"main: explicitly_launched=$explicitly_launched"); if (app.explicitly_launched) { app.show(); } if (headless) { - #if !IPC_MSRPC +#if !IPC_MSRPC MainLoop loop = new MainLoop(); loop.run(); - #endif +#endif } else { Gtk.main(); diff --git a/src/moonshot-logger.vala b/src/moonshot-logger.vala new file mode 100644 index 0000000..d815210 --- /dev/null +++ b/src/moonshot-logger.vala @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2011-2014, JANET(UK) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of JANET(UK) nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + +public MoonshotLogger get_logger(string name) { + return new MoonshotLogger(name); +} + + +#if USE_LOG4VALA + +/** Logger class that wraps the Log4Vala logger */ +public class MoonshotLogger : Object { + static bool logger_is_initialized = false; + + private Log4Vala.Logger logger; + + public MoonshotLogger(string name) { + if (!logger_is_initialized) { + //!! TODO: Don't hard-code the pathname. + Log4Vala.init("/home/dbreslau/log4vala.conf"); + logger_is_initialized = true; + } + + logger = Log4Vala.Logger.get_logger(name); + } + + /** + * Log a trace message. + * @param message log message + * @param e optional Error to be logged + */ + public void trace(string message, Error? e = null) { + logger.trace(message, e); + } + + + /** + * Log a debug message. + * @param message log message + * @param e optional Error to be logged + */ + public void debug(string message, Error? e = null) { + logger.debug(message, e); + } + + + /** + * Log an info message. + * @param e optional Error to be logged + */ + public void info(string message, Error? e = null) { + logger.info(message, e); + } + + /** + * Log a warning message. + * @param message log message + * @param e optional Error to be logged + */ + public void warn(string message, Error? e = null) { + logger.warn(message, e); + } + + /** + * Log an error message. + * @param message log message + * @param e optional Error to be logged + */ + public void error(string message, Error? e = null) { + logger.error(message, e); + } + + /** + * Log a fatal message. + * @param message log message + * @param e optional Error to be logged + */ + public void fatal(string message, Error? e = null) { + logger.fatal(message, e); + } +} + + +#else + +/** Logger that currently does nothing, but may eventually write to stdout or a file if enabled */ +public class MoonshotLogger : Object { + + internal MoonshotLogger(string name) { + } + + /** + * Log a trace message. + * @param message log message + * @param e optional Error to be logged + */ + public void trace(string message, Error? e = null) { + } + + + /** + * Log a debug message. + * @param message log message + * @param e optional Error to be logged + */ + public void debug(string message, Error? e = null) { + } + + + /** + * Log an info message. + * @param e optional Error to be logged + */ + public void info(string message, Error? e = null) { + } + + /** + * Log a warning message. + * @param message log message + * @param e optional Error to be logged + */ + public void warn(string message, Error? e = null) { + } + + /** + * Log an error message. + * @param message log message + * @param e optional Error to be logged + */ + public void error(string message, Error? e = null) { + } + + /** + * Log a fatal message. + * @param message log message + * @param e optional Error to be logged + */ + public void fatal(string message, Error? e = null) { + } +} + +#endif diff --git a/src/moonshot-server.vala b/src/moonshot-server.vala index b78e8e0..bb26dc0 100644 --- a/src/moonshot-server.vala +++ b/src/moonshot-server.vala @@ -34,20 +34,30 @@ [DBus (name = "org.janet.Moonshot")] public class MoonshotServer : Object { + static MoonshotLogger logger = get_logger("MoonshotServer"); + + private string app_name = "Moonshot"; + private IdentityManagerApp parent_app; public MoonshotServer(IdentityManagerApp app) { + logger.trace("MoonshotServer.; app=" + (app == null ? "null" : "non-null")); this.parent_app = app; } public bool show_ui() { + logger.trace("MoonshotServer.show_ui"); + if (parent_app.view == null) { + stderr.printf(app_name, "show_ui: parent_app.view is null!\n"); + logger.warn("show_ui: parent_app.view is null!"); return false; } parent_app.show(); parent_app.explicitly_launched = true; + logger.trace("MoonshotServer.show_ui: returning true"); return true; } @@ -61,13 +71,17 @@ public class MoonshotServer : Object { out string subject_name_constraint, out string subject_alt_name_constraint) { + logger.trace(@"MoonshotServer.get_identity: nai='$nai'; service='$service'"); var request = new IdentityRequest(parent_app, nai, password, service); + logger.trace(@"MoonshotServer.get_identity: Calling request.execute()"); request.set_callback((IdentityRequest) => get_identity.callback()); request.execute(); + logger.trace(@"MoonshotServer.get_identity: Back from request.execute()"); yield; + logger.trace(@"MoonshotServer.get_identity: back from yield"); nai_out = ""; password_out = ""; @@ -103,9 +117,12 @@ public class MoonshotServer : Object { if (subject_alt_name_constraint == null) subject_alt_name_constraint = ""; + logger.trace("MoonshotServer.get_identity: returning true"); + return true; } + logger.trace("MoonshotServer.get_identity: returning false"); return false; } @@ -116,6 +133,7 @@ public class MoonshotServer : Object { out string subject_name_constraint, out string subject_alt_name_constraint) { + logger.trace("MoonshotServer.get_default_identity"); var request = new IdentityRequest.default(parent_app); request.set_callback((IdentityRequest) => get_default_identity.callback()); request.execute(); @@ -151,6 +169,7 @@ public class MoonshotServer : Object { if (subject_alt_name_constraint == null) subject_alt_name_constraint = ""; + logger.trace("MoonshotServer.get_default_identity: returning true"); return true; } @@ -290,6 +309,8 @@ public class MoonshotServer : Object { ref string subject_name_constraint, ref string subject_alt_name_constraint) { + logger.trace("(static) get_identity"); + bool result = false; var request = new IdentityRequest(parent_app, @@ -355,6 +376,8 @@ public class MoonshotServer : Object { ref string subject_name_constraint, ref string subject_alt_name_constraint) { + logger.trace("(static) get_default_identity"); + bool result; var request = new IdentityRequest.default(parent_app); @@ -428,7 +451,9 @@ public class MoonshotServer : Object { string server_cert, bool force_flat_file_store) { + logger.trace("(static) install_id_card"); IdCard idcard = new IdCard(); + bool success = false; Mutex mutex = new Mutex(); Cond cond = new Cond(); -- 2.1.4