Improved Logger (#1292)

* Logger class changes only

Now compile-time checking is possible with the help of Nullable Value
types.

* Misc formatting

* Manual optimizations

PrintGuestLog
PrintGuestStackTrace
Surfaceflinger DequeueBuffer

* Reduce SendVibrationXX log level to Debug

* Add Notice log level

This level is always enabled and used to print system info, etc...
Also, rewrite LogColor to switch expression as colors are static

* Unify unhandled exception event handlers

* Print enabled LogLevels during init

* Re-add App Exit disposes in proper order

nit: switch case spacing

* Revert PrintGuestStackTrace to Info logs due to #1407

PrintGuestStackTrace is now called in some critical error handlers
so revert to old behavior as KThread isn't part of Guest.

* Batch replace Logger statements
This commit is contained in:
mageven 2020-08-04 05:02:53 +05:30 committed by GitHub
parent 60db4c3530
commit a33dc2f491
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
120 changed files with 800 additions and 809 deletions

View file

@ -532,7 +532,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 0 || configurationFileFormat.Version > ConfigurationFileFormat.CurrentVersion)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Unsupported configuration version {configurationFileFormat.Version}, loading default.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Unsupported configuration version {configurationFileFormat.Version}, loading default.");
LoadDefault();
@ -541,7 +541,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 2)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 2.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 2.");
configurationFileFormat.SystemRegion = Region.USA;
@ -550,7 +550,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 3)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 3.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 3.");
configurationFileFormat.SystemTimeZone = "UTC";
@ -559,7 +559,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 4)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 4.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 4.");
configurationFileFormat.MaxAnisotropy = -1;
@ -568,7 +568,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 5)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 5.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 5.");
configurationFileFormat.SystemTimeOffset = 0;
@ -577,7 +577,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 6)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 6.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 6.");
configurationFileFormat.ControllerConfig = new List<ControllerConfig>();
configurationFileFormat.KeyboardConfig = new List<KeyboardConfig>
@ -630,7 +630,7 @@ namespace Ryujinx.Configuration
// Only needed for version 6 configurations.
if (configurationFileFormat.Version == 6)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 7.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 7.");
for (int i = 0; i < configurationFileFormat.KeyboardConfig.Count; i++)
{
@ -643,7 +643,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 8)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 8.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 8.");
configurationFileFormat.EnablePtc = false;
@ -652,7 +652,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 9)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 9.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 9.");
configurationFileFormat.ColumnSort = new ColumnSort
{
@ -670,7 +670,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 10)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 10.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 10.");
configurationFileFormat.AudioBackend = AudioBackend.OpenAl;
@ -679,7 +679,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 11)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 11.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 11.");
configurationFileFormat.ResScale = 1;
configurationFileFormat.ResScaleCustom = 1.0f;
@ -689,7 +689,7 @@ namespace Ryujinx.Configuration
if (configurationFileFormat.Version < 12)
{
Common.Logging.Logger.PrintWarning(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 12.");
Common.Logging.Logger.Warning?.Print(LogClass.Application, $"Outdated configuration version {configurationFileFormat.Version}, migrating to version 12.");
configurationFileFormat.LoggingGraphicsDebugLevel = GraphicsDebugLevel.None;
@ -750,7 +750,7 @@ namespace Ryujinx.Configuration
{
ToFileFormat().SaveConfig(configurationFilePath);
Common.Logging.Logger.PrintWarning(LogClass.Application, "Configuration file has been updated!");
Common.Logging.Logger.Warning?.Print(LogClass.Application, "Configuration file has been updated!");
}
}

View file

@ -9,9 +9,6 @@ namespace Ryujinx.Configuration
{
public static void Initialize()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
ConfigurationState.Instance.Logger.EnableDebug.Event += ReloadEnableDebug;
ConfigurationState.Instance.Logger.EnableStub.Event += ReloadEnableStub;
ConfigurationState.Instance.Logger.EnableInfo.Event += ReloadEnableInfo;
@ -88,22 +85,5 @@ namespace Ryujinx.Configuration
Logger.RemoveTarget("file");
}
}
private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
Logger.Shutdown();
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = e.ExceptionObject as Exception;
Logger.PrintError(LogClass.Emulation, $"Unhandled exception caught: {exception}");
if (e.IsTerminating)
{
Logger.Shutdown();
}
}
}
}

View file

