Commit 42ca569e authored by Pape, David (FWCC) - 139658's avatar Pape, David (FWCC) - 139658
Browse files

Refactoring program configuration.

parent 14c7e1e9
Pipeline #19859 passed with stages
in 4 minutes and 22 seconds
......@@ -36,8 +36,10 @@ int main(int argc, char *argv[]) {
return 50;
}
std::filesystem::path home(h);
auto config = config::make(home);
std::unique_ptr<libconfig::Config> config(config::make(home));
if (config == nullptr)
return 51;
int result = logging::setup(*config, home);
if (result != 0)
......@@ -64,12 +66,8 @@ int main(int argc, char *argv[]) {
std::string scriptName = argv[2] ? argv[2] : "";
std::string subStage = argv[3] ? argv[3] : "";
bool unifiedOutput;
try {
unifiedOutput = static_cast<bool>(config->lookup("unified_output"));
} catch (const libconfig::SettingNotFoundException &) {
unifiedOutput = true;
}
bool unifiedOutput = true;
config->lookupValue("unified_output", unifiedOutput);
CIJobFactory jf(&env);
std::unique_ptr<CIJob> job(jf.CreateCIJob(jobStage, scriptName, subStage, unifiedOutput));
......@@ -79,6 +77,5 @@ int main(int argc, char *argv[]) {
result = job->Run();
delete config;
return result;
}
......@@ -15,6 +15,7 @@ libconfig::Config *config::make(const std::filesystem::path &home) {
} catch(const libconfig::ParseException &pex) {
std::cerr << "Error parsing config file at " << pex.getFile() << ":" << pex.getLine()
<< ": " << pex.getError() << std::endl;
return nullptr;
}
return config;
......
......@@ -14,13 +14,18 @@
#include "spdlog/spdlog.h"
#include "spdlog/sinks/daily_file_sink.h"
int logging::setup(libconfig::Config &config, std::filesystem::path home) {
std::string logsDir;
try {
logsDir = static_cast<std::string>(config.lookup("logging_directory"));
} catch (const libconfig::SettingNotFoundException &) {
logsDir = home / "logs";
}
static std::array<std::string, 8> logLevels = { "trace",
"debug",
"info",
"warning",
"error",
"critical",
"off",
"null" };
int logging::setup(const libconfig::Config &config, const std::filesystem::path &home) {
std::string logsDir = home / "logs";
config.lookupValue("logging_directory", logsDir);
int fail = ::mkdir(logsDir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP); // drwxr-x---
if (fail && errno != EEXIST) {
......@@ -39,15 +44,12 @@ int logging::setup(libconfig::Config &config, std::filesystem::path home) {
return 43;
}
try {
std::array<std::string, 8> levels = { "trace", "debug", "info", "warning",
"error", "critical", "off", "null" };
auto l = static_cast<std::string>(config.lookup("log_level"));
if (std::any_of(levels.begin(), levels.end(), [l](std::string level) { return l == level;}))
spdlog::set_level(spdlog::level::from_str(l));
else
spdlog::set_level(spdlog::level::info);
} catch(const libconfig::SettingNotFoundException &) {}
std::string logLevel = "info";
config.lookupValue("log_level", logLevel);
if (std::find(logLevels.begin(), logLevels.end(), logLevel) != logLevels.end())
spdlog::set_level(spdlog::level::from_str(logLevel));
else
spdlog::set_level(spdlog::level::info);
return 0;
}
......@@ -10,6 +10,7 @@
* @brief Functions related to logging.
*/
namespace logging {
/**
* @brief Sets up logging (log level, logs directory, ...).
* @param[in] config A \c libconfig::Config from which the path for log files and the log level are
......@@ -18,7 +19,8 @@ namespace logging {
* logging directory is specified.
* @return 0 on success, a non-zero number on failure.
*/
int setup(libconfig::Config &config, std::filesystem::path home);
int setup(const libconfig::Config &config, const std::filesystem::path &home);
}
#endif // LOGGING_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment