From a33dc2f4919f7fdc8ea9db41c4c70c38cedfd3df Mon Sep 17 00:00:00 2001 From: mageven <62494521+mageven@users.noreply.github.com> Date: Tue, 4 Aug 2020 05:02:53 +0530 Subject: 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 --- .../Logging/Formatters/DefaultLogFormatter.cs | 19 ++- Ryujinx.Common/Logging/LogEventArgs.cs | 10 +- Ryujinx.Common/Logging/LogLevel.cs | 3 +- Ryujinx.Common/Logging/Logger.cs | 176 ++++++++++++--------- Ryujinx.Common/Logging/Targets/ConsoleLogTarget.cs | 34 ++-- 5 files changed, 132 insertions(+), 110 deletions(-) (limited to 'Ryujinx.Common/Logging') diff --git a/Ryujinx.Common/Logging/Formatters/DefaultLogFormatter.cs b/Ryujinx.Common/Logging/Formatters/DefaultLogFormatter.cs index 73b0e2b6..b9a08323 100644 --- a/Ryujinx.Common/Logging/Formatters/DefaultLogFormatter.cs +++ b/Ryujinx.Common/Logging/Formatters/DefaultLogFormatter.cs @@ -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(); diff --git a/Ryujinx.Common/Logging/LogEventArgs.cs b/Ryujinx.Common/Logging/LogEventArgs.cs index af334632..511c8e6e 100644 --- a/Ryujinx.Common/Logging/LogEventArgs.cs +++ b/Ryujinx.Common/Logging/LogEventArgs.cs @@ -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) { diff --git a/Ryujinx.Common/Logging/LogLevel.cs b/Ryujinx.Common/Logging/LogLevel.cs index 5f80714f..b1c2d23b 100644 --- a/Ryujinx.Common/Logging/LogLevel.cs +++ b/Ryujinx.Common/Logging/LogLevel.cs @@ -8,6 +8,7 @@ namespace Ryujinx.Common.Logging Warning, Error, Guest, - AccessLog + AccessLog, + Notice } } diff --git a/Ryujinx.Common/Logging/Logger.cs b/Ryujinx.Common/Logging/Logger.cs index f650582d..9246368c 100644 --- a/Ryujinx.Common/Logging/Logger.cs +++ b/Ryujinx.Common/Logging/Logger.cs @@ -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 m_LogTargets; public static event EventHandler 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 void SetEnable(LogLevel logLevel, bool enabled) - { - m_EnabledLevels[(int)logLevel] = enabled; - } - - 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 = "") + public static IReadOnlyCollection GetEnabledLevels() { - 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(LogClass logClass, T obj, [CallerMemberName] string caller = "") - { - Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed.", caller), obj); - } - - public static void PrintStub(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]) + var logs = new Log?[] { Debug, Info, Warning, Error, Guest, AccessLog, Stub }; + List levels = new List(logs.Length); + foreach (var log in logs) { - Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.Name, message)); + if (log.HasValue) + { + levels.Add(log.Value.Level); + } } + + return levels; } - private static void Print(LogLevel logLevel, LogClass logClass, string message, object data) + public static void SetEnable(LogLevel logLevel, bool enabled) { - if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass]) + switch (logLevel) { - Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.Name, message, data)); + 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"); } } - private static string GetFormattedMessage(LogClass Class, string Message, string Caller) + public static void SetEnable(LogClass logClass, bool enabled) { - return $"{Class} {Caller}: {Message}"; + m_EnabledClasses[(int)logClass] = enabled; } } } diff --git a/Ryujinx.Common/Logging/Targets/ConsoleLogTarget.cs b/Ryujinx.Common/Logging/Targets/ConsoleLogTarget.cs index ff5c6f5a..15f0e153 100644 --- a/Ryujinx.Common/Logging/Targets/ConsoleLogTarget.cs +++ b/Ryujinx.Common/Logging/Targets/ConsoleLogTarget.cs @@ -5,23 +5,20 @@ namespace Ryujinx.Common.Logging { public class ConsoleLogTarget : ILogTarget { - private static readonly ConcurrentDictionary _logColors; - private readonly ILogFormatter _formatter; private readonly string _name; string ILogTarget.Name { get => _name; } - static ConsoleLogTarget() - { - _logColors = new ConcurrentDictionary { - [ 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() -- cgit v1.2.3