1#ifndef XPED_LOGGING_HPP_
2#define XPED_LOGGING_HPP_
8#include "spdlog/sinks/basic_file_sink.h"
9#include "spdlog/sinks/stdout_color_sinks.h"
10#include "spdlog/spdlog.h"
12#include <boost/algorithm/string.hpp>
13#include <boost/describe.hpp>
19BOOST_DEFINE_ENUM_CLASS(Verbosity, SILENT, CRITICAL, WARNING, ON_EXIT, ON_ENTRY, PER_ITERATION, DEBUG)
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);
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);
35 my_logger->set_level(spdlog::level::trace);
36 spdlog::set_default_logger(my_logger);
37 spdlog::flush_every(std::chrono::seconds(3));
49template <
typename... Args>
50auto log(Verbosity verb, Verbosity policy, std::string_view fmt, Args&&... args)
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"));
57 case Verbosity::DEBUG: {
58 for(
const auto& line : lines) { spdlog::trace(
"{}", line); }
61 case Verbosity::PER_ITERATION: {
62 for(
const auto& line : lines) { spdlog::debug(
"{}", line); }
65 case Verbosity::ON_ENTRY: {
67 for(
const auto& line : lines) { spdlog::info(
"{}", line); }
70 case Verbosity::ON_EXIT: {
71 for(
const auto& line : lines) { spdlog::warn(
"{}", line); }
74 case Verbosity::WARNING: {
75 for(
const auto& line : lines) { spdlog::error(
"{}", line); }
78 case Verbosity::CRITICAL: {
79 for(
const auto& line : lines) { spdlog::critical(
"{}", line); }
82 case Verbosity::SILENT: {
89template <
typename... Args>
90constexpr void critical(Verbosity policy, Args&&... args)
92 return log(Verbosity::CRITICAL, policy, args...);
95template <
typename... Args>
96constexpr void warning(Verbosity policy, Args&&... args)
98 return log(Verbosity::WARNING, policy, args...);
101template <
typename... Args>
102constexpr void on_exit(Verbosity policy, Args&&... args)
104 return log(Verbosity::ON_EXIT, policy, args...);
107template <
typename... Args>
108constexpr void on_entry(Verbosity policy, Args&&... args)
110 return log(Verbosity::ON_ENTRY, policy, args...);
113template <
typename... Args>
116 return log(Verbosity::PER_ITERATION, policy, args...);
119template <
typename... Args>
120constexpr void debug(Verbosity policy, Args&&... args)
122 return log(Verbosity::DEBUG, policy, args...);
125template <
typename... Args>
131template <
typename... Args>
137template <
typename... Args>
143template <
typename... Args>
149template <
typename... Args>
155template <
typename... Args>
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
int rank
Definition: Mpi.hpp:35
int np
Definition: Mpi.hpp:36