[Logger] Allow for log filtering based on level

This commit is contained in:
Matt Nadareski
2017-03-01 21:58:09 -08:00
parent 1eff6ce217
commit 4ab5f4578f
3 changed files with 29 additions and 15 deletions

View File

@@ -304,6 +304,21 @@
#endregion #endregion
#region Logging related
/// <summary>
/// Severity of the logging statement
/// </summary>
public enum LogLevel
{
VERBOSE = 0,
USER,
WARNING,
ERROR,
}
#endregion
#region Skippers and Mappers #region Skippers and Mappers
/// <summary> /// <summary>

View File

@@ -1,6 +1,8 @@
using System; using System;
using System.IO; using System.IO;
using SabreTools.Helper.Data;
namespace SabreTools.Helper namespace SabreTools.Helper
{ {
/// <summary> /// <summary>
@@ -8,27 +10,15 @@ namespace SabreTools.Helper
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// TODO: Allow for "triggerable" logging done on an interval (async) /// TODO: Allow for "triggerable" logging done on an interval (async)
/// TODO: Log filtering? (#if debug?)
/// TODO: Reduce user-verbosity in later builds
/// </remarks> /// </remarks>
public class Logger public class Logger
{ {
/// <summary>
/// Severity of the logging statement
/// </summary>
private enum LogLevel
{
VERBOSE = 0,
USER,
WARNING,
ERROR,
}
// Private instance variables // Private instance variables
private bool _tofile; private bool _tofile;
private bool _warnings; private bool _warnings;
private bool _errors; private bool _errors;
private string _filename; private string _filename;
private LogLevel _filter;
private DateTime _start; private DateTime _start;
private StreamWriter _log; private StreamWriter _log;
@@ -44,6 +34,7 @@ namespace SabreTools.Helper
_warnings = false; _warnings = false;
_errors = false; _errors = false;
_filename = null; _filename = null;
_filter = LogLevel.VERBOSE;
Start(); Start();
} }
@@ -53,12 +44,14 @@ namespace SabreTools.Helper
/// </summary> /// </summary>
/// <param name="tofile">True if file should be written to instead of console</param> /// <param name="tofile">True if file should be written to instead of console</param>
/// <param name="filename">Filename representing log location</param> /// <param name="filename">Filename representing log location</param>
public Logger(bool tofile, string filename) /// <param name="filter">Highest filtering level to be kept, default VERBOSE</param>
public Logger(bool tofile, string filename, LogLevel filter = LogLevel.VERBOSE)
{ {
_tofile = tofile; _tofile = tofile;
_warnings = false; _warnings = false;
_errors = false; _errors = false;
_filename = Path.GetFileNameWithoutExtension(filename) + " (" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ")" + Path.GetExtension(filename); _filename = Path.GetFileNameWithoutExtension(filename) + " (" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ")" + Path.GetExtension(filename);
_filter = filter;
if (!Directory.Exists(_basepath)) if (!Directory.Exists(_basepath))
{ {
@@ -169,6 +162,12 @@ namespace SabreTools.Helper
/// <returns>True if the output could be written, false otherwise</returns> /// <returns>True if the output could be written, false otherwise</returns>
private bool Log(string output, LogLevel loglevel, bool appendPrefix) private bool Log(string output, LogLevel loglevel, bool appendPrefix)
{ {
// If the log level is less than the filter level, we skip it but claim we didn't
if (loglevel < _filter)
{
return true;
}
// USER and ERROR writes to console // USER and ERROR writes to console
if (loglevel == LogLevel.USER || loglevel == LogLevel.ERROR) if (loglevel == LogLevel.USER || loglevel == LogLevel.ERROR)
{ {

View File

@@ -36,7 +36,7 @@ using SharpCompress.Writers;
namespace SabreTools.Helper.Tools namespace SabreTools.Helper.Tools
{ {
/* /*
* TODO: Full archive support for: 7-Zip, RAR, XZ, LRZip * TODO: Full archive support for: RAR, XZ, LRZip
*/ */
public static class ArchiveTools public static class ArchiveTools
{ {