Log.cs
changeset 1246 98a10b3984e6
child 1249 00cc05656f71
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Log.cs	Wed Sep 07 13:39:32 2016 +0200
     1.3 @@ -0,0 +1,263 @@
     1.4 +´╗┐using System;
     1.5 +using System.IO;
     1.6 +
     1.7 +namespace pEp
     1.8 +{
     1.9 +    /// <summary>
    1.10 +    /// Class to handle all the logging of pEp for Outlook.
    1.11 +    /// </summary>
    1.12 +    internal class Log
    1.13 +    {
    1.14 +        public const string LOG_FILE_NAME              = "log.txt";
    1.15 +        public const int    LOG_LINES_TO_REPLACE       = 50;                              // The number of lines to replace when the log file gets cleaned up
    1.16 +        public const int    LOG_MAX_LINES              = 1000;                            // The maximum lines a log file can reach before the first lines get replaced (FIFO)
    1.17 +
    1.18 +        private static int                logLineCount       = 0;
    1.19 +        private static StreamWriter       logWriter          = null;
    1.20 +        private static object             mutexLogFile       = new object();
    1.21 +
    1.22 +        /**************************************************************
    1.23 +         * 
    1.24 +         * Methods
    1.25 +         * 
    1.26 +         *************************************************************/
    1.27 +
    1.28 +        /// <summary>
    1.29 +        /// Gets the path of the log file.
    1.30 +        /// </summary>
    1.31 +        /// <returns>The path of the log file.</returns>
    1.32 +        private static string GetLogFilePath()
    1.33 +        {
    1.34 +            return (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "pEp", LOG_FILE_NAME));
    1.35 +        }
    1.36 +
    1.37 +        /// <summary>
    1.38 +        /// Adds the given info text to the pEp for Outlook log.
    1.39 +        /// </summary>
    1.40 +        /// <param name="text">The text to add to the log.</param>
    1.41 +        public static void Info(string text)
    1.42 +        {
    1.43 +            if (text != null)
    1.44 +            {
    1.45 +                Log.Write(System.DateTime.Now.ToString("HH:mm:ss.fff") + " | " + text);
    1.46 +            }
    1.47 +            return;
    1.48 +        }
    1.49 +
    1.50 +        /// <summary>
    1.51 +        /// Adds the given error text to the pEp for Outlook log.
    1.52 +        /// </summary>
    1.53 +        /// <param name="text">The error text to add to the log.</param>
    1.54 +        public static void Error(string text)
    1.55 +        {
    1.56 +            if (text != null)
    1.57 +            {
    1.58 +                Log.Write(System.DateTime.Now.ToString("HH:mm:ss.fff") + " |E| " + text);
    1.59 +            }
    1.60 +            return;
    1.61 +        }
    1.62 +
    1.63 +        /// <summary>
    1.64 +        /// Adds the given warning text to the pEp for Outlook log.
    1.65 +        /// </summary>
    1.66 +        /// <param name="text">The warning text to add to the log.</param>
    1.67 +        public static void Warning(string text)
    1.68 +        {
    1.69 +            if (text != null)
    1.70 +            {
    1.71 +                Log.Write(System.DateTime.Now.ToString("HH:mm:ss.fff") + " |W| " + text);
    1.72 +            }
    1.73 +            return;
    1.74 +        }
    1.75 +
    1.76 +        /// <summary>
    1.77 +        /// Adds the given verbose text to the pEp for Outlook log only
    1.78 +        /// when verbose logging is enabled.
    1.79 +        /// </summary>
    1.80 +        /// <param name="text">The verbose text to add to the log.</param>
    1.81 +        public static void Verbose(string text)
    1.82 +        {
    1.83 +            if ((text != null) &&
    1.84 +                (Globals.ThisAddIn != null) &&
    1.85 +                (Globals.ThisAddIn.Settings.IsVerboseLoggingEnabled))
    1.86 +            {
    1.87 +                Log.Write(System.DateTime.Now.ToString("HH:mm:ss.fff") + " |V| " + text);
    1.88 +            }
    1.89 +            return;
    1.90 +        }
    1.91 +
    1.92 +        /// <summary>
    1.93 +        /// Writes the given text to the log file.
    1.94 +        /// </summary>
    1.95 +        /// <param name="text">The text to write to the log file.</param>
    1.96 +        private static void Write(string text)
    1.97 +        {
    1.98 +            try
    1.99 +            {
   1.100 +                if (text != null)
   1.101 +                {
   1.102 +                    lock (mutexLogFile)
   1.103 +                    {
   1.104 +                        if (Log.logWriter == null)
   1.105 +                        {
   1.106 +                            // If file exists, set line counter to file line count
   1.107 +                            if (File.Exists(Log.GetLogFilePath()))
   1.108 +                            {
   1.109 +                                try
   1.110 +                                {
   1.111 +                                    var lines = File.ReadAllLines(Log.GetLogFilePath());
   1.112 +                                    logLineCount = lines.Length;
   1.113 +                                }
   1.114 +                                catch { }
   1.115 +                            }
   1.116 +
   1.117 +                            // Open the file
   1.118 +                            Log.logWriter = File.AppendText(Log.GetLogFilePath());
   1.119 +                        }
   1.120 +
   1.121 +                        Log.logWriter.WriteLine(text);
   1.122 +
   1.123 +                        Log.logLineCount++;
   1.124 +
   1.125 +                        // If the maximum line count was reached, clean up the log file by removing the first lines.
   1.126 +                        if (Log.logLineCount >= Log.LOG_MAX_LINES)
   1.127 +                        {
   1.128 +                            Log.Sanitize();
   1.129 +                        }
   1.130 +                    }
   1.131 +                }
   1.132 +            }
   1.133 +            catch { }
   1.134 +
   1.135 +            return;
   1.136 +        }
   1.137 +
   1.138 +        /// <summary>
   1.139 +        /// FIFO function to remove the first lines of the log file when a defined maximum 
   1.140 +        /// of lines has been reached. 
   1.141 +        /// </summary>
   1.142 +        private static void Sanitize()
   1.143 +        {
   1.144 +            try
   1.145 +            {
   1.146 +                lock (mutexLogFile)
   1.147 +                {
   1.148 +                    Log.Close();
   1.149 +
   1.150 +                    var lines = File.ReadAllLines(Log.GetLogFilePath());
   1.151 +
   1.152 +                    Log.Clear();
   1.153 +
   1.154 +                    int startIndex = (lines.Length - (Log.LOG_MAX_LINES - Log.LOG_LINES_TO_REPLACE));
   1.155 +                    if (startIndex < 0)
   1.156 +                    {
   1.157 +                        startIndex = 0;
   1.158 +                    }
   1.159 +
   1.160 +                    using (StreamWriter streamWriter = File.AppendText(Log.GetLogFilePath()))
   1.161 +                    {
   1.162 +                        for (int i = startIndex; i < lines.Length; i++)
   1.163 +                        {
   1.164 +                            if (string.IsNullOrEmpty(lines[i]) == false)
   1.165 +                            {
   1.166 +                                streamWriter.WriteLine(lines[i]);
   1.167 +                            }
   1.168 +                        }
   1.169 +                    }
   1.170 +
   1.171 +                    Log.logLineCount = (Log.LOG_MAX_LINES - Log.LOG_LINES_TO_REPLACE);
   1.172 +                }
   1.173 +            }
   1.174 +            catch { }
   1.175 +
   1.176 +            return;
   1.177 +        }
   1.178 +
   1.179 +        /// <summary>
   1.180 +        /// Reads all lines of text from the log file.
   1.181 +        /// This will never return null.
   1.182 +        /// </summary>
   1.183 +        /// <returns>The lines of text in the log file.</returns>
   1.184 +        public static string Read()
   1.185 +        {
   1.186 +            string log = "";
   1.187 +            string path;
   1.188 +            StreamReader logReader;
   1.189 +
   1.190 +            try
   1.191 +            {
   1.192 +                Log.Close();
   1.193 +
   1.194 +                lock (mutexLogFile)
   1.195 +                {
   1.196 +                    path = Log.GetLogFilePath();
   1.197 +                    if (File.Exists(path))
   1.198 +                    {
   1.199 +                        logReader = new StreamReader(path);
   1.200 +                        log = logReader.ReadToEnd();
   1.201 +
   1.202 +                        logReader.Close();
   1.203 +                        logReader.Dispose();
   1.204 +                    }
   1.205 +                }
   1.206 +
   1.207 +                // Never allow null
   1.208 +                if (log == null)
   1.209 +                {
   1.210 +                    log = "";
   1.211 +                }
   1.212 +            }
   1.213 +            catch { }
   1.214 +
   1.215 +            return (log);
   1.216 +        }
   1.217 +
   1.218 +        /// <summary>
   1.219 +        /// Closes and releases any open log file writer.
   1.220 +        /// </summary>
   1.221 +        public static void Close()
   1.222 +        {
   1.223 +            try
   1.224 +            {
   1.225 +                lock (mutexLogFile)
   1.226 +                {
   1.227 +                    if (Log.logWriter != null)
   1.228 +                    {
   1.229 +                        Log.logWriter.Flush();
   1.230 +                        Log.logWriter.Close();
   1.231 +                        Log.logWriter.Dispose();
   1.232 +                        Log.logWriter = null;
   1.233 +                    }
   1.234 +                }
   1.235 +            }
   1.236 +            catch { }
   1.237 +
   1.238 +            return;
   1.239 +        }
   1.240 +
   1.241 +        /// <summary>
   1.242 +        /// Clears all logged text by deleting the file.
   1.243 +        /// </summary>
   1.244 +        public static void Clear()
   1.245 +        {
   1.246 +            string path;
   1.247 +
   1.248 +            try
   1.249 +            {
   1.250 +                Log.Close();
   1.251 +
   1.252 +                lock (mutexLogFile)
   1.253 +                {
   1.254 +                    path = Log.GetLogFilePath();
   1.255 +                    if (File.Exists(path))
   1.256 +                    {
   1.257 +                        File.Delete(path);
   1.258 +                    }
   1.259 +                }
   1.260 +            }
   1.261 +            catch { }
   1.262 +
   1.263 +            return;
   1.264 +        }
   1.265 +    }
   1.266 +}