Commit Description:
Fix rendering of selected areas.
Commit Description:
Fix rendering of selected areas.
File last commit:
Show/Diff file:
Action:
isometric-park-fna/Logging.cs
176 lines | 4.8 KiB | text/x-csharp | CSharpLexer
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Reflection;
// using System.Linq;
/* Tiny log library inspired by Python's logging and Glögi.
*/
namespace isometricparkfna
{
public enum LogLevel {
Critical,
Error,
Warning,
Success,
Info,
Debug,
Trace,
Spy
}
public class Logging
{
// private
//
private static Dictionary<LogLevel, (ConsoleColor, ConsoleColor)> mappings = new Dictionary<LogLevel, (ConsoleColor, ConsoleColor)>
{
{LogLevel.Critical, (ConsoleColor.White, ConsoleColor.Red)},
{LogLevel.Error, (ConsoleColor.Red, ConsoleColor.Black)},
{LogLevel.Warning, (ConsoleColor.Yellow, ConsoleColor.Black)},
{LogLevel.Success, (ConsoleColor.Green, ConsoleColor.Black)},
{LogLevel.Info, (ConsoleColor.White, ConsoleColor.Black)},
{LogLevel.Debug, (ConsoleColor.White, ConsoleColor.Blue)},
{LogLevel.Trace, (ConsoleColor.White, ConsoleColor.DarkGray)},
{LogLevel.Spy, (ConsoleColor.Black, ConsoleColor.White)},
};
private static void Log_(LogLevel level, string message,
int lineNumber, string caller, string path)
{
var timestamp = DateTime.Now;
var start_foreground = Console.ForegroundColor;
var start_background = Console.BackgroundColor;
var (new_foreground, new_background) = Logging.mappings[level];
Console.ForegroundColor = new_foreground;
Console.BackgroundColor = new_background;
//29/Apr/2021 22:43:30
Console.Out.Write(string.Format("[{0}] {1}", timestamp.ToString("s"), level.ToString()));
Console.ForegroundColor = start_foreground;
Console.BackgroundColor = start_background;
Console.Out.WriteLine(string.Format(" {0} [{1}:{2}]", message, path, lineNumber));
}
public static void Log(LogLevel level, string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = "")
{
Logging.Log_(level, message, lineNumber, caller, path);
}
public static void Critical(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = "")
{
Logging.Log_(LogLevel.Critical, message, lineNumber, caller, path);
}
public static void Error(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = "")
{
Logging.Log_(LogLevel.Error, message, lineNumber, caller, path);
}
public static void Warning(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = "")
{
Logging.Log_(LogLevel.Warning, message, lineNumber, caller, path);
}
public static void Success(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = "")
{
Logging.Log_(LogLevel.Success, message, lineNumber, caller, path);
}
public static void Info(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = "")
{
Logging.Log_(LogLevel.Info, message, lineNumber, caller, path);
}
public static void Debug(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = "")
{
Logging.Log_(LogLevel.Debug, message, lineNumber, caller, path);
}
public static void Trace(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = "")
{
Logging.Log_(LogLevel.Trace, message, lineNumber, caller, path);
}
public static void Spy(object value,
string name,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = ""
)
{
string message = string.Format("{0} ({1}) = {2}",
value.GetType().ToString(), name, value.ToString());
Logging.Log_(LogLevel.Spy, message, lineNumber, caller, path);
}
public static void Spy<T>(T value,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null,
[CallerFilePath] string path = ""
) where T : class
{
// var properties = typeof(T).GetProperties();
// string message = string.Format("{0} = {1}",
// value.ToString(), .ToString() );
// var message = String.Join(", ", (object[])properties);
//
var properties = new List<string>();
foreach (var property in typeof(T).GetProperties())
{
properties.Add(property.ToString() + "=" + property.GetValue(value));
}
var message = "{" + String.Join(", ", properties) + "}";
Logging.Log_(LogLevel.Spy, message, lineNumber, caller, path);
}
}
}