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

Add environment variables to control library logging

Closes #23
parent 310729cf
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -66,7 +66,7 @@ public:
            } else if(type == "file") {
                m_internal_logger =
                        spdlog::basic_logger_mt<spdlog::async_factory>(
                                ident, log_file.string());
                                ident, log_file.string(), true);

            }
#ifdef SPDLOG_ENABLE_SYSLOG
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ add_library(adm_iosched SHARED)

target_sources(adm_iosched
  PUBLIC admire.h admire.hpp
  PRIVATE admire.cpp c_wrapper.cpp detail/impl.hpp detail/impl.cpp errors.c)
  PRIVATE admire.cpp c_wrapper.cpp detail/impl.hpp detail/impl.cpp errors.c env.hpp)

set_target_properties(adm_iosched PROPERTIES PUBLIC_HEADER "admire.h;admire.hpp")

+22 −4
Original line number Diff line number Diff line
@@ -27,18 +27,20 @@
#include <net/proto/rpc_types.h>
#include <logger/logger.hpp>
#include <utils/ctype_ptr.hpp>
#include <env.hpp>
#include <iostream>
#include "detail/impl.hpp"


namespace {

void
[[maybe_unused]] void
init_library() __attribute__((constructor));

void
init_logger();

void
[[maybe_unused]] void
init_library() {
    init_logger();
}
@@ -46,8 +48,24 @@ init_library() {
/** Logging for the library */
void
init_logger() {
    // for now, just create a simple console logger
    scord::logger::create_global_logger("libadm_iosched", "console color");

    try {


        if(const auto p = std::getenv(admire::env::LOG);
           p && !std::string{p}.empty() && std::string{p} != "0") {
            if(const auto log_file = std::getenv(admire::env::LOG_OUTPUT)) {
                scord::logger::create_global_logger("libadm_iosched", "file",
                                                    log_file);
            } else {
                scord::logger::create_global_logger("libadm_iosched",
                                                    "console color");
            }
        }
    } catch(const std::exception& ex) {
        std::cerr << fmt::format("WARNING: Error initializing logger: {}",
                                 ex.what());
    }
}

void

src/lib/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 LIBSCORD_ENV_HPP
#define LIBSCORD_ENV_HPP

#define LIBSCORD_ENV_PREFIX "LIBSCORD_"
#define ADD_PREFIX(str)     LIBSCORD_ENV_PREFIX str

namespace admire::env {

static constexpr auto LOG = ADD_PREFIX("LOG");
static constexpr auto LOG_OUTPUT = ADD_PREFIX("LOG_OUTPUT");

} // namespace admire::env


#endif // LIBSCORD_ENV_HPP