Verified Commit 064a325d authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add environment variables to control daemon logging

parent d2db2db2
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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

src/scord-ctl/env.hpp

0 → 100644
+39 −0
Original line number Diff line number Diff line
/******************************************************************************
 * 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
+25 −0
Original line number Diff line number Diff line
@@ -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
+1 −1
Original line number Diff line number Diff line
@@ -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

src/scord/env.hpp

0 → 100644
+39 −0
Original line number Diff line number Diff line
/******************************************************************************
 * 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
Loading