2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// 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
|
|
|
|
2015-10-03 11:34:36 +00:00
|
|
|
#include <array>
|
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
|
|
|
|
2015-10-03 11:34:36 +00:00
|
|
|
#include "Common/BitSet.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 20:13:54 +00:00
|
|
|
#include "Common/NonCopyable.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
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;
|
2015-10-03 11:34:36 +00:00
|
|
|
|
|
|
|
enum LISTENER
|
|
|
|
{
|
|
|
|
FILE_LISTENER = 0,
|
|
|
|
CONSOLE_LISTENER,
|
|
|
|
LOG_WINDOW_LISTENER,
|
|
|
|
|
|
|
|
NUMBER_OF_LISTENERS // Must be last
|
|
|
|
};
|
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
|
|
|
|
2015-04-15 04:38:21 +00:00
|
|
|
bool IsValid() const { return m_logfile.good(); }
|
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;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2015-10-03 11:34:36 +00:00
|
|
|
void AddListener(LogListener::LISTENER id) { m_listener_ids[id] = 1; }
|
|
|
|
void RemoveListener(LogListener::LISTENER id) { m_listener_ids[id] = 0; }
|
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
|
|
|
|
2015-10-03 12:10:11 +00:00
|
|
|
bool HasListeners() const { return bool(m_listener_ids); }
|
2015-10-03 11:34:36 +00:00
|
|
|
|
|
|
|
typedef class BitSet32::Iterator iterator;
|
|
|
|
iterator begin() const { return m_listener_ids.begin(); }
|
|
|
|
iterator end() const { return m_listener_ids.end(); }
|
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;
|
2015-10-03 11:34:36 +00:00
|
|
|
BitSet32 m_listener_ids;
|
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-20 19:12:04 +00:00
|
|
|
static LogManager *m_logManager; // Singleton. Ugh.
|
2015-10-03 11:34:36 +00:00
|
|
|
std::array<LogListener*, LogListener::NUMBER_OF_LISTENERS> m_listeners;
|
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
|
|
|
}
|
|
|
|
|
2014-12-28 20:46:00 +00:00
|
|
|
bool IsEnabled(LogTypes::LOG_TYPE type, LogTypes::LOG_LEVELS level = LogTypes::LNOTICE) const
|
2011-04-01 07:43:02 +00:00
|
|
|
{
|
2014-12-28 20:46:00 +00:00
|
|
|
return m_Log[type]->IsEnabled() && m_Log[type]->GetLevel() >= level;
|
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
|
|
|
}
|
|
|
|
|
2015-10-03 11:34:36 +00:00
|
|
|
void RegisterListener(LogListener::LISTENER id, LogListener *listener)
|
2011-04-01 07:43:02 +00:00
|
|
|
{
|
2015-10-03 11:34:36 +00:00
|
|
|
m_listeners[id] = listener;
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 11:34:36 +00:00
|
|
|
void AddListener(LogTypes::LOG_TYPE type, LogListener::LISTENER id)
|
2011-04-01 07:43:02 +00:00
|
|
|
{
|
2015-10-03 11:34:36 +00:00
|
|
|
m_Log[type]->AddListener(id);
|
2009-03-18 17:17:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 11:34:36 +00:00
|
|
|
void RemoveListener(LogTypes::LOG_TYPE type, LogListener::LISTENER id)
|
2014-02-23 06:33:03 +00:00
|
|
|
{
|
2015-10-03 11:34:36 +00:00
|
|
|
m_Log[type]->RemoveListener(id);
|
2014-02-23 06:33:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|