[longomatch] Add support for logging to file



commit 298d82f46186166a6de8e0e40c62ed358edbd3d1
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Thu Apr 23 15:51:46 2015 +0200

    Add support for logging to file

 LongoMatch.Core/Common/Log.cs       |   28 +++++++++++++++++++++++++---
 LongoMatch.Services/CoreServices.cs |    5 ++++-
 2 files changed, 29 insertions(+), 4 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Log.cs b/LongoMatch.Core/Common/Log.cs
index d4f4000..df7452b 100644
--- a/LongoMatch.Core/Common/Log.cs
+++ b/LongoMatch.Core/Common/Log.cs
@@ -30,6 +30,7 @@ using System;
 using System.Text;
 using System.Collections.Generic;
 using System.Threading;
+using System.IO;
 
 namespace LongoMatch.Core.Common
 {
@@ -103,6 +104,8 @@ namespace LongoMatch.Core.Common
        {
                public static event LogNotifyHandler Notify;
 
+               public static StreamWriter LogFile;
+
                private static Dictionary<uint, DateTime> timers = new Dictionary<uint, DateTime> ();
                private static uint next_timer_id = 1;
 
@@ -117,6 +120,15 @@ namespace LongoMatch.Core.Common
                        }
                }
 
+               public static void SetLogFile (string filename)
+               {
+                       if (LogFile != null) {
+                               LogFile.Close ();
+                       }
+                       LogFile = File.CreateText (filename);
+                       LogFile.AutoFlush = true;
+               }
+
                public static void Commit (LogEntryType type, string message, string details, bool showUser)
                {
                        if (type == LogEntryType.Debug && !Debugging) {
@@ -145,15 +157,15 @@ namespace LongoMatch.Core.Common
                                        thread_name = String.Format ("{0} ", thread.ManagedThreadId);
                                }
 
-                               Console.Write ("[{5}{0} {1:00}:{2:00}:{3:00}.{4:000}]", TypeString (type), 
DateTime.Now.Hour,
+                               Write ("[{5}{0} {1:00}:{2:00}:{3:00}.{4:000}]", TypeString (type), 
DateTime.Now.Hour,
                                        DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond, 
thread_name);
 
                                ConsoleCrayon.ResetColor ();
 
                                if (details != null) {
-                                       Console.WriteLine (" {0} - {1}", message, details);
+                                       Write (" {0} - {1}\n", message, details);
                                } else {
-                                       Console.WriteLine (" {0}", message);
+                                       Write (" {0}\n", message);
                                }
                        }
 
@@ -162,6 +174,16 @@ namespace LongoMatch.Core.Common
                        }
                }
 
+               private static void Write (string format, params object[] args)
+               {
+                       try {
+                               if (LogFile != null)
+                                       LogFile.Write (format, args);
+                       } catch {
+                       }
+                       Console.Write (format, args);
+               }
+
                private static string TypeString (LogEntryType type)
                {
                        switch (type) {
diff --git a/LongoMatch.Services/CoreServices.cs b/LongoMatch.Services/CoreServices.cs
index 195e89d..a8eec4a 100644
--- a/LongoMatch.Services/CoreServices.cs
+++ b/LongoMatch.Services/CoreServices.cs
@@ -61,7 +61,10 @@ namespace LongoMatch.Services
 
                        /* Check default folders */
                        CheckDirs ();
-                       
+
+                       Log.SetLogFile (Path.Combine (Config.HomeDir,
+                               Constants.SOFTWARE_NAME.ToLower () + ".log"));
+
                        /* Load user config */
                        Config.Load ();
                        


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