Iguana 0.0.0
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
Logger.h
1#pragma once
2
3// workaround https://github.com/fmtlib/fmt/issues/4133
4#ifdef __clang__
5#pragma clang diagnostic push
6#pragma clang diagnostic ignored "-Wshift-overflow"
7#else
8#pragma GCC diagnostic push
9#pragma GCC diagnostic ignored "-Wstringop-overflow"
10#endif
11#include <fmt/color.h>
12#include <fmt/format.h>
13#include <fmt/ranges.h>
14#ifdef __clang__
15#pragma clang diagnostic pop
16#else
17#pragma GCC diagnostic pop
18#endif
19
20#include <functional>
21#include <unordered_map>
22
23namespace iguana {
24
30 class Logger
31 {
32
33 friend class Object;
34
35 public:
36
45 enum Level {
46 trace,
47 debug,
48 info,
49 quiet,
50 warn,
51 error,
52 silent
53 };
54
56 static Level const DEFAULT_LEVEL = info;
57
61 Logger(std::string_view name = "log", Level const lev = DEFAULT_LEVEL, bool const enable_style = true);
62 ~Logger() {}
63
67 void SetLevel(std::string_view lev);
68
72 void SetLevel(Level const lev);
73
77
80
83
88 static std::string Header(std::string_view message, int const width = 50);
89
91 template <typename... VALUES>
92 void Trace(std::string_view message, const VALUES... vals) const { Print(trace, message, vals...); }
94 template <typename... VALUES>
95 void Debug(std::string_view message, const VALUES... vals) const { Print(debug, message, vals...); }
97 template <typename... VALUES>
98 void Info(std::string_view message, const VALUES... vals) const { Print(info, message, vals...); }
100 template <typename... VALUES>
101 void Warn(std::string_view message, const VALUES... vals) const { Print(warn, message, vals...); }
103 template <typename... VALUES>
104 void Error(std::string_view message, const VALUES... vals) const { Print(error, message, vals...); }
105
111 template <typename... VALUES>
112 void Print(Level const lev, std::string_view message, const VALUES... vals) const
113 {
114 if(lev >= m_level) {
115 if(auto it{m_level_names.find(lev)}; it != m_level_names.end()) {
116 std::function<std::string(std::string)> style = [](std::string s)
117 { return fmt::format("[{}]", s); };
118 if(m_enable_style) {
119 switch(lev) {
120 case warn:
121 style = [](std::string s)
122 { return fmt::format("[{}]", fmt::styled(s, fmt::emphasis::bold | fmt::fg(fmt::terminal_color::magenta))); };
123 break;
124 case error:
125 style = [](std::string s)
126 { return fmt::format("[{}]", fmt::styled(s, fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red))); };
127 break;
128 default:
129 style = [](std::string s)
130 { return fmt::format("[{}]", fmt::styled(s, fmt::emphasis::bold)); };
131 }
132 }
133 fmt::print(
134 lev >= warn ? stderr : stdout,
135 fmt::runtime(fmt::format("{} {} {}\n", style(it->second), style(m_name), message)),
136 vals...);
137 }
138 else {
139 Warn("Logger::Print called with unknown log level '{}'; printing as error instead", static_cast<int>(lev)); // FIXME: static_cast -> fmt::underlying, but needs new version of fmt
140 Error(message, vals...);
141 }
142 }
143 }
144
145 private:
146
148 std::string m_name;
149
151 Level m_level;
152
154 std::unordered_map<Level, std::string> m_level_names;
155
157 bool m_enable_style;
158 };
159}
Simple logger service.
Definition Logger.h:31
void Info(std::string_view message, const VALUES... vals) const
Printout a log message at the info level.
Definition Logger.h:98
void Error(std::string_view message, const VALUES... vals) const
Printout a log message at the error level.
Definition Logger.h:104
void EnableStyle()
Enable styled log printouts, with color and emphasis.
Logger(std::string_view name="log", Level const lev=DEFAULT_LEVEL, bool const enable_style=true)
void Trace(std::string_view message, const VALUES... vals) const
Printout a log message at the trace level.
Definition Logger.h:92
void Debug(std::string_view message, const VALUES... vals) const
Printout a log message at the debug level.
Definition Logger.h:95
void Print(Level const lev, std::string_view message, const VALUES... vals) const
Definition Logger.h:112
static std::string Header(std::string_view message, int const width=50)
void SetLevel(std::string_view lev)
void Warn(std::string_view message, const VALUES... vals) const
Printout a log message at the warn level.
Definition Logger.h:101
Level GetLevel()
void SetLevel(Level const lev)
static Level const DEFAULT_LEVEL
The default log level.
Definition Logger.h:56
void DisableStyle()
Disable styled log printout color and emphasis.
A named object with a Logger instance.
Definition Object.h:12
General, top-level namespace for algorithms and infrastructure. For algorithms and bindings,...
Definition Algorithm.h:14