Add color coding for console log categories

This commit is contained in:
Cuber 2015-02-07 23:16:24 +02:00
parent d92e26b3f7
commit af6da5b935
3 changed files with 51 additions and 1 deletions

View File

@ -16,7 +16,19 @@ namespace TrueCraft.Core.Logging
{
if ((EnabledCategories & category) != 0)
{
Console.WriteLine(text, parameters);
Console.Write(LogHelpers.GetTimestamp());
ConsoleColor currentColor = Console.ForegroundColor;
Console.ForegroundColor = LogHelpers.GetCategoryColor(category);
Console.Write(category.ToString());
// Better to restore original than ResetColor
Console.ForegroundColor = currentColor;
// TODO: Check Console.BufferWidth and indent wrapping text onto the same level as the end of the timestamp
// Longest LogCategory is Warning (length is 7 characters)
// The log will probably mostly contain messages belonging to the
// category Notice (6 chars). We want a pad of 4 spaces on average
// and also want the text to be aligned with the last message
// 7 + 4 = 11 is the max length of (category.ToString() + pad of 4 spaces)
Console.WriteLine(new string(' ', 11 - category.ToString().Length) + text, parameters);
}
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrueCraft.API.Logging;
namespace TrueCraft.Core.Logging
{
public static class LogHelpers
{
public static string GetTimestamp(bool utc = true, string timeFormat = "u", string suffix = " ")
{
return (utc ? DateTime.UtcNow : DateTime.Now).ToString(timeFormat) + suffix;
}
public static ConsoleColor GetCategoryColor(LogCategory category)
{
switch (category)
{
case LogCategory.Packets:
return ConsoleColor.White;
case LogCategory.Debug:
return ConsoleColor.Cyan;
case LogCategory.Warning:
return ConsoleColor.Yellow;
case LogCategory.Error:
return ConsoleColor.Red;
case LogCategory.Notice:
return ConsoleColor.Green;
case LogCategory.All:
return ConsoleColor.Magenta;
default:
return ConsoleColor.Gray;
}
}
}
}

View File

@ -35,6 +35,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Logging\LogHelpers.cs" />
<Compile Include="Logic\ItemProvider.cs" />
<Compile Include="Logic\Items\AppleItem.cs" />
<Compile Include="Logic\Items\ArmourItem.cs" />