aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Common/Logging/Targets/ConsoleLogTarget.cs
blob: 7b77c4f28e0c04cbfa1f7131cd9270e3730c9697 (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
using System;

namespace Ryujinx.Common.Logging
{
    public class ConsoleLogTarget : ILogTarget
    {
        private readonly ILogFormatter _formatter;

        private readonly string _name;

        string ILogTarget.Name { get => _name; }

        private static ConsoleColor GetLogColor(LogLevel level) => level switch {
            LogLevel.Info    => ConsoleColor.White,
            LogLevel.Warning => ConsoleColor.Yellow,
            LogLevel.Error   => ConsoleColor.Red,
            LogLevel.Stub    => ConsoleColor.DarkGray,
            LogLevel.Notice  => ConsoleColor.Cyan,
            LogLevel.Trace   => ConsoleColor.DarkCyan,
            _                => ConsoleColor.Gray,
        };

        public ConsoleLogTarget(string name)
        {
            _formatter = new DefaultLogFormatter();
            _name      = name;
        }

        public void Log(object sender, LogEventArgs args)
        {
            Console.ForegroundColor = GetLogColor(args.Level);
            Console.WriteLine(_formatter.Format(args));
            Console.ResetColor();
        }

        public void Dispose()
        {
            Console.ResetColor();
        }
    }
}