Xped
Loading...
Searching...
No Matches
Logging.hpp
Go to the documentation of this file.
1#ifndef XPED_LOGGING_HPP_
2#define XPED_LOGGING_HPP_
3
4#include <string>
5
6#include "fmt/core.h"
7
8#include "spdlog/sinks/basic_file_sink.h"
9#include "spdlog/sinks/stdout_color_sinks.h"
10#include "spdlog/spdlog.h"
11
12#include <boost/algorithm/string.hpp>
13#include <boost/describe.hpp>
14
15#include "Xped/Util/Mpi.hpp"
16
17namespace Xped {
18
19BOOST_DEFINE_ENUM_CLASS(Verbosity, SILENT, CRITICAL, WARNING, ON_EXIT, ON_ENTRY, PER_ITERATION, DEBUG)
20
21namespace Log {
22
23void init_logging(const mpi::XpedWorld& world, const std::string& name)
24{
25 auto my_logger = world.np > 1 ? spdlog::basic_logger_mt("xlog", "logs/" + name + "_" + std::to_string(world.rank) + ".txt")
26 : spdlog::basic_logger_mt("xlog", "logs/" + name + ".txt");
27 my_logger->sinks()[0]->set_pattern("[%H:%M:%S] [%n] [%^---%L---%$] [process %P] %v");
28 my_logger->sinks()[0]->set_level(spdlog::level::trace);
29 if(world.rank == 0) {
30 auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
31 console_sink->set_pattern("[%^---%l---%$] %v");
32 console_sink->set_level(spdlog::level::trace);
33 my_logger->sinks().push_back(console_sink);
34 }
35 my_logger->set_level(spdlog::level::trace);
36 spdlog::set_default_logger(my_logger);
37 spdlog::flush_every(std::chrono::seconds(3));
38}
39// template <typename... Args>
40// auto log(Verbosity verb, Verbosity policy, fmt::format_string<Args...> s, Args&&... args)
41// {
42// if(verb <= policy) {
43// fmt::print(s, std::forward<Args>(args)...);
44// fmt::print("\n");
45// }
46// }
47inline Verbosity globalLevel = Verbosity::DEBUG;
48
49template <typename... Args>
50auto log(Verbosity verb, Verbosity policy, std::string_view fmt, Args&&... args)
51{
52 if(verb <= policy) {
53 std::string message = fmt::vformat(fmt, fmt::make_format_args(std::forward<Args>(args)...));
54 std::vector<std::string> lines;
55 boost::split(lines, message, boost::is_any_of("\n"));
56 switch(verb) {
57 case Verbosity::DEBUG: {
58 for(const auto& line : lines) { spdlog::trace("{}", line); }
59 break;
60 }
61 case Verbosity::PER_ITERATION: {
62 for(const auto& line : lines) { spdlog::debug("{}", line); }
63 break;
64 }
65 case Verbosity::ON_ENTRY: {
66
67 for(const auto& line : lines) { spdlog::info("{}", line); }
68 break;
69 }
70 case Verbosity::ON_EXIT: {
71 for(const auto& line : lines) { spdlog::warn("{}", line); }
72 break;
73 }
74 case Verbosity::WARNING: {
75 for(const auto& line : lines) { spdlog::error("{}", line); }
76 break;
77 }
78 case Verbosity::CRITICAL: {
79 for(const auto& line : lines) { spdlog::critical("{}", line); }
80 break;
81 }
82 case Verbosity::SILENT: {
83 break;
84 }
85 }
86 }
87}
88
89template <typename... Args>
90constexpr void critical(Verbosity policy, Args&&... args)
91{
92 return log(Verbosity::CRITICAL, policy, args...);
93}
94
95template <typename... Args>
96constexpr void warning(Verbosity policy, Args&&... args)
97{
98 return log(Verbosity::WARNING, policy, args...);
99}
100
101template <typename... Args>
102constexpr void on_exit(Verbosity policy, Args&&... args)
103{
104 return log(Verbosity::ON_EXIT, policy, args...);
105}
106
107template <typename... Args>
108constexpr void on_entry(Verbosity policy, Args&&... args)
109{
110 return log(Verbosity::ON_ENTRY, policy, args...);
111}
112
113template <typename... Args>
114constexpr void per_iteration(Verbosity policy, Args&&... args)
115{
116 return log(Verbosity::PER_ITERATION, policy, args...);
117}
118
119template <typename... Args>
120constexpr void debug(Verbosity policy, Args&&... args)
121{
122 return log(Verbosity::DEBUG, policy, args...);
123}
124
125template <typename... Args>
126constexpr void critical(Args&&... args)
127{
128 return log(Verbosity::CRITICAL, globalLevel, args...);
129}
130
131template <typename... Args>
132constexpr void warning(Args&&... args)
133{
134 return log(Verbosity::WARNING, globalLevel, args...);
135}
136
137template <typename... Args>
138constexpr void on_exit(Args&&... args)
139{
140 return log(Verbosity::ON_EXIT, globalLevel, args...);
141}
142
143template <typename... Args>
144constexpr void on_entry(Args&&... args)
145{
146 return log(Verbosity::ON_ENTRY, globalLevel, args...);
147}
148
149template <typename... Args>
150constexpr void per_iteration(Args&&... args)
151{
152 return log(Verbosity::PER_ITERATION, globalLevel, args...);
153}
154
155template <typename... Args>
156constexpr void debug(Args&&... args)
157{
158 return log(Verbosity::DEBUG, globalLevel, args...);
159}
160
161} // namespace Log
162} // namespace Xped
163
164#endif
auto log(Verbosity verb, Verbosity policy, std::string_view fmt, Args &&... args)
Definition: Logging.hpp:50
constexpr void on_exit(Verbosity policy, Args &&... args)
Definition: Logging.hpp:102
constexpr void on_entry(Verbosity policy, Args &&... args)
Definition: Logging.hpp:108
void init_logging(const mpi::XpedWorld &world, const std::string &name)
Definition: Logging.hpp:23
constexpr void warning(Verbosity policy, Args &&... args)
Definition: Logging.hpp:96
constexpr void per_iteration(Verbosity policy, Args &&... args)
Definition: Logging.hpp:114
Verbosity globalLevel
Definition: Logging.hpp:47
constexpr void critical(Verbosity policy, Args &&... args)
Definition: Logging.hpp:90
constexpr void debug(Verbosity policy, Args &&... args)
Definition: Logging.hpp:120
Definition: bench.cpp:62
Definition: Mpi.hpp:34
int rank
Definition: Mpi.hpp:35
int np
Definition: Mpi.hpp:36