diff --git a/Ryujinx/Program.cs b/Ryujinx/Program.cs index dcedc66a5c..d80f6831d0 100644 --- a/Ryujinx/Program.cs +++ b/Ryujinx/Program.cs @@ -22,10 +22,7 @@ namespace Ryujinx Config.Read(device); - Logger.Updated += ConsoleLog.Log; - - if (Logger.EnableFileLog) - Logger.Updated += FileLog.Log; + Logger.Updated += Log.LogMessage; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit; @@ -94,7 +91,7 @@ namespace Ryujinx private static void CurrentDomain_ProcessExit(object sender, EventArgs e) { - FileLog.Close(); + Log.Close(); } private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) @@ -104,7 +101,9 @@ namespace Ryujinx Logger.PrintError(LogClass.Emulation, $"Unhandled exception caught: {exception}"); if (e.IsTerminating) - FileLog.Close(); + { + Log.Close(); + } } /// diff --git a/Ryujinx/Ui/FileLog.cs b/Ryujinx/Ui/FileLog.cs deleted file mode 100644 index 97cdfe54ba..0000000000 --- a/Ryujinx/Ui/FileLog.cs +++ /dev/null @@ -1,116 +0,0 @@ -using Ryujinx.Common.Logging; -using System; -using System.Collections.Concurrent; -using System.IO; -using System.Reflection; -using System.Text; -using System.Threading; - -namespace Ryujinx -{ - static class FileLog - { - private static string _path; - - private static Thread _messageThread; - - private static BlockingCollection _messageQueue; - - private static StreamWriter _logWriter; - - static FileLog() - { - if (!Logger.EnableFileLog) - return; - - _path = Path.Combine(Environment.CurrentDirectory, "Ryujinx.log"); - - if (File.Exists(_path)) - { - File.Delete(_path); - } - - _messageQueue = new BlockingCollection(10); - - _messageThread = new Thread(() => - { - while (!_messageQueue.IsCompleted) - { - try - { - PrintLog(_messageQueue.Take()); - } - catch (InvalidOperationException) - { - // IOE means that Take() was called on a completed collection. - // Some other thread can call CompleteAdding after we pass the - // IsCompleted check but before we call Take. - // We can simply catch the exception since the loop will break - // on the next iteration. - } - } - }); - - _logWriter = new StreamWriter(File.OpenWrite(_path)); - - _messageThread.IsBackground = true; - _messageThread.Start(); - } - - public static void Log(object sender, LogEventArgs e) - { - if (!_messageQueue.IsAddingCompleted) - { - _messageQueue.Add(e); - } - } - - private static void PrintLog(LogEventArgs e) - { - StringBuilder sb = new StringBuilder(); - - sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", e.Time); - sb.Append(" | "); - sb.AppendFormat("{0:d4}", e.ThreadId); - sb.Append(' '); - sb.Append(e.Message); - - if (e.Data != null) - { - PropertyInfo[] props = e.Data.GetType().GetProperties(); - - sb.Append(' '); - - foreach (var prop in props) - { - sb.Append(prop.Name); - sb.Append(": "); - sb.Append(prop.GetValue(e.Data)); - sb.Append(" - "); - } - - // We remove the final '-' from the string - if (props.Length > 0) - { - sb.Remove(sb.Length - 3, 3); - } - } - - _logWriter.WriteLine(sb.ToString()); - } - - public static void Close() - { - if (!Logger.EnableFileLog) - return; - - _messageQueue.CompleteAdding(); - - _messageThread.Join(); - - _logWriter.Flush(); - _logWriter.Close(); - _logWriter.Dispose(); - } - } -} diff --git a/Ryujinx/Ui/ConsoleLog.cs b/Ryujinx/Ui/Log.cs similarity index 73% rename from Ryujinx/Ui/ConsoleLog.cs rename to Ryujinx/Ui/Log.cs index ac3d41c9e1..5daae14026 100644 --- a/Ryujinx/Ui/ConsoleLog.cs +++ b/Ryujinx/Ui/Log.cs @@ -1,22 +1,27 @@ -using Ryujinx.Common.Logging; +using Ryujinx.Common.Logging; using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.IO; using System.Reflection; using System.Text; using System.Threading; namespace Ryujinx { - static class ConsoleLog + static class Log { + private static readonly string _path; + + private static StreamWriter _logWriter; + private static Thread _messageThread; private static BlockingCollection _messageQueue; private static Dictionary _logColors; - static ConsoleLog() + static Log() { _logColors = new Dictionary() { @@ -47,6 +52,13 @@ namespace Ryujinx } }); + _path = Path.Combine(Environment.CurrentDirectory, "Ryujinx.log"); + + if (Logger.EnableFileLog) + { + _logWriter = new StreamWriter(File.Open(_path,FileMode.Create, FileAccess.Write)); + } + _messageThread.IsBackground = true; _messageThread.Start(); } @@ -82,26 +94,47 @@ namespace Ryujinx } } + string message = sb.ToString(); + if (_logColors.TryGetValue(e.Level, out ConsoleColor color)) { Console.ForegroundColor = color; - Console.WriteLine(sb.ToString()); + Console.WriteLine(message); Console.ResetColor(); } else { - Console.WriteLine(sb.ToString()); + Console.WriteLine(message); + } + + if (Logger.EnableFileLog) + { + _logWriter.WriteLine(message); } } - public static void Log(object sender, LogEventArgs e) + public static void LogMessage(object sender, LogEventArgs e) { if (!_messageQueue.IsAddingCompleted) { _messageQueue.Add(e); } } + + public static void Close() + { + _messageQueue.CompleteAdding(); + + _messageThread.Join(); + + if (Logger.EnableFileLog) + { + _logWriter.Flush(); + _logWriter.Close(); + _logWriter.Dispose(); + } + } } -} \ No newline at end of file +}