created the a log handler that logs to a console window

This commit is contained in:
tmont 2009-04-12 05:29:27 +00:00
parent 15e77c1574
commit a5e5a68ffd
4 changed files with 108 additions and 11 deletions

View File

@ -22,12 +22,8 @@ namespace SimpleLog.Tests {
[Test]
public void TestGetOutputStream() {
Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Warning), Is.SameAs(Console.Error));
Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Error), Is.SameAs(Console.Error));
Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Critical), Is.SameAs(Console.Error));
Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Debug), Is.SameAs(Console.Out));
Assert.That(ConsoleLogHandler.GetOutputStream(LogLevel.Info), Is.SameAs(Console.Out));
Assert.That(this.handler.GetOutputStream(ConsoleLogHandler.STD_ERROR_HANDLE), Is.SameAs(Console.Error));
Assert.That(this.handler.GetOutputStream(ConsoleLogHandler.STD_OUTPUT_HANDLE), Is.SameAs(Console.Out));
}
[Test]

View File

@ -6,13 +6,16 @@ namespace SimpleLog.LogHandlers {
protected IMessageHandler messageHandler;
protected LogLevel? logLevel;
public const UInt32 STD_OUTPUT_HANDLE = unchecked((UInt32)(-11));
public const UInt32 STD_ERROR_HANDLE = unchecked((UInt32)(-12));
public ConsoleLogHandler() {
this.messageHandler = new DefaultMessageHandler();
this.logLevel = null;
}
#region ILogHandler Members
public void GracefulShutDown() {
public virtual void GracefulShutDown() {
throw new NotImplementedException();
}
@ -25,8 +28,9 @@ namespace SimpleLog.LogHandlers {
}
}
public bool Log(string message, LogLevel level) {
System.IO.TextWriter outputStream = ConsoleLogHandler.GetOutputStream(level);
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);
return true;
}
@ -35,14 +39,24 @@ namespace SimpleLog.LogHandlers {
/// Gets the output stream for the specified log level
/// </summary>
/// <returns>Console.Out or Console.Error</returns>
public static System.IO.TextWriter GetOutputStream(LogLevel level) {
if (level >= SimpleLog.LogLevel.Warning) {
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;

View File

@ -0,0 +1,86 @@
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace SimpleLog.LogHandlers {
[Flags]
public enum ConsoleColor : int {
Blue = 0x0001,
Green = 0x0002,
Red = 0x0004,
White = Blue | Green | Red,
Yellow = Red | Green,
Purple = Red | Blue,
Cyan = Green | Blue,
HighIntensity = 0x0008,
}
public class ConsoleWindowLogHandler : ConsoleLogHandler {
public ConsoleWindowLogHandler() : this("Log Window") {
}
public ConsoleWindowLogHandler(string title) : base() {
ConsoleWindowLogHandler.AllocConsole();
Console.Title = title;
}
protected IntPtr GetConsoleHandle(uint streamType) {
return ConsoleWindowLogHandler.GetStdHandle(streamType);
}
protected ConsoleColor GetColorInfo(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;
case SimpleLog.LogLevel.Error:
return ConsoleColor.Red;
case SimpleLog.LogLevel.Critical:
return ConsoleColor.Red | ConsoleColor.HighIntensity;
default:
return ConsoleColor.White;
}
}
public override bool Log(string message, LogLevel level) {
uint streamType = this.GetStreamType(level);
System.IO.TextWriter writer = this.GetOutputStream(streamType);
IntPtr consoleHandle = GetConsoleHandle(streamType);
ushort colorInfo = (ushort)this.GetColorInfo(level);
ConsoleWindowLogHandler.SetConsoleTextAttribute(consoleHandle, colorInfo);
writer.Write(message);
return true;
}
public new void GracefulShutDown() {
}
#region Win32 DLL imports
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole();
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetConsoleOutputCP();
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr GetStdHandle(UInt32 type);
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool SetConsoleTextAttribute(IntPtr consoleHandle, ushort attributes);
#endregion
}
}

View File

@ -55,6 +55,7 @@
<Compile Include="ILogHandler.cs" />
<Compile Include="IMessageHandler.cs" />
<Compile Include="LogHandlers\ConsoleLogHandler.cs" />
<Compile Include="LogHandlers\ConsoleWindowLogHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util.cs" />
</ItemGroup>