aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Common/Logging/Logger.cs
blob: 88ebe4731ac982144d0de1fed1f377410f139c82 (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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;

namespace Ryujinx.Common.Logging
{
    public static class Logger
    {
        private static Stopwatch m_Time;

        private static readonly bool[] m_EnabledLevels;
        private static readonly bool[] m_EnabledClasses;

        private static readonly List<ILogTarget> m_LogTargets;

        public static event EventHandler<LogEventArgs> Updated;

        static Logger()
        {
            m_EnabledLevels  = new bool[Enum.GetNames(typeof(LogLevel)).Length];
            m_EnabledClasses = new bool[Enum.GetNames(typeof(LogClass)).Length];

            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 < m_EnabledClasses.Length; index++)
            {
                m_EnabledClasses[index] = true;
            }

            m_LogTargets = new List<ILogTarget>();

            m_Time = Stopwatch.StartNew();
        }

        public static void AddTarget(ILogTarget target)
        {
            m_LogTargets.Add(target);

            Updated += target.Log;
        }

        public static void Shutdown()
        {
            Updated = null;

            foreach(var target in m_LogTargets)
            {
                target.Dispose();
            }

            m_LogTargets.Clear();
        }

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

        public static void PrintDebug(LogClass logClass, string message, [CallerMemberName] string caller = "")
        {
            Print(LogLevel.Debug, logClass, GetFormattedMessage(logClass, message, caller));
        }

        public static void PrintInfo(LogClass logClass, string message, [CallerMemberName] string Caller = "")
        {
            Print(LogLevel.Info, logClass, GetFormattedMessage(logClass, message, Caller));
        }

        public static void PrintWarning(LogClass logClass, string message, [CallerMemberName] string Caller = "")
        {
            Print(LogLevel.Warning, logClass, GetFormattedMessage(logClass, message, Caller));
        }

        public static void PrintError(LogClass logClass, string message, [CallerMemberName] string Caller = "")
        {
            Print(LogLevel.Error, logClass, GetFormattedMessage(logClass, message, Caller));
        }

        public static void PrintStub(LogClass logClass, string message = "", [CallerMemberName] string caller = "")
        {
            Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller));
        }

        public static void PrintStub<T>(LogClass logClass, T obj, [CallerMemberName] string caller = "")
        {
            Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed.", caller), obj);
        }

        public static void PrintStub<T>(LogClass logClass, string message, T obj, [CallerMemberName] string caller = "")
        {
            Print(LogLevel.Stub, logClass, GetFormattedMessage(logClass, "Stubbed. " + message, caller), obj);
        }

        private static void Print(LogLevel logLevel, LogClass logClass, string message)
        {
            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 logLevel, LogClass logClass, string message, object data)
        {
            if (m_EnabledLevels[(int)logLevel] && m_EnabledClasses[(int)logClass])
            {
                Updated?.Invoke(null, new LogEventArgs(logLevel, m_Time.Elapsed, Thread.CurrentThread.ManagedThreadId, message, data));
            }
        }

        private static string GetFormattedMessage(LogClass Class, string Message, string Caller)
        {
            return $"{Class} {Caller}: {Message}";
        }
    }
}