presence: Enable use of alternate json config file
Set the location and file for an alternate json config file that would
be used in place of the default if it exists.
Tested:
Default config used when alternate does not exist
Alternate config used when present
Change-Id: I79ac24f261776a60edff949598aea0be97e64bbe
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/presence/json_config.cpp b/presence/json_config.cpp
index a13f5f9..d85f721 100644
--- a/presence/json_config.cpp
+++ b/presence/json_config.cpp
@@ -36,6 +36,8 @@
namespace fs = std::filesystem;
using namespace phosphor::logging;
+constexpr auto jsonFileAlt = "/etc/phosphor-fan-presence/presence/config.json";
+
policies JsonConfig::_policies;
const std::map<std::string, methodHandler> JsonConfig::_methods =
{
@@ -82,12 +84,22 @@
void JsonConfig::load()
{
+ fs::path confFile{jsonFileAlt};
+ if (!fs::exists(confFile))
+ {
+ confFile = _defaultFile;
+ }
+ else
+ {
+ log<level::INFO>("Loading alternate configuration",
+ entry("JSON_FILE=%s", confFile.c_str()));
+ }
std::ifstream file;
json jsonConf;
- if (fs::exists(_defaultFile))
+ if (fs::exists(confFile))
{
- file.open(_defaultFile);
+ file.open(confFile);
try
{
jsonConf = json::parse(file);
@@ -95,7 +107,7 @@
catch (std::exception& e)
{
log<level::ERR>("Failed to parse JSON config file",
- entry("JSON_FILE=%s", _defaultFile.c_str()),
+ entry("JSON_FILE=%s", confFile.c_str()),
entry("JSON_ERROR=%s", e.what()));
throw std::runtime_error("Failed to parse JSON config file");
}
@@ -103,7 +115,7 @@
else
{
log<level::ERR>("Unable to open JSON config file",
- entry("JSON_FILE=%s", _defaultFile.c_str()));
+ entry("JSON_FILE=%s", confFile.c_str()));
throw std::runtime_error("Unable to open JSON config file");
}