From aea08270ac2d69bd139dd5ff7d6936f67d19da09 Mon Sep 17 00:00:00 2001 From: jduncanator Date: Mon, 17 Dec 2018 10:33:24 +1100 Subject: [PATCH] Process logged data objects in the logger target Pass the data object all the way to the output logger targets, to allow them to "serialize" this in whatever appropriate format they're logging in. --- Ryujinx.Common/Logging/LogEventArgs.cs | 14 ++++++-- Ryujinx.Common/Logging/Logger.cs | 46 ++++++-------------------- Ryujinx/Ui/ConsoleLog.cs | 34 ++++++++++++++++--- 3 files changed, 51 insertions(+), 43 deletions(-) diff --git a/Ryujinx.Common/Logging/LogEventArgs.cs b/Ryujinx.Common/Logging/LogEventArgs.cs index ada2c9e93f..462b27f3e1 100644 --- a/Ryujinx.Common/Logging/LogEventArgs.cs +++ b/Ryujinx.Common/Logging/LogEventArgs.cs @@ -9,13 +9,23 @@ namespace Ryujinx.Common.Logging public int ThreadId { get; private set; } public string Message { get; private set; } + public object Data { get; private set; } public LogEventArgs(LogLevel level, TimeSpan time, int threadId, string message) { - this.Level = level; - this.Time = time; + this.Level = level; + this.Time = time; this.ThreadId = threadId; this.Message = message; } + + public LogEventArgs(LogLevel level, TimeSpan time, int threadId, string message, object data) + { + this.Level = level; + this.Time = time; + this.ThreadId = threadId; + this.Message = message; + this.Data = data; + } } } \ No newline at end of file diff --git a/Ryujinx.Common/Logging/Logger.cs b/Ryujinx.Common/Logging/Logger.cs index 107a84faff..5e58f8064c 100644 --- a/Ryujinx.Common/Logging/Logger.cs +++ b/Ryujinx.Common/Logging/Logger.cs @@ -71,46 +71,12 @@ namespace Ryujinx.Common.Logging public static void PrintStub(LogClass logClass, T obj, [CallerMemberName] string caller = "") { - StringBuilder sb = new StringBuilder(); - PropertyInfo[] props = typeof(T).GetProperties(); - - sb.Append("Stubbed. "); - - foreach (var prop in props) - { - sb.Append($"{prop.Name}: {prop.GetValue(obj)}"); - sb.Append(" - "); - } - - if (props.Length > 0) - { - sb.Remove(sb.Length - 3, 3); - } - - Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, sb.ToString(), caller)); + Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed.", caller), obj); } public static void PrintStub(LogClass logClass, string message, T obj, [CallerMemberName] string caller = "") { - StringBuilder sb = new StringBuilder(); - PropertyInfo[] props = typeof(T).GetProperties(); - - sb.Append("Stubbed. "); - sb.Append(message); - sb.Append(' '); - - foreach (var prop in props) - { - sb.Append($"{prop.Name}: {prop.GetValue(obj)}"); - sb.Append(" - "); - } - - if (props.Length > 0) - { - sb.Remove(sb.Length - 3, 3); - } - - Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, sb.ToString(), caller)); + Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller), obj); } private static void Print(LogLevel logLevel, LogClass logClass, string message) @@ -121,6 +87,14 @@ namespace Ryujinx.Common.Logging } } + 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.ManagedThreadId, message, data)); + } + } + private static string GetFormattedMessage(LogClass Class, string Message, string Caller) { return $"{Class} {Caller}: {Message}"; diff --git a/Ryujinx/Ui/ConsoleLog.cs b/Ryujinx/Ui/ConsoleLog.cs index 0a5d0524dc..aa12a7243d 100644 --- a/Ryujinx/Ui/ConsoleLog.cs +++ b/Ryujinx/Ui/ConsoleLog.cs @@ -2,6 +2,8 @@ using Ryujinx.Common.Logging; using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Reflection; +using System.Text; using System.Threading; namespace Ryujinx @@ -51,22 +53,44 @@ namespace Ryujinx private static void PrintLog(LogEventArgs e) { - string formattedTime = e.Time.ToString(@"hh\:mm\:ss\.fff"); + StringBuilder sb = new StringBuilder(); - string currentThread = e.ThreadId.ToString("d4"); + sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", e.Time); + sb.Append(" | "); + sb.AppendFormat("{0:d4}", e.ThreadId); + sb.Append(' '); + sb.Append(e.Message); - string message = formattedTime + " | " + currentThread + " " + e.Message; + if (e.Data != null) + { + PropertyInfo[] props = e.Data.GetType().GetProperties(); + + sb.Append(' '); + + foreach (var prop in props) + { + sb.Append($"{prop.Name}: {prop.GetValue(e.Data)}"); + sb.Append(" - "); + } + + // We remove the final '-' from the string + if (props.Length > 0) + { + sb.Remove(sb.Length - 3, 3); + } + } if (_logColors.TryGetValue(e.Level, out ConsoleColor color)) { Console.ForegroundColor = color; - Console.WriteLine(message); + Console.WriteLine(sb.ToString()); + Console.ResetColor(); } else { - Console.WriteLine(message); + Console.WriteLine(sb.ToString()); } }