@ -17,7 +17,7 @@ namespace Ryujinx.Common.Logging
sb.Clear();
sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", args.Time);
sb.Append(" | ");
sb.Append($" |{args.Level.ToString()[0]}| ");
if (args.ThreadName != null)
{
@ -31,7 +31,7 @@ namespace Ryujinx.Common.Logging
{
PropertyInfo[] props = args.Data.GetType().GetProperties();
sb.Append(' ');
sb.Append(" {");
foreach (var prop in props)
{
@ -40,28 +40,33 @@ namespace Ryujinx.Common.Logging
if (typeof(Array).IsAssignableFrom(prop.PropertyType))
{
Array enumerable = (Array)prop.GetValue(args.Data);
foreach (var item in enumerable)
Array array = (Array)prop.GetValue(args.Data);
foreach (var item in array)
{
sb.Append(item.ToString());
sb.Append(", ");
}
sb.Remove(sb.Length - 2, 2);
if (array.Length > 0)
{
sb.Remove(sb.Length - 2, 2);
}
}
else
{
sb.Append(prop.GetValue(args.Data));
}
sb.Append(" - ");
sb.Append(" ; ");
}
// We remove the final '-' from the string
// We remove the final ';' from the string
if (props.Length > 0)
{
sb.Remove(sb.Length - 3, 3);
}
sb.Append('}');
}
return sb.ToString();

View file

@ -4,12 +4,12 @@ namespace Ryujinx.Common.Logging
{
public class LogEventArgs : EventArgs
{
public LogLevel Level { get; private set; }
public TimeSpan Time { get; private set; }
public string ThreadName { get; private set; }
public readonly LogLevel Level;
public readonly TimeSpan Time;
public readonly string ThreadName;
public string Message { get; private set; }
public object Data { get; private set; }
public readonly string Message;
public readonly object Data;
public LogEventArgs(LogLevel level, TimeSpan time, string threadName, string message)
{

View file

@ -8,6 +8,7 @@ namespace Ryujinx.Common.Logging
Warning,
Error,
Guest,
AccessLog
AccessLog,
Notice
}
}

View file

@ -8,27 +8,94 @@ namespace Ryujinx.Common.Logging
{
public static class Logger
{
private static Stopwatch m_Time;
private static readonly Stopwatch m_Time;
private static readonly bool[] m_EnabledLevels;
private static readonly bool[] m_EnabledClasses;
private static readonly List<ILogTarget> m_LogTargets;
public static event EventHandler<LogEventArgs> Updated;
public struct Log
{
internal readonly LogLevel Level;
internal Log(LogLevel level)
{
Level = level;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PrintMsg(LogClass logClass, string message)
{
if (m_EnabledClasses[(int)logClass])
{
Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, "", message)));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Print(LogClass logClass, string message, [CallerMemberName] string caller = "")
{
if (m_EnabledClasses[(int)logClass])
{
Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, message)));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Print(LogClass logClass, string message, object data, [CallerMemberName] string caller = "")
{
if (m_EnabledClasses[(int)logClass])
{
Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, message), data));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PrintStub(LogClass logClass, string message = "", [CallerMemberName] string caller = "")
{
if (m_EnabledClasses[(int)logClass])
{
Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, "Stubbed. " + message)));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PrintStub(LogClass logClass, object data, [CallerMemberName] string caller = "")
{
if (m_EnabledClasses[(int)logClass])
{
Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, "Stubbed."), data));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PrintStub(LogClass logClass, string message, object data, [CallerMemberName] string caller = "")
{
if (m_EnabledClasses[(int)logClass])
{
Updated?.Invoke(null, new LogEventArgs(Level, m_Time.Elapsed, Thread.CurrentThread.Name, FormatMessage(logClass, caller, "Stubbed. " + message), data));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string FormatMessage(LogClass Class, string Caller, string Message) => $"{Class} {Caller}: {Message}";
}
public static Log? Debug { get; private set; }
public static Log? Info { get; private set; }
public static Log? Warning { get; private set; }
public static Log? Error { get; private set; }
public static Log? Guest { get; private set; }
public static Log? AccessLog { get; private set; }
public static Log? Stub { get; private set; }
public static Log Notice { get; } // Always enabled
static Logger()
{
m_EnabledLevels = new bool[Enum.GetNames(typeof(LogLevel)).Length];
m_EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length];
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;
for (int index = 0; index < m_EnabledClasses.Length; index++)
{
m_EnabledClasses[index] = true;
@ -43,6 +110,8 @@ namespace Ryujinx.Common.Logging
new ConsoleLogTarget("console"),
1000,
AsyncLogTargetOverflowAction.Discard));
Notice = new Log(LogLevel.Notice);
}
public static void RestartTime()
@ -88,7 +157,7 @@ namespace Ryujinx.Common.Logging
{
Updated = null;
foreach(var target in m_LogTargets)
foreach (var target in m_LogTargets)
{
target.Dispose();
}
@ -96,80 +165,39 @@ namespace Ryujinx.Common.Logging
m_LogTargets.Clear();
}
public static IReadOnlyCollection<LogLevel> GetEnabledLevels()
{
var logs = new Log?[] { Debug, Info, Warning, Error, Guest, AccessLog, Stub };
List<LogLevel> levels = new List<LogLevel>(logs.Length);
foreach (var log in logs)
{
if (log.HasValue)
{
levels.Add(log.Value.Level);
}
}
return levels;
}
public static void SetEnable(LogLevel logLevel, bool enabled)
{
m_EnabledLevels[(int)logLevel] = enabled;
switch (logLevel)
{
case LogLevel.Debug : Debug = enabled ? new Log(LogLevel.Debug) : new Log?(); break;
case LogLevel.Info : Info = enabled ? new Log(LogLevel.Info) : new Log?(); break;
case LogLevel.Warning : Warning = enabled ? new Log(LogLevel.Warning) : new Log?(); break;
case LogLevel.Error : Error = enabled ? new Log(LogLevel.Error) : new Log?(); break;
case LogLevel.Guest : Guest = enabled ? new Log(LogLevel.Guest) : new Log?(); break;
case LogLevel.AccessLog : AccessLog = enabled ? new Log(LogLevel.AccessLog): new Log?(); break;
case LogLevel.Stub : Stub = enabled ? new Log(LogLevel.Stub) : new Log?(); break;
default: throw new ArgumentException("Unknown Log Level");
}
}
public static void SetEnable(LogClass logClass, bool enabled)
{
m_EnabledClasses[(int)logClass] = enabled;
}
public static void PrintDebug(LogClass logClass, string message, [CallerMemberName] string caller = "")
{
Print(LogLevel.Debug, logClass, GetFormattedMessage(logClass, message, caller));
}
public static void PrintInfo(LogClass logClass, string message, [CallerMemberName] string Caller = "")
{
Print(LogLevel.Info, logClass, GetFormattedMessage(logClass, message, Caller));
}
public static void PrintWarning(LogClass logClass, string message, [CallerMemberName] string Caller = "")
{
Print(LogLevel.Warning, logClass, GetFormattedMessage(logClass, message, Caller));
}
public static void PrintError(LogClass logClass, string message, [CallerMemberName] string Caller = "")
{
Print(LogLevel.Error, logClass, GetFormattedMessage(logClass, message, Caller));
}
public static void PrintStub(LogClass logClass, string message = "", [CallerMemberName] string caller = "")
{
Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller));
}
public static void PrintStub<T>(LogClass logClass, T obj, [CallerMemberName] string caller = "")
{
Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed.", caller), obj);
}
public static void PrintStub<T>(LogClass logClass, string message, T obj, [CallerMemberName] string caller = "")
{
Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller), obj);
}
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);
}
private static void Print(LogLevel logLevel, LogClass logClass, string message)
{
if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass])
{
Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.Name, message));
}
}
private static void Print(LogLevel logLevel, LogClass logClass, string message, object data)
{
if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass])
{
Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.Name, message, data));
}
}
private static string GetFormattedMessage(LogClass Class, string Message, string Caller)
{
return $"{Class} {Caller}: {Message}";
}
}
}

