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;
namespace SimpleLog.LogHandlers {
/// <summary>
/// Log handler for console output (e.g. use this instead of Console.WriteLine()).
/// </summary>
public class ConsoleLogHandler : ILogHandler {
protected IMessageHandler messageHandler;
protected LogLevel? logLevel;
//stolen from log4net
public const UInt32 STD_OUTPUT_HANDLE = unchecked((UInt32)(-11));
public const UInt32 STD_ERROR_HANDLE = unchecked((UInt32)(-12));
/// <summary>
/// Constructs a new ConsoleLogHandler with a default
/// message handler
/// </summary>
public ConsoleLogHandler() {
this.messageHandler = new DefaultMessageHandler();
this.logLevel = null;
@ -16,9 +25,12 @@ namespace SimpleLog.LogHandlers {
#region ILogHandler Members
public virtual void GracefulShutDown() {
throw new NotImplementedException();
//does nothing
}
/// <summary>
/// Gets or sets the log level for this handler
/// </summary>
public LogLevel? LogLevel {
get {
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) {
UInt32 streamType = this.GetStreamType(level);
System.IO.TextWriter outputStream = (System.IO.TextWriter)this.GetOutputStream(streamType);
outputStream.Write(message);
uint streamType = this.GetStreamType(level);
System.IO.TextWriter writer = (System.IO.TextWriter)this.GetOutputStream(streamType);
writer.Write(message);
return true;
}
/// <summary>
/// Gets the output stream for the specified log level
/// Gets or sets the message handler for this log handler
/// </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 {
get {
return this.messageHandler;
@ -66,5 +64,35 @@ namespace SimpleLog.LogHandlers {
}
}
#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 {
/// <summary>
/// Colors for console output. Stolen from log4net.ColoredConsoleAppender (verbatim).
/// </summary>
[Flags]
public enum ConsoleColor : int {
Blue = 0x0001,
@ -16,68 +19,114 @@ namespace SimpleLog.LogHandlers {
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 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>
/// Constructs a new ConsoleWindowLogHandler with the specified 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) {
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) {
case SimpleLog.LogLevel.Debug:
return ConsoleColor.Green;
case SimpleLog.LogLevel.Info:
return ConsoleColor.White;
case SimpleLog.LogLevel.Warning:
return ConsoleColor.Yellow;
return ConsoleColor.Yellow | ConsoleColor.HighIntensity;
case SimpleLog.LogLevel.Error:
return ConsoleColor.Red;
case SimpleLog.LogLevel.Critical:
return ConsoleColor.Red | ConsoleColor.HighIntensity;
case SimpleLog.LogLevel.Critical:
return (ConsoleColor.White | ConsoleColor.HighIntensity) + ((int)(ConsoleColor.Red | ConsoleColor.HighIntensity) << 4);
default:
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) {
uint streamType = this.GetStreamType(level);
uint streamType = this.GetStreamType(level);
System.IO.TextWriter writer = this.GetOutputStream(streamType);
IntPtr consoleHandle = GetConsoleHandle(streamType);
IntPtr consoleHandle = GetConsoleHandle(streamType);
ushort colorInfo = (ushort)this.GetColorInfo(level);
ushort colorInfo = (ushort)this.GetConsoleColors(level);
ConsoleWindowLogHandler.SetConsoleTextAttribute(consoleHandle, colorInfo);
writer.Write(message);
return true;
}
public new void GracefulShutDown() {
}
#endregion
#region Win32 DLL imports
/// <summary>
/// Allocates a console window
/// </summary>
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole();
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetConsoleOutputCP();
/// <summary>
/// Gets the output pointer based on the stream type (stderr or stdout)
/// </summary>
/// <param name="type">One of the STD_* constants</param>
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
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)]
private static extern bool SetConsoleTextAttribute(IntPtr consoleHandle, ushort attributes);
#endregion