added a bunch of comments

This commit is contained in:
tmont 2009-04-12 06:06:43 +00:00
parent a5e5a68ffd
commit c3969fc7f8
2 changed files with 123 additions and 46 deletions

View File

@ -1,14 +1,23 @@
using System; using System;
namespace SimpleLog.LogHandlers { namespace SimpleLog.LogHandlers {
/// <summary>
/// Log handler for console output (e.g. use this instead of Console.WriteLine()).
/// </summary>
public class ConsoleLogHandler : ILogHandler { public class ConsoleLogHandler : ILogHandler {
protected IMessageHandler messageHandler; protected IMessageHandler messageHandler;
protected LogLevel? logLevel; protected LogLevel? logLevel;
//stolen from log4net
public const UInt32 STD_OUTPUT_HANDLE = unchecked((UInt32)(-11)); public const UInt32 STD_OUTPUT_HANDLE = unchecked((UInt32)(-11));
public const UInt32 STD_ERROR_HANDLE = unchecked((UInt32)(-12)); public const UInt32 STD_ERROR_HANDLE = unchecked((UInt32)(-12));
/// <summary>
/// Constructs a new ConsoleLogHandler with a default
/// message handler
/// </summary>
public ConsoleLogHandler() { public ConsoleLogHandler() {
this.messageHandler = new DefaultMessageHandler(); this.messageHandler = new DefaultMessageHandler();
this.logLevel = null; this.logLevel = null;
@ -16,9 +25,12 @@ namespace SimpleLog.LogHandlers {
#region ILogHandler Members #region ILogHandler Members
public virtual void GracefulShutDown() { public virtual void GracefulShutDown() {
throw new NotImplementedException(); //does nothing
} }
/// <summary>
/// Gets or sets the log level for this handler
/// </summary>
public LogLevel? LogLevel { public LogLevel? LogLevel {
get { get {
return this.logLevel; return this.logLevel;
@ -28,35 +40,21 @@ namespace SimpleLog.LogHandlers {
} }
} }
/// <summary>
/// Logs a message to the console
/// </summary>
/// <param name="message">The message to log</param>
/// <param name="level">The log level of the message</param>
public virtual bool Log(string message, LogLevel level) { public virtual bool Log(string message, LogLevel level) {
UInt32 streamType = this.GetStreamType(level); uint streamType = this.GetStreamType(level);
System.IO.TextWriter outputStream = (System.IO.TextWriter)this.GetOutputStream(streamType); System.IO.TextWriter writer = (System.IO.TextWriter)this.GetOutputStream(streamType);
outputStream.Write(message); writer.Write(message);
return true; return true;
} }
/// <summary> /// <summary>
/// Gets the output stream for the specified log level /// Gets or sets the message handler for this log handler
/// </summary> /// </summary>
/// <returns>Console.Out or Console.Error</returns>
public virtual System.IO.TextWriter GetOutputStream(UInt32 streamType) {
if (streamType == STD_ERROR_HANDLE) {
return Console.Error;
}
return Console.Out;
}
public UInt32 GetStreamType(LogLevel level) {
if (level >= SimpleLog.LogLevel.Warning) {
return STD_ERROR_HANDLE;
}
return STD_OUTPUT_HANDLE;
}
public IMessageHandler MessageHandler { public IMessageHandler MessageHandler {
get { get {
return this.messageHandler; return this.messageHandler;
@ -66,5 +64,35 @@ namespace SimpleLog.LogHandlers {
} }
} }
#endregion #endregion
/// <summary>
/// Gets the output stream for the specified log level
/// </summary>
/// <returns>Console.Out or Console.Error</returns>
public System.IO.TextWriter GetOutputStream(uint streamType) {
if (streamType == ConsoleLogHandler.STD_ERROR_HANDLE) {
return Console.Error;
}
return Console.Out;
}
/// <summary>
/// Gets the output stream type (stderr or stdout) based on the
/// log level.
///
/// Override to change the stderr settings.
/// </summary>
/// <param name="level">The log level</param>
/// <returns>One of the STD_* constants</returns>
protected virtual uint GetStreamType(LogLevel level) {
if (level >= SimpleLog.LogLevel.Warning) {
return STD_ERROR_HANDLE;
}
return STD_OUTPUT_HANDLE;
}
} }
} }

View File

@ -4,6 +4,9 @@ using System.Diagnostics;
namespace SimpleLog.LogHandlers { namespace SimpleLog.LogHandlers {
/// <summary>
/// Colors for console output. Stolen from log4net.ColoredConsoleAppender (verbatim).
/// </summary>
[Flags] [Flags]
public enum ConsoleColor : int { public enum ConsoleColor : int {
Blue = 0x0001, Blue = 0x0001,
@ -16,68 +19,114 @@ namespace SimpleLog.LogHandlers {
HighIntensity = 0x0008, HighIntensity = 0x0008,
} }
/// <summary>
/// Log handler for writing log messages to a console window (e.g. cmd.exe).
///
/// The colors of the various log levels are defined in GetConsoleColor().
/// </summary>
public class ConsoleWindowLogHandler : ConsoleLogHandler { public class ConsoleWindowLogHandler : ConsoleLogHandler {
public ConsoleWindowLogHandler() : this("Log Window") { #region Constructors
/// <summary>
/// Constructs a new ConsoleWindowLogHandler with the default title
/// and width
/// </summary>
public ConsoleWindowLogHandler() : this("SimpleLog Console Window") {
} }
public ConsoleWindowLogHandler(string title) : base() { /// <summary>
ConsoleWindowLogHandler.AllocConsole(); /// Constructs a new ConsoleWindowLogHandler with the specified title
Console.Title = title; /// and default width
/// </summary>
/// <param name="title">Title of the console window</param>
public ConsoleWindowLogHandler(string title) : this(title, 100) {
} }
/// <summary>
/// Constructs a new ConsoleWindowLogHandler with a user defined
/// title and width
/// </summary>
/// <param name="title">Title of the console window</param>
/// <param name="width">Width of the console window</param>
public ConsoleWindowLogHandler(string title, int width) : base() {
ConsoleWindowLogHandler.AllocConsole();
Console.Title = title;
Console.WindowWidth = width;
}
#endregion
/// <summary>
/// Gets the console handler
/// </summary>
/// <param name="streamType">Either stderr or stdout</param>
protected IntPtr GetConsoleHandle(uint streamType) { protected IntPtr GetConsoleHandle(uint streamType) {
return ConsoleWindowLogHandler.GetStdHandle(streamType); return ConsoleWindowLogHandler.GetStdHandle(streamType);
} }
protected ConsoleColor GetColorInfo(LogLevel level) { /// <summary>
/// Gets the colors for the console based on the given log level.
///
/// Override this method in your own class if you want to modify the
/// colors. Ideally these would be set by some config file, but
/// that can come later.
/// </summary>
protected virtual ConsoleColor GetConsoleColors(LogLevel level) {
switch (level) { switch (level) {
case SimpleLog.LogLevel.Debug: case SimpleLog.LogLevel.Debug:
return ConsoleColor.Green; return ConsoleColor.Green;
case SimpleLog.LogLevel.Info: case SimpleLog.LogLevel.Info:
return ConsoleColor.White; return ConsoleColor.White;
case SimpleLog.LogLevel.Warning: case SimpleLog.LogLevel.Warning:
return ConsoleColor.Yellow; return ConsoleColor.Yellow | ConsoleColor.HighIntensity;
case SimpleLog.LogLevel.Error: case SimpleLog.LogLevel.Error:
return ConsoleColor.Red;
case SimpleLog.LogLevel.Critical:
return ConsoleColor.Red | ConsoleColor.HighIntensity; return ConsoleColor.Red | ConsoleColor.HighIntensity;
case SimpleLog.LogLevel.Critical:
return (ConsoleColor.White | ConsoleColor.HighIntensity) + ((int)(ConsoleColor.Red | ConsoleColor.HighIntensity) << 4);
default: default:
return ConsoleColor.White; return ConsoleColor.White;
} }
} }
#region Overrides
/// <summary>
/// Logs a message to the console window
/// </summary>
/// <param name="message">The message to log</param>
/// <param name="level">The log level of the message</param>
public override bool Log(string message, LogLevel level) { public override bool Log(string message, LogLevel level) {
uint streamType = this.GetStreamType(level); uint streamType = this.GetStreamType(level);
System.IO.TextWriter writer = this.GetOutputStream(streamType); System.IO.TextWriter writer = this.GetOutputStream(streamType);
IntPtr consoleHandle = GetConsoleHandle(streamType);
IntPtr consoleHandle = GetConsoleHandle(streamType); ushort colorInfo = (ushort)this.GetConsoleColors(level);
ushort colorInfo = (ushort)this.GetColorInfo(level);
ConsoleWindowLogHandler.SetConsoleTextAttribute(consoleHandle, colorInfo); ConsoleWindowLogHandler.SetConsoleTextAttribute(consoleHandle, colorInfo);
writer.Write(message); writer.Write(message);
return true; return true;
} }
#endregion
public new void GracefulShutDown() {
}
#region Win32 DLL imports #region Win32 DLL imports
/// <summary>
/// Allocates a console window
/// </summary>
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole(); private static extern bool AllocConsole();
/// <summary>
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] /// Gets the output pointer based on the stream type (stderr or stdout)
private static extern int GetConsoleOutputCP(); /// </summary>
/// <param name="type">One of the STD_* constants</param>
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr GetStdHandle(UInt32 type); private static extern IntPtr GetStdHandle(UInt32 type);
/// <summary>
/// Sets the color of the text to be written to the console
/// </summary>
/// <param name="consoleHandle">A console handle returned by GetStdHandle()</param>
/// <param name="attributes">Bitwise pairing of ConsoleColors</param>
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool SetConsoleTextAttribute(IntPtr consoleHandle, ushort attributes); private static extern bool SetConsoleTextAttribute(IntPtr consoleHandle, ushort attributes);
#endregion #endregion