View file

@ -5,23 +5,20 @@ namespace Ryujinx.Common.Logging
{
public class ConsoleLogTarget : ILogTarget
{
private static readonly ConcurrentDictionary<LogLevel, ConsoleColor> _logColors;
private readonly ILogFormatter _formatter;
private readonly string _name;
string ILogTarget.Name { get => _name; }
static ConsoleLogTarget()
{
_logColors = new ConcurrentDictionary<LogLevel, ConsoleColor> {
[ LogLevel.Stub ] = ConsoleColor.DarkGray,
[ LogLevel.Info ] = ConsoleColor.White,
[ LogLevel.Warning ] = ConsoleColor.Yellow,
[ LogLevel.Error ] = ConsoleColor.Red
};
}
private static ConsoleColor GetLogColor(LogLevel level) => level switch {
LogLevel.Info => ConsoleColor.White,
LogLevel.Warning => ConsoleColor.Yellow,
LogLevel.Error => ConsoleColor.Red,
LogLevel.Stub => ConsoleColor.DarkGray,
LogLevel.Notice => ConsoleColor.Cyan,
_ => ConsoleColor.Gray,
};
public ConsoleLogTarget(string name)
{
@ -31,18 +28,9 @@ namespace Ryujinx.Common.Logging
public void Log(object sender, LogEventArgs args)
{
if (_logColors.TryGetValue(args.Level, out ConsoleColor color))
{
Console.ForegroundColor = color;
Console.WriteLine(_formatter.Format(args));
Console.ResetColor();
}
else
{
Console.WriteLine(_formatter.Format(args));
}
Console.ForegroundColor = GetLogColor(args.Level);
Console.WriteLine(_formatter.Format(args));
Console.ResetColor();
}
public void Dispose()

View file

@ -75,7 +75,7 @@ namespace Ryujinx.Common.SystemInfo
}
else
{
Logger.PrintError(LogClass.Application, $"Cannot get memory size, sysctlbyname error: {res}");
Logger.Error?.Print(LogClass.Application, $"Cannot get memory size, sysctlbyname error: {res}");
RamSize = 0;
}
@ -88,7 +88,7 @@ namespace Ryujinx.Common.SystemInfo
}
else
{
Logger.PrintError(LogClass.Application, $"Cannot get CPU name, sysctlbyname error: {res}");
Logger.Error?.Print(LogClass.Application, $"Cannot get CPU name, sysctlbyname error: {res}");
CpuName = "Unknown";
}

View file

@ -37,7 +37,7 @@ namespace Ryujinx.Common.SystemInfo
if (wmiNotAvailable)
{
Logger.PrintError(LogClass.Application, "WMI isn't available, system informations will use default values.");
Logger.Error?.Print(LogClass.Application, "WMI isn't available, system informations will use default values.");
CpuName = "Unknown";
}