Logging updates
authorDan Breslau <dbreslau@painless-security.com>
Tue, 5 Apr 2016 14:24:16 +0000 (10:24 -0400)
committerDan Breslau <dbreslau@painless-security.com>
Tue, 5 Apr 2016 14:24:16 +0000 (10:24 -0400)
Makefile.am
src/moonshot-id.vala
src/moonshot-identities-manager.vala
src/moonshot-identity-management-view.vala
src/moonshot-identity-manager-app.vala
src/moonshot-provisioning-common.vala
src/moonshot-server.vala
src/moonshot-utils.vala

index 7cd9a6a..f5ecce4 100644 (file)
@@ -65,10 +65,10 @@ src_moonshot_webp_SOURCES = \
         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  --pkg log4vala-0.1 -X -llog4vala-0.1 $(AM_VALAFLAGS)
 src_moonshot_CPPFLAGS = $(moonshot_CFLAGS) $(AM_CPPFLAGS)  
 src_moonshot_LDADD = $(moonshot_LIBS) 
-src_moonshot_LDFLAGS = -g -O0
+src_moonshot_LDFLAGS = -g -O0  -llog4vala-0.1
 
 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)
index 2e0af57..3172b16 100644 (file)
@@ -71,6 +71,11 @@ public class IdCard : Object
   public string display_name { get; set; default = ""; }
   
   public string username { get; set; default = ""; }
+
+  public IdCard() {
+         stdout.printf("new IdCard!\n");
+  }
+
 #if GNOME_KEYRING
   private unowned string _password;
   public string password {
index 292fb0e..55ab0bb 100644 (file)
@@ -117,25 +117,26 @@ public class IdentityManagerModel : Object {
                                        out string? candidate)
     {
         if (&candidate != null)
-          candidate = null;
+            candidate = null;
         foreach (IdCard id_card in this.store.get_card_list())
         {
-          if (id_card.display_name == name)
-          {
-            if (&candidate != null)
+            if (id_card.display_name == name)
             {
-              for (int i=0; i<1000; i++)
-              {
-                string tmp = "%s %d".printf (name, i);
-                if (display_name_is_valid (tmp, null))
+                if (&candidate != null)
                 {
-                  candidate = tmp;
-                  break;
+                    for (int i=0; i<1000; i++)
+                    {
+                        string tmp = "%s %d".printf (name, i);
+                        if (display_name_is_valid (tmp, null))
+                        {
+                    
+                            candidate = tmp;
+                            break;
+                        }
+                    }
                 }
-              }
+                return false;
             }
-            return false;
-          }
         }
         return true;
     }
index d9d7c5a..a7e2247 100644 (file)
@@ -33,6 +33,8 @@ using Gee;
 using Gtk;
 
 public class IdentityManagerView : Window {
+       static Log4Vala.Logger logger = get_logger("IdentityManagerView");
+
     private const int WINDOW_WIDTH = 400;
     private const int WINDOW_HEIGHT = 500;
     protected IdentityManagerApp parent_app;
@@ -786,6 +788,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 ();
     }
index a013cfb..3050099 100644 (file)
@@ -35,49 +35,54 @@ using Gtk;
 #if IPC_DBUS
 [DBus (name = "org.janet.Moonshot")]
 interface IIdentityManager : GLib.Object {
-#if IPC_DBUS_GLIB
-    public abstract bool show_ui() throws DBus.Error;
-#else
-    public abstract bool show_ui() throws IOError;
-#endif
-}
+    #if IPC_DBUS_GLIB
+        public abstract bool show_ui() throws DBus.Error;
+    #else
+        public abstract bool show_ui() throws IOError;
+    #endif
+        }
 #endif
 
