Index: Log.cs =================================================================== RCS file: /cvsroot/nant/nant/src/NAnt.Core/Log.cs,v retrieving revision 1.49 diff -u -r1.49 Log.cs --- Log.cs 3 Dec 2006 13:08:02 -0000 1.49 +++ Log.cs 11 Mar 2008 07:57:04 -0000 @@ -598,6 +598,10 @@ protected virtual void Log(string message) { } + protected virtual void OutputMessageToConsole(string message, Level messageLevel) { + Console.Out.WriteLine(message); + } + #endregion Protected Instance Methods #region Private Instance Methods @@ -667,7 +671,7 @@ string indentedMessage = sb.ToString(); // output the message to the console - Console.Out.WriteLine(indentedMessage); + OutputMessageToConsole(indentedMessage, e.MessageLevel); // if an OutputWriter was set, write the message to it if (OutputWriter != null) { @@ -734,6 +738,99 @@ } } + public class ConsoleColorLogger : DefaultLogger { + private readonly MessageLevelColorMap levelColorMap = new MessageLevelColorMap(); + + public override void BuildStarted(object sender, BuildEventArgs e) + { + base.BuildStarted(sender, e); + levelColorMap.ConfigureUsing(e.Project.Properties, "ConsoleColorLogger"); + } + + protected override void OutputMessageToConsole(string message, Level messageLevel) + { + using (new ConsoleColorScope(levelColorMap[messageLevel])) + { + base.OutputMessageToConsole(message, messageLevel); + } + } + } + + public class MessageLevelColorMap + { + private readonly IDictionary map = new Hashtable(); + + public MessageLevelColorMap() + { + map[Level.Error] = ConsoleColor.Red; + map[Level.Warning] = ConsoleColor.Yellow; + map[Level.Info] = Console.ForegroundColor; + map[Level.Verbose] = ConsoleColor.DarkGray; + map[Level.Debug] = ConsoleColor.Blue; + } + + public ConsoleColor this[Level level] + { + get { return (ConsoleColor)map[level]; } + } + + /// keys and values should both be of type + /// property key prefix without the "dot" (example: "some.prefix") + public void ConfigureUsing(IDictionary properties, string propertyKeyPrefix) + { + ConfigureColorForLevel(Level.Error, "error", properties, propertyKeyPrefix); + ConfigureColorForLevel(Level.Warning, "warning", properties, propertyKeyPrefix); + ConfigureColorForLevel(Level.Info, "info", properties, propertyKeyPrefix); + ConfigureColorForLevel(Level.Debug, "debug", properties, propertyKeyPrefix); + ConfigureColorForLevel(Level.Verbose, "verbose", properties, propertyKeyPrefix); + } + + private void ConfigureColorForLevel(Level level, string propertySuffix, IDictionary properties, + string propertyKeyPrefix) + { + string propertyKey = propertyKeyPrefix + "." + propertySuffix; + if (!properties.Contains(propertyKey)) return; + + string colorName = (string)properties[propertyKey]; + map[level] = ParseConsoleColor(colorName); + } + + private static ConsoleColor ParseConsoleColor(string colorName) + { + if (colorName.Equals("default", StringComparison.CurrentCultureIgnoreCase)) + { + return Console.ForegroundColor; + } + + try + { + return (ConsoleColor)Enum.Parse(typeof(ConsoleColor), colorName, true); + } + catch (ArgumentException) + { + throw new BuildException( + string.Format("Unrecognized console color:<{0}>. Accepted colors are 'default' and those in this list:<{1}>", colorName, + "http://msdn2.microsoft.com/en-us/library/system.consolecolor.aspx")); + } + } + } + + public class ConsoleColorScope : IDisposable + { + private readonly ConsoleColor originalColor; + + public ConsoleColorScope(ConsoleColor newForegroundColor) + { + originalColor = Console.ForegroundColor; + Console.ForegroundColor = newForegroundColor; + } + + public void Dispose() + { + Console.ForegroundColor = originalColor; + } + } + /// /// Buffers log messages from DefaultLogger, and sends an e-mail with the /// results.