Skip to content
Snippets Groups Projects
Verified Commit 064a325d authored by Alberto Miranda's avatar Alberto Miranda :hotsprings:
Browse files

Add environment variables to control daemon logging

parent d2db2db2
No related branches found
No related tags found
1 merge request!31Resolve "Add tests to verify RPC arguments."
......@@ -25,7 +25,7 @@
# scord-ctl daemon
add_executable(scord-ctl)
target_sources(scord-ctl PRIVATE scord-ctl.cpp rpc_handlers.hpp rpc_handlers.cpp)
target_sources(scord-ctl PRIVATE scord-ctl.cpp rpc_handlers.hpp rpc_handlers.cpp env.hpp)
target_include_directories(
scord-ctl
......
/******************************************************************************
* Copyright 2021-2022, Barcelona Supercomputing Center (BSC), Spain
*
* This software was partially supported by the EuroHPC-funded project ADMIRE
* (Project ID: 956748, https://www.admire-eurohpc.eu).
*
* This file is part of scord.
*
* scord is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* scord is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with scord. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*****************************************************************************/
#ifndef SCORD_CTL_ENV_HPP
#define SCORD_CTL_ENV_HPP
#define SCORD_ENV_PREFIX "SCORDCTL_"
#define ADD_PREFIX(str) SCORD_ENV_PREFIX str
namespace scord_ctl::env {
static constexpr auto LOG = ADD_PREFIX("LOG");
static constexpr auto LOG_OUTPUT = ADD_PREFIX("LOG_OUTPUT");
} // namespace scord_ctl::env
#endif // SCORD_CTL_ENV_HPP
......@@ -36,6 +36,7 @@
#include <net/proto/rpc_types.h>
#include <config/settings.hpp>
#include "rpc_handlers.hpp"
#include "env.hpp"
namespace fs = std::filesystem;
namespace bpo = boost::program_options;
......@@ -52,6 +53,20 @@ print_help(const std::string& progname,
fmt::print("{}", opt_desc);
}
std::unordered_map<std::string, std::string>
load_envs() {
std::unordered_map<std::string, std::string> envs;
if(const auto p = std::getenv(scord_ctl::env::LOG);
p && !std::string{p}.empty() && std::string{p} != "0") {
if(const auto log_file = std::getenv(scord_ctl::env::LOG_OUTPUT)) {
envs.emplace(scord_ctl::env::LOG_OUTPUT, log_file);
}
}
return envs;
}
int
main(int argc, char* argv[]) {
......@@ -75,6 +90,7 @@ main(int argc, char* argv[]) {
->implicit_value("")
->zero_tokens()
->notifier([&](const std::string&) {
cfg.log_file(fs::path{});
cfg.use_console(true);
}),
"override any logging options defined in configuration files and "
......@@ -140,6 +156,15 @@ main(int argc, char* argv[]) {
return EXIT_FAILURE;
}
// override settings from the configuration file with settings
// from environment variables
const auto env_opts = load_envs();
if(const auto& it = env_opts.find(scord_ctl::env::LOG_OUTPUT);
it != env_opts.end()) {
cfg.log_file(it->second);
}
// calling notify() here basically invokes all define notifiers, thus
// overriding any configuration loaded from the global configuration
// file with its command-line counterparts if provided (for those
......
......@@ -25,7 +25,7 @@
# scord daemon
add_executable(scord)
target_sources(scord PRIVATE scord.cpp rpc_handlers.hpp rpc_handlers.cpp)
target_sources(scord PRIVATE scord.cpp rpc_handlers.hpp rpc_handlers.cpp env.hpp)
target_include_directories(
scord
......
/******************************************************************************
* Copyright 2021-2022, Barcelona Supercomputing Center (BSC), Spain
*
* This software was partially supported by the EuroHPC-funded project ADMIRE
* (Project ID: 956748, https://www.admire-eurohpc.eu).
*
* This file is part of scord.
*
* scord is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* scord is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with scord. If not, see <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*****************************************************************************/
#ifndef SCORD_ENV_HPP
#define SCORD_ENV_HPP
#define SCORD_ENV_PREFIX "SCORD_"
#define ADD_PREFIX(str) SCORD_ENV_PREFIX str
namespace scord::env {
static constexpr auto LOG = ADD_PREFIX("LOG");
static constexpr auto LOG_OUTPUT = ADD_PREFIX("LOG_OUTPUT");
} // namespace scord::env
#endif // SCORD_ENV_HPP
......@@ -36,6 +36,7 @@
#include <net/proto/rpc_types.h>
#include <config/settings.hpp>
#include "rpc_handlers.hpp"
#include "env.hpp"
#include <agios.h>
......@@ -54,6 +55,20 @@ print_help(const std::string& progname,
fmt::print("{}", opt_desc);
}
std::unordered_map<std::string, std::string>
load_envs() {
std::unordered_map<std::string, std::string> envs;
if(const auto p = std::getenv(scord::env::LOG);
p && !std::string{p}.empty() && std::string{p} != "0") {
if(const auto log_file = std::getenv(scord::env::LOG_OUTPUT)) {
envs.emplace(scord::env::LOG_OUTPUT, log_file);
}
}
return envs;
}
int
main(int argc, char* argv[]) {
......@@ -77,6 +92,7 @@ main(int argc, char* argv[]) {
->implicit_value("")
->zero_tokens()
->notifier([&](const std::string&) {
cfg.log_file(fs::path{});
cfg.use_console(true);
}),
"override any logging options defined in configuration files and "
......@@ -142,6 +158,15 @@ main(int argc, char* argv[]) {
return EXIT_FAILURE;
}
// override settings from the configuration file with settings
// from environment variables
const auto env_opts = load_envs();
if(const auto& it = env_opts.find(scord::env::LOG_OUTPUT);
it != env_opts.end()) {
cfg.log_file(it->second);
}
// calling notify() here basically invokes all define notifiers, thus
// overriding any configuration loaded from the global configuration
// file with its command-line counterparts if provided (for those
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment