Core/Config: Error logging for ptree_bad_data

Ref https://github.com/TrinityCore/TrinityCore/commit/6487e2f2d6a1ddbeff77362219658376b8a3f9f0#commitcomment-16888976

(cherry picked from commit 9431b94bff939457cb915da3728cda6067674558)
This commit is contained in:
DDuarte authored and Aokromes committed 2016-05-12 08:24:32 +02:00
1 parent 25fe85f031
commit 06a91b090d
3 files changed
+50 -42

No files matched your search

+43 -37
View File
@@ -68,64 +68,70 @@ bool ConfigMgr::Reload(std::string& error)
return LoadInitial(_filename, error);
}
std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def) const
template<class T>
T ConfigMgr::GetValueDefault(std::string const& name, T def) const
{
try
{
std::string value = _config.get<std::string>(ptree::path_type(name, '/'));
value.erase(std::remove(value.begin(), value.end(), '"'), value.end());
return value;
return _config.get<T>(ptree::path_type(name, '/'));
}
catch (boost::property_tree::ptree_bad_path)
{
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 (boost::property_tree::ptree_bad_data)
{
TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use %s instead",
name.c_str(), _filename.c_str(), std::to_string(def).c_str());
}
return def;
}
template<>
std::string ConfigMgr::GetValueDefault<std::string>(std::string const& name, std::string def) const
{
try
{
return _config.get<std::string>(ptree::path_type(name, '/'));
}
catch (boost::property_tree::ptree_bad_path)
{
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());
return def;
}
catch (boost::property_tree::ptree_bad_data)
{
TC_LOG_ERROR("server.loading", "Bad value defined for name %s in config file %s, going to use %s instead",
name.c_str(), _filename.c_str(), def.c_str());
}
return def;
}
std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def) const
{
std::string val = GetValueDefault(name, def);
val.erase(std::remove(val.begin(), val.end(), '"'), val.end());
return val;
}
bool ConfigMgr::GetBoolDefault(std::string const& name, bool def) const
{
try
{
std::string val = _config.get<std::string>(ptree::path_type(name, '/'));
val.erase(std::remove(val.begin(), val.end(), '"'), val.end());
return (val == "true" || val == "TRUE" || val == "yes" || val == "YES" || val == "1");
}
catch (boost::property_tree::ptree_bad_path)
{
TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %d\" to this file",
name.c_str(), _filename.c_str(), name.c_str(), def ? 1 : 0);
return def;
}
std::string val = GetValueDefault(name, std::string(def ? "1" : "0"));
val.erase(std::remove(val.begin(), val.end(), '"'), val.end());
return (val == "true" || val == "TRUE" || val == "yes" || val == "YES" || val == "1");
}
int ConfigMgr::GetIntDefault(std::string const& name, int def) const
{
try
{
return _config.get<int>(ptree::path_type(name, '/'));
}
catch (boost::property_tree::ptree_bad_path)
{
TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %d\" to this file",
name.c_str(), _filename.c_str(), name.c_str(), def);
return def;
}
return GetValueDefault(name, def);
}
float ConfigMgr::GetFloatDefault(std::string const& name, float def) const
{
try
{
return _config.get<float>(ptree::path_type(name, '/'));
}
catch (boost::property_tree::ptree_bad_path)
{
TC_LOG_WARN("server.loading", "Missing name %s in config file %s, add \"%s = %f\" to this file",
name.c_str(), _filename.c_str(), name.c_str(), def);
return def;
}
return GetValueDefault(name, def);
}
std::string const& ConfigMgr::GetFilename()