2018-10-17 17:15:50 +00:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-04-24 18:57:39 +00:00
|
|
|
using System;
|
2018-09-04 00:15:41 +00:00
|
|
|
using System.Collections.Concurrent;
|
2018-04-24 18:57:39 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx
|
|
|
|
{
|
|
|
|
static class ConsoleLog
|
|
|
|
{
|
2018-09-04 00:15:41 +00:00
|
|
|
private static Thread MessageThread;
|
|
|
|
|
|
|
|
private static BlockingCollection<LogEventArgs> MessageQueue;
|
|
|
|
|
2018-04-24 18:57:39 +00:00
|
|
|
private static Dictionary<LogLevel, ConsoleColor> LogColors;
|
|
|
|
|
|
|
|
private static object ConsoleLock;
|
|
|
|
|
|
|
|
static ConsoleLog()
|
|
|
|
{
|
|
|
|
LogColors = new Dictionary<LogLevel, ConsoleColor>()
|
|
|
|
{
|
|
|
|
{ LogLevel.Stub, ConsoleColor.DarkGray },
|
|
|
|
{ LogLevel.Info, ConsoleColor.White },
|
|
|
|
{ LogLevel.Warning, ConsoleColor.Yellow },
|
|
|
|
{ LogLevel.Error, ConsoleColor.Red }
|
|
|
|
};
|
|
|
|
|
2018-09-04 00:15:41 +00:00
|
|
|
MessageQueue = new BlockingCollection<LogEventArgs>();
|
|
|
|
|
2018-04-24 18:57:39 +00:00
|
|
|
ConsoleLock = new object();
|
2018-09-04 00:15:41 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
MessageThread.IsBackground = true;
|
|
|
|
MessageThread.Start();
|
2018-04-24 18:57:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-04 00:15:41 +00:00
|
|
|
private static void PrintLog(LogEventArgs e)
|
2018-04-24 18:57:39 +00:00
|
|
|
{
|
|
|
|
string FormattedTime = e.Time.ToString(@"hh\:mm\:ss\.fff");
|
|
|
|
|
|
|
|
string CurrentThread = Thread.CurrentThread.ManagedThreadId.ToString("d4");
|
2018-09-04 00:15:41 +00:00
|
|
|
|
2018-04-24 18:57:39 +00:00
|
|
|
string Message = FormattedTime + " | " + CurrentThread + " " + e.Message;
|
|
|
|
|
|
|
|
if (LogColors.TryGetValue(e.Level, out ConsoleColor Color))
|
|
|
|
{
|
|
|
|
lock (ConsoleLock)
|
|
|
|
{
|
|
|
|
Console.ForegroundColor = Color;
|
|
|
|
|
|
|
|
Console.WriteLine(Message);
|
|
|
|
Console.ResetColor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.WriteLine(Message);
|
|
|
|
}
|
|
|
|
}
|
2018-09-04 00:15:41 +00:00
|
|
|
|
|
|
|
public static void Log(object sender, LogEventArgs e)
|
|
|
|
{
|
|
|
|
if (!MessageQueue.IsAddingCompleted)
|
|
|
|
{
|
|
|
|
MessageQueue.Add(e);
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 18:57:39 +00:00
|
|
|
}
|
|
|
|
}
|