using System;
using System.IO;
namespace SabreTools.Helper
{
///
/// Log either to file or to the console
///
public class Logger
{
// Private instance variables
private bool _tofile;
private string _filename;
private DateTime _start;
private StreamWriter _log;
// Public wrappers
public bool ToFile
{
get { return _tofile; }
set
{
if (!value)
{
Close();
}
_tofile = value;
if (_tofile)
{
Start();
}
}
}
///
/// Initialize a Logger object with the given information
///
/// True if file should be written to instead of console
/// Optional filename representing log location
public Logger(bool tofile, string filename = "")
{
_tofile = tofile;
_filename = filename;
}
///
/// Start logging by opening output file (if necessary)
///
/// True if the logging was started correctly, false otherwise
public bool Start()
{
if (!_tofile)
{
return true;
}
try
{
_log = new StreamWriter(File.Open(_filename, FileMode.OpenOrCreate | FileMode.Append));
_log.WriteLine("Logging started " + DateTime.Now);
_start = DateTime.Now;
}
catch
{
return false;
}
return true;
}
///
/// End logging by closing output file (if necessary)
///
/// True if the logging was ended correctly, false otherwise
public bool Close()
{
TimeSpan elapsed = DateTime.Now - _start;
if (!_tofile)
{
Console.WriteLine("Total runtime: " + elapsed);
return true;
}
try
{
_log.WriteLine("Logging ended " + DateTime.Now);
_log.WriteLine("Total runtime: " + elapsed.TotalMinutes);
Console.WriteLine("Total runtime: " + elapsed);
_log.Close();
}
catch
{
return false;
}
return true;
}
///
/// Write the given string to the log output
///
/// String to be written log
/// Severity of the information being logged
/// True if the output could be written, false otherwise
public bool Log(string output, LogLevel loglevel = LogLevel.VERBOSE)
{
// Everything writes to console
Console.WriteLine(loglevel.ToString() + " " + output);
// If we're writing to file, use the existing stream
if (_tofile)
{
try
{
_log.WriteLine(loglevel.ToString() + " - " + DateTime.Now + " - " + output);
_log.Flush();
}
catch
{
Console.WriteLine("Could not write to log file!");
return false;
}
}
return true;
}
///
/// Write the given string as a warning to the log output
///
/// String to be written log
/// True if the output could be written, false otherwise
public bool Warning(string output)
{
return Log(output, LogLevel.WARNING);
}
///
/// Writes the given string as an error in the log
///
/// String to be written log
/// True if the output could be written, false otherwise
public bool Error(string output)
{
return Log(output, LogLevel.ERROR);
}
}
}