version 0.4.1
init.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10
11#include <chrono>
12#include <utility>
13
14#include <dune/common/parallel/mpihelper.hh>
15
16#include <spdlog/details/registry.h>
17#include <spdlog/fmt/chrono.h>
18#include <spdlog/fmt/ostr.h>
19#include <spdlog/fmt/ranges.h>
20#include <spdlog/sinks/basic_file_sink.h>
21#include <spdlog/sinks/stdout_color_sinks.h>
22#include <spdlog/spdlog.h>
23
24namespace Ikarus {
25
33{
34public:
41 static IkarusInstance instance;
42 return instance;
43 }
44
50 void enableFileLogger(std::string&& filename = "") {
51 using namespace std::chrono;
52 std::string currentTime = fmt::format("_{}", std::chrono::system_clock::now());
53
54 std::ranges::transform(currentTime, currentTime.begin(), [](char ch) {
55 return (ch == ' ' or ch == ':') ? '_' : ch;
56 }); // replace space and colon with underscore
57 auto logFilename = (filename.empty() ? executableName_ : filename) + currentTime + ".log";
58 auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFilename, true);
59 fileSink->set_pattern("%v");
60 fileSink->set_level(spdlog::level::trace);
61 auto logger = spdlog::default_logger();
62 logger->sinks().push_back(fileSink);
63 }
64
65private:
66 friend void init(int argc, char** argv, bool enableFileLogger);
67 IkarusInstance() = default;
68 std::string executableName_;
69
70public:
72 void operator=(const IkarusInstance&) = delete;
73};
74
82void inline init(int argc, char** argv, bool enableFileLogger = true) {
83 Dune::MPIHelper::instance(argc, argv);
84 auto& instance = IkarusInstance::getInstance();
85 instance.executableName_ = argv[0];
86 auto logger = spdlog::default_logger();
87 logger->set_pattern("%v");
88 if (enableFileLogger)
89 instance.enableFileLogger();
90
91 auto currentTime = std::chrono::system_clock::now();
92 const std::chrono::year_month_day currentYearMonthDay{floor<std::chrono::days>(currentTime)};
93
94 spdlog::info("Start of execution: {}", currentTime);
96 spdlog::info(R"xxx( _/_/_/ _/ _/ _/_/ _/_/_/ _/ _/ _/_/_/)xxx");
97 spdlog::info(R"xxx( _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ )xxx");
98 spdlog::info(R"xxx( _/ _/_/ _/_/_/_/ _/_/_/ _/ _/ _/_/ )xxx");
99 spdlog::info(R"xxx( _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ )xxx");
100 spdlog::info(R"xxx(_/_/_/ _/ _/ _/ _/ _/ _/ _/_/ _/_/_/ )xxx");
101
102 spdlog::info("© 2021-{} The Ikarus Developers, see LICENSE.md ", static_cast<int>(currentYearMonthDay.year()));
103 spdlog::info("You are using Ikarus v{}. Please don't forget to cite us:", IKARUS_VERSION);
104 spdlog::info(
105 "Müller, A., Vinod Kumar Mitruka, T. K. M., Jakob, H. (2024). Ikarus v0.4 (Version V1). "
106 "doi:<https://doi.org/10.18419/darus-3889>");
107}
108} // namespace Ikarus
Definition: simpleassemblers.hh:22
void init(int argc, char **argv, bool enableFileLogger=true)
Initializes the Ikarus framework.
Definition: init.hh:82
Singleton class representing an instance of the Ikarus framework.
Definition: init.hh:33
friend void init(int argc, char **argv, bool enableFileLogger)
Initializes the Ikarus framework.
Definition: init.hh:82
static IkarusInstance & getInstance()
Gets the singleton instance of the Ikarus framework.
Definition: init.hh:40
void operator=(const IkarusInstance &)=delete
void enableFileLogger(std::string &&filename="")
Enables a file logger.
Definition: init.hh:50
IkarusInstance(const IkarusInstance &)=delete