//----------------------------------------------------------------------------
//
// Copyright © 2009-2014, Intel Corporation. All rights reserved.
//
// File: LogManager.cs
//
//----------------------------------------------------------------------------
using System;
using System.IO;
using UCT.Utils;
using System.Text;
using System.Collections;
using System.Globalization;
using UCT.Forms;
namespace UCT
{
#region - Enum -
public enum LogInteraction
{
FWOperation,
UserOperation,
Error,
Information,
Connection
};
public enum LogLevel
{
BASIC = 1,
DETAILED
};
#endregion
///
/// This class treat the log operations
///
class LogManager
{
private static LogManager _log;
private static ArrayList _logContent;
private static StreamWriter _writer;
private static String _logPath;
private static LogLevel _logLevel;
private static bool _hasLog;
private static string _lastLine;
public LogManager()
{
InitLog();
}
public static LogManager GetInstance()
{
try
{
lock (typeof(LogManager))
{
if (_log == null)
{
_log = new LogManager();
}
}
}
catch (Exception) { }
return _log;
}
public string GetLastLine()
{
return _lastLine;
}
///
/// Create a log file
///
public void InitLog()
{
try
{
_hasLog = Properties.Settings.Default.CREATE_LOG;
_logContent = new ArrayList();
string folderPath = "Logs";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string fileName = String.Format(@"{0}\UCT_Log_{1}.log", folderPath, DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture));
_logPath = fileName;
// _writer = new StreamWriter(fileName, true);
}
catch (Exception e)
{
AMT_SW_GUI.MessageManager.ShowErrorMessage("Failed to create the log: " + e.Message, "Failure message");
}
}
public ArrayList GetLogContent()
{
return _logContent;
}
///
/// Write operation to the log
///
public static StringBuilder WriteOperation(LogLevel messageLevel, string message, LogInteraction interacton)
{
StringBuilder line = new StringBuilder();
try
{
_logLevel = (LogLevel)Properties.Settings.Default.LOG_LEVEL;
_log = LogManager.GetInstance();
if (_hasLog)
{
if (((_logLevel == LogLevel.BASIC) && (messageLevel == LogLevel.BASIC)) ||
(_logLevel == LogLevel.DETAILED))
{
_writer = new StreamWriter(_logPath, true);
// Add the current date and time
line.Append("[" + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + "] : ");
line.Append(interacton.ToString() + " : ");
line.Append(message);
_logContent.Add(line);
_writer.WriteLine(line);
_writer.Flush();
_lastLine = line.ToString();
}
}
}
catch (Exception)
{
}
finally
{
if (_writer != null)
_writer.Close();
}
return line;
}
public void CloseLog()
{
try
{
_writer.Close();
}
catch (Exception) { }
}
internal void ViewLogFile()
{
try
{
LogWindow logwindow = new LogWindow();
StringBuilder log = new StringBuilder();
using (StreamReader reader = new StreamReader(_logPath))
{
try
{
do
{
log.Append(reader.ReadLine() + "\n");
}
while (reader.Peek() != -1);
logwindow.WriteLog(log.ToString());
logwindow.Show();
}
catch
{
log.Append("File is empty");
}
finally
{
reader.Close();
}
}
}
catch (Exception e)
{
throw new UCTException(e.Message);
}
}
internal static void CloseResource()
{
try
{
_writer.Close();
}
catch (Exception) { }
}
}
}