mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
This change brings about a few changes: 1) A new LogLevel called "User" that is used for non-verbose, non-error output 2) Only User and Error are output to console now, not all of them 3) All programs have log to file enabled by default and all flags for enabling logging have been removed 4) Some former Verbose statements have been converted over to User because of the shift in usage.
159 lines
3.5 KiB
C#
159 lines
3.5 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace SabreTools.Helper
|
|
{
|
|
/// <summary>
|
|
/// Log either to file or to the console
|
|
/// </summary>
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <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>
|
|
public Logger(bool tofile, string filename = "")
|
|
{
|
|
_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>
|
|
public bool Start()
|
|
{
|
|
_start = DateTime.Now;
|
|
if (!_tofile)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
try
|
|
{
|
|
_log = new StreamWriter(File.Open(_filename, FileMode.OpenOrCreate | FileMode.Append));
|
|
_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>
|
|
public bool Close()
|
|
{
|
|
|
|
TimeSpan span = DateTime.Now.Subtract(_start);
|
|
string total = span.ToString(@"hh\:mm\:ss\.fffff");
|
|
if (!_tofile)
|
|
{
|
|
Console.WriteLine("Total runtime: " + total);
|
|
return true;
|
|
}
|
|
|
|
try
|
|
{
|
|
_log.WriteLine("Logging ended " + DateTime.Now);
|
|
_log.WriteLine("Total runtime: " + total);
|
|
Console.WriteLine("Total runtime: " + total);
|
|
_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>
|
|
/// <param name="loglevel">Severity of the information being logged</param>
|
|
/// <returns>True if the output could be written, false otherwise</returns>
|
|
public bool Log(string output, LogLevel loglevel = LogLevel.VERBOSE)
|
|
{
|
|
// USER and ERROR writes to console
|
|
if (loglevel == LogLevel.USER || loglevel == LogLevel.ERROR)
|
|
{
|
|
Console.WriteLine((loglevel == LogLevel.ERROR ? 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;
|
|
}
|
|
|
|
public bool User(string output)
|
|
{
|
|
return Log(output, LogLevel.USER);
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
return Log(output, LogLevel.ERROR);
|
|
}
|
|
}
|
|
}
|