diff options
author | Treeston <treeston.mmoc@gmail.com> | 2020-07-31 17:12:09 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-31 17:12:09 +0200 |
commit | 5ad064976edfc931cdda6faffe723d7c3dfb0ae4 (patch) | |
tree | dbd0570b7bd6dcc9f6f4e80ad940acbe7c86c567 /src/common/Configuration/Config.cpp | |
parent | e3aa87641f08ecaa42ebef1915d7de88dc73ef25 (diff) |
Core/Common: Allow config settings to be loaded as optional (PR #25137)
If the config setting is not present, returns the default without issuing a warning.
Diffstat (limited to 'src/common/Configuration/Config.cpp')
-rw-r--r-- | src/common/Configuration/Config.cpp | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/src/common/Configuration/Config.cpp b/src/common/Configuration/Config.cpp index 9d669e930e2..fa0e546ccc3 100644 --- a/src/common/Configuration/Config.cpp +++ b/src/common/Configuration/Config.cpp @@ -79,7 +79,7 @@ bool ConfigMgr::Reload(std::string& error) } template<class T> -T ConfigMgr::GetValueDefault(std::string const& name, T def) const +T ConfigMgr::GetValueDefault(std::string const& name, T def, bool quiet) const { try { @@ -87,8 +87,11 @@ T ConfigMgr::GetValueDefault(std::string const& name, T def) const } catch (bpt::ptree_bad_path const&) { - TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", - name.c_str(), _filename.c_str(), name.c_str(), std::to_string(def).c_str()); + if (!quiet) + { + TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", + name.c_str(), _filename.c_str(), name.c_str(), std::to_string(def).c_str()); + } } catch (bpt::ptree_bad_data const&) { @@ -100,7 +103,7 @@ T ConfigMgr::GetValueDefault(std::string const& name, T def) const } template<> -std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std::string def) const +std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std::string def, bool quiet) const { try { @@ -108,8 +111,11 @@ std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std } catch (bpt::ptree_bad_path const&) { - TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", - name.c_str(), _filename.c_str(), name.c_str(), def.c_str()); + if (!quiet) + { + TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %s\" to this file", + name.c_str(), _filename.c_str(), name.c_str(), def.c_str()); + } } catch (bpt::ptree_bad_data const&) { @@ -120,28 +126,28 @@ std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std return def; } -std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def) const +std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def, bool quiet) const { - std::string val = GetValueDefault(name, def); + std::string val = GetValueDefault(name, def, quiet); val.erase(std::remove(val.begin(), val.end(), '"'), val.end()); return val; } -bool ConfigMgr::GetBoolDefault(std::string const& name, bool def) const +bool ConfigMgr::GetBoolDefault(std::string const& name, bool def, bool quiet) const { - std::string val = GetValueDefault(name, std::string(def ? "1" : "0")); + std::string val = GetValueDefault(name, std::string(def ? "1" : "0"), quiet); val.erase(std::remove(val.begin(), val.end(), '"'), val.end()); return StringToBool(val); } -int ConfigMgr::GetIntDefault(std::string const& name, int def) const +int ConfigMgr::GetIntDefault(std::string const& name, int def, bool quiet) const { - return GetValueDefault(name, def); + return GetValueDefault(name, def, quiet); } -float ConfigMgr::GetFloatDefault(std::string const& name, float def) const +float ConfigMgr::GetFloatDefault(std::string const& name, float def, bool quiet) const { - return GetValueDefault(name, def); + return GetValueDefault(name, def, quiet); } std::string const& ConfigMgr::GetFilename() |