Unverified Commit bb64eaf7 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

Introduce wrapper for get_env

parent a0f3d6ca
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
/*
  Copyright 2018-2019, Barcelona Supercomputing Center (BSC), Spain
  Copyright 2015-2019, Johannes Gutenberg Universitaet Mainz, Germany

  This software was partially supported by the
  EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).

  This software was partially supported by the
  ADA-FS project under the SPPEXA project funded by the DFG.

  SPDX-License-Identifier: MIT
*/

#ifndef GKFS_ENV_UTIL_HPP
#define GKFS_ENV_UTIL_HPP

#include <string>

namespace gkfs {

std::string get_env(const std::string& env_name);
std::string get_env_own(const std::string& env_name);

}

#endif //IFS_ENV_UTIL_HPP
+1 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ target_link_libraries(gkfs_intercept
    metadata
    distributor
    log_util
    env_util
    # external
    Syscall_intercept::Syscall_intercept
    dl
+11 −14
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
*/

#include <global/log_util.hpp>
#include <global/env_util.hpp>
#include <global/path_util.hpp>
#include <global/global_defs.hpp>
#include <global/configure.hpp>
@@ -182,22 +183,18 @@ void init_ld_env_if_needed() {
}

void init_logging() {
    std::string path = DEFAULT_PRELOAD_LOG_PATH;
    // Try to get log path from env variable
    std::string env_key = ENV_PREFIX;
    env_key += "PRELOAD_LOG_PATH";
    char* env_log_path = getenv(env_key.c_str());
    if (env_log_path != nullptr) {
        path = env_log_path;
    std::string path;
    try {
        path = gkfs::get_env_own("PRELOAD_LOG_PATH");
    } catch (const std::exception& e) {
        path = DEFAULT_PRELOAD_LOG_PATH;
    }

    spdlog::level::level_enum level = get_spdlog_level(DEFAULT_DAEMON_LOG_LEVEL);
    // Try to get log path from env variable
    std::string env_level_key = ENV_PREFIX;
    env_level_key += "LOG_LEVEL";
    char* env_level = getenv(env_level_key.c_str());
    if (env_level != nullptr) {
        level = get_spdlog_level(env_level);
    spdlog::level::level_enum level;
    try {
        level = get_spdlog_level(gkfs::get_env_own("LOG_LEVEL"));
    } catch (const std::exception& e) {
        level = get_spdlog_level(DEFAULT_DAEMON_LOG_LEVEL);
    }

    auto logger_names = std::vector<std::string> {"main"};
+9 −0
Original line number Diff line number Diff line
@@ -19,6 +19,15 @@ target_link_libraries(log_util
    spdlog
    )

add_library(env_util STATIC)
set_property(TARGET env_util PROPERTY POSITION_INDEPENDENT_CODE ON)
target_sources(env_util
    PUBLIC
    ${INCLUDE_DIR}/global/env_util.hpp
    PRIVATE
    ${CMAKE_CURRENT_LIST_DIR}/env_util.cpp
    )

add_library(metadata STATIC)
set_property(TARGET metadata PROPERTY POSITION_INDEPENDENT_CODE ON)
target_sources(metadata
+37 −0
Original line number Diff line number Diff line
/*
  Copyright 2018-2019, Barcelona Supercomputing Center (BSC), Spain
  Copyright 2015-2019, Johannes Gutenberg Universitaet Mainz, Germany

  This software was partially supported by the
  EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).

  This software was partially supported by the
  ADA-FS project under the SPPEXA project funded by the DFG.

  SPDX-License-Identifier: MIT
*/

#include <global/env_util.hpp>
#include <global/configure.hpp>
#include <cstdlib>
#include <stdexcept>


namespace gkfs {

using namespace std;

string get_env(const string& env_name) {
    char* env_value = getenv(env_name.c_str());
    if (env_value == nullptr) {
        throw runtime_error("Environment variable not set: " + env_name);
    }
    return env_value;
}

string get_env_own(const string& env_name) {
    string env_key = ENV_PREFIX + env_name;
    return get_env(env_key);
}

}