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
---
 Ryujinx.Common/Logging/Logger.cs | 176 +++++++++++++++++++++++----------------
 1 file changed, 102 insertions(+), 74 deletions(-)

(limited to 'Ryujinx.Common/Logging/Logger.cs')

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<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 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<LogLevel> 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<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])
+            var logs = new Log?[] { Debug, Info, Warning, Error, Guest, AccessLog, Stub };
+            List<LogLevel> levels = new List<LogLevel>(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;
         }
     }
 }
-- 
cgit v1.2.3-70-g09d2