2016-03-28 17:54:24 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
2016-03-29 13:48:10 -07:00
|
|
|
|
namespace SabreTools.Helper
|
2016-03-28 17:54:24 -07:00
|
|
|
|
{
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Log either to file or to the console
|
|
|
|
|
|
/// </summary>
|
2016-05-22 23:57:44 -07:00
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// Things to do:
|
|
|
|
|
|
/// - Allow for "triggerable" logging done on an interval (async)
|
2016-05-23 12:11:22 -07:00
|
|
|
|
/// - Log filtering? (#if debug?)
|
2016-05-22 23:57:44 -07:00
|
|
|
|
/// </remarks>
|
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;
|
|
|
|
|
|
|
2016-05-10 16:01:32 -07:00
|
|
|
|
// Private required variables
|
|
|
|
|
|
private string _basepath = "Logs" + Path.DirectorySeparatorChar;
|
|
|
|
|
|
|
2016-03-28 17:54:24 -07:00
|
|
|
|
// Public wrappers
|
|
|
|
|
|
public bool ToFile
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _tofile; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!value)
|
|
|
|
|
|
{
|
|
|
|
|
|
Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
_tofile = value;
|
|
|
|
|
|
if (_tofile)
|
|
|
|
|
|
{
|
|
|
|
|
|
Start();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initialize a Logger object with the given information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="tofile">True if file should be written to instead of console</param>
|
2016-06-02 00:08:55 -07:00
|
|
|
|
/// <param name="filename">Filename representing log location</param>
|
|
|
|
|
|
public Logger(bool tofile, string filename)
|
2016-03-28 17:54:24 -07:00
|
|
|
|
{
|
|
|
|
|
|
_tofile = tofile;
|
2016-06-02 00:08:55 -07:00
|
|
|
|
_filename = Path.GetFileNameWithoutExtension(filename) + " (" + DateTime.Now.ToString("yyyy-MM-dd HHmmss") + ")" + Path.GetExtension(filename);
|
2016-05-10 16:01:32 -07:00
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(_basepath))
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.CreateDirectory(_basepath);
|
|
|
|
|
|
}
|
2016-03-28 17:54:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <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-05-10 16:01:32 -07:00
|
|
|
|
_log = new StreamWriter(File.Open(_basepath + _filename, FileMode.OpenOrCreate | FileMode.Append));
|
2016-03-28 17:54:24 -07:00
|
|
|
|
_log.WriteLine("Logging started " + DateTime.Now);
|
2016-05-10 16:05:23 -07:00
|
|
|
|
_log.Flush();
|
2016-03-28 17:54:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// End logging by closing output file (if necessary)
|
|
|
|
|
|
/// </summary>
|
2016-06-13 00:40:32 -07:00
|
|
|
|
/// <param name="suppress">True if all ending output is to be suppressed, false otherwise (default)</param>
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <returns>True if the logging was ended correctly, false otherwise</returns>
|
2016-06-13 00:40:32 -07:00
|
|
|
|
public bool Close(bool suppress = false)
|
2016-03-28 17:54:24 -07:00
|
|
|
|
{
|
2016-06-13 00:40:32 -07:00
|
|
|
|
if (!suppress)
|
2016-03-28 17:54:24 -07:00
|
|
|
|
{
|
2016-06-13 00:40:32 -07:00
|
|
|
|
TimeSpan span = DateTime.Now.Subtract(_start);
|
|
|
|
|
|
string total = span.ToString(@"hh\:mm\:ss\.fffff");
|
|
|
|
|
|
if (!_tofile)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Total runtime: " + total);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2016-03-28 17:54:24 -07:00
|
|
|
|
|
2016-06-13 00:40:32 -07:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_log.WriteLine("Logging ended " + DateTime.Now);
|
|
|
|
|
|
_log.WriteLine("Total runtime: " + total);
|
|
|
|
|
|
Console.WriteLine("Total runtime: " + total);
|
|
|
|
|
|
_log.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-03-28 17:54:24 -07:00
|
|
|
|
}
|
2016-06-13 00:40:32 -07:00
|
|
|
|
else
|
2016-03-28 17:54:24 -07:00
|
|
|
|
{
|
2016-06-13 00:40:32 -07:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
_log.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2016-03-28 17:54:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <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>
|
2016-03-29 14:49:03 -07:00
|
|
|
|
/// <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
|
|
|
|
{
|
2016-05-10 15:41:33 -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);
|
2016-04-22 12:15:49 -07:00
|
|
|
|
_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
|
|
|
|
|
2016-05-22 23:57:44 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write the given string as a user message 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>
|
2016-05-10 15:41:33 -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
|
|
|
|
}
|
|
|
|
|
|
}
|