aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx/Ui/ConsoleLog.cs
blob: 1ecd4cde446e5b1387a61cb52eb1fd2efc59f906 (plain) (blame)
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
using Ryujinx.Common.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace Ryujinx
{
    static class ConsoleLog
    {
        private static Thread _messageThread;

        private static BlockingCollection<LogEventArgs> _messageQueue;

        private static Dictionary<LogLevel, ConsoleColor> _logColors;

        private static object _consoleLock;

        static ConsoleLog()
        {
            _logColors = new Dictionary<LogLevel, ConsoleColor>()
            {
                { LogLevel.Stub,    ConsoleColor.DarkGray },
                { LogLevel.Info,    ConsoleColor.White    },
                { LogLevel.Warning, ConsoleColor.Yellow   },
                { LogLevel.Error,   ConsoleColor.Red      }
            };

            _messageQueue = new BlockingCollection<LogEventArgs>();

            _consoleLock = new object();

            _messageThread = new Thread(() =>
            {
                while (!_messageQueue.IsCompleted)
                {
                    try
                    {
                        PrintLog(_messageQueue.Take());
                    }
                    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.
                    }
                }
            });

            _messageThread.IsBackground = true;
            _messageThread.Start();
        }

        private static void PrintLog(LogEventArgs e)
        {
            string formattedTime = e.Time.ToString(@"hh\:mm\:ss\.fff");

            string currentThread = Thread.CurrentThread.ManagedThreadId.ToString("d4");
            
            string message = formattedTime + " | " + currentThread + " " + e.Message;

            if (_logColors.TryGetValue(e.Level, out ConsoleColor color))
            {
                lock (_consoleLock)
                {
                    Console.ForegroundColor = color;

                    Console.WriteLine(message);
                    Console.ResetColor();
                }
            }
            else
            {
                Console.WriteLine(message);
            }
        }

        public static void Log(object sender, LogEventArgs e)
        {
            if (!_messageQueue.IsAddingCompleted)
            {
                _messageQueue.Add(e);
            }
        }
    }
}