consolidated console and file log

This commit is contained in:
emmaus 2019-01-30 19:07:54 +00:00
parent f9b7230e36
commit cd9bee372f
3 changed files with 45 additions and 129 deletions

View file

@ -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();
}
}
/// <summary>

View file

@ -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<LogEventArgs> _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<LogEventArgs>(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();
}
}
}

View file

@ -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<LogEventArgs> _messageQueue;
private static Dictionary<LogLevel, ConsoleColor> _logColors;
static ConsoleLog()
static Log()
{
_logColors = new Dictionary<LogLevel, ConsoleColor>()
{
@ -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();
}
}
}
}
}