2018-02-25 13:58:16 -05:00
|
|
|
|
using Ryujinx.Core.OsHle.Ipc;
|
|
|
|
|
using System;
|
2018-02-08 19:43:22 -05:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2018-04-08 16:30:50 -04:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2018-02-25 13:58:16 -05:00
|
|
|
|
using System.Text;
|
2018-04-13 21:02:24 -04:00
|
|
|
|
using System.Threading;
|
2018-02-08 19:43:22 -05:00
|
|
|
|
|
2018-02-20 15:09:23 -05:00
|
|
|
|
namespace Ryujinx.Core
|
2018-02-08 19:43:22 -05:00
|
|
|
|
{
|
|
|
|
|
public static class Logging
|
|
|
|
|
{
|
2018-03-12 00:04:52 -04:00
|
|
|
|
private static Stopwatch ExecutionTime;
|
|
|
|
|
|
2018-02-20 05:54:00 -05:00
|
|
|
|
private const string LogFileName = "Ryujinx.log";
|
2018-02-08 19:43:22 -05:00
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
|
private static bool EnableInfo = Config.LoggingEnableInfo;
|
|
|
|
|
private static bool EnableTrace = Config.LoggingEnableTrace;
|
|
|
|
|
private static bool EnableDebug = Config.LoggingEnableDebug;
|
|
|
|
|
private static bool EnableWarn = Config.LoggingEnableWarn;
|
|
|
|
|
private static bool EnableError = Config.LoggingEnableError;
|
|
|
|
|
private static bool EnableFatal = Config.LoggingEnableFatal;
|
2018-04-20 11:40:33 -04:00
|
|
|
|
private static bool EnableStub = Config.LoggingEnableStub;
|
2018-04-13 21:02:24 -04:00
|
|
|
|
private static bool EnableIpc = Config.LoggingEnableIpc;
|
|
|
|
|
private static bool EnableFilter = Config.LoggingEnableFilter;
|
|
|
|
|
private static bool EnableLogFile = Config.LoggingEnableLogFile;
|
|
|
|
|
private static bool[] FilteredLogClasses = Config.LoggingFilteredClasses;
|
2018-02-08 19:43:22 -05:00
|
|
|
|
|
2018-04-08 16:30:50 -04:00
|
|
|
|
private enum LogLevel
|
|
|
|
|
{
|
2018-04-16 20:24:42 -04:00
|
|
|
|
Debug,
|
|
|
|
|
Error,
|
|
|
|
|
Fatal,
|
|
|
|
|
Info,
|
|
|
|
|
Stub,
|
|
|
|
|
Trace,
|
|
|
|
|
Warn
|
2018-04-08 16:30:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-08 19:43:22 -05:00
|
|
|
|
static Logging()
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(LogFileName)) File.Delete(LogFileName);
|
2018-03-12 00:04:52 -04:00
|
|
|
|
|
|
|
|
|
ExecutionTime = new Stopwatch();
|
|
|
|
|
|
|
|
|
|
ExecutionTime.Start();
|
2018-02-08 19:43:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-08 16:30:50 -04:00
|
|
|
|
public static string GetExecutionTime() => ExecutionTime.ElapsedMilliseconds.ToString().PadLeft(8, '0') + "ms";
|
2018-02-08 19:43:22 -05:00
|
|
|
|
|
2018-04-08 16:30:50 -04:00
|
|
|
|
private static void LogMessage(LogEntry LogEntry)
|
2018-02-09 14:39:50 -05:00
|
|
|
|
{
|
2018-04-13 21:02:24 -04:00
|
|
|
|
if (EnableFilter)
|
|
|
|
|
if (!FilteredLogClasses[(int)LogEntry.LogClass])
|
|
|
|
|
return;
|
|
|
|
|
|
2018-04-08 16:30:50 -04:00
|
|
|
|
ConsoleColor consoleColor = ConsoleColor.White;
|
|
|
|
|
|
|
|
|
|
switch (LogEntry.LogLevel)
|
|
|
|
|
{
|
|
|
|
|
case LogLevel.Debug:
|
|
|
|
|
consoleColor = ConsoleColor.Gray;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Error:
|
|
|
|
|
consoleColor = ConsoleColor.Red;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Fatal:
|
|
|
|
|
consoleColor = ConsoleColor.Magenta;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Info:
|
|
|
|
|
consoleColor = ConsoleColor.White;
|
|
|
|
|
break;
|
2018-04-16 20:24:42 -04:00
|
|
|
|
case LogLevel.Stub:
|
|
|
|
|
consoleColor = ConsoleColor.DarkYellow;
|
|
|
|
|
break;
|
2018-04-08 16:30:50 -04:00
|
|
|
|
case LogLevel.Trace:
|
|
|
|
|
consoleColor = ConsoleColor.DarkGray;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Warn:
|
|
|
|
|
consoleColor = ConsoleColor.Yellow;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
|
LogEntry.ManagedThreadId = Thread.CurrentThread.ManagedThreadId;
|
|
|
|
|
|
|
|
|
|
string Text = $"{LogEntry.ExecutionTime} | {LogEntry.ManagedThreadId} > {LogEntry.LogClass} > " +
|
|
|
|
|
$"{LogEntry.LogLevel.ToString()} > {LogEntry.CallingMember} > {LogEntry.Message}";
|
2018-04-08 16:30:50 -04:00
|
|
|
|
|
|
|
|
|
Console.ForegroundColor = consoleColor;
|
|
|
|
|
Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
|
|
|
|
|
LogFile(Text);
|
2018-02-09 14:39:50 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-08 19:43:22 -05:00
|
|
|
|
private static void LogFile(string Message)
|
|
|
|
|
{
|
|
|
|
|
if (EnableLogFile)
|
|
|
|
|
{
|
|
|
|
|
using (StreamWriter Writer = File.AppendText(LogFileName))
|
|
|
|
|
{
|
|
|
|
|
Writer.WriteLine(Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
|
public static void Info(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
|
2018-02-08 19:43:22 -05:00
|
|
|
|
{
|
|
|
|
|
if (EnableInfo)
|
|
|
|
|
{
|
2018-04-08 16:30:50 -04:00
|
|
|
|
LogMessage(new LogEntry
|
|
|
|
|
{
|
2018-04-08 16:38:27 -04:00
|
|
|
|
CallingMember = CallingMember,
|
|
|
|
|
LogLevel = LogLevel.Info,
|
2018-04-13 21:02:24 -04:00
|
|
|
|
LogClass = LogClass,
|
2018-04-08 16:38:27 -04:00
|
|
|
|
Message = Message,
|
|
|
|
|
ExecutionTime = GetExecutionTime()
|
2018-04-08 16:30:50 -04:00
|
|
|
|
});
|
2018-02-08 19:43:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-08 16:38:27 -04:00
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
|
public static void Trace(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
|
2018-02-08 19:43:22 -05:00
|
|
|
|
{
|
|
|
|
|
if (EnableTrace)
|
|
|
|
|
{
|
2018-04-08 16:30:50 -04:00
|
|
|
|
LogMessage(new LogEntry
|
|
|
|
|
{
|
2018-04-08 16:38:27 -04:00
|
|
|
|
CallingMember = CallingMember,
|
|
|
|
|
LogLevel = LogLevel.Trace,
|
2018-04-13 21:02:24 -04:00
|
|
|
|
LogClass = LogClass,
|
2018-04-08 16:38:27 -04:00
|
|
|
|
Message = Message,
|
|
|
|
|
ExecutionTime = GetExecutionTime()
|
2018-04-08 16:30:50 -04:00
|
|
|
|
});
|
2018-02-08 19:43:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 20:24:42 -04:00
|
|
|
|
public static void Stub(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
|
|
|
|
|
{
|
|
|
|
|
if (EnableStub)
|
|
|
|
|
{
|
|
|
|
|
LogMessage(new LogEntry
|
|
|
|
|
{
|
|
|
|
|
CallingMember = CallingMember,
|
|
|
|
|
LogLevel = LogLevel.Stub,
|
|
|
|
|
LogClass = LogClass,
|
|
|
|
|
Message = Message,
|
|
|
|
|
ExecutionTime = GetExecutionTime()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
|
public static void Debug(LogClass LogClass,string Message, [CallerMemberName] string CallingMember = "")
|
2018-02-08 19:43:22 -05:00
|
|
|
|
{
|
|
|
|
|
if (EnableDebug)
|
|
|
|
|
{
|
2018-04-08 16:30:50 -04:00
|
|
|
|
LogMessage(new LogEntry
|
|
|
|
|
{
|
2018-04-08 16:38:27 -04:00
|
|
|
|
CallingMember = CallingMember,
|
|
|
|
|
LogLevel = LogLevel.Debug,
|
2018-04-13 21:02:24 -04:00
|
|
|
|
LogClass = LogClass,
|
2018-04-08 16:38:27 -04:00
|
|
|
|
Message = Message,
|
|
|
|
|
ExecutionTime = GetExecutionTime()
|
2018-04-08 16:30:50 -04:00
|
|
|
|
});
|
2018-02-08 19:43:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
|
public static void Warn(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
|
2018-02-08 19:43:22 -05:00
|
|
|
|
{
|
|
|
|
|
if (EnableWarn)
|
|
|
|
|
{
|
2018-04-08 16:30:50 -04:00
|
|
|
|
LogMessage(new LogEntry
|
|
|
|
|
{
|
2018-04-08 16:38:27 -04:00
|
|
|
|
CallingMember = CallingMember,
|
|
|
|
|
LogLevel = LogLevel.Warn,
|
2018-04-13 21:02:24 -04:00
|
|
|
|
LogClass = LogClass,
|
2018-04-08 16:38:27 -04:00
|
|
|
|
Message = Message,
|
|
|
|
|
ExecutionTime = GetExecutionTime()
|
2018-04-08 16:30:50 -04:00
|
|
|
|
});
|
2018-02-08 19:43:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
|
public static void Error(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
|
2018-02-08 19:43:22 -05:00
|
|
|
|
{
|
|
|
|
|
if (EnableError)
|
|
|
|
|
{
|
2018-04-08 16:30:50 -04:00
|
|
|
|
LogMessage(new LogEntry
|
|
|
|
|
{
|
2018-04-08 16:38:27 -04:00
|
|
|
|
CallingMember = CallingMember,
|
|
|
|
|
LogLevel = LogLevel.Error,
|
2018-04-13 21:02:24 -04:00
|
|
|
|
LogClass = LogClass,
|
2018-04-08 16:38:27 -04:00
|
|
|
|
Message = Message,
|
|
|
|
|
ExecutionTime = GetExecutionTime()
|
2018-04-08 16:30:50 -04:00
|
|
|
|
});
|
2018-02-08 19:43:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 21:02:24 -04:00
|
|
|
|
public static void Fatal(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
|
2018-02-08 19:43:22 -05:00
|
|
|
|
{
|
|
|
|
|
if (EnableFatal)
|
|
|
|
|
{
|
2018-04-08 16:30:50 -04:00
|
|
|
|
LogMessage(new LogEntry
|
|
|
|
|
{
|
2018-04-08 16:38:27 -04:00
|
|
|
|
CallingMember = CallingMember,
|
|
|
|
|
LogLevel = LogLevel.Fatal,
|
2018-04-13 21:02:24 -04:00
|
|
|
|
LogClass = LogClass,
|
2018-04-08 16:38:27 -04:00
|
|
|
|
Message = Message,
|
|
|
|
|
ExecutionTime = GetExecutionTime()
|
2018-04-08 16:30:50 -04:00
|
|
|
|
});
|
2018-02-08 19:43:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-25 13:58:16 -05:00
|
|
|
|
|
|
|
|
|
public static void Ipc(byte[] Data, long CmdPtr, bool Domain)
|
|
|
|
|
{
|
|
|
|
|
if (EnableIpc)
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
|
|
|
|
Console.WriteLine(IpcLog.Message(Data, CmdPtr, Domain));
|
|
|
|
|
Console.ResetColor();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//https://www.codeproject.com/Articles/36747/Quick-and-Dirty-HexDump-of-a-Byte-Array
|
|
|
|
|
public static string HexDump(byte[] bytes, int bytesPerLine = 16)
|
|
|
|
|
{
|
|
|
|
|
if (bytes == null) return "<null>";
|
|
|
|
|
int bytesLength = bytes.Length;
|
|
|
|
|
|
|
|
|
|
char[] HexChars = "0123456789ABCDEF".ToCharArray();
|
|
|
|
|
|
|
|
|
|
int firstHexColumn =
|
|
|
|
|
8 // 8 characters for the address
|
|
|
|
|
+ 3; // 3 spaces
|
|
|
|
|
|
|
|
|
|
int firstCharColumn = firstHexColumn
|
|
|
|
|
+ bytesPerLine * 3 // - 2 digit for the hexadecimal value and 1 space
|
|
|
|
|
+ (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
|
2018-04-08 16:38:27 -04:00
|
|
|
|
+ 2; // 2 spaces
|
2018-02-25 13:58:16 -05:00
|
|
|
|
|
|
|
|
|
int lineLength = firstCharColumn
|
|
|
|
|
+ bytesPerLine // - characters to show the ascii value
|
|
|
|
|
+ Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
|
|
|
|
|
|
|
|
|
|
char[] line = (new String(' ', lineLength - Environment.NewLine.Length) + Environment.NewLine).ToCharArray();
|
|
|
|
|
int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
|
|
|
|
|
StringBuilder result = new StringBuilder(expectedLines * lineLength);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < bytesLength; i += bytesPerLine)
|
|
|
|
|
{
|
|
|
|
|
line[0] = HexChars[(i >> 28) & 0xF];
|
|
|
|
|
line[1] = HexChars[(i >> 24) & 0xF];
|
|
|
|
|
line[2] = HexChars[(i >> 20) & 0xF];
|
|
|
|
|
line[3] = HexChars[(i >> 16) & 0xF];
|
|
|
|
|
line[4] = HexChars[(i >> 12) & 0xF];
|
|
|
|
|
line[5] = HexChars[(i >> 8) & 0xF];
|
|
|
|
|
line[6] = HexChars[(i >> 4) & 0xF];
|
|
|
|
|
line[7] = HexChars[(i >> 0) & 0xF];
|
|
|
|
|
|
|
|
|
|
int hexColumn = firstHexColumn;
|
|
|
|
|
int charColumn = firstCharColumn;
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < bytesPerLine; j++)
|
|
|
|
|
{
|
|
|
|
|
if (j > 0 && (j & 7) == 0) hexColumn++;
|
|
|
|
|
if (i + j >= bytesLength)
|
|
|
|
|
{
|
|
|
|
|
line[hexColumn] = ' ';
|
|
|
|
|
line[hexColumn + 1] = ' ';
|
|
|
|
|
line[charColumn] = ' ';
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
byte b = bytes[i + j];
|
|
|
|
|
line[hexColumn] = HexChars[(b >> 4) & 0xF];
|
|
|
|
|
line[hexColumn + 1] = HexChars[b & 0xF];
|
|
|
|
|
line[charColumn] = (b < 32 ? '·' : (char)b);
|
|
|
|
|
}
|
|
|
|
|
hexColumn += 3;
|
|
|
|
|
charColumn++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Append(line);
|
|
|
|
|
}
|
|
|
|
|
return result.ToString();
|
|
|
|
|
}
|
2018-04-08 16:30:50 -04:00
|
|
|
|
|
|
|
|
|
private struct LogEntry
|
2018-04-08 16:38:27 -04:00
|
|
|
|
{
|
2018-04-08 16:30:50 -04:00
|
|
|
|
public string CallingMember;
|
|
|
|
|
public string ExecutionTime;
|
|
|
|
|
public string Message;
|
2018-04-13 21:02:24 -04:00
|
|
|
|
public int ManagedThreadId;
|
|
|
|
|
public LogClass LogClass;
|
2018-04-08 16:30:50 -04:00
|
|
|
|
public LogLevel LogLevel;
|
|
|
|
|
}
|
2018-04-08 16:38:27 -04:00
|
|
|
|
}
|
2018-02-08 19:43:22 -05:00
|
|
|
|
}
|