← Back to team overview

zeitgeist team mailing list archive

[Branch ~zeitgeist/zeitgeist/bluebird] Rev 409: - Implement logging to file.

 

------------------------------------------------------------
revno: 409
committer: Siegfried-Angel Gevatter Pujals <siegfried@xxxxxxxxxxxx>
branch nick: bluebird
timestamp: Thu 2012-03-01 15:44:32 +0100
message:
   - Implement logging to file.
   - Update NEWS.
modified:
  NEWS
  src/logging.vala
  src/utils.vala
  src/zeitgeist-daemon.vala


--
lp:zeitgeist
https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird

Your team Zeitgeist Framework Team is subscribed to branch lp:zeitgeist.
To unsubscribe from this branch go to https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird/+edit-subscription
=== modified file 'NEWS'
--- NEWS	2012-02-14 16:57:23 +0000
+++ NEWS	2012-03-01 14:44:32 +0000
@@ -1,3 +1,11 @@
+2012-03-01: Zeitgeist Bluebird Beta 1
+-------------------------------------
+
+Engine:
+ - Fixed crash when events had NULL fields (LP: #941530).
+ - Made sure no aborted SQLite transactions are left open.
+ - Enhanced logging and added --log-file option.
+
 2012-02-14: Zeitgeist Bluebird Alpha 3
 --------------------------------------
 

=== modified file 'src/logging.vala'
--- src/logging.vala	2012-02-19 20:47:38 +0000
+++ src/logging.vala	2012-03-01 14:44:32 +0000
@@ -20,16 +20,18 @@
 
 namespace Zeitgeist
 {
-    namespace Logging
+    class Logging
     {
 
+        private static FileStream? log_file = null;
+
         private const string BLUE = "\x1b[34m";
         private const string GREEN = "\x1b[32m";
         private const string RED = "\x1b[31m";
         private const string YELLOW = "\x1b[33m";
         private const string RESET = "\x1b[0m";
 
-        private string get_log_level_string (LogLevelFlags log_levels,
+        private static string get_log_level_string (LogLevelFlags log_levels,
             out string color)
         {
             string log_level;
@@ -73,15 +75,18 @@
             return log_level;
         }
 
-        private void log_handler (string? log_domain, LogLevelFlags log_levels,
-            string message)
+        private static void log_handler (string? log_domain,
+            LogLevelFlags log_levels, string message)
         {
             string color;
             string log_level = get_log_level_string (log_levels, out color);
             string timestamp = TimeVal ().to_iso8601 ().substring (11, 15);
-            //string datestamp = new DateTime.now_local ().format (
-            //    "%Y-%m-%d %H:%M:%S");
-            // FIXME: get PID
+
+            DateTime datetime = new DateTime.now_local ();
+            string date_string = "%s,%.3d".printf (
+                datetime.format ("%Y-%m-%d %H:%M:%S"),
+                (int) ((datetime.get_microsecond () / 1000.0)));
+            int pid = Posix.getpid ();
 
             unowned FileStream output;
             if (log_levels >= LogLevelFlags.LEVEL_MESSAGE)
@@ -94,11 +99,15 @@
                 log_level, RESET, message);
 
             // Log to file
-            // FIXME:
-            //printf ("[%s] - %s - %s\n", datestamp, log_level, message);
+            if (Logging.log_file != null)
+            {
+                Logging.log_file.printf ("%d [%s] - %s - %s\n",
+                    pid, date_string, log_level, message);
+            }
         }
 
-        public void setup_logging (string? log_level)
+        public static void setup_logging (string name, string? log_level,
+            string? log_file=null)
         {
             LogLevelFlags discarded = LogLevelFlags.LEVEL_DEBUG;
             if (log_level != null)
@@ -130,10 +139,55 @@
             if (discarded != 0)
                 Log.set_handler (null, discarded, () => {});
 
+            /*
+            try
+            {
+                string filename = rotate_and_get_log_file (name);
+                log_file = FileStream.open (filename, "a");
+            }
+            catch (Error e)
+            {
+                warning ("Couldn't setup file logging: %s", e.message);
+                log_file = null;
+            }
+            */
+
+            if (log_file != null)
+                Logging.log_file = FileStream.open (log_file, "a");
+
             LogLevelFlags logged = ~discarded & ~LogLevelFlags.FLAG_RECURSION;
             Log.set_handler (null, logged, log_handler);
         }
 
+        /*
+        private static string rotate_and_get_log_file (string name) throws Error
+        {
+            string log_path = Utils.get_logging_path ();
+            string filename = Path.build_path (Path.DIR_SEPARATOR_S,
+                log_path, "%s.log".printf (name));
+
+            File log_file = File.new_for_path (filename);
+            try
+            {
+                FileInfo info = log_file.query_info (
+                    FILE_ATTRIBUTE_TIME_MODIFIED, FileQueryInfoFlags.NONE);
+
+                TimeVal last_log_time_val;
+                info.get_modification_time (out last_log_time_val);
+
+                Date last_log_date = Date();
+                last_log_date.set_time_val (last_log_time_val);
+            }
+            catch (Error e)
+            {
+                if (!(e is IOError.NOT_FOUND))
+                    throw e;
+            }
+
+            return filename;
+        }
+        */
+
     }
 }
 

=== modified file 'src/utils.vala'
--- src/utils.vala	2012-02-12 20:52:37 +0000
+++ src/utils.vala	2012-03-01 14:44:32 +0000
@@ -66,6 +66,14 @@
                 DATA_FOLDER);
         }
 
+/*
+        public string get_logging_path ()
+        {
+            return Path.build_filename (Environment.get_user_cache_dir (),
+                DATA_FOLDER);
+        }
+*/
+
         public unowned string get_database_file_path ()
         {
             if (DATABASE_FILE_PATH != null) return DATABASE_FILE_PATH;

=== modified file 'src/zeitgeist-daemon.vala'
--- src/zeitgeist-daemon.vala	2012-02-15 19:55:59 +0000
+++ src/zeitgeist-daemon.vala	2012-03-01 14:44:32 +0000
@@ -41,6 +41,7 @@
         private static bool replace_mode = false;
         private static bool quit_daemon = false;
         private static string log_level = "";
+        private static string? log_file = null;
 
         const OptionEntry[] options =
         {
@@ -71,6 +72,10 @@
                 "DEBUG, INFO, WARNING, ERROR, CRITICAL", "LEVEL"
             },
             {
+                "log-file", 0, 0, OptionArg.STRING, out log_file,
+                "File to which the log output will be appended", null
+            },
+            {
                 "shell-completion", 0, OptionFlags.HIDDEN, OptionArg.NONE,
                 out show_options, null, null
             },
@@ -452,7 +457,7 @@
                     return 0;
                 }
 
-                Logging.setup_logging (log_level);
+                Logging.setup_logging ("daemon", log_level, log_file);
 
                 run ();
             }