2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// 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>
|
2014-08-13 23:14:35 -06:00
|
|
|
#include <mutex>
|
2011-04-01 01:43:02 -06:00
|
|
|
#include <set>
|
2014-06-05 16:24:10 -06:00
|
|
|
#include <string>
|
2009-03-18 11:17:58 -06:00
|
|
|
|
2014-02-19 20:11:52 -07:00
|
|
|
#include "Common/Common.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:
|
2014-06-05 16:24:10 -06:00
|
|
|
FileLogListener(const std::string& 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
|
|
|
|
2015-04-14 22:38:21 -06:00
|
|
|
bool IsValid() const { return m_logfile.good(); }
|
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;
|
|
|
|
};
|
|
|
|
|
2011-04-01 01:43:02 -06:00
|
|
|
class LogContainer
|
|
|
|
{
|
2009-03-18 11:17:58 -06:00
|
|
|
public:
|
2014-06-05 16:24:10 -06:00
|
|
|
LogContainer(const std::string& shortName, const std::string& fullName, bool enable = false);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-06-05 16:24:10 -06:00
|
|
|
std::string GetShortName() const { return m_shortName; }
|
|
|
|
std::string 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:
|
2014-06-05 16:24:10 -06:00
|
|
|
std::string m_fullName;
|
|
|
|
std::string m_shortName;
|
2009-03-18 11:17:58 -06:00
|
|
|
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;
|
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
|
|
|
}
|
|
|
|
|
2014-12-28 13:46:00 -07:00
|
|
|
bool IsEnabled(LogTypes::LOG_TYPE type, LogTypes::LOG_LEVELS level = LogTypes::LNOTICE) const
|
2011-04-01 01:43:02 -06:00
|
|
|
{
|
2014-12-28 13:46:00 -07:00
|
|
|
return m_Log[type]->IsEnabled() && m_Log[type]->GetLevel() >= level;
|
2009-09-04 05:34:21 -06:00
|
|
|
}
|
|
|
|
|
2014-06-05 16:24:10 -06:00
|
|
|
std::string GetShortName(LogTypes::LOG_TYPE type) const
|
2011-04-01 01:43:02 -06:00
|
|
|
{
|
|
|
|
return m_Log[type]->GetShortName();
|
2009-03-18 11:17:58 -06:00
|
|
|
}
|
|
|
|
|
2014-06-05 16:24:10 -06:00
|
|
|
std::string GetFullName(LogTypes::LOG_TYPE type) const
|
2011-04-01 01:43:02 -06:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|