2013-09-05 00:17:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-08-17 17:45:50 +00:00
|
|
|
#pragma once
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-09 00:15:08 +00:00
|
|
|
#include "common/log.h"
|
|
|
|
#include "common/string_util.h"
|
|
|
|
#include "common/file_util.h"
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-08-17 17:45:50 +00:00
|
|
|
#include <cstring>
|
2013-09-05 00:17:46 +00:00
|
|
|
#include <set>
|
2014-09-03 05:05:45 +00:00
|
|
|
#include <mutex>
|
2013-09-05 00:17:46 +00:00
|
|
|
|
|
|
|
#define MAX_MESSAGES 8000
|
|
|
|
#define MAX_MSGLEN 1024
|
|
|
|
|
|
|
|
|
|
|
|
// pure virtual interface
|
|
|
|
class LogListener
|
|
|
|
{
|
|
|
|
public:
|
2014-04-01 22:20:08 +00:00
|
|
|
virtual ~LogListener() {}
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
virtual void Log(LogTypes::LOG_LEVELS, const char *msg) = 0;
|
2013-09-05 00:17:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileLogListener : public LogListener
|
|
|
|
{
|
|
|
|
public:
|
2014-04-01 22:20:08 +00:00
|
|
|
FileLogListener(const char *filename);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-10-26 04:56:13 +00:00
|
|
|
void Log(LogTypes::LOG_LEVELS, const char *msg) override;
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
bool IsValid() { return !m_logfile.fail(); }
|
|
|
|
bool IsEnabled() const { return m_enable; }
|
|
|
|
void SetEnable(bool enable) { m_enable = enable; }
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
const char* GetName() const { return "file"; }
|
2013-09-05 00:17:46 +00:00
|
|
|
|
|
|
|
private:
|
2014-04-01 22:20:08 +00:00
|
|
|
std::mutex m_log_lock;
|
|
|
|
std::ofstream m_logfile;
|
|
|
|
bool m_enable;
|
2013-09-05 00:17:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class DebuggerLogListener : public LogListener
|
|
|
|
{
|
|
|
|
public:
|
2014-10-26 04:56:13 +00:00
|
|
|
void Log(LogTypes::LOG_LEVELS, const char *msg) override;
|
2013-09-05 00:17:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class LogContainer
|
|
|
|
{
|
|
|
|
public:
|
2014-04-01 22:20:08 +00:00
|
|
|
LogContainer(const char* shortName, const char* fullName, bool enable = false);
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
const char* GetShortName() const { return m_shortName; }
|
|
|
|
const char* GetFullName() const { return m_fullName; }
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
void AddListener(LogListener* listener);
|
|
|
|
void RemoveListener(LogListener* listener);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
void Trigger(LogTypes::LOG_LEVELS, const char *msg);
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
bool IsEnabled() const { return m_enable; }
|
|
|
|
void SetEnable(bool enable) { m_enable = enable; }
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
LogTypes::LOG_LEVELS GetLevel() const { return m_level; }
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
void SetLevel(LogTypes::LOG_LEVELS level) { m_level = level; }
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
bool HasListeners() const { return !m_listeners.empty(); }
|
2013-09-05 00:17:46 +00:00
|
|
|
|
|
|
|
private:
|
2014-04-01 22:20:08 +00:00
|
|
|
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;
|
2013-09-05 00:17:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ConsoleListener;
|
|
|
|
|
|
|
|
class LogManager : NonCopyable
|
|
|
|
{
|
|
|
|
private:
|
2014-04-01 22:20:08 +00:00
|
|
|
LogContainer* m_Log[LogTypes::NUMBER_OF_LOGS];
|
|
|
|
FileLogListener *m_fileLog;
|
|
|
|
ConsoleListener *m_consoleLog;
|
|
|
|
DebuggerLogListener *m_debuggerLog;
|
|
|
|
static LogManager *m_logManager; // Singleton. Ugh.
|
|
|
|
|
|
|
|
LogManager();
|
|
|
|
~LogManager();
|
2013-09-05 00:17:46 +00:00
|
|
|
public:
|
|
|
|
|
2014-06-01 15:49:58 +00:00
|
|
|
static u32 GetMaxLevel() { return LogTypes::MAX_LOGLEVEL; }
|
2014-04-01 22:20:08 +00:00
|
|
|
|
2014-11-19 08:49:13 +00:00
|
|
|
void Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char* file, int line,
|
2014-05-30 03:03:03 +00:00
|
|
|
const char* function, const char *fmt, va_list args);
|
2014-04-01 22:20:08 +00:00
|
|
|
|
|
|
|
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();
|
2013-09-05 00:17:46 +00:00
|
|
|
};
|