Add log levels

This commit is contained in:
Matt Nadareski
2016-03-30 13:53:14 -07:00
parent ab0392404e
commit 2644a4b1a6

View File

@@ -13,6 +13,13 @@ namespace SabreTools.Helper
private string _filename; private string _filename;
private StreamWriter _log; private StreamWriter _log;
public enum LogLevel
{
VERBOSE = 0,
WARNING,
ERROR,
}
// Public wrappers // Public wrappers
public bool ToFile public bool ToFile
{ {
@@ -94,18 +101,19 @@ namespace SabreTools.Helper
/// Write the given string to the log output /// Write the given string to the log output
/// </summary> /// </summary>
/// <param name="output">String to be written log</param> /// <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> /// <returns>True if the output could be written, false otherwise</returns>
public bool Log(string output) public bool Log(string output, LogLevel loglevel = LogLevel.VERBOSE)
{ {
// Everything writes to console // Everything writes to console
Console.WriteLine(output); Console.WriteLine(loglevel.ToString() + " " + output);
// If we're writing to file, use the existing stream // If we're writing to file, use the existing stream
if (_tofile) if (_tofile)
{ {
try try
{ {
_log.WriteLine(output); _log.WriteLine(loglevel.ToString() + " " + output);
} }
catch catch
{ {
@@ -117,6 +125,16 @@ namespace SabreTools.Helper
return true; return true;
} }
/// <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> /// <summary>
/// Writes the given string as an error in the log /// Writes the given string as an error in the log
/// </summary> /// </summary>
@@ -124,7 +142,7 @@ 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>
public bool Error(string output) public bool Error(string output)
{ {
return Log("ERROR: " + output); return Log(output, LogLevel.ERROR);
} }
} }
} }