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