Ryujinx/Ryujinx.Common/Logging/Logger.cs

84 lines
2.8 KiB
C#
Raw Normal View History

2018-04-24 11:57:39 -07:00
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Ryujinx.Common.Logging
2018-04-24 11:57:39 -07:00
{
public static class Logger
2018-04-24 11:57:39 -07:00
{
private static bool[] EnabledLevels;
private static bool[] EnabledClasses;
2018-04-24 11:57:39 -07:00
public static event EventHandler<LogEventArgs> Updated;
2018-04-24 11:57:39 -07:00
private static Stopwatch Time;
2018-04-24 11:57:39 -07:00
static Logger()
2018-04-24 11:57:39 -07:00
{
EnabledLevels = new bool[Enum.GetNames(typeof(LogLevel)).Length];
EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length];
EnabledLevels[(int)LogLevel.Stub] = true;
EnabledLevels[(int)LogLevel.Info] = true;
EnabledLevels[(int)LogLevel.Warning] = true;
EnabledLevels[(int)LogLevel.Error] = true;
for (int Index = 0; Index < EnabledClasses.Length; Index++)
{
EnabledClasses[Index] = true;
}
Time = new Stopwatch();
Time.Start();
}
public static void SetEnable(LogLevel Level, bool Enabled)
2018-04-24 11:57:39 -07:00
{
EnabledLevels[(int)Level] = Enabled;
}
public static void SetEnable(LogClass Class, bool Enabled)
2018-04-24 11:57:39 -07:00
{
EnabledClasses[(int)Class] = Enabled;
}
public static void PrintDebug(LogClass Class, string Message, [CallerMemberName] string Caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Debug, Class, GetFormattedMessage(Class, Message, Caller));
}
public static void PrintStub(LogClass Class, string Message, [CallerMemberName] string Caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Stub, Class, GetFormattedMessage(Class, Message, Caller));
}
public static void PrintInfo(LogClass Class, string Message, [CallerMemberName] string Caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Info, Class, GetFormattedMessage(Class, Message, Caller));
}
public static void PrintWarning(LogClass Class, string Message, [CallerMemberName] string Caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Warning, Class, GetFormattedMessage(Class, Message, Caller));
}
public static void PrintError(LogClass Class, string Message, [CallerMemberName] string Caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Error, Class, GetFormattedMessage(Class, Message, Caller));
}
private static void Print(LogLevel Level, LogClass Class, string Message)
2018-04-24 11:57:39 -07:00
{
if (EnabledLevels[(int)Level] && EnabledClasses[(int)Class])
{
Updated?.Invoke(null, new LogEventArgs(Level, Time.Elapsed, Message));
2018-04-24 11:57:39 -07:00
}
}
private static string GetFormattedMessage(LogClass Class, string Message, string Caller)
2018-04-24 11:57:39 -07:00
{
return $"{Class} {Caller}: {Message}";
}
}
}