Ryujinx/Ryujinx.Common/Logging/Logger.cs

105 lines
3.9 KiB
C#
Raw Normal View History

2018-04-24 11:57:39 -07:00
using System;
using System.Diagnostics;
using System.Reflection;
2018-04-24 11:57:39 -07:00
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
2018-04-24 11:57:39 -07:00
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 Stopwatch m_Time;
2018-04-24 11:57:39 -07:00
private static readonly bool[] m_EnabledLevels;
private static readonly bool[] m_EnabledClasses;
2018-04-24 11:57:39 -07:00
public static event EventHandler<LogEventArgs> Updated;
2018-04-24 11:57:39 -07:00
public static bool EnableFileLog { get; set; }
static Logger()
2018-04-24 11:57:39 -07:00
{
m_EnabledLevels = new bool[Enum.GetNames(typeof(LogLevel)).Length];
m_EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length];
2018-04-24 11:57:39 -07:00
m_EnabledLevels[(int)LogLevel.Stub] = true;
m_EnabledLevels[(int)LogLevel.Info] = true;
m_EnabledLevels[(int)LogLevel.Warning] = true;
m_EnabledLevels[(int)LogLevel.Error] = true;
2018-04-24 11:57:39 -07:00
for (int index = 0; index < m_EnabledClasses.Length; index++)
2018-04-24 11:57:39 -07:00
{
m_EnabledClasses[index] = true;
2018-04-24 11:57:39 -07:00
}
m_Time = Stopwatch.StartNew();
}
public static void SetEnable(LogLevel logLevel, bool enabled)
{
m_EnabledLevels[(int)logLevel] = enabled;
}
public static void SetEnable(LogClass logClass, bool enabled)
{
m_EnabledClasses[(int)logClass] = enabled;
}
2018-04-24 11:57:39 -07:00
public static void PrintDebug(LogClass logClass, string message, [CallerMemberName] string caller = "")
{
Print(LogLevel.Debug, logClass, GetFormattedMessage(logClass, message, caller));
2018-04-24 11:57:39 -07:00
}
public static void PrintInfo(LogClass logClass, string message, [CallerMemberName] string Caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Info, logClass, GetFormattedMessage(logClass, message, Caller));
2018-04-24 11:57:39 -07:00
}
public static void PrintWarning(LogClass logClass, string message, [CallerMemberName] string Caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Warning, logClass, GetFormattedMessage(logClass, message, Caller));
2018-04-24 11:57:39 -07:00
}
public static void PrintError(LogClass logClass, string message, [CallerMemberName] string Caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Error, logClass, GetFormattedMessage(logClass, message, Caller));
2018-04-24 11:57:39 -07:00
}
public static void PrintStub(LogClass logClass, string message = "", [CallerMemberName] string caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller));
2018-04-24 11:57:39 -07:00
}
public static void PrintStub<T>(LogClass logClass, T obj, [CallerMemberName] string caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed.", caller), obj);
2018-04-24 11:57:39 -07:00
}
public static void PrintStub<T>(LogClass logClass, string message, T obj, [CallerMemberName] string caller = "")
2018-04-24 11:57:39 -07:00
{
Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller), obj);
2018-04-24 11:57:39 -07:00
}
private static void Print(LogLevel logLevel, LogClass logClass, string message)
2018-04-24 11:57:39 -07:00
{
if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass])
{
Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.ManagedThreadId, message));
}
2018-04-24 11:57:39 -07:00
}
private static void Print(LogLevel logLevel, LogClass logClass, string message, object data)
2018-04-24 11:57:39 -07:00
{
if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass])
2018-04-24 11:57:39 -07:00
{
Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.ManagedThreadId, message, data));
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}";
}
}
}