Files
SabreTools/SabreHelper/Logger.cs

159 lines
3.5 KiB
C#
Raw Normal View History

2016-03-28 17:54:24 -07:00
using System;
using System.IO;
namespace SabreTools.Helper
2016-03-28 17:54:24 -07:00
{
/// <summary>
/// Log either to file or to the console
/// </summary>
2016-03-28 18:40:35 -07:00
public class Logger
2016-03-28 17:54:24 -07:00
{
// Private instance variables
private bool _tofile;
private string _filename;
2016-04-27 22:17:19 -07:00
private DateTime _start;
2016-03-28 17:54:24 -07:00
private StreamWriter _log;
// Public wrappers
public bool ToFile
{
get { return _tofile; }
set
{
if (!value)
{
Close();
}
_tofile = value;
if (_tofile)
{
Start();
}
}
}
/// <summary>
/// Initialize a Logger object with the given information
/// </summary>
/// <param name="tofile">True if file should be written to instead of console</param>
/// <param name="filename">Optional filename representing log location</param>
2016-03-28 18:40:35 -07:00
public Logger(bool tofile, string filename = "")
2016-03-28 17:54:24 -07:00
{
_tofile = tofile;
_filename = filename;
}
/// <summary>
/// Start logging by opening output file (if necessary)
/// </summary>
/// <returns>True if the logging was started correctly, false otherwise</returns>
2016-03-28 17:54:24 -07:00
public bool Start()
{
2016-04-28 17:39:10 -07:00
_start = DateTime.Now;
2016-03-28 17:54:24 -07:00
if (!_tofile)
{
return true;
}
try
{
2016-03-28 18:00:21 -07:00
_log = new StreamWriter(File.Open(_filename, FileMode.OpenOrCreate | FileMode.Append));
2016-03-28 17:54:24 -07:00
_log.WriteLine("Logging started " + DateTime.Now);
}
catch
{
return false;
}
return true;
}
/// <summary>
/// End logging by closing output file (if necessary)
/// </summary>
/// <returns>True if the logging was ended correctly, false otherwise</returns>
2016-03-28 17:54:24 -07:00
public bool Close()
{
2016-04-28 17:39:10 -07:00
TimeSpan span = DateTime.Now.Subtract(_start);
string total = span.ToString(@"hh\:mm\:ss\.fffff");
2016-03-28 17:54:24 -07:00
if (!_tofile)
{
2016-04-28 17:39:10 -07:00
Console.WriteLine("Total runtime: " + total);
2016-03-28 17:54:24 -07:00
return true;
}
try
{
2016-03-28 17:54:24 -07:00
_log.WriteLine("Logging ended " + DateTime.Now);
2016-04-28 17:39:10 -07:00
_log.WriteLine("Total runtime: " + total);
Console.WriteLine("Total runtime: " + total);
2016-03-28 17:54:24 -07:00
_log.Close();
}
catch
{
return false;
}
return true;
}
/// <summary>
/// Write the given string to the log output
/// </summary>
/// <param name="output">String to be written log</param>
2016-03-30 13:53:14 -07:00
/// <param name="loglevel">Severity of the information being logged</param>
/// <returns>True if the output could be written, false otherwise</returns>
2016-03-30 13:53:14 -07:00
public bool Log(string output, LogLevel loglevel = LogLevel.VERBOSE)
2016-03-28 17:54:24 -07:00
{
// USER and ERROR writes to console
if (loglevel == LogLevel.USER || loglevel == LogLevel.ERROR)
{
Console.WriteLine((loglevel == LogLevel.ERROR ? loglevel.ToString() + " " : "") + output);
}
2016-03-30 12:18:56 -07:00
2016-03-28 17:54:24 -07:00
// If we're writing to file, use the existing stream
2016-03-30 12:18:56 -07:00
if (_tofile)
2016-03-28 17:54:24 -07:00
{
2016-03-30 12:18:56 -07:00
try
{
2016-04-21 17:47:23 -07:00
_log.WriteLine(loglevel.ToString() + " - " + DateTime.Now + " - " + output);
_log.Flush();
2016-03-30 12:18:56 -07:00
}
catch
{
Console.WriteLine("Could not write to log file!");
return false;
}
2016-03-28 17:54:24 -07:00
}
return true;
}
2016-03-30 13:36:52 -07:00
public bool User(string output)
{
return Log(output, LogLevel.USER);
}
2016-03-30 13:53:14 -07:00
/// <summary>
/// Write the given string as a warning to the log output
/// </summary>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public bool Warning(string output)
{
return Log(output, LogLevel.WARNING);
}
2016-03-30 13:36:52 -07:00
/// <summary>
/// Writes the given string as an error in the log
/// </summary>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
public bool Error(string output)
{
2016-03-30 13:53:14 -07:00
return Log(output, LogLevel.ERROR);
2016-03-30 13:36:52 -07:00
}
2016-03-28 17:54:24 -07:00
}
}