Log.cs
changeset 1246 98a10b3984e6
child 1249 00cc05656f71
equal deleted inserted replaced
1245:abec57ca48d4 1246:98a10b3984e6
       
     1 ´╗┐using System;
       
     2 using System.IO;
       
     3 
       
     4 namespace pEp
       
     5 {
       
     6     /// <summary>
       
     7     /// Class to handle all the logging of pEp for Outlook.
       
     8     /// </summary>
       
     9     internal class Log
       
    10     {
       
    11         public const string LOG_FILE_NAME              = "log.txt";
       
    12         public const int    LOG_LINES_TO_REPLACE       = 50;                              // The number of lines to replace when the log file gets cleaned up
       
    13         public const int    LOG_MAX_LINES              = 1000;                            // The maximum lines a log file can reach before the first lines get replaced (FIFO)
       
    14 
       
    15         private static int                logLineCount       = 0;
       
    16         private static StreamWriter       logWriter          = null;
       
    17         private static object             mutexLogFile       = new object();
       
    18 
       
    19         /**************************************************************
       
    20          * 
       
    21          * Methods
       
    22          * 
       
    23          *************************************************************/
       
    24 
       
    25         /// <summary>
       
    26         /// Gets the path of the log file.
       
    27         /// </summary>
       
    28         /// <returns>The path of the log file.</returns>
       
    29         private static string GetLogFilePath()
       
    30         {
       
    31             return (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", LOG_FILE_NAME));
       
    32         }
       
    33 
       
    34         /// <summary>
       
    35         /// Adds the given info text to the pEp for Outlook log.
       
    36         /// </summary>
       
    37         /// <param name="text">The text to add to the log.</param>
       
    38         public static void Info(string text)
       
    39         {
       
    40             if (text != null)
       
    41             {
       
    42                 Log.Write(System.DateTime.Now.ToString("HH:mm:ss.fff") + " | " + text);
       
    43             }
       
    44             return;
       
    45         }
       
    46 
       
    47         /// <summary>
       
    48         /// Adds the given error text to the pEp for Outlook log.
       
    49         /// </summary>
       
    50         /// <param name="text">The error text to add to the log.</param>
       
    51         public static void Error(string text)
       
    52         {
       
    53             if (text != null)
       
    54             {
       
    55                 Log.Write(System.DateTime.Now.ToString("HH:mm:ss.fff") + " |E| " + text);
       
    56             }
       
    57             return;
       
    58         }
       
    59 
       
    60         /// <summary>
       
    61         /// Adds the given warning text to the pEp for Outlook log.
       
    62         /// </summary>
       
    63         /// <param name="text">The warning text to add to the log.</param>
       
    64         public static void Warning(string text)
       
    65         {
       
    66             if (text != null)
       
    67             {
       
    68                 Log.Write(System.DateTime.Now.ToString("HH:mm:ss.fff") + " |W| " + text);
       
    69             }
       
    70             return;
       
    71         }
       
    72 
       
    73         /// <summary>
       
    74         /// Adds the given verbose text to the pEp for Outlook log only
       
    75         /// when verbose logging is enabled.
       
    76         /// </summary>
       
    77         /// <param name="text">The verbose text to add to the log.</param>
       
    78         public static void Verbose(string text)
       
    79         {
       
    80             if ((text != null) &&
       
    81                 (Globals.ThisAddIn != null) &&
       
    82                 (Globals.ThisAddIn.Settings.IsVerboseLoggingEnabled))
       
    83             {
       
    84                 Log.Write(System.DateTime.Now.ToString("HH:mm:ss.fff") + " |V| " + text);
       
    85             }
       
    86             return;
       
    87         }
       
    88 
       
    89         /// <summary>
       
    90         /// Writes the given text to the log file.
       
    91         /// </summary>
       
    92         /// <param name="text">The text to write to the log file.</param>
       
    93         private static void Write(string text)
       
    94         {
       
    95             try
       
    96             {
       
    97                 if (text != null)
       
    98                 {
       
    99                     lock (mutexLogFile)
       
   100                     {
       
   101                         if (Log.logWriter == null)
       
   102                         {
       
   103                             // If file exists, set line counter to file line count
       
   104                             if (File.Exists(Log.GetLogFilePath()))
       
   105                             {
       
   106                                 try
       
   107                                 {
       
   108                                     var lines = File.ReadAllLines(Log.GetLogFilePath());
       
   109                                     logLineCount = lines.Length;
       
   110                                 }
       
   111                                 catch { }
       
   112                             }
       
   113 
       
   114                             // Open the file
       
   115                             Log.logWriter = File.AppendText(Log.GetLogFilePath());
       
   116                         }
       
   117 
       
   118                         Log.logWriter.WriteLine(text);
       
   119 
       
   120                         Log.logLineCount++;
       
   121 
       
   122                         // If the maximum line count was reached, clean up the log file by removing the first lines.
       
   123                         if (Log.logLineCount >= Log.LOG_MAX_LINES)
       
   124                         {
       
   125                             Log.Sanitize();
       
   126                         }
       
   127                     }
       
   128                 }
       
   129             }
       
   130             catch { }
       
   131 
       
   132             return;
       
   133         }
       
   134 
       
   135         /// <summary>
       
   136         /// FIFO function to remove the first lines of the log file when a defined maximum 
       
   137         /// of lines has been reached. 
       
   138         /// </summary>
       
   139         private static void Sanitize()
       
   140         {
       
   141             try
       
   142             {
       
   143                 lock (mutexLogFile)
       
   144                 {
       
   145                     Log.Close();
       
   146 
       
   147                     var lines = File.ReadAllLines(Log.GetLogFilePath());
       
   148 
       
   149                     Log.Clear();
       
   150 
       
   151                     int startIndex = (lines.Length - (Log.LOG_MAX_LINES - Log.LOG_LINES_TO_REPLACE));
       
   152                     if (startIndex < 0)
       
   153                     {
       
   154                         startIndex = 0;
       
   155                     }
       
   156 
       
   157                     using (StreamWriter streamWriter = File.AppendText(Log.GetLogFilePath()))
       
   158                     {
       
   159                         for (int i = startIndex; i < lines.Length; i++)
       
   160                         {
       
   161                             if (string.IsNullOrEmpty(lines[i]) == false)
       
   162                             {
       
   163                                 streamWriter.WriteLine(lines[i]);
       
   164                             }
       
   165                         }
       
   166                     }
       
   167 
       
   168                     Log.logLineCount = (Log.LOG_MAX_LINES - Log.LOG_LINES_TO_REPLACE);
       
   169                 }
       
   170             }
       
   171             catch { }
       
   172 
       
   173             return;
       
   174         }
       
   175 
       
   176         /// <summary>
       
   177         /// Reads all lines of text from the log file.
       
   178         /// This will never return null.
       
   179         /// </summary>
       
   180         /// <returns>The lines of text in the log file.</returns>
       
   181         public static string Read()
       
   182         {
       
   183             string log = "";
       
   184             string path;
       
   185             StreamReader logReader;
       
   186 
       
   187             try
       
   188             {
       
   189                 Log.Close();
       
   190 
       
   191                 lock (mutexLogFile)
       
   192                 {
       
   193                     path = Log.GetLogFilePath();
       
   194                     if (File.Exists(path))
       
   195                     {
       
   196                         logReader = new StreamReader(path);
       
   197                         log = logReader.ReadToEnd();
       
   198 
       
   199                         logReader.Close();
       
   200                         logReader.Dispose();
       
   201                     }
       
   202                 }
       
   203 
       
   204                 // Never allow null
       
   205                 if (log == null)
       
   206                 {
       
   207                     log = "";
       
   208                 }
       
   209             }
       
   210             catch { }
       
   211 
       
   212             return (log);
       
   213         }
       
   214 
       
   215         /// <summary>
       
   216         /// Closes and releases any open log file writer.
       
   217         /// </summary>
       
   218         public static void Close()
       
   219         {
       
   220             try
       
   221             {
       
   222                 lock (mutexLogFile)
       
   223                 {
       
   224                     if (Log.logWriter != null)
       
   225                     {
       
   226                         Log.logWriter.Flush();
       
   227                         Log.logWriter.Close();
       
   228                         Log.logWriter.Dispose();
       
   229                         Log.logWriter = null;
       
   230                     }
       
   231                 }
       
   232             }
       
   233             catch { }
       
   234 
       
   235             return;
       
   236         }
       
   237 
       
   238         /// <summary>
       
   239         /// Clears all logged text by deleting the file.
       
   240         /// </summary>
       
   241         public static void Clear()
       
   242         {
       
   243             string path;
       
   244 
       
   245             try
       
   246             {
       
   247                 Log.Close();
       
   248 
       
   249                 lock (mutexLogFile)
       
   250                 {
       
   251                     path = Log.GetLogFilePath();
       
   252                     if (File.Exists(path))
       
   253                     {
       
   254                         File.Delete(path);
       
   255                     }
       
   256                 }
       
   257             }
       
   258             catch { }
       
   259 
       
   260             return;
       
   261         }
       
   262     }
       
   263 }