2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <fstream>
|
2014-08-14 05:14:35 +00:00
|
|
|
#include <mutex>
|
2011-04-01 07:43:02 +00:00
|
|
|
#include <set>
|
2014-06-05 22:24:10 +00:00
|
|
|
#include <string>
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2014-02-20 03:11:52 +00:00
|
|
|
#include "Common/Common.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2013-03-20 01:51:12 +00:00
|
|
|
#define MAX_MESSAGES 8000
|
2010-04-10 15:41:44 +00:00
|
|
|
#define MAX_MSGLEN 1024
|
2009-03-18 17:17:58 +00:00
|
|
|
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
// pure virtual interface
|
|
|
|
class LogListener
|
|
|
|
{
|
2009-03-18 17:17:58 +00:00
|
|
|
public:
|
2009-03-20 19:12:04 +00:00
|
|
|
virtual ~LogListener() {}
|
2011-04-01 07:43:02 +00:00
|
|
|
|
2010-12-14 17:52:01 +00:00
|
|
|
virtual void Log(LogTypes::LOG_LEVELS, const char *msg) = 0;
|
2009-03-18 17:17:58 +00:00
|
|
|
};
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
class FileLogListener : public LogListener
|
|
|
|
{
|
2009-03-18 17:17:58 +00:00
|
|
|
public:
|
2014-06-05 22:24:10 +00:00
|
|
|
FileLogListener(const std::string& filename);
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2014-03-08 00:54:44 +00:00
|
|
|
void Log(LogTypes::LOG_LEVELS, const char *msg) override;
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2013-08-15 08:47:47 +00:00
|
|
|
bool IsValid() { return !m_logfile.fail(); }
|
2011-04-01 07:43:02 +00:00
|
|
|
bool IsEnabled() const { return m_enable; }
|
|
|
|
void SetEnable(bool enable) { m_enable = enable; }
|
2009-03-20 19:12:04 +00:00
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
const char* GetName() const { return "file"; }
|
2009-03-20 19:12:04 +00:00
|
|
|
|
2009-03-18 17:17:58 +00:00
|
|
|
private:
|
2011-04-01 07:43:02 +00:00
|
|
|
std::mutex m_log_lock;
|
2011-03-11 10:21:46 +00:00
|
|
|
std::ofstream m_logfile;
|
2009-03-18 17:17:58 +00:00
|
|
|
bool m_enable;
|
|
|
|
};
|
|
|
|
|
2014-02-23 06:33:03 +00:00
|
|
|
class DebuggerLogListener : public LogListener
|
|
|
|
{
|
|
|
|
public:
|
2014-03-08 00:54:44 +00:00
|
|
|
void Log(LogTypes::LOG_LEVELS, const char *msg) override;
|
2014-02-23 06:33:03 +00:00
|
|
|
};
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
class LogContainer
|
|
|
|
{
|
2009-03-18 17:17:58 +00:00
|
|
|
public:
|
2014-06-05 22:24:10 +00:00
|
|
|
LogContainer(const std::string& shortName, const std::string& fullName, bool enable = false);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-06-05 22:24:10 +00:00
|
|
|
std::string GetShortName() const { return m_shortName; }
|
|
|
|
std::string GetFullName() const { return m_fullName; }
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
void AddListener(LogListener* listener);
|
|
|
|
void RemoveListener(LogListener* listener);
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
void Trigger(LogTypes::LOG_LEVELS, const char *msg);
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
bool IsEnabled() const { return m_enable; }
|
|
|
|
void SetEnable(bool enable) { m_enable = enable; }
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2014-02-17 04:51:41 +00:00
|
|
|
LogTypes::LOG_LEVELS GetLevel() const { return m_level; }
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2014-02-17 04:51:41 +00:00
|
|
|
void SetLevel(LogTypes::LOG_LEVELS level) { m_level = level; }
|
2011-04-01 07:43:02 +00:00
|
|
|
|
|
|
|
bool HasListeners() const { return !m_listeners.empty(); }
|
2009-03-18 17:17:58 +00:00
|
|
|
|
|
|
|
private:
|
2014-06-05 22:24:10 +00:00
|
|
|
std::string m_fullName;
|
|
|
|
std::string m_shortName;
|
2009-03-18 17:17:58 +00:00
|
|
|
bool m_enable;
|
2010-12-14 17:52:01 +00:00
|
|
|
LogTypes::LOG_LEVELS m_level;
|
2011-04-01 07:43:02 +00:00
|
|
|
std::mutex m_listeners_lock;
|
|
|
|
std::set<LogListener*> m_listeners;
|
2009-03-18 17:17:58 +00:00
|
|
|
};
|
|
|
|
|
2014-02-23 06:33:03 +00:00
|
|
|
class ConsoleListener;
|
|
|
|
|
2010-11-15 05:29:10 +00:00
|
|
|
class LogManager : NonCopyable
|
2009-03-18 17:17:58 +00:00
|
|
|
{
|
|
|
|
private:
|
2010-12-14 17:52:01 +00:00
|
|
|
LogContainer* m_Log[LogTypes::NUMBER_OF_LOGS];
|
2009-03-18 17:17:58 +00:00
|
|
|
FileLogListener *m_fileLog;
|
2014-02-23 06:33:03 +00:00
|
|
|
ConsoleListener *m_consoleLog;
|
|
|
|
DebuggerLogListener *m_debuggerLog;
|
2009-03-20 19:12:04 +00:00
|
|
|
static LogManager *m_logManager; // Singleton. Ugh.
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2010-01-19 19:28:27 +00:00
|
|
|
LogManager();
|
|
|
|
~LogManager();
|
2010-02-19 18:50:01 +00:00
|
|
|
public:
|
2010-01-19 19:28:27 +00:00
|
|
|
|
2014-02-17 04:51:41 +00:00
|
|
|
static u32 GetMaxLevel() { return MAX_LOGLEVEL; }
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2013-10-29 05:23:17 +00:00
|
|
|
void Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type,
|
2010-01-03 08:48:48 +00:00
|
|
|
const char *file, int line, const char *fmt, va_list args);
|
2009-03-18 17:17:58 +00:00
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
void SetLogLevel(LogTypes::LOG_TYPE type, LogTypes::LOG_LEVELS level)
|
|
|
|
{
|
|
|
|
m_Log[type]->SetLevel(level);
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
void SetEnable(LogTypes::LOG_TYPE type, bool enable)
|
|
|
|
{
|
|
|
|
m_Log[type]->SetEnable(enable);
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
bool IsEnabled(LogTypes::LOG_TYPE type) const
|
|
|
|
{
|
|
|
|
return m_Log[type]->IsEnabled();
|
2009-09-04 11:34:21 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 22:24:10 +00:00
|
|
|
std::string GetShortName(LogTypes::LOG_TYPE type) const
|
2011-04-01 07:43:02 +00:00
|
|
|
{
|
|
|
|
return m_Log[type]->GetShortName();
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 22:24:10 +00:00
|
|
|
std::string GetFullName(LogTypes::LOG_TYPE type) const
|
2011-04-01 07:43:02 +00:00
|
|
|
{
|
|
|
|
return m_Log[type]->GetFullName();
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
void AddListener(LogTypes::LOG_TYPE type, LogListener *listener)
|
|
|
|
{
|
|
|
|
m_Log[type]->AddListener(listener);
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
void RemoveListener(LogTypes::LOG_TYPE type, LogListener *listener)
|
|
|
|
{
|
|
|
|
m_Log[type]->RemoveListener(listener);
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
FileLogListener *GetFileListener() const
|
|
|
|
{
|
2009-03-18 17:17:58 +00:00
|
|
|
return m_fileLog;
|
|
|
|
}
|
|
|
|
|
2014-02-23 06:33:03 +00:00
|
|
|
ConsoleListener *GetConsoleListener() const
|
|
|
|
{
|
|
|
|
return m_consoleLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebuggerLogListener *GetDebuggerListener() const
|
|
|
|
{
|
|
|
|
return m_debuggerLog;
|
|
|
|
}
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
static LogManager* GetInstance()
|
|
|
|
{
|
2009-03-18 17:17:58 +00:00
|
|
|
return m_logManager;
|
|
|
|
}
|
|
|
|
|
2011-04-01 07:43:02 +00:00
|
|
|
static void SetInstance(LogManager *logManager)
|
|
|
|
{
|
2009-03-18 17:17:58 +00:00
|
|
|
m_logManager = logManager;
|
|
|
|
}
|
2010-02-16 04:34:41 +00:00
|
|
|
|
|
|
|
static void Init();
|
|
|
|
static void Shutdown();
|
2009-03-18 17:17:58 +00:00
|
|
|
};
|