+
 public class IdentityManagerApp {
+       public static Log4Vala.Logger logger = get_logger("IdentityManagerApp");
+
     public IdentityManagerModel model;
     public IdCard default_id_card;
     public bool explicitly_launched;
     public IdentityManagerView view;
     private MoonshotServer ipc_server;
 
-#if OS_MACOS
-       public OSXApplication osxApp;
+    #if OS_MACOS
+    public OSXApplication osxApp;
   
     // the signal handler function.
     // the current instance of our app class is passed in the 
     // id_manager_app_instanceparameter 
-       public static bool on_osx_open_files (OSXApplication osx_app_instance, 
-                                        string file_name, 
-                                        IdentityManagerApp id_manager_app_instance ) {
-    int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
-    return true;
-       }
-#endif
+    public static bool on_osx_open_files (OSXApplication osx_app_instance, 
+                                          string file_name, 
+                                          IdentityManagerApp id_manager_app_instance ) {
+        int added_cards = id_manager_app_instance.ipc_server.install_from_file(file_name);
+        return true;
+    }
+    #endif
 
     private const int WINDOW_WIDTH = 400;
     private const int WINDOW_HEIGHT = 500;
     public void show() {
         if (view != null) view.show();    
     }
-       
+    
+       internal IdentityManagerApp.dummy() {}
+
     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
-        IIdentityCardStore.StoreType store_type;
+        #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)
             store_type = IIdentityCardStore.StoreType.FLAT_FILE;
         else
@@ -97,16 +102,16 @@ public class IdentityManagerApp {
 
         init_ipc_server ();
 
-#if OS_MACOS
+        #if OS_MACOS
 
-        osxApp = OSXApplication.get_instance();
-        // The 'correct' way of connrcting wont work in Mac OS with Vala 0.12  e.g.    
-        //             osxApp.ns_application_open_file.connect(install_from_file);
+            osxApp = OSXApplication.get_instance();
+        // The 'correct' way of connrcting wont 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) {
         if (view != null) return view.add_identity(id, force_flat_file_store);
@@ -231,7 +236,7 @@ public class IdentityManagerApp {
             {
                 if (!explicitly_launched)
                     show();
-               view.queue_identity_request(request);
+                view.queue_identity_request(request);
                 return;
             }
         }
@@ -249,98 +254,108 @@ public class IdentityManagerApp {
 //                    Idle.add( () => { Gtk.main_quit(); return false; } );
                 return false;
             }
-        );
+            );
         return;
     }
 
     private bool match_service_pattern (string service, string pattern)
-    {
-        var pspec = new PatternSpec (pattern);
-        return pspec.match_string (service);
-    }   
+        {
+            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
-        //
-        this.ipc_server = MoonshotServer.get_instance ();
-        MoonshotServer.start (this);
-    }
-#elif IPC_DBUS_GLIB
+        {
+            // Errors will currently be sent via g_log - ie. to an
+            // obtrusive message box, on Windows
+            //
+            this.ipc_server = MoonshotServer.get_instance ();
+            MoonshotServer.start (this);
+        }
+    #elif IPC_DBUS_GLIB
     private void init_ipc_server ()
-    {
-        try {
-            var conn = DBus.Bus.get (DBus.BusType.SESSION);
-            dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
-                                                       "/org/freedesktop/DBus",
-                                                       "org.freedesktop.DBus");
-
-            // try to register service in session bus
-            uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
-            if (reply == DBus.RequestNameReply.PRIMARY_OWNER)
-            {
-                this.ipc_server = new MoonshotServer (this);
-                conn.register_object ("/org/janet/moonshot", ipc_server);
-            } else {
-                bool shown=false;
-                GLib.Error e;
-                DBus.Object manager_proxy = conn.get_object ("org.janet.Moonshot",
-                                                             "/org/janet/moonshot",
-                                                             "org.janet.Moonshot");
-                if (manager_proxy != null)
-                    manager_proxy.call("ShowUi", out e, GLib.Type.INVALID, typeof(bool), out shown, GLib.Type.INVALID);
-
-                if (!shown) {
-                    GLib.error ("Couldn't own name org.janet.Moonshot on dbus or show previously launched identity manager.");
+        {
+            try {
+                var conn = DBus.Bus.get (DBus.BusType.SESSION);
+                dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
+                                                           "/org/freedesktop/DBus",
+                                                           "org.freedesktop.DBus");
+
+                // try to register service in session bus
+                uint reply = bus.request_name ("org.janet.Moonshot", (uint) 0);
+                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 {
-                    stdout.printf("Showed previously launched identity manager.\n");
-                    GLib.Process.exit(0);
+                                       logger.trace("init_ipc_server: reply != PRIMARY_OWNER");
+                    bool shown=false;
+                    GLib.Error e;
+                    DBus.Object manager_proxy = conn.get_object ("org.janet.Moonshot",
+                                                                 "/org/janet/moonshot",
+                                                                 "org.janet.Moonshot");
+                    if (manager_proxy != null)
+                        manager_proxy.call("ShowUi", out e, GLib.Type.INVALID, typeof(bool), out shown, GLib.Type.INVALID);
+
+                    if (!shown) {
+                        GLib.error ("Couldn't own name org.janet.Moonshot on dbus or show previously launched identity manager.");
+                    } else {
+                        stdout.printf("Showed previously launched identity manager.\n");
+                        GLib.Process.exit(0);
+                    }
                 }
             }
+            catch (DBus.Error e)
+            {
+                stderr.printf ("%s\n", e.message);
+                               logger.error("init_ipc_server: Caught DBus.Error: " + e.message);
+            }
         }
-        catch (DBus.Error e)
-        {
-            stderr.printf ("%s\n", e.message);
-        }
-    }
-#else
+    #else
     private void bus_acquired_cb (DBusConnection conn)
-    {
-        try {
-            conn.register_object ("/org/janet/moonshot", ipc_server);
-        }
-        catch (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);
+            }
         }
