2018-10-17 19:15:50 +02:00
|
|
|
using Ryujinx.Common.Logging;
|
2018-04-24 20:57:39 +02:00
|
|
|
using System;
|
2018-09-04 02:15:41 +02:00
|
|
|
using System.Collections.Concurrent;
|
2018-04-24 20:57:39 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx
|
|
|
|
{
|
|
|
|
static class ConsoleLog
|
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
private static Thread _messageThread;
|
2018-09-04 02:15:41 +02:00
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
private static BlockingCollection<LogEventArgs> _messageQueue;
|
2018-09-04 02:15:41 +02:00
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
private static Dictionary<LogLevel, ConsoleColor> _logColors;
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
private static object _consoleLock;
|
2018-04-24 20:57:39 +02:00
|
|
|
|
|
|
|
static ConsoleLog()
|
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
_logColors = new Dictionary<LogLevel, ConsoleColor>()
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
|
|
|
{ LogLevel.Stub, ConsoleColor.DarkGray },
|
|
|
|
{ LogLevel.Info, ConsoleColor.White },
|
|
|
|
{ LogLevel.Warning, ConsoleColor.Yellow },
|
|
|
|
{ LogLevel.Error, ConsoleColor.Red }
|
|
|
|
};
|
|
|
|
|
2018-11-28 23:18:09 +01:00
|
|
|
_messageQueue = new BlockingCollection<LogEventArgs>(10);
|
2018-09-04 02:15:41 +02:00
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
_consoleLock = new object();
|
2018-09-04 02:15:41 +02:00
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
_messageThread = new Thread(() =>
|
2018-09-04 02:15:41 +02:00
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
while (!_messageQueue.IsCompleted)
|
2018-09-04 02:15:41 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
PrintLog(_messageQueue.Take());
|
2018-09-04 02:15:41 +02:00
|
|
|
}
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
_messageThread.IsBackground = true;
|
|
|
|
_messageThread.Start();
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 02:15:41 +02:00
|
|
|
private static void PrintLog(LogEventArgs e)
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
string formattedTime = e.Time.ToString(@"hh\:mm\:ss\.fff");
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
string currentThread = Thread.CurrentThread.ManagedThreadId.ToString("d4");
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
string message = formattedTime + " | " + currentThread + " " + e.Message;
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
if (_logColors.TryGetValue(e.Level, out ConsoleColor color))
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
lock (_consoleLock)
|
2018-04-24 20:57:39 +02:00
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
Console.ForegroundColor = color;
|
2018-04-24 20:57:39 +02:00
|
|
|
|
2018-10-31 02:43:02 +01:00
|
|
|
Console.WriteLine(message);
|
2018-04-24 20:57:39 +02:00
|
|
|
Console.ResetColor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
Console.WriteLine(message);
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-04 02:15:41 +02:00
|
|
|
|
|
|
|
public static void Log(object sender, LogEventArgs e)
|
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
if (!_messageQueue.IsAddingCompleted)
|
2018-09-04 02:15:41 +02:00
|
|
|
{
|
2018-10-31 02:43:02 +01:00
|
|
|
_messageQueue.Add(e);
|
2018-09-04 02:15:41 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-24 20:57:39 +02:00
|
|
|
}
|
|
|
|
}
|