mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
Merge pull request #5770 from ligfx/lognewconfig
LogManager: use layered config
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
#include <algorithm>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
|
||||
#include "Common/Assert.h"
|
||||
#include "Common/Config/Config.h"
|
||||
@ -144,4 +145,34 @@ const std::string& GetLayerName(LayerType layer)
|
||||
};
|
||||
return layer_to_name.at(layer);
|
||||
}
|
||||
|
||||
bool ConfigLocation::operator==(const ConfigLocation& other) const
|
||||
{
|
||||
return std::tie(system, section, key) == std::tie(other.system, other.section, other.key);
|
||||
}
|
||||
|
||||
bool ConfigLocation::operator!=(const ConfigLocation& other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
bool ConfigLocation::operator<(const ConfigLocation& other) const
|
||||
{
|
||||
return std::tie(system, section, key) < std::tie(other.system, other.section, other.key);
|
||||
}
|
||||
|
||||
LayerType GetActiveLayerForConfig(const ConfigLocation& config)
|
||||
{
|
||||
for (auto layer : SEARCH_ORDER)
|
||||
{
|
||||
if (!LayerExists(layer))
|
||||
continue;
|
||||
|
||||
if (GetLayer(layer)->Exists(config.system, config.section, config.key))
|
||||
return layer;
|
||||
}
|
||||
|
||||
// If config is not present in any layer, base layer is considered active.
|
||||
return LayerType::Base;
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,24 @@
|
||||
|
||||
namespace Config
|
||||
{
|
||||
struct ConfigLocation
|
||||
{
|
||||
System system;
|
||||
std::string section;
|
||||
std::string key;
|
||||
|
||||
bool operator==(const ConfigLocation& other) const;
|
||||
bool operator!=(const ConfigLocation& other) const;
|
||||
bool operator<(const ConfigLocation& other) const;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct ConfigInfo
|
||||
{
|
||||
ConfigLocation location;
|
||||
T default_value;
|
||||
};
|
||||
|
||||
using Layers = std::map<LayerType, std::unique_ptr<Layer>>;
|
||||
using ConfigChangedCallback = std::function<void()>;
|
||||
|
||||
@ -45,4 +63,60 @@ void ClearCurrentRunLayer();
|
||||
const std::string& GetSystemName(System system);
|
||||
System GetSystemFromName(const std::string& system);
|
||||
const std::string& GetLayerName(LayerType layer);
|
||||
LayerType GetActiveLayerForConfig(const ConfigLocation&);
|
||||
|
||||
template <typename T>
|
||||
T Get(LayerType layer, const ConfigInfo<T>& info)
|
||||
{
|
||||
return GetLayer(layer)
|
||||
->GetOrCreateSection(info.location.system, info.location.section)
|
||||
->template Get<T>(info.location.key, info.default_value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T Get(const ConfigInfo<T>& info)
|
||||
{
|
||||
return Get(LayerType::Meta, info);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T GetBase(const ConfigInfo<T>& info)
|
||||
{
|
||||
return Get(LayerType::Base, info);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
LayerType GetActiveLayerForConfig(const ConfigInfo<T>& info)
|
||||
{
|
||||
return GetActiveLayerForConfig(info.location);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Set(LayerType layer, const ConfigInfo<T>& info, const T& value)
|
||||
{
|
||||
GetLayer(layer)
|
||||
->GetOrCreateSection(info.location.system, info.location.section)
|
||||
->Set(info.location.key, value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void SetBase(const ConfigInfo<T>& info, const T& value)
|
||||
{
|
||||
Set<T>(LayerType::Base, info, value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void SetCurrent(const ConfigInfo<T>& info, const T& value)
|
||||
{
|
||||
Set<T>(LayerType::CurrentRun, info, value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void SetBaseOrCurrent(const ConfigInfo<T>& info, const T& value)
|
||||
{
|
||||
if (GetActiveLayerForConfig(info) == LayerType::Base)
|
||||
Set<T>(LayerType::Base, info, value);
|
||||
else
|
||||
Set<T>(LayerType::CurrentRun, info, value);
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,8 @@
|
||||
#include <string>
|
||||
|
||||
#include "Common/CommonPaths.h"
|
||||
#include "Common/Config/Config.h"
|
||||
#include "Common/FileUtil.h"
|
||||
#include "Common/IniFile.h"
|
||||
#include "Common/Logging/ConsoleListener.h"
|
||||
#include "Common/Logging/Log.h"
|
||||
#include "Common/Logging/LogManager.h"
|
||||
@ -19,6 +19,14 @@
|
||||
|
||||
constexpr size_t MAX_MSGLEN = 1024;
|
||||
|
||||
const Config::ConfigInfo<bool> LOGGER_WRITE_TO_FILE{
|
||||
{Config::System::Logger, "Options", "WriteToFile"}, false};
|
||||
const Config::ConfigInfo<bool> LOGGER_WRITE_TO_CONSOLE{
|
||||
{Config::System::Logger, "Options", "WriteToConsole"}, true};
|
||||
const Config::ConfigInfo<bool> LOGGER_WRITE_TO_WINDOW{
|
||||
{Config::System::Logger, "Options", "WriteToWindow"}, true};
|
||||
const Config::ConfigInfo<int> LOGGER_VERBOSITY{{Config::System::Logger, "Options", "Verbosity"}, 0};
|
||||
|
||||
class FileLogListener : public LogListener
|
||||
{
|
||||
public:
|
||||
@ -40,7 +48,6 @@ public:
|
||||
bool IsValid() const { return m_logfile.good(); }
|
||||
bool IsEnabled() const { return m_enable; }
|
||||
void SetEnable(bool enable) { m_enable = enable; }
|
||||
// const char* GetName() const { return "file"; }
|
||||
private:
|
||||
std::mutex m_log_lock;
|
||||
std::ofstream m_logfile;
|
||||
@ -121,20 +128,9 @@ LogManager::LogManager()
|
||||
new FileLogListener(File::GetUserPath(F_MAINLOG_IDX)));
|
||||
RegisterListener(LogListener::CONSOLE_LISTENER, new ConsoleListener());
|
||||
|
||||
IniFile ini;
|
||||
ini.Load(File::GetUserPath(F_LOGGERCONFIG_IDX));
|
||||
IniFile::Section* logs = ini.GetOrCreateSection("Logs");
|
||||
IniFile::Section* options = ini.GetOrCreateSection("Options");
|
||||
bool write_file;
|
||||
bool write_console;
|
||||
bool write_window;
|
||||
options->Get("WriteToFile", &write_file, false);
|
||||
options->Get("WriteToConsole", &write_console, true);
|
||||
options->Get("WriteToWindow", &write_window, true);
|
||||
|
||||
// Set up log listeners
|
||||
int verbosity;
|
||||
options->Get("Verbosity", &verbosity, 0);
|
||||
int verbosity = Config::Get(LOGGER_VERBOSITY);
|
||||
;
|
||||
|
||||
// Ensure the verbosity level is valid
|
||||
if (verbosity < 1)
|
||||
@ -143,12 +139,13 @@ 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);
|
||||
EnableListener(LogListener::FILE_LISTENER, Config::Get(LOGGER_WRITE_TO_FILE));
|
||||
EnableListener(LogListener::CONSOLE_LISTENER, Config::Get(LOGGER_WRITE_TO_CONSOLE));
|
||||
EnableListener(LogListener::LOG_WINDOW_LISTENER, Config::Get(LOGGER_WRITE_TO_WINDOW));
|
||||
|
||||
for (LogContainer& container : m_log)
|
||||
logs->Get(container.m_short_name, &container.m_enable, false);
|
||||
container.m_enable = Config::Get(
|
||||
Config::ConfigInfo<bool>{{Config::System::Logger, "Logs", container.m_short_name}, false});
|
||||
|
||||
m_path_cutoff_point = DeterminePathCutOffPoint();
|
||||
}
|
||||
@ -162,20 +159,18 @@ LogManager::~LogManager()
|
||||
|
||||
void LogManager::SaveSettings()
|
||||
{
|
||||
IniFile ini;
|
||||
ini.Load(File::GetUserPath(F_LOGGERCONFIG_IDX));
|
||||
Config::SetBaseOrCurrent(LOGGER_WRITE_TO_FILE, IsListenerEnabled(LogListener::FILE_LISTENER));
|
||||
Config::SetBaseOrCurrent(LOGGER_WRITE_TO_CONSOLE,
|
||||
IsListenerEnabled(LogListener::CONSOLE_LISTENER));
|
||||
Config::SetBaseOrCurrent(LOGGER_WRITE_TO_WINDOW,
|
||||
IsListenerEnabled(LogListener::LOG_WINDOW_LISTENER));
|
||||
Config::SetBaseOrCurrent(LOGGER_VERBOSITY, static_cast<int>(GetLogLevel()));
|
||||
|
||||
IniFile::Section* options = ini.GetOrCreateSection("Options");
|
||||
options->Set("Verbosity", GetLogLevel());
|
||||
options->Set("WriteToFile", m_listener_ids[LogListener::FILE_LISTENER]);
|
||||
options->Set("WriteToConsole", m_listener_ids[LogListener::CONSOLE_LISTENER]);
|
||||
options->Set("WriteToWindow", m_listener_ids[LogListener::LOG_WINDOW_LISTENER]);
|
||||
|
||||
// Save all enabled/disabled states of the log types to the config ini.
|
||||
for (const auto& container : m_log)
|
||||
ini.GetOrCreateSection("Logs")->Set(container.m_short_name, container.m_enable);
|
||||
Config::SetBaseOrCurrent({{Config::System::Logger, "Logs", container.m_short_name}, false},
|
||||
container.m_enable);
|
||||
|
||||
ini.Save(File::GetUserPath(F_LOGGERCONFIG_IDX));
|
||||
Config::Save();
|
||||
}
|
||||
|
||||
void LogManager::Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type, const char* file,
|
||||
|
Reference in New Issue
Block a user