-    }
 
     private void init_ipc_server ()
-    {
-        this.ipc_server = new MoonshotServer (this);
-        GLib.Bus.own_name (GLib.BusType.SESSION,
-                           "org.janet.Moonshot",
-                           GLib.BusNameOwnerFlags.NONE,
-                           bus_acquired_cb,
-                           (conn, name) => {},
-                           (conn, name) => {
-                               bool shown=false;
-                               try {
-                                   IIdentityManager manager = Bus.get_proxy_sync (BusType.SESSION, name, "/org/janet/moonshot");
-                                   shown = manager.show_ui();
-                               } catch (IOError e) {
-                               }
-                               if (!shown) {
-                                   GLib.error ("Couldn't own name %s on dbus or show previously launched identity manager.", name);
-                               } else {
-                                   stdout.printf("Showed previously launched identity manager.\n");
-                                   GLib.Process.exit(0);
-                               }
-                           });
-    }
-#endif
+        {
+            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);
+                                   }
+                               });
+        }
+    #endif
 }
 
 static bool explicitly_launched = true;
@@ -355,11 +370,16 @@ const GLib.OptionEntry[] options = {
 
 
 public static int main(string[] args){
-#if IPC_MSRPC
-       bool headless = false;
-#else
+       new IdentityManagerApp.dummy().show();
+
+    #if IPC_MSRPC
+        bool headless = false;
+    #else
         bool headless = GLib.Environment.get_variable("DISPLAY") == null;
-#endif
+    #endif
+
+               stdout.printf("Hi from main()!\n");
+               IdentityManagerApp.logger.trace("Hi from main()!");
 
         if (headless) {
             try {
@@ -387,34 +407,35 @@ public static int main(string[] args){
             gtk_available = true;
         }
 
-#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
+    #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
 
-        Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
-        Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
-        Intl.textdomain (Config.GETTEXT_PACKAGE);
+    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);
-        app.explicitly_launched = explicitly_launched;
+    var app = new IdentityManagerApp(headless, use_flat_file_store);
+       IdentityManagerApp.logger.trace("Hi again from main()!");
+    app.explicitly_launched = explicitly_launched;
         
-       if (app.explicitly_launched) {
-            app.show();
-        }
+    if (app.explicitly_launched) {
+        app.show();
+    }
 
-        if (headless) {
-#if !IPC_MSRPC
+    if (headless) {
+        #if !IPC_MSRPC
             MainLoop loop = new MainLoop();
-            loop.run();
-#endif
-        } else {
-            Gtk.main();
-        }
-
-        return 0;
+        loop.run();
+        #endif
+            } else {
+        Gtk.main();
     }
 
+    return 0;
+}
+
index de891cd..24d10b5 100644 (file)
@@ -167,7 +167,6 @@ namespace WebProvisioning
                      size_t             text_len) throws MarkupError
   {
     unowned SList<string> stack = context.get_element_stack ();
-    
     if (text_len < 1)
       return;
     
index 74288f7..343a684 100644 (file)
 [DBus (name = "org.janet.Moonshot")]
 public class MoonshotServer : Object {
 
+       private static Log4Vala.Logger logger = get_logger("MoonshotServer");
+
+       private string app_name = "Moonshot";
+
     private IdentityManagerApp parent_app;
 
     public MoonshotServer (IdentityManagerApp app)
     {
+               logger.trace("MoonshotServer.<constructor>; 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;
     }
 
-    public async bool get_identity (string nai,
+    private async bool get_identity (string nai,
                                     string password,
                                     string service,
                                     out string nai_out,
@@ -61,13 +71,18 @@ 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);
         request.set_callback ((IdentityRequest) => get_identity.callback());
+               logger.trace(@"MoonshotServer.get_identity: Calling request.execute()");
         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 +118,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 +134,8 @@ 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 +171,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 +311,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 +378,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,6 +453,8 @@ 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();
index 72df968..f0cb38b 100644 (file)
@@ -100,3 +100,14 @@ public bool UserForcesFlatFileStore()
     }
     return false;
 }
+
+static bool logger_is_initialized = false;
+public Log4Vala.Logger get_logger(string name) {
+       if (!logger_is_initialized) {
+               Log4Vala.init("/home/dbreslau/log4vala.conf");
+               logger_is_initialized = true;
+               Log4Vala.Logger.get_logger("utils.get_logger").info("I'm alive!");
+       }
+
+       return Log4Vala.Logger.get_logger(name);
+}
\ No newline at end of file