Files
SabreTools/SabreTools.Library/Logging/Logger.cs

380 lines
13 KiB
C#
Raw Normal View History

2016-03-28 17:54:24 -07:00
using System;
using System.IO;
using System.Text;
2016-03-28 17:54:24 -07:00
2017-05-04 02:41:11 -07:00
using SabreTools.Library.Data;
2020-08-01 23:04:11 -07:00
using SabreTools.Library.IO;
2020-08-01 22:10:29 -07:00
namespace SabreTools.Library.Logging
2016-03-28 17:54:24 -07:00
{
2019-02-08 21:03:28 -08:00
/// <summary>
/// Log either to file or to the console
/// </summary>
public class Logger
{
#region Fields
/// <summary>
/// Optional output filename for logs
/// </summary>
public string Filename { get; set; } = null;
/// <summary>
/// Determines if we're logging to file or not
/// </summary>
public bool LogToFile { get { return !string.IsNullOrWhiteSpace(Filename); } }
/// <summary>
/// Optional output log directory
/// </summary>
/// TODO: Make this either passed in or optional
public string LogDirectory { get; set; } = Path.Combine(Globals.ExeDir, "logs") + Path.DirectorySeparatorChar;
/// <summary>
/// Determines the lowest log level to output
/// </summary>
public LogLevel LowestLogLevel { get; set; } = LogLevel.VERBOSE;
2020-10-07 13:16:53 -07:00
/// <summary>
/// Determines whether to prefix log lines with level and datetime
/// </summary>
public bool AppendPrefix { get; set; } = true;
/// <summary>
/// Determines whether to throw if an exception is logged
/// </summary>
public bool ThrowOnError { get; set; } = false;
/// <summary>
/// Logging start time for metrics
/// </summary>
public DateTime StartTime { get; private set; }
/// <summary>
/// Determines if there were errors logged
/// </summary>
public bool LoggedErrors { get; private set; } = false;
/// <summary>
/// Determines if there were warnings logged
/// </summary>
public bool LoggedWarnings { get; private set; } = false;
#endregion
#region Private instance variables
/// <summary>
/// StreamWriter representing the output log file
/// </summary>
2019-02-08 21:03:28 -08:00
private StreamWriter _log;
/// <summary>
/// Object lock for multithreaded logging
/// </summary>
private readonly object _lock = new object();
#endregion
2019-02-08 21:03:28 -08:00
2020-10-07 13:16:53 -07:00
#region Constructors
2019-02-08 21:03:28 -08:00
/// <summary>
/// Initialize a console-only logger object
/// </summary>
public Logger()
{
Start();
}
/// <summary>
/// Initialize a Logger object with the given information
/// </summary>
/// <param name="filename">Filename representing log location</param>
/// <param name="addDate">True to add a date string to the filename (default), false otherwise</param>
public Logger(string filename, bool addDate = true)
2019-02-08 21:03:28 -08:00
{
2020-10-07 13:16:53 -07:00
// Set and create the output
if (addDate)
Filename = $"{Path.GetFileNameWithoutExtension(filename)} ({DateTime.Now:yyyy-MM-dd HH-mm-ss}).{PathExtensions.GetNormalizedExtension(filename)}";
else
Filename = filename;
2019-02-08 21:03:28 -08:00
if (!string.IsNullOrEmpty(LogDirectory) && !Directory.Exists(LogDirectory))
Directory.CreateDirectory(LogDirectory);
2019-02-08 21:03:28 -08:00
Start();
}
2020-10-07 13:16:53 -07:00
#endregion
#region Control
2019-02-08 21:03:28 -08:00
/// <summary>
/// Start logging by opening output file (if necessary)
/// </summary>
/// <returns>True if the logging was started correctly, false otherwise</returns>
public bool Start()
{
2020-10-07 13:16:53 -07:00
// Setup the logging handler to always use the internal log
LogEventHandler += HandleLogEvent;
// Start the logging
StartTime = DateTime.Now;
if (!LogToFile)
2019-02-08 21:03:28 -08:00
return true;
2020-10-07 13:16:53 -07:00
// Setup file output and perform initial log
2019-02-08 21:03:28 -08:00
try
{
FileStream logfile = FileExtensions.TryCreate(Path.Combine(LogDirectory, Filename));
_log = new StreamWriter(logfile, Encoding.UTF8, (int)(4 * Constants.KibiByte), true)
{
AutoFlush = true
};
2019-02-08 21:03:28 -08:00
_log.WriteLine($"Logging started {StartTime:yyyy-MM-dd HH:mm:ss}");
_log.WriteLine($"Command run: {Globals.CommandLineArgs}");
2019-02-08 21:03:28 -08:00
}
catch
{
return false;
}
return true;
}
/// <summary>
/// End logging by closing output file (if necessary)
/// </summary>
/// <param name="suppress">True if all ending output is to be suppressed, false otherwise (default)</param>
/// <returns>True if the logging was ended correctly, false otherwise</returns>
public bool Close(bool suppress = false)
{
if (!suppress)
{
if (LoggedWarnings)
2019-02-08 21:03:28 -08:00
Console.WriteLine("There were warnings in the last run! Check the log for more details");
if (LoggedErrors)
2019-02-08 21:03:28 -08:00
Console.WriteLine("There were errors in the last run! Check the log for more details");
TimeSpan span = DateTime.Now.Subtract(StartTime);
2019-02-08 21:03:28 -08:00
// Special case for multi-day runs
string total;
2019-02-08 21:03:28 -08:00
if (span >= TimeSpan.FromDays(1))
total = span.ToString(@"d\:hh\:mm\:ss");
else
total = span.ToString(@"hh\:mm\:ss");
if (!LogToFile)
2019-02-08 21:03:28 -08:00
{
Console.WriteLine($"Total runtime: {total}");
2019-02-08 21:03:28 -08:00
return true;
}
try
{
_log.WriteLine($"Logging ended {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
_log.WriteLine($"Total runtime: {total}");
Console.WriteLine($"Total runtime: {total}");
2019-02-08 21:03:28 -08:00
_log.Close();
}
catch
{
return false;
}
}
else
{
try
{
_log.Close();
}
catch
{
return false;
}
}
return true;
}
2020-10-07 13:16:53 -07:00
#endregion
#region Event Handling
/// <summary>
/// Handler for log events
/// </summary>
public static event LogEventHandler LogEventHandler = delegate { };
/// <summary>
/// Default log event handling
/// </summary>
public void HandleLogEvent(object sender, LogEventArgs args)
{
// Null args means we can't handle it
if (args == null)
return;
// If we have an exception and we're throwing on that
if (ThrowOnError && args.Exception != null)
throw args.Exception;
// If we have a warning or error, set the flags accordingly
if (args.LogLevel == LogLevel.WARNING)
LoggedWarnings = true;
if (args.LogLevel == LogLevel.ERROR)
LoggedErrors = true;
// Setup the statement based on the inputs
string logLine;
if (args.Exception == null)
{
logLine = args.Statement ?? string.Empty;
}
else if (args.TotalCount != null && args.CurrentCount != null)
{
double percentage = (args.CurrentCount.Value / args.TotalCount.Value) * 100;
logLine = $"{percentage:N2}%{(args.Statement != null ? ": " + args.Statement : string.Empty)}";
}
else
{
logLine = $"{(args.Statement != null ? args.Statement + ": " : string.Empty)}{args.Exception}";
}
// Then write to the log
Log(logLine, args.LogLevel);
}
2019-02-08 21:03:28 -08:00
/// <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>
2020-10-07 13:16:53 -07:00
private bool Log(string output, LogLevel loglevel)
2019-02-08 21:03:28 -08:00
{
// If the log level is less than the filter level, we skip it but claim we didn't
if (loglevel < LowestLogLevel)
2019-02-08 21:03:28 -08:00
return true;
// USER and ERROR writes to console
if (loglevel == LogLevel.USER || loglevel == LogLevel.ERROR)
2020-10-07 13:16:53 -07:00
Console.WriteLine((loglevel == LogLevel.ERROR && this.AppendPrefix ? loglevel.ToString() + " " : string.Empty) + output);
2019-02-08 21:03:28 -08:00
// If we're writing to file, use the existing stream
if (LogToFile)
2019-02-08 21:03:28 -08:00
{
try
{
2020-10-07 13:16:53 -07:00
lock (_lock)
2019-02-08 21:03:28 -08:00
{
2020-10-07 13:16:53 -07:00
_log.WriteLine((this.AppendPrefix ? $"{loglevel} - {DateTime.Now} - " : string.Empty) + output);
2019-02-08 21:03:28 -08:00
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine("Could not write to log file!");
if (ThrowOnError) throw ex;
2019-02-08 21:03:28 -08:00
return false;
}
}
return true;
}
2020-10-07 13:16:53 -07:00
#endregion
2019-02-08 21:03:28 -08:00
2020-10-07 13:16:53 -07:00
#region Log Event Triggers
2019-02-08 21:03:28 -08:00
2020-09-15 17:09:35 -07:00
/// <summary>
/// Write the given exception as a verbose message to the log output
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
2020-10-07 13:16:53 -07:00
public void Verbose(Exception ex, string output = null)
2020-09-15 17:09:35 -07:00
{
2020-10-07 13:16:53 -07:00
LogEventHandler(this, new LogEventArgs(LogLevel.VERBOSE, output, ex));
2020-09-15 17:09:35 -07:00
}
2019-02-08 21:03:28 -08:00
/// <summary>
/// Write the given string as a verbose 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>
2020-10-07 13:16:53 -07:00
public void Verbose(string output)
2019-02-08 21:03:28 -08:00
{
2020-10-07 13:16:53 -07:00
LogEventHandler(this, new LogEventArgs(LogLevel.VERBOSE, output, null));
2019-02-08 21:03:28 -08:00
}
2020-09-15 17:09:35 -07:00
/// <summary>
/// Write the given exception as a user message to the log output
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
2020-10-07 13:16:53 -07:00
public void User(Exception ex, string output = null)
2020-09-15 17:09:35 -07:00
{
2020-10-07 13:16:53 -07:00
LogEventHandler(this, new LogEventArgs(LogLevel.USER, output, ex));
2020-09-15 17:09:35 -07:00
}
2019-02-08 21:03:28 -08: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>
2020-10-07 13:16:53 -07:00
public void User(string output)
2019-02-08 21:03:28 -08:00
{
2020-10-07 13:16:53 -07:00
LogEventHandler(this, new LogEventArgs(LogLevel.USER, output, null));
2019-02-08 21:03:28 -08:00
}
2020-09-15 14:38:37 -07:00
/// <summary>
/// Write the given exception as a warning to the log output
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
2020-10-07 13:16:53 -07:00
public void Warning(Exception ex, string output = null)
2020-09-15 14:38:37 -07:00
{
2020-10-07 13:16:53 -07:00
LogEventHandler(this, new LogEventArgs(LogLevel.WARNING, output, ex));
2020-09-15 14:38:37 -07:00
}
2019-02-08 21:03:28 -08: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>
2020-10-07 13:16:53 -07:00
public void Warning(string output)
2019-02-08 21:03:28 -08:00
{
2020-10-07 13:16:53 -07:00
LogEventHandler(this, new LogEventArgs(LogLevel.WARNING, output, null));
2019-02-08 21:03:28 -08:00
}
2020-09-15 14:38:37 -07:00
/// <summary>
/// Writes the given exception as an error in the log
/// </summary>
/// <param name="ex">Exception to be written log</param>
/// <param name="output">String to be written log</param>
/// <returns>True if the output could be written, false otherwise</returns>
2020-10-07 13:16:53 -07:00
public void Error(Exception ex, string output = null)
2020-09-15 14:38:37 -07:00
{
2020-10-07 13:16:53 -07:00
LogEventHandler(this, new LogEventArgs(LogLevel.ERROR, output, ex));
2020-09-15 14:38:37 -07:00
}
2019-02-08 21:03:28 -08: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>
2020-10-07 13:16:53 -07:00
public void Error(string output)
2019-02-08 21:03:28 -08:00
{
2020-10-07 13:16:53 -07:00
LogEventHandler(this, new LogEventArgs(LogLevel.ERROR, output, null));
2019-02-08 21:03:28 -08:00
}
2020-10-07 13:16:53 -07:00
#endregion
2019-02-08 21:03:28 -08:00
}
2016-03-28 17:54:24 -07:00
}