diff options
Diffstat (limited to 'Ryujinx.Common')
| -rw-r--r-- | Ryujinx.Common/BitUtils.cs | 90 | ||||
| -rw-r--r-- | Ryujinx.Common/HexUtils.cs | 90 | ||||
| -rw-r--r-- | Ryujinx.Common/Logging/LogEventArgs.cs | 24 | ||||
| -rw-r--r-- | Ryujinx.Common/Logging/Logger.cs | 81 |
4 files changed, 203 insertions, 82 deletions
diff --git a/Ryujinx.Common/BitUtils.cs b/Ryujinx.Common/BitUtils.cs index 5c858029..135b397d 100644 --- a/Ryujinx.Common/BitUtils.cs +++ b/Ryujinx.Common/BitUtils.cs @@ -2,103 +2,103 @@ namespace Ryujinx.Common { public static class BitUtils { - public static int AlignUp(int Value, int Size) + private static readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }; + + public static int AlignUp(int value, int size) { - return (Value + (Size - 1)) & -Size; + return (value + (size - 1)) & -size; } - public static ulong AlignUp(ulong Value, int Size) + public static ulong AlignUp(ulong value, int size) { - return (ulong)AlignUp((long)Value, Size); + return (ulong)AlignUp((long)value, size); } - public static long AlignUp(long Value, int Size) + public static long AlignUp(long value, int size) { - return (Value + (Size - 1)) & -(long)Size; + return (value + (size - 1)) & -(long)size; } - public static int AlignDown(int Value, int Size) + public static int AlignDown(int value, int size) { - return Value & -Size; + return value & -size; } - public static ulong AlignDown(ulong Value, int Size) + public static ulong AlignDown(ulong value, int size) { - return (ulong)AlignDown((long)Value, Size); + return (ulong)AlignDown((long)value, size); } - public static long AlignDown(long Value, int Size) + public static long AlignDown(long value, int size) { - return Value & -(long)Size; + return value & -(long)size; } - public static ulong DivRoundUp(ulong Value, uint Dividend) + public static ulong DivRoundUp(ulong value, uint dividend) { - return (Value + Dividend - 1) / Dividend; + return (value + dividend - 1) / dividend; } - public static long DivRoundUp(long Value, int Dividend) + public static long DivRoundUp(long value, int dividend) { - return (Value + Dividend - 1) / Dividend; + return (value + dividend - 1) / dividend; } - public static bool IsPowerOfTwo32(int Value) + public static bool IsPowerOfTwo32(int value) { - return Value != 0 && (Value & (Value - 1)) == 0; + return value != 0 && (value & (value - 1)) == 0; } - public static bool IsPowerOfTwo64(long Value) + public static bool IsPowerOfTwo64(long value) { - return Value != 0 && (Value & (Value - 1)) == 0; + return value != 0 && (value & (value - 1)) == 0; } - public static int CountLeadingZeros32(int Value) + public static int CountLeadingZeros32(int value) { - return (int)CountLeadingZeros((ulong)Value, 32); + return (int)CountLeadingZeros((ulong)value, 32); } - public static int CountLeadingZeros64(long Value) + public static int CountLeadingZeros64(long value) { - return (int)CountLeadingZeros((ulong)Value, 64); + return (int)CountLeadingZeros((ulong)value, 64); } - private static readonly byte[] ClzNibbleTbl = { 4, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }; - - private static ulong CountLeadingZeros(ulong Value, int Size) // Size is 8, 16, 32 or 64 (SIMD&FP or Base Inst.). + private static ulong CountLeadingZeros(ulong value, int size) { - if (Value == 0ul) + if (value == 0ul) { - return (ulong)Size; + return (ulong)size; } - int NibbleIdx = Size; - int PreCount, Count = 0; + int nibbleIdx = size; + int preCount, count = 0; do { - NibbleIdx -= 4; - PreCount = ClzNibbleTbl[(Value >> NibbleIdx) & 0b1111]; - Count += PreCount; + nibbleIdx -= 4; + preCount = ClzNibbleTbl[(value >> nibbleIdx) & 0b1111]; + count += preCount; } - while (PreCount == 4); + while (preCount == 4); - return (ulong)Count; + return (ulong)count; } - public static long ReverseBits64(long Value) + public static long ReverseBits64(long value) { - return (long)ReverseBits64((ulong)Value); + return (long)ReverseBits64((ulong)value); } - private static ulong ReverseBits64(ulong Value) + private static ulong ReverseBits64(ulong value) { - Value = ((Value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((Value & 0x5555555555555555) << 1 ); - Value = ((Value & 0xcccccccccccccccc) >> 2 ) | ((Value & 0x3333333333333333) << 2 ); - Value = ((Value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((Value & 0x0f0f0f0f0f0f0f0f) << 4 ); - Value = ((Value & 0xff00ff00ff00ff00) >> 8 ) | ((Value & 0x00ff00ff00ff00ff) << 8 ); - Value = ((Value & 0xffff0000ffff0000) >> 16) | ((Value & 0x0000ffff0000ffff) << 16); + value = ((value & 0xaaaaaaaaaaaaaaaa) >> 1 ) | ((value & 0x5555555555555555) << 1 ); + value = ((value & 0xcccccccccccccccc) >> 2 ) | ((value & 0x3333333333333333) << 2 ); + value = ((value & 0xf0f0f0f0f0f0f0f0) >> 4 ) | ((value & 0x0f0f0f0f0f0f0f0f) << 4 ); + value = ((value & 0xff00ff00ff00ff00) >> 8 ) | ((value & 0x00ff00ff00ff00ff) << 8 ); + value = ((value & 0xffff0000ffff0000) >> 16) | ((value & 0x0000ffff0000ffff) << 16); - return (Value >> 32) | (Value << 32); + return (value >> 32) | (value << 32); } } }
\ No newline at end of file diff --git a/Ryujinx.Common/HexUtils.cs b/Ryujinx.Common/HexUtils.cs new file mode 100644 index 00000000..63587cea --- /dev/null +++ b/Ryujinx.Common/HexUtils.cs @@ -0,0 +1,90 @@ +using System; +using System.Text; + +namespace Ryujinx.Common +{ + public static class HexUtils + { + private static readonly char[] HexChars = "0123456789ABCDEF".ToCharArray(); + + private const int HexTableColumnWidth = 8; + private const int HexTableColumnSpace = 3; + + // Modified for Ryujinx + // Original by Pascal Ganaye - CPOL License + // https://www.codeproject.com/Articles/36747/Quick-and-Dirty-HexDump-of-a-Byte-Array + public static string HexTable(byte[] bytes, int bytesPerLine = 16) + { + if (bytes == null) + { + return "<null>"; + } + + int bytesLength = bytes.Length; + + int firstHexColumn = + HexTableColumnWidth + + HexTableColumnSpace; + + int firstCharColumn = firstHexColumn + + bytesPerLine * HexTableColumnSpace + + (bytesPerLine - 1) / HexTableColumnWidth + + 2; + + int lineLength = firstCharColumn + + bytesPerLine + + Environment.NewLine.Length; + + 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(); + } + } +} diff --git a/Ryujinx.Common/Logging/LogEventArgs.cs b/Ryujinx.Common/Logging/LogEventArgs.cs index 7a479b71..363a45b9 100644 --- a/Ryujinx.Common/Logging/LogEventArgs.cs +++ b/Ryujinx.Common/Logging/LogEventArgs.cs @@ -4,16 +4,28 @@ namespace Ryujinx.Common.Logging { public class LogEventArgs : EventArgs { - public LogLevel Level { get; private set; } - public TimeSpan Time { get; private set; } + public LogLevel Level { get; private set; } + public TimeSpan Time { get; private set; } + public int ThreadId { get; private set; } public string Message { get; private set; } + public object Data { get; private set; } - public LogEventArgs(LogLevel Level, TimeSpan Time, string Message) + public LogEventArgs(LogLevel level, TimeSpan time, int threadId, string message) { - this.Level = Level; - this.Time = Time; - this.Message = Message; + this.Level = level; + this.Time = time; + this.ThreadId = threadId; + this.Message = message; + } + + public LogEventArgs(LogLevel level, TimeSpan time, int threadId, string message, object data) + { + this.Level = level; + this.Time = time; + this.ThreadId = threadId; + this.Message = message; + this.Data = data; } } }
\ No newline at end of file diff --git a/Ryujinx.Common/Logging/Logger.cs b/Ryujinx.Common/Logging/Logger.cs index 6422f113..5e58f806 100644 --- a/Ryujinx.Common/Logging/Logger.cs +++ b/Ryujinx.Common/Logging/Logger.cs @@ -1,78 +1,97 @@ using System; using System.Diagnostics; +using System.Reflection; using System.Runtime.CompilerServices; +using System.Text; +using System.Threading; namespace Ryujinx.Common.Logging { public static class Logger { - private static bool[] EnabledLevels; - private static bool[] EnabledClasses; + private static Stopwatch m_Time; - public static event EventHandler<LogEventArgs> Updated; + private static readonly bool[] m_EnabledLevels; + private static readonly bool[] m_EnabledClasses; - private static Stopwatch Time; + public static event EventHandler<LogEventArgs> Updated; static Logger() { - EnabledLevels = new bool[Enum.GetNames(typeof(LogLevel)).Length]; - EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length]; + m_EnabledLevels = new bool[Enum.GetNames(typeof(LogLevel)).Length]; + m_EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length]; - EnabledLevels[(int)LogLevel.Stub] = true; - EnabledLevels[(int)LogLevel.Info] = true; - EnabledLevels[(int)LogLevel.Warning] = true; - EnabledLevels[(int)LogLevel.Error] = true; + m_EnabledLevels[(int)LogLevel.Stub] = true; + m_EnabledLevels[(int)LogLevel.Info] = true; + m_EnabledLevels[(int)LogLevel.Warning] = true; + m_EnabledLevels[(int)LogLevel.Error] = true; - for (int Index = 0; Index < EnabledClasses.Length; Index++) + for (int index = 0; index < m_EnabledClasses.Length; index++) { - EnabledClasses[Index] = true; + m_EnabledClasses[index] = true; } - Time = new Stopwatch(); + m_Time = Stopwatch.StartNew(); + } + + public static void SetEnable(LogLevel logLevel, bool enabled) + { + m_EnabledLevels[(int)logLevel] = enabled; + } + + public static void SetEnable(LogClass logClass, bool enabled) + { + m_EnabledClasses[(int)logClass] = enabled; + } - Time.Start(); + public static void PrintDebug(LogClass logClass, string message, [CallerMemberName] string caller = "") + { + Print(LogLevel.Debug, logClass, GetFormattedMessage(logClass, message, caller)); } - public static void SetEnable(LogLevel Level, bool Enabled) + public static void PrintInfo(LogClass logClass, string message, [CallerMemberName] string Caller = "") { - EnabledLevels[(int)Level] = Enabled; + Print(LogLevel.Info, logClass, GetFormattedMessage(logClass, message, Caller)); } - public static void SetEnable(LogClass Class, bool Enabled) + public static void PrintWarning(LogClass logClass, string message, [CallerMemberName] string Caller = "") { - EnabledClasses[(int)Class] = Enabled; + Print(LogLevel.Warning, logClass, GetFormattedMessage(logClass, message, Caller)); } - public static void PrintDebug(LogClass Class, string Message, [CallerMemberName] string Caller = "") + public static void PrintError(LogClass logClass, string message, [CallerMemberName] string Caller = "") { - Print(LogLevel.Debug, Class, GetFormattedMessage(Class, Message, Caller)); + Print(LogLevel.Error, logClass, GetFormattedMessage(logClass, message, Caller)); } - public static void PrintStub(LogClass Class, string Message, [CallerMemberName] string Caller = "") + public static void PrintStub(LogClass logClass, string message = "", [CallerMemberName] string caller = "") { - Print(LogLevel.Stub, Class, GetFormattedMessage(Class, Message, Caller)); + Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller)); } - public static void PrintInfo(LogClass Class, string Message, [CallerMemberName] string Caller = "") + public static void PrintStub<T>(LogClass logClass, T obj, [CallerMemberName] string caller = "") { - Print(LogLevel.Info, Class, GetFormattedMessage(Class, Message, Caller)); + Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed.", caller), obj); } - public static void PrintWarning(LogClass Class, string Message, [CallerMemberName] string Caller = "") + public static void PrintStub<T>(LogClass logClass, string message, T obj, [CallerMemberName] string caller = "") { - Print(LogLevel.Warning, Class, GetFormattedMessage(Class, Message, Caller)); + Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller), obj); } - public static void PrintError(LogClass Class, string Message, [CallerMemberName] string Caller = "") + private static void Print(LogLevel logLevel, LogClass logClass, string message) { - Print(LogLevel.Error, Class, GetFormattedMessage(Class, Message, Caller)); + if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass]) + { + Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.ManagedThreadId, message)); + } } - private static void Print(LogLevel Level, LogClass Class, string Message) + private static void Print(LogLevel logLevel, LogClass logClass, string message, object data) { - if (EnabledLevels[(int)Level] && EnabledClasses[(int)Class]) + if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass]) { - Updated?.Invoke(null, new LogEventArgs(Level, Time.Elapsed, Message)); + Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.ManagedThreadId, message, data)); } } |
