version 0.4
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 public:
40 static IkarusInstance instance;
41 return instance;
42 }
43
49 void enableFileLogger(std::string&& filename = "") {
50 using namespace std::chrono;
51 std::string currentTime = fmt::format("_{}", std::chrono::system_clock::now());
52
53 std::ranges::transform(currentTime, currentTime.begin(), [](char ch) {
54 return (ch == ' ' or ch == ':') ? '_' : ch;
55 }); // replace space and colon with underscore
56 auto logFilename = (filename.empty() ? executableName : filename) + currentTime + ".log";
57 auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFilename, true);
58 file_sink->set_pattern("%v");
59 file_sink->set_level(spdlog::level::trace);
60 auto logger = spdlog::default_logger();
61 logger->sinks().push_back(file_sink);
62 }
63
64 private:
65 friend void init(int argc, char** argv, bool enableFileLogger);
66 IkarusInstance() = default;
67 std::string executableName;
68
69 public:
71 void operator=(IkarusInstance const&) = delete;
72 };
73
81 void inline init(int argc, char** argv, bool enableFileLogger = true) {
82 Dune::MPIHelper::instance(argc, argv);
83 auto& instance = IkarusInstance::getInstance();
84 instance.executableName = argv[0];
85 auto logger = spdlog::default_logger();
86 logger->set_pattern("%v");
87 if (enableFileLogger) instance.enableFileLogger();
88
89 auto currentTime = std::chrono::system_clock::now();
90 const std::chrono::year_month_day currentYearMonthDay{floor<std::chrono::days>(currentTime)};
91
92 spdlog::info("Start of execution: {}", currentTime);
94 spdlog::info(R"xxx( _/_/_/ _/ _/ _/_/ _/_/_/ _/ _/ _/_/_/)xxx");
95 spdlog::info(R"xxx( _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ )xxx");
96 spdlog::info(R"xxx( _/ _/_/ _/_/_/_/ _/_/_/ _/ _/ _/_/ )xxx");
97 spdlog::info(R"xxx( _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ )xxx");
98 spdlog::info(R"xxx(_/_/_/ _/ _/ _/ _/ _/ _/ _/_/ _/_/_/ )xxx");
99
100 spdlog::info("© 2021-{} The Ikarus Developers, see LICENSE.md ", static_cast<int>(currentYearMonthDay.year()));
101 spdlog::info("You are using Ikarus v{}. Please don't forget to cite us:", IKARUS_VERSION);
102 spdlog::info(
103 "Müller, A., & Vinod Kumar Mitruka, T. K. M. (2023). Ikarus v0.3 (Version V1). Version V1. "
104 "doi:<https://doi.org/10.18419/darus-3303>");
105 }
106} // namespace Ikarus
Definition: simpleassemblers.hh:21
void init(int argc, char **argv, bool enableFileLogger=true)
Initializes the Ikarus framework.
Definition: init.hh:81
Singleton class representing an instance of the Ikarus framework.
Definition: init.hh:32
friend void init(int argc, char **argv, bool enableFileLogger)
Initializes the Ikarus framework.
Definition: init.hh:81
IkarusInstance(IkarusInstance const &)=delete
static IkarusInstance & getInstance()
Gets the singleton instance of the Ikarus framework.
Definition: init.hh:39
void operator=(IkarusInstance const &)=delete
void enableFileLogger(std::string &&filename="")
Enables a file logger.
Definition: init.hh:49