From 3ae9bb6d83540ef5acf3cbe4969d0f9c01164d21 Mon Sep 17 00:00:00 2001 From: Melanie Thielker Date: Wed, 20 May 2009 14:40:50 +0000 Subject: [PATCH] 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. --- OpenSim/Framework/Console/ConsoleBase.cs | 7 +- OpenSim/Framework/Console/LocalConsole.cs | 86 +++++++++++++++++++- OpenSim/Framework/Console/OpenSimAppender.cs | 82 ++----------------- OpenSim/Framework/Console/RemoteConsole.cs | 3 +- 4 files changed, 101 insertions(+), 77 deletions(-) diff --git a/OpenSim/Framework/Console/ConsoleBase.cs b/OpenSim/Framework/Console/ConsoleBase.cs index 0747ae58c4..6e008883a2 100644 --- a/OpenSim/Framework/Console/ConsoleBase.cs +++ b/OpenSim/Framework/Console/ConsoleBase.cs @@ -66,9 +66,14 @@ namespace OpenSim.Framework.Console { } + public virtual void Output(string text, string level) + { + Output(text); + } + public virtual void Output(string text) { - System.Console.WriteLine(text); + System.Console.Write(text); } public string CmdPrompt(string p) diff --git a/OpenSim/Framework/Console/LocalConsole.cs b/OpenSim/Framework/Console/LocalConsole.cs index ca57bd60b8..4d768b98c7 100644 --- a/OpenSim/Framework/Console/LocalConsole.cs +++ b/OpenSim/Framework/Console/LocalConsole.cs @@ -30,6 +30,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using System.Text; +using System.Text.RegularExpressions; using System.Threading; using log4net; @@ -50,6 +51,28 @@ namespace OpenSim.Framework.Console private bool echo = true; private List history = new List(); + 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) { } @@ -158,13 +181,72 @@ namespace OpenSim.Framework.Console 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 = @"^(?.*?)\[(?[^\]]+)\]:?(?.*)"; + + 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) + { + Output(text, "normal"); + } + + public override void Output(string text, string level) { lock (cmdline) { if (y == -1) { - System.Console.WriteLine(text); + WriteLocalText(text, level); return; } @@ -180,7 +262,7 @@ namespace OpenSim.Framework.Console y = SetCursorTop(y); System.Console.CursorLeft = 0; - System.Console.WriteLine(text); + WriteLocalText(text, level); y = System.Console.CursorTop; diff --git a/OpenSim/Framework/Console/OpenSimAppender.cs b/OpenSim/Framework/Console/OpenSimAppender.cs index 6193bacb6c..400bd8319f 100644 --- a/OpenSim/Framework/Console/OpenSimAppender.cs +++ b/OpenSim/Framework/Console/OpenSimAppender.cs @@ -26,7 +26,6 @@ */ using System; -using System.Text.RegularExpressions; using log4net.Appender; using log4net.Core; @@ -45,62 +44,29 @@ namespace OpenSim.Framework.Console 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) { if (m_console != null) m_console.LockOutput(); + string loggingMessage = RenderLoggingEvent(le); + try { - string loggingMessage = RenderLoggingEvent(le); - - string regex = @"^(?.*?)\[(?[^\]]+)\]:?(?.*)"; - - Regex RE = new Regex(regex, RegexOptions.Multiline); - MatchCollection matches = RE.Matches(loggingMessage); - - // Get some direct matches $1 $4 is a - if (matches.Count == 1) + if (m_console != null) { - 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("]:"); + string level = "normal"; if (le.Level == Level.Error) - { - WriteColorText(ConsoleColor.Red, matches[0].Groups["End"].Value); - } + level = "error"; else if (le.Level == Level.Warn) - { - WriteColorText(ConsoleColor.Yellow, matches[0].Groups["End"].Value); - } - else - { - System.Console.Write(matches[0].Groups["End"].Value); - } - System.Console.WriteLine(); + level = "warn"; + + m_console.Output(loggingMessage, level); } else { - System.Console.Write(loggingMessage); + System.Console.WriteLine(loggingMessage); } } catch (Exception e) @@ -113,35 +79,5 @@ namespace OpenSim.Framework.Console 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)]; - } } } diff --git a/OpenSim/Framework/Console/RemoteConsole.cs b/OpenSim/Framework/Console/RemoteConsole.cs index ea46afd710..fdbf1f498a 100644 --- a/OpenSim/Framework/Console/RemoteConsole.cs +++ b/OpenSim/Framework/Console/RemoteConsole.cs @@ -60,8 +60,9 @@ namespace OpenSim.Framework.Console 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)