2018-04-24 20:57:39 +02:00
|
|
|
using System;
|
2019-02-11 13:00:32 +01:00
|
|
|
using System.Collections.Generic;
|
2018-04-24 20:57:39 +02:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Runtime.CompilerServices;
|
2019-01-11 01:11:46 +01:00
|
|
|
using System.Threading;
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2018-10-17 19:15:50 +02:00
|
|
|
namespace Ryujinx.Common.Logging
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2018-10-17 19:15:50 +02:00
|
|
|
public static class Logger
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
private static Stopwatch m_Time;
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
private static readonly bool[] m_EnabledLevels;
|
|
|
|
private static readonly bool[] m_EnabledClasses;
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2019-02-11 13:00:32 +01:00
|
|
|
private static readonly List<ILogTarget> m_LogTargets;
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2019-02-11 13:00:32 +01:00
|
|
|
public static event EventHandler<LogEventArgs> Updated;
|
2019-01-31 03:49:15 +01:00
|
|
|
|
2018-10-17 19:15:50 +02:00
|
|
|
static Logger()
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
m_EnabledLevels = new bool[Enum.GetNames(typeof(LogLevel)).Length];
|
|
|
|
m_EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length];
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2019-06-16 03:31:18 +02: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;
|
|
|
|
m_EnabledLevels[(int)LogLevel.Guest] = true;
|
|
|
|
m_EnabledLevels[(int)LogLevel.AccessLog] = true;
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
for (int index = 0; index < m_EnabledClasses.Length; index++)
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
m_EnabledClasses[index] = true;
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 13:00:32 +01:00
|
|
|
m_LogTargets = new List<ILogTarget>();
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
m_Time = Stopwatch.StartNew();
|
2019-12-21 20:52:31 +01:00
|
|
|
|
|
|
|
// Logger should log to console by default
|
|
|
|
AddTarget(new AsyncLogTargetWrapper(
|
|
|
|
new ConsoleLogTarget("console"),
|
|
|
|
1000,
|
|
|
|
AsyncLogTargetOverflowAction.Block));
|
2019-01-11 01:11:46 +01:00
|
|
|
}
|
|
|
|
|
2019-09-20 01:59:48 +02:00
|
|
|
public static void RestartTime()
|
|
|
|
{
|
|
|
|
m_Time.Restart();
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:52:31 +01:00
|
|
|
private static ILogTarget GetTarget(string targetName)
|
|
|
|
{
|
|
|
|
foreach (var target in m_LogTargets)
|
|
|
|
{
|
|
|
|
if (target.Name.Equals(targetName))
|
|
|
|
{
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-11 13:00:32 +01:00
|
|
|
public static void AddTarget(ILogTarget target)
|
|
|
|
{
|
|
|
|
m_LogTargets.Add(target);
|
|
|
|
|
|
|
|
Updated += target.Log;
|
|
|
|
}
|
|
|
|
|
2019-12-21 20:52:31 +01:00
|
|
|
public static void RemoveTarget(string target)
|
|
|
|
{
|
|
|
|
ILogTarget logTarget = GetTarget(target);
|
|
|
|
|
|
|
|
if (logTarget != null)
|
|
|
|
{
|
|
|
|
Updated -= logTarget.Log;
|
|
|
|
|
|
|
|
m_LogTargets.Remove(logTarget);
|
|
|
|
|
|
|
|
logTarget.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 13:00:32 +01:00
|
|
|
public static void Shutdown()
|
|
|
|
{
|
|
|
|
Updated = null;
|
|
|
|
|
|
|
|
foreach(var target in m_LogTargets)
|
|
|
|
{
|
|
|
|
target.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_LogTargets.Clear();
|
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
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 20:57:39 +02:00
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static void PrintDebug(LogClass logClass, string message, [CallerMemberName] string caller = "")
|
|
|
|
{
|
|
|
|
Print(LogLevel.Debug, logClass, GetFormattedMessage(logClass, message, caller));
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static void PrintInfo(LogClass logClass, string message, [CallerMemberName] string Caller = "")
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
Print(LogLevel.Info, logClass, GetFormattedMessage(logClass, message, Caller));
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static void PrintWarning(LogClass logClass, string message, [CallerMemberName] string Caller = "")
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
Print(LogLevel.Warning, logClass, GetFormattedMessage(logClass, message, Caller));
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static void PrintError(LogClass logClass, string message, [CallerMemberName] string Caller = "")
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
Print(LogLevel.Error, logClass, GetFormattedMessage(logClass, message, Caller));
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static void PrintStub(LogClass logClass, string message = "", [CallerMemberName] string caller = "")
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller));
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static void PrintStub<T>(LogClass logClass, T obj, [CallerMemberName] string caller = "")
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed.", caller), obj);
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
public static void PrintStub<T>(LogClass logClass, string message, T obj, [CallerMemberName] string caller = "")
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller), obj);
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 03:31:18 +02:00
|
|
|
public static void PrintGuest(LogClass logClass, string message, [CallerMemberName] string caller = "")
|
|
|
|
{
|
|
|
|
Print(LogLevel.Guest, logClass, GetFormattedMessage(logClass, message, caller));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void PrintAccessLog(LogClass logClass, string message)
|
|
|
|
{
|
|
|
|
Print(LogLevel.AccessLog, logClass, message);
|
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
private static void Print(LogLevel logLevel, LogClass logClass, string message)
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass])
|
|
|
|
{
|
2020-01-13 01:21:54 +01:00
|
|
|
Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.Name, message));
|
2019-01-11 01:11:46 +01:00
|
|
|
}
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-11 01:11:46 +01:00
|
|
|
private static void Print(LogLevel logLevel, LogClass logClass, string message, object data)
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2019-01-11 01:11:46 +01:00
|
|
|
if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass])
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2020-01-13 01:21:54 +01:00
|
|
|
Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.Name, message, data));
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 19:15:50 +02:00
|
|
|
private static string GetFormattedMessage(LogClass Class, string Message, string Caller)
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
|
|
|
return $"{Class} {Caller}: {Message}";
|
|
|
|
}
|
|
|
|
}
|
2019-09-20 01:59:48 +02:00
|
|
|
}
|