Files
energy_storage/src/app/Config.cpp

38 lines
1.1 KiB
C++
Raw Normal View History

#include "Config.h"
#include <fstream>
#include "common/JsonN.h"
#include "Logger.h"
AppOption Config::option;
bool Config::init(std::string filename)
{
NJson jsonroot;
bool ret = NJsonLoad(filename, jsonroot);
if (!ret)
{
XLOGE() << "[APP] load config failed, filename=" << filename;
return false;
}
XLOGI() << "[APP] load config success, filename=" << filename;
if (jsonroot.contains("database"))
{
NJson json = jsonroot.at("database");
option.database.host = json.contains("host") ? json.at("host") : "";
option.database.port = json.contains("port") ? json.at("port") : 0;
option.database.user = json.contains("user") ? json.at("user") : "";
option.database.passwd = json.contains("passwd") ? json.at("passwd") : "";
option.database.dbname = json.contains("dbname") ? json.at("dbname") : "";
XLOGI() << "[APP] load database config end. host=" << option.database.host;
}
else
{
XLOGI() << "[APP] load database config error: not found. host=" << option.database.host;
}
return true;
}