-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc41581
commit 1bce4ce
Showing
10 changed files
with
221 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#ifndef PHARE_CORE_ENV_HPP | ||
#define PHARE_CORE_ENV_HPP | ||
|
||
// Single source for handling env vars | ||
|
||
#include <array> | ||
#include <cstdint> | ||
#include <optional> | ||
#include <string_view> | ||
#include "core/utilities/types.hpp" | ||
#include "core/utilities/mpi_utils.hpp" | ||
|
||
namespace PHARE::env | ||
{ | ||
|
||
struct Var | ||
{ | ||
using value_type = std::string; | ||
|
||
std::string_view const id; | ||
std::string_view const desc; | ||
std::vector<std::pair<std::string_view, std::string_view>> const options; | ||
|
||
std::optional<value_type> const _default = std::nullopt; | ||
std::function<std::string(Var const&)> const _result | ||
= [](Var const&) { return std::string{""}; }; // noop | ||
std::optional<value_type> const v = get(); | ||
std::string const r = _result(*this); | ||
|
||
std::optional<value_type> get() const | ||
{ | ||
std::string _id{id}; | ||
if (_default) | ||
return core::get_env(_id, *_default); | ||
return core::get_env(_id); | ||
} | ||
auto& operator()() const { return v; } | ||
bool exists() const { return v != std::nullopt; } | ||
operator bool() const { return exists(); } | ||
auto result() const { return r; } | ||
}; | ||
|
||
} // namespace PHARE::env | ||
|
||
namespace PHARE | ||
{ | ||
|
||
class Env | ||
{ | ||
public: | ||
template<typename T> | ||
using map_t = std::unordered_map<std::string, T const* const>; | ||
|
||
static Env& INSTANCE() | ||
{ | ||
if (!self) | ||
self = std::make_unique<Env>(); | ||
return *self; | ||
} | ||
static auto& reinit() { return *(self = std::make_unique<Env>()); } | ||
|
||
env::Var const PHARE_LOG{ | ||
"PHARE_LOG", | ||
"Write logs to $CWD/.log", | ||
{{{"RANK_FILES", "Write logs $CWD/.log, a file per rank"}, | ||
{"DATETIME_FILES", "Write logs $CWD/.log, filename per rank and datetime"}, | ||
{"NONE", "print normally to std::cout"}}}, | ||
std::nullopt, | ||
[](auto const& self) { | ||
if (auto const& opt = self()) | ||
{ | ||
auto const& val = *opt; | ||
if (val == "RANK_FILES") | ||
return ".log/" + std::to_string(core::mpi::rank()) + ".out"; | ||
if (val == "DATETIME_FILES") | ||
{ | ||
auto date_time = core::mpi::date_time(); | ||
auto rank = std::to_string(core::mpi::rank()); | ||
auto size = std::to_string(core::mpi::size()); | ||
return ".log/" + date_time + "_" + rank + "_of_" + size + ".out"; | ||
} | ||
if (val != "NONE") | ||
throw std::runtime_error( | ||
"PHARE_LOG invalid type, valid keys are RANK_FILES/DATETIME_FILES/NONE"); | ||
} | ||
return std::string{""}; | ||
} // | ||
}; | ||
|
||
map_t<env::Var> const vars = {{{"PHARE_LOG", &PHARE_LOG}}}; | ||
|
||
private: | ||
static inline std::unique_ptr<Env> self = nullptr; | ||
}; | ||
|
||
} // namespace PHARE | ||
|
||
#endif /* PHARE_CORE_ERRORS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required (VERSION 3.20.1) | ||
|
||
project(test-phare-env) | ||
|
||
set(SOURCES test_env.cpp) | ||
|
||
add_executable(${PROJECT_NAME} ${SOURCES}) | ||
|
||
target_include_directories(${PROJECT_NAME} PRIVATE | ||
${GTEST_INCLUDE_DIRS} | ||
) | ||
|
||
target_link_directories(${PROJECT_NAME} PUBLIC ${MPI_LIBRARY_PATH}) | ||
|
||
target_link_libraries(${PROJECT_NAME} PRIVATE | ||
phare_core | ||
MPI::MPI_C | ||
${GTEST_LIBS} | ||
) | ||
|
||
add_phare_test(${PROJECT_NAME} ${CMAKE_CURRENT_BINARY_DIR}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
|
||
#include "core/env.hpp" | ||
#include "gtest/gtest.h" | ||
#include <cstdlib> | ||
|
||
namespace PHARE | ||
{ | ||
|
||
TEST(Env, logging_works) | ||
{ | ||
setenv("PHARE_LOG", "NONE", true); | ||
auto& env = Env::reinit(); // init | ||
|
||
EXPECT_EQ(env.vars.size(), 1); | ||
|
||
for (auto const& [key, var] : env.vars) | ||
{ | ||
EXPECT_EQ(key, std::string{"PHARE_LOG"}); | ||
EXPECT_EQ(key, std::string{var->id}); | ||
} | ||
} | ||
|
||
TEST(Env, logging_works_rank_files) | ||
{ | ||
setenv("PHARE_LOG", "RANK_FILES", true); | ||
auto& env = Env::reinit(); // init | ||
|
||
EXPECT_EQ(env.vars.size(), 1); | ||
|
||
for (auto const& [key, var] : env.vars) | ||
{ | ||
EXPECT_EQ(key, std::string{"PHARE_LOG"}); | ||
EXPECT_EQ(key, std::string{var->id}); | ||
EXPECT_EQ(var->result(), ".log/" + std::to_string(core::mpi::rank()) + ".out"); | ||
} | ||
} | ||
|
||
} // namespace PHARE | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
PHARE::core::mpi::Lifecycle mpi_init(argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |