LogManager: remove per-container listeners

This commit is contained in:
Michael Maltese 2017-07-07 15:23:47 -07:00
parent 546fa628fb
commit 18da6dee8e
4 changed files with 21 additions and 78 deletions

View File

@ -8,7 +8,6 @@
#include <ostream>
#include <string>
#include "Common/BitSet.h"
#include "Common/CommonPaths.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
@ -58,19 +57,12 @@ public:
std::string GetShortName() const { return m_short_name; }
std::string GetFullName() const { return m_full_name; }
void AddListener(LogListener::LISTENER id) { m_listener_ids[id] = 1; }
void RemoveListener(LogListener::LISTENER id) { m_listener_ids[id] = 0; }
bool IsEnabled() const { return m_enable; }
void SetEnable(bool enable) { m_enable = enable; }
bool HasListeners() const { return bool(m_listener_ids); }
typedef class BitSet32::Iterator iterator;
iterator begin() const { return m_listener_ids.begin(); }
iterator end() const { return m_listener_ids.end(); }
private:
std::string m_full_name;
std::string m_short_name;
bool m_enable;
BitSet32 m_listener_ids;
};
void GenericLog(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char* file, int line,
@ -169,18 +161,15 @@ LogManager::LogManager()
verbosity = MAX_LOGLEVEL;
SetLogLevel(static_cast<LogTypes::LOG_LEVELS>(verbosity));
EnableListener(LogListener::FILE_LISTENER, write_file);
EnableListener(LogListener::CONSOLE_LISTENER, write_console);
EnableListener(LogListener::LOG_WINDOW_LISTENER, write_window);
for (LogContainer* container : m_log)
{
bool enable;
logs->Get(container->GetShortName(), &enable, false);
container->SetEnable(enable);
if (enable && write_file)
container->AddListener(LogListener::FILE_LISTENER);
if (enable && write_console)
container->AddListener(LogListener::CONSOLE_LISTENER);
if (enable && write_window)
container->AddListener(LogListener::LOG_WINDOW_LISTENER);
}
m_path_cutoff_point = DeterminePathCutOffPoint();
@ -208,7 +197,7 @@ void LogManager::LogWithFullPath(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE
char temp[MAX_MSGLEN];
LogContainer* log = m_log[type];
if (!log->IsEnabled() || level > GetLogLevel() || !log->HasListeners())
if (!log->IsEnabled() || level > GetLogLevel() || !static_cast<bool>(m_listener_ids))
return;
CharArrayFromFormatV(temp, MAX_MSGLEN, format, args);
@ -217,7 +206,7 @@ void LogManager::LogWithFullPath(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE
"%s %s:%u %c[%s]: %s\n", Common::Timer::GetTimeFormatted().c_str(), file, line,
LogTypes::LOG_LEVEL_TO_CHAR[(int)level], log->GetShortName().c_str(), temp);
for (auto listener_id : *log)
for (auto listener_id : m_listener_ids)
if (m_listeners[listener_id])
m_listeners[listener_id]->Log(level, msg.c_str());
}
@ -257,14 +246,14 @@ void LogManager::RegisterListener(LogListener::LISTENER id, LogListener* listene
m_listeners[id] = listener;
}
void LogManager::AddListener(LogTypes::LOG_TYPE type, LogListener::LISTENER id)
void LogManager::EnableListener(LogListener::LISTENER id, bool enable)
{
m_log[type]->AddListener(id);
m_listener_ids[id] = enable;
}
void LogManager::RemoveListener(LogTypes::LOG_TYPE type, LogListener::LISTENER id)
bool LogManager::IsListenerEnabled(LogListener::LISTENER id) const
{
m_log[type]->RemoveListener(id);
return m_listener_ids[id];
}
// Singleton. Ugh.

View File

@ -8,6 +8,7 @@
#include <cstdarg>
#include <string>
#include "Common/BitSet.h"
#include "Common/Logging/Log.h"
#include "Common/NonCopyable.h"
@ -50,9 +51,10 @@ public:
std::string GetShortName(LogTypes::LOG_TYPE type) const;
std::string GetFullName(LogTypes::LOG_TYPE type) const;
void RegisterListener(LogListener::LISTENER id, LogListener* listener);
void AddListener(LogTypes::LOG_TYPE type, LogListener::LISTENER id);
void RemoveListener(LogTypes::LOG_TYPE type, LogListener::LISTENER id);
void EnableListener(LogListener::LISTENER id, bool enable);
bool IsListenerEnabled(LogListener::LISTENER id) const;
private:
LogManager();
@ -61,5 +63,6 @@ private:
LogTypes::LOG_LEVELS m_level;
LogContainer* m_log[LogTypes::NUMBER_OF_LOGS];
std::array<LogListener*, LogListener::NUMBER_OF_LISTENERS> m_listeners{};
BitSet32 m_listener_ids;
size_t m_path_cutoff_point = 0;
};

View File

@ -163,47 +163,20 @@ void LogConfigWindow::OnVerbosityChange(wxCommandEvent& event)
void LogConfigWindow::OnWriteFileChecked(wxCommandEvent& event)
{
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
{
m_writeFile = event.IsChecked();
if (m_checks->IsChecked(i))
{
if (m_writeFile)
m_LogManager->AddListener((LogTypes::LOG_TYPE)i, LogListener::FILE_LISTENER);
else
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, LogListener::FILE_LISTENER);
}
}
m_writeFile = event.IsChecked();
m_LogManager->EnableListener(LogListener::FILE_LISTENER, m_writeFile);
}
void LogConfigWindow::OnWriteConsoleChecked(wxCommandEvent& event)
{
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
{
m_writeConsole = event.IsChecked();
if (m_checks->IsChecked(i))
{
if (m_writeConsole)
m_LogManager->AddListener((LogTypes::LOG_TYPE)i, LogListener::CONSOLE_LISTENER);
else
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, LogListener::CONSOLE_LISTENER);
}
}
m_writeConsole = event.IsChecked();
m_LogManager->EnableListener(LogListener::CONSOLE_LISTENER, m_writeConsole);
}
void LogConfigWindow::OnWriteWindowChecked(wxCommandEvent& event)
{
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
{
m_writeWindow = event.IsChecked();
if (m_checks->IsChecked(i))
{
if (m_writeWindow)
m_LogManager->AddListener((LogTypes::LOG_TYPE)i, LogListener::LOG_WINDOW_LISTENER);
else
m_LogManager->RemoveListener((LogTypes::LOG_TYPE)i, LogListener::LOG_WINDOW_LISTENER);
}
}
m_writeWindow = event.IsChecked();
m_LogManager->EnableListener(LogListener::LOG_WINDOW_LISTENER, m_writeWindow);
}
void LogConfigWindow::OnToggleAll(wxCommandEvent& WXUNUSED(event))
@ -217,26 +190,8 @@ void LogConfigWindow::OnToggleAll(wxCommandEvent& WXUNUSED(event))
void LogConfigWindow::ToggleLog(int _logType, bool enable)
{
LogTypes::LOG_TYPE logType = (LogTypes::LOG_TYPE)_logType;
m_checks->Check(_logType, enable);
m_LogManager->SetEnable(logType, enable);
if (enable)
{
if (m_writeWindow)
m_LogManager->AddListener(logType, LogListener::LOG_WINDOW_LISTENER);
if (m_writeFile)
m_LogManager->AddListener(logType, LogListener::FILE_LISTENER);
if (m_writeConsole)
m_LogManager->AddListener(logType, LogListener::CONSOLE_LISTENER);
}
else
{
m_LogManager->RemoveListener(logType, LogListener::LOG_WINDOW_LISTENER);
m_LogManager->RemoveListener(logType, LogListener::FILE_LISTENER);
m_LogManager->RemoveListener(logType, LogListener::CONSOLE_LISTENER);
}
}
void LogConfigWindow::OnLogCheck(wxCommandEvent& event)

View File

@ -133,11 +133,7 @@ void CLogWindow::RemoveAllListeners()
return;
m_has_listeners = false;
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i)
{
m_LogManager->RemoveListener(static_cast<LogTypes::LOG_TYPE>(i),
LogListener::LOG_WINDOW_LISTENER);
}
m_LogManager->EnableListener(LogListener::LOG_WINDOW_LISTENER, false);
}
void CLogWindow::SaveSettings()