aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Core/Logging.cs
blob: f6e2079a692c6f3059f24bd6161224bdb56400ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using Ryujinx.Core.OsHle.Ipc;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace Ryujinx.Core
{
    public static class Logging
    {
        private static Stopwatch ExecutionTime;

        private const string LogFileName = "Ryujinx.log";

        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;
        private static bool   EnableStub         = Config.LoggingEnableStub;
        private static bool   EnableIpc          = Config.LoggingEnableIpc;
        private static bool   EnableFilter       = Config.LoggingEnableFilter;
        private static bool   EnableLogFile      = Config.LoggingEnableLogFile;
        private static bool[] FilteredLogClasses = Config.LoggingFilteredClasses;

        private enum LogLevel
        {
            Debug,
            Error,
            Fatal,
            Info,
            Stub,
            Trace,
            Warn
        }

        static Logging()
        {
            if (File.Exists(LogFileName)) File.Delete(LogFileName);

            ExecutionTime = new Stopwatch();

            ExecutionTime.Start();
        }

        public static string GetExecutionTime() => ExecutionTime.ElapsedMilliseconds.ToString().PadLeft(8, '0') + "ms";

        private static void LogMessage(LogEntry LogEntry)
        {
            if (EnableFilter)
                if (!FilteredLogClasses[(int)LogEntry.LogClass])
                    return;

            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;
                case LogLevel.Stub:
                    consoleColor = ConsoleColor.DarkYellow;
                    break;
                case LogLevel.Trace:
                    consoleColor = ConsoleColor.DarkGray;
                    break;
                case LogLevel.Warn:
                    consoleColor = ConsoleColor.Yellow;
                    break;
            }

            LogEntry.ManagedThreadId = Thread.CurrentThread.ManagedThreadId;

            string Text = $"{LogEntry.ExecutionTime} | {LogEntry.ManagedThreadId} > {LogEntry.LogClass} > " +
                $"{LogEntry.LogLevel.ToString()}  > {LogEntry.CallingMember} > {LogEntry.Message}";

            Console.ForegroundColor = consoleColor;
            Console.WriteLine(Text.PadLeft(Text.Length + 1, ' '));
            Console.ResetColor();

            LogFile(Text);
        }

        private static void LogFile(string Message)
        {
            if (EnableLogFile)
            {
                using (StreamWriter Writer = File.AppendText(LogFileName))
                {
                    Writer.WriteLine(Message);
                }
            }
        }

        public static void Info(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
        {
            if (EnableInfo)
            {
                LogMessage(new LogEntry
                {
                    CallingMember = CallingMember,
                    LogLevel      = LogLevel.Info,
                    LogClass      = LogClass,
                    Message       = Message,
                    ExecutionTime = GetExecutionTime()
                });
            }
        }

        public static void Trace(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
        {
            if (EnableTrace)
            {
                LogMessage(new LogEntry
                {
                    CallingMember = CallingMember,
                    LogLevel      = LogLevel.Trace,
                    LogClass      = LogClass,
                    Message       = Message,
                    ExecutionTime = GetExecutionTime()
                });
            }
        }

        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()
                });
            }
        }

        public static void Debug(LogClass LogClass,string Message, [CallerMemberName] string CallingMember = "")
        {
            if (EnableDebug)
            {
                LogMessage(new LogEntry
                {
                    CallingMember = CallingMember,
                    LogLevel      = LogLevel.Debug,
                    LogClass      = LogClass,
                    Message       = Message,
                    ExecutionTime = GetExecutionTime()
                });
            }
        }

        public static void Warn(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
        {
            if (EnableWarn)
            {
                LogMessage(new LogEntry
                {
                    CallingMember = CallingMember,
                    LogLevel      = LogLevel.Warn,
                    LogClass      = LogClass,
                    Message       = Message,
                    ExecutionTime = GetExecutionTime()
                });
            }
        }

        public static void Error(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
        {
            if (EnableError)
            {
                LogMessage(new LogEntry
                {
                    CallingMember = CallingMember,
                    LogLevel      = LogLevel.Error,
                    LogClass      = LogClass,
                    Message       = Message,
                    ExecutionTime = GetExecutionTime()
                });
            }
        }

        public static void Fatal(LogClass LogClass, string Message, [CallerMemberName] string CallingMember = "")
        {
            if (EnableFatal)
            {
                LogMessage(new LogEntry
                {
                    CallingMember = CallingMember,
                    LogLevel      = LogLevel.Fatal,
                    LogClass      = LogClass,
                    Message       = Message,
                    ExecutionTime = GetExecutionTime()
                });
            }
        }

        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
                + 2;                  // 2 spaces

            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();
        }

        private struct LogEntry
        {
            public string   CallingMember;
            public string   ExecutionTime;
            public string   Message;
            public int      ManagedThreadId;
            public LogClass LogClass;
            public LogLevel LogLevel;
        }
    }
}