Move the color console logic from the appender into the local console, since

that is the only one that can use it. Change appender output to always go
through the console output functions.
0.6.6-post-fixes
Melanie Thielker 2009-05-20 14:40:50 +00:00
parent 4065ebff15
commit 3ae9bb6d83
4 changed files with 101 additions and 77 deletions

View File

@ -66,9 +66,14 @@ namespace OpenSim.Framework.Console
{ {
} }
public virtual void Output(string text, string level)
{
Output(text);
}
public virtual void Output(string text) public virtual void Output(string text)
{ {
System.Console.WriteLine(text); System.Console.Write(text);
} }
public string CmdPrompt(string p) public string CmdPrompt(string p)

View File

@ -30,6 +30,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using log4net; using log4net;
@ -50,6 +51,28 @@ namespace OpenSim.Framework.Console
private bool echo = true; private bool echo = true;
private List<string> history = new List<string>(); private List<string> history = new List<string>();
private static readonly ConsoleColor[] Colors = {
// the dark colors don't seem to be visible on some black background terminals like putty :(
//ConsoleColor.DarkBlue,
//ConsoleColor.DarkGreen,
//ConsoleColor.DarkCyan,
//ConsoleColor.DarkMagenta,
//ConsoleColor.DarkYellow,
ConsoleColor.Gray,
//ConsoleColor.DarkGray,
ConsoleColor.Blue,
ConsoleColor.Green,
ConsoleColor.Cyan,
ConsoleColor.Magenta,
ConsoleColor.Yellow
};
private static ConsoleColor DeriveColor(string input)
{
// it is important to do Abs, hash values can be negative
return Colors[(Math.Abs(input.ToUpper().GetHashCode()) % Colors.Length)];
}
public LocalConsole(string defaultPrompt) : base(defaultPrompt) public LocalConsole(string defaultPrompt) : base(defaultPrompt)
{ {
} }
@ -158,13 +181,72 @@ namespace OpenSim.Framework.Console
Monitor.Exit(cmdline); Monitor.Exit(cmdline);
} }
private void WriteColorText(ConsoleColor color, string sender)
{
try
{
lock (this)
{
try
{
System.Console.ForegroundColor = color;
System.Console.Write(sender);
System.Console.ResetColor();
}
catch (ArgumentNullException)
{
// Some older systems dont support coloured text.
System.Console.WriteLine(sender);
}
}
}
catch (ObjectDisposedException)
{
}
}
private void WriteLocalText(string text, string level)
{
string regex = @"^(?<Front>.*?)\[(?<Category>[^\]]+)\]:?(?<End>.*)";
Regex RE = new Regex(regex, RegexOptions.Multiline);
MatchCollection matches = RE.Matches(text);
string outText = text;
ConsoleColor color = ConsoleColor.White;
if (matches.Count == 1)
{
outText = matches[0].Groups["End"].Value;
System.Console.Write(matches[0].Groups["Front"].Value);
System.Console.Write("[");
WriteColorText(DeriveColor(matches[0].Groups["Category"].Value),
matches[0].Groups["Category"].Value);
System.Console.Write("]:");
}
if (level == "error")
color = ConsoleColor.Red;
else if (level == "warn")
color = ConsoleColor.Yellow;
WriteColorText(color, outText);
System.Console.WriteLine();
}
public override void Output(string text) public override void Output(string text)
{
Output(text, "normal");
}
public override void Output(string text, string level)
{ {
lock (cmdline) lock (cmdline)
{ {
if (y == -1) if (y == -1)
{ {
System.Console.WriteLine(text); WriteLocalText(text, level);
return; return;
} }
@ -180,7 +262,7 @@ namespace OpenSim.Framework.Console
y = SetCursorTop(y); y = SetCursorTop(y);
System.Console.CursorLeft = 0; System.Console.CursorLeft = 0;
System.Console.WriteLine(text); WriteLocalText(text, level);
y = System.Console.CursorTop; y = System.Console.CursorTop;

View File

@ -26,7 +26,6 @@
*/ */
using System; using System;
using System.Text.RegularExpressions;
using log4net.Appender; using log4net.Appender;
using log4net.Core; using log4net.Core;
@ -45,62 +44,29 @@ namespace OpenSim.Framework.Console
set { m_console = value; } set { m_console = value; }
} }
private static readonly ConsoleColor[] Colors = {
// the dark colors don't seem to be visible on some black background terminals like putty :(
//ConsoleColor.DarkBlue,
//ConsoleColor.DarkGreen,
//ConsoleColor.DarkCyan,
//ConsoleColor.DarkMagenta,
//ConsoleColor.DarkYellow,
ConsoleColor.Gray,
//ConsoleColor.DarkGray,
ConsoleColor.Blue,
ConsoleColor.Green,
ConsoleColor.Cyan,
ConsoleColor.Magenta,
ConsoleColor.Yellow
};
override protected void Append(LoggingEvent le) override protected void Append(LoggingEvent le)
{ {
if (m_console != null) if (m_console != null)
m_console.LockOutput(); m_console.LockOutput();
string loggingMessage = RenderLoggingEvent(le);
try try
{ {
string loggingMessage = RenderLoggingEvent(le); if (m_console != null)
string regex = @"^(?<Front>.*?)\[(?<Category>[^\]]+)\]:?(?<End>.*)";
Regex RE = new Regex(regex, RegexOptions.Multiline);
MatchCollection matches = RE.Matches(loggingMessage);
// Get some direct matches $1 $4 is a
if (matches.Count == 1)
{ {
System.Console.Write(matches[0].Groups["Front"].Value); string level = "normal";
System.Console.Write("[");
WriteColorText(DeriveColor(matches[0].Groups["Category"].Value), matches[0].Groups["Category"].Value);
System.Console.Write("]:");
if (le.Level == Level.Error) if (le.Level == Level.Error)
{ level = "error";
WriteColorText(ConsoleColor.Red, matches[0].Groups["End"].Value);
}
else if (le.Level == Level.Warn) else if (le.Level == Level.Warn)
{ level = "warn";
WriteColorText(ConsoleColor.Yellow, matches[0].Groups["End"].Value);
} m_console.Output(loggingMessage, level);
else
{
System.Console.Write(matches[0].Groups["End"].Value);
}
System.Console.WriteLine();
} }
else else
{ {
System.Console.Write(loggingMessage); System.Console.WriteLine(loggingMessage);
} }
} }
catch (Exception e) catch (Exception e)
@ -113,35 +79,5 @@ namespace OpenSim.Framework.Console
m_console.UnlockOutput(); m_console.UnlockOutput();
} }
} }
private void WriteColorText(ConsoleColor color, string sender)
{
try
{
lock (this)
{
try
{
System.Console.ForegroundColor = color;
System.Console.Write(sender);
System.Console.ResetColor();
}
catch (ArgumentNullException)
{
// Some older systems dont support coloured text.
System.Console.WriteLine(sender);
}
}
}
catch (ObjectDisposedException)
{
}
}
private static ConsoleColor DeriveColor(string input)
{
// it is important to do Abs, hash values can be negative
return Colors[(Math.Abs(input.ToUpper().GetHashCode()) % Colors.Length)];
}
} }
} }

View File

@ -60,8 +60,9 @@ namespace OpenSim.Framework.Console
m_Server = server; m_Server = server;
} }
public override void Output(string text) public override void Output(string text, string level)
{ {
System.Console.Write(text);
} }
public override string ReadLine(string p, bool isCommand, bool e) public override string ReadLine(string p, bool isCommand, bool e)