summaryrefslogtreecommitdiffstats
path: root/src/common/log_manager.h
blob: 579198ff6d4ab181f22d4d1f218f4e6d8a39b246 (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
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
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#ifndef _LOGMANAGER_H_
#define _LOGMANAGER_H_

#include "log.h"
#include "string_util.h"
#include "thread.h"
#include "file_util.h"

#include <set>
#include <string.h>

#define MAX_MESSAGES 8000
#define MAX_MSGLEN  1024


// pure virtual interface
class LogListener
{
public:
    virtual ~LogListener() {}

    virtual void Log(LogTypes::LOG_LEVELS, const char *msg) = 0;
};

class FileLogListener : public LogListener
{
public:
    FileLogListener(const char *filename);

    void Log(LogTypes::LOG_LEVELS, const char *msg);

    bool IsValid() { return !m_logfile.fail(); }
    bool IsEnabled() const { return m_enable; }
    void SetEnable(bool enable) { m_enable = enable; }

    const char* GetName() const { return "file"; }

private:
    std::mutex m_log_lock;
    std::ofstream m_logfile;
    bool m_enable;
};

class DebuggerLogListener : public LogListener
{
public:
    void Log(LogTypes::LOG_LEVELS, const char *msg);
};

class LogContainer
{
public:
    LogContainer(const char* shortName, const char* fullName, bool enable = false);
    
    const char* GetShortName() const { return m_shortName; }
    const char* GetFullName() const { return m_fullName; }

    void AddListener(LogListener* listener);
    void RemoveListener(LogListener* listener);

    void Trigger(LogTypes::LOG_LEVELS, const char *msg);

    bool IsEnabled() const { return m_enable; }
    void SetEnable(bool enable) { m_enable = enable; }

    LogTypes::LOG_LEVELS GetLevel() const { return m_level;    }

    void SetLevel(LogTypes::LOG_LEVELS level) {    m_level = level; }

    bool HasListeners() const { return !m_listeners.empty(); }

private:
    char m_fullName[128];
    char m_shortName[32];
    bool m_enable;
    LogTypes::LOG_LEVELS m_level;
    std::mutex m_listeners_lock;
    std::set<LogListener*> m_listeners;
};

class ConsoleListener;

class LogManager : NonCopyable
{
private:
    LogContainer* m_Log[LogTypes::NUMBER_OF_LOGS];
    FileLogListener *m_fileLog;
    ConsoleListener *m_consoleLog;
    DebuggerLogListener *m_debuggerLog;
    static LogManager *m_logManager;  // Singleton. Ugh.

    LogManager();
    ~LogManager();
public:

    static u32 GetMaxLevel() { return MAX_LOGLEVEL;    }

    void Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, 
             const char *file, int line, const char *fmt, va_list args);

    void SetLogLevel(LogTypes::LOG_TYPE type, LogTypes::LOG_LEVELS level)
    {
        m_Log[type]->SetLevel(level);
    }

    void SetEnable(LogTypes::LOG_TYPE type, bool enable)
    {
        m_Log[type]->SetEnable(enable);
    }

    bool IsEnabled(LogTypes::LOG_TYPE type) const
    {
        return m_Log[type]->IsEnabled();
    }

    const char* GetShortName(LogTypes::LOG_TYPE type) const
    {
        return m_Log[type]->GetShortName();
    }

    const char* GetFullName(LogTypes::LOG_TYPE type) const
    {
        return m_Log[type]->GetFullName();
    }

    void AddListener(LogTypes::LOG_TYPE type, LogListener *listener)
    {
        m_Log[type]->AddListener(listener);
    }

    void RemoveListener(LogTypes::LOG_TYPE type, LogListener *listener)
    {
        m_Log[type]->RemoveListener(listener);
    }

    FileLogListener *GetFileListener() const
    {
        return m_fileLog;
    }

    ConsoleListener *GetConsoleListener() const
    {
        return m_consoleLog;
    }

    DebuggerLogListener *GetDebuggerListener() const
    {
        return m_debuggerLog;
    }

    static LogManager* GetInstance()
    {
        return m_logManager;
    }

    static void SetInstance(LogManager *logManager)
    {
        m_logManager = logManager;
    }

    static void Init();
    static void Shutdown();
};

#endif // _LOGMANAGER_H_