From b3a4662be15bd63d5b70cf4be21d79959e11ccfc Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Wed, 17 Oct 2018 14:15:50 -0300 Subject: Move logging to Ryujinx.Common and make it a static class (#413) --- Ryujinx.Common/Logging/LogClass.cs | 44 ++++++++++++++++++ Ryujinx.Common/Logging/LogEventArgs.cs | 19 ++++++++ Ryujinx.Common/Logging/LogLevel.cs | 11 +++++ Ryujinx.Common/Logging/Logger.cs | 84 ++++++++++++++++++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 Ryujinx.Common/Logging/LogClass.cs create mode 100644 Ryujinx.Common/Logging/LogEventArgs.cs create mode 100644 Ryujinx.Common/Logging/LogLevel.cs create mode 100644 Ryujinx.Common/Logging/Logger.cs (limited to 'Ryujinx.Common/Logging') diff --git a/Ryujinx.Common/Logging/LogClass.cs b/Ryujinx.Common/Logging/LogClass.cs new file mode 100644 index 00000000..97ffb314 --- /dev/null +++ b/Ryujinx.Common/Logging/LogClass.cs @@ -0,0 +1,44 @@ +namespace Ryujinx.Common.Logging +{ + public enum LogClass + { + Audio, + Cpu, + Font, + Gpu, + Hid, + Kernel, + KernelIpc, + KernelScheduler, + KernelSvc, + Loader, + Service, + ServiceAcc, + ServiceAm, + ServiceApm, + ServiceAudio, + ServiceBsd, + ServiceCaps, + ServiceFriend, + ServiceFs, + ServiceHid, + ServiceIrs, + ServiceLdr, + ServiceLm, + ServiceMm, + ServiceNfp, + ServiceNifm, + ServiceNs, + ServiceNv, + ServicePctl, + ServicePl, + ServicePrepo, + ServiceSet, + ServiceSfdnsres, + ServiceSm, + ServiceSsl, + ServiceSss, + ServiceTime, + ServiceVi + } +} diff --git a/Ryujinx.Common/Logging/LogEventArgs.cs b/Ryujinx.Common/Logging/LogEventArgs.cs new file mode 100644 index 00000000..7a479b71 --- /dev/null +++ b/Ryujinx.Common/Logging/LogEventArgs.cs @@ -0,0 +1,19 @@ +using System; + +namespace Ryujinx.Common.Logging +{ + public class LogEventArgs : EventArgs + { + public LogLevel Level { get; private set; } + public TimeSpan Time { get; private set; } + + public string Message { get; private set; } + + public LogEventArgs(LogLevel Level, TimeSpan Time, string Message) + { + this.Level = Level; + this.Time = Time; + this.Message = Message; + } + } +} \ No newline at end of file diff --git a/Ryujinx.Common/Logging/LogLevel.cs b/Ryujinx.Common/Logging/LogLevel.cs new file mode 100644 index 00000000..ba3fa99f --- /dev/null +++ b/Ryujinx.Common/Logging/LogLevel.cs @@ -0,0 +1,11 @@ +namespace Ryujinx.Common.Logging +{ + public enum LogLevel + { + Debug, + Stub, + Info, + Warning, + Error + } +} diff --git a/Ryujinx.Common/Logging/Logger.cs b/Ryujinx.Common/Logging/Logger.cs new file mode 100644 index 00000000..6422f113 --- /dev/null +++ b/Ryujinx.Common/Logging/Logger.cs @@ -0,0 +1,84 @@ +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; + +namespace Ryujinx.Common.Logging +{ + public static class Logger + { + private static bool[] EnabledLevels; + private static bool[] EnabledClasses; + + public static event EventHandler Updated; + + private static Stopwatch Time; + + static Logger() + { + EnabledLevels = new bool[Enum.GetNames(typeof(LogLevel)).Length]; + EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length]; + + EnabledLevels[(int)LogLevel.Stub] = true; + EnabledLevels[(int)LogLevel.Info] = true; + EnabledLevels[(int)LogLevel.Warning] = true; + EnabledLevels[(int)LogLevel.Error] = true; + + for (int Index = 0; Index < EnabledClasses.Length; Index++) + { + EnabledClasses[Index] = true; + } + + Time = new Stopwatch(); + + Time.Start(); + } + + public static void SetEnable(LogLevel Level, bool Enabled) + { + EnabledLevels[(int)Level] = Enabled; + } + + public static void SetEnable(LogClass Class, bool Enabled) + { + EnabledClasses[(int)Class] = Enabled; + } + + public static void PrintDebug(LogClass Class, string Message, [CallerMemberName] string Caller = "") + { + Print(LogLevel.Debug, Class, GetFormattedMessage(Class, Message, Caller)); + } + + public static void PrintStub(LogClass Class, string Message, [CallerMemberName] string Caller = "") + { + Print(LogLevel.Stub, Class, GetFormattedMessage(Class, Message, Caller)); + } + + public static void PrintInfo(LogClass Class, string Message, [CallerMemberName] string Caller = "") + { + Print(LogLevel.Info, Class, GetFormattedMessage(Class, Message, Caller)); + } + + public static void PrintWarning(LogClass Class, string Message, [CallerMemberName] string Caller = "") + { + Print(LogLevel.Warning, Class, GetFormattedMessage(Class, Message, Caller)); + } + + public static void PrintError(LogClass Class, string Message, [CallerMemberName] string Caller = "") + { + Print(LogLevel.Error, Class, GetFormattedMessage(Class, Message, Caller)); + } + + private static void Print(LogLevel Level, LogClass Class, string Message) + { + if (EnabledLevels[(int)Level] && EnabledClasses[(int)Class]) + { + Updated?.Invoke(null, new LogEventArgs(Level, Time.Elapsed, Message)); + } + } + + private static string GetFormattedMessage(LogClass Class, string Message, string Caller) + { + return $"{Class} {Caller}: {Message}"; + } + } +} \ No newline at end of file -- cgit v1.2.3