2019-01-31 02:49:15 +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;
|
2019-01-31 02:49:15 +00:00
|
|
|
|
using System.IO;
|
2019-01-11 00:11:46 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
2018-04-24 18:57:39 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx
|
|
|
|
|
{
|
2019-01-31 02:49:15 +00:00
|
|
|
|
static class Log
|
2018-04-24 18:57:39 +00:00
|
|
|
|
{
|
2019-01-31 02:49:15 +00:00
|
|
|
|
private static readonly string _path;
|
|
|
|
|
|
|
|
|
|
private static StreamWriter _logWriter;
|
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
private static Thread _messageThread;
|
2018-09-04 00:15:41 +00:00
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
private static BlockingCollection<LogEventArgs> _messageQueue;
|
2018-09-04 00:15:41 +00:00
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
private static Dictionary<LogLevel, ConsoleColor> _logColors;
|
2018-04-24 18:57:39 +00:00
|
|
|
|
|
2019-01-31 02:49:15 +00:00
|
|
|
|
static Log()
|
2018-04-24 18:57:39 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
_logColors = new Dictionary<LogLevel, ConsoleColor>()
|
2018-04-24 18:57:39 +00:00
|
|
|
|
{
|
|
|
|
|
{ LogLevel.Stub, ConsoleColor.DarkGray },
|
|
|
|
|
{ LogLevel.Info, ConsoleColor.White },
|
|
|
|
|
{ LogLevel.Warning, ConsoleColor.Yellow },
|
|
|
|
|
{ LogLevel.Error, ConsoleColor.Red }
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
|
_messageQueue = new BlockingCollection<LogEventArgs>(10);
|
2018-09-04 00:15:41 +00:00
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
_messageThread = new Thread(() =>
|
2018-09-04 00:15:41 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
while (!_messageQueue.IsCompleted)
|
2018-09-04 00:15:41 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
PrintLog(_messageQueue.Take());
|
2018-09-04 00:15:41 +00: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.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-01-31 02:49:15 +00:00
|
|
|
|
_path = Path.Combine(Environment.CurrentDirectory, "Ryujinx.log");
|
|
|
|
|
|
|
|
|
|
if (Logger.EnableFileLog)
|
|
|
|
|
{
|
|
|
|
|
_logWriter = new StreamWriter(File.Open(_path,FileMode.Create, FileAccess.Write));
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
_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
|
|
|
|
{
|
2019-01-11 00:11:46 +00:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2018-04-24 18:57:39 +00:00
|
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
|
sb.AppendFormat(@"{0:hh\:mm\:ss\.fff}", e.Time);
|
|
|
|
|
sb.Append(" | ");
|
|
|
|
|
sb.AppendFormat("{0:d4}", e.ThreadId);
|
|
|
|
|
sb.Append(' ');
|
|
|
|
|
sb.Append(e.Message);
|
2018-11-28 22:18:09 +00:00
|
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
|
if (e.Data != null)
|
2018-04-24 18:57:39 +00:00
|
|
|
|
{
|
2019-01-11 00:11:46 +00:00
|
|
|
|
PropertyInfo[] props = e.Data.GetType().GetProperties();
|
|
|
|
|
|
|
|
|
|
sb.Append(' ');
|
|
|
|
|
|
|
|
|
|
foreach (var prop in props)
|
2018-04-24 18:57:39 +00:00
|
|
|
|
{
|
2019-01-11 00:11:46 +00:00
|
|
|
|
sb.Append(prop.Name);
|
|
|
|
|
sb.Append(": ");
|
|
|
|
|
sb.Append(prop.GetValue(e.Data));
|
|
|
|
|
sb.Append(" - ");
|
|
|
|
|
}
|
2018-04-24 18:57:39 +00:00
|
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
|
// We remove the final '-' from the string
|
|
|
|
|
if (props.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
sb.Remove(sb.Length - 3, 3);
|
2018-04-24 18:57:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-11 00:11:46 +00:00
|
|
|
|
|
2019-01-31 02:49:15 +00:00
|
|
|
|
string message = sb.ToString();
|
|
|
|
|
|
2019-01-11 00:11:46 +00:00
|
|
|
|
if (_logColors.TryGetValue(e.Level, out ConsoleColor color))
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = color;
|
|
|
|
|
|
2019-01-31 02:49:15 +00:00
|
|
|
|
Console.WriteLine(message);
|
2019-01-11 00:11:46 +00:00
|
|
|
|
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
}
|
2018-04-24 18:57:39 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2019-01-31 02:49:15 +00:00
|
|
|
|
Console.WriteLine(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Logger.EnableFileLog)
|
|
|
|
|
{
|
|
|
|
|
_logWriter.WriteLine(message);
|
2018-04-24 18:57:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-04 00:15:41 +00:00
|
|
|
|
|
2019-01-31 02:49:15 +00:00
|
|
|
|
public static void LogMessage(object sender, LogEventArgs e)
|
2018-09-04 00:15:41 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
if (!_messageQueue.IsAddingCompleted)
|
2018-09-04 00:15:41 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
_messageQueue.Add(e);
|
2018-09-04 00:15:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-31 02:49:15 +00:00
|
|
|
|
|
|
|
|
|
public static void Close()
|
|
|
|
|
{
|
|
|
|
|
_messageQueue.CompleteAdding();
|
|
|
|
|
|
|
|
|
|
_messageThread.Join();
|
|
|
|
|
|
|
|
|
|
if (Logger.EnableFileLog)
|
|
|
|
|
{
|
|
|
|
|
_logWriter.Flush();
|
|
|
|
|
_logWriter.Close();
|
|
|
|
|
_logWriter.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-24 18:57:39 +00:00
|
|
|
|
}
|
2019-01-31 02:49:15 +00:00
|
|
|
|
}
|