Iguana
1.1.0
Implementation Guardian of Analysis Algorithms
Toggle main menu visibility
Loading...
Searching...
No Matches
Logger.h
1
#pragma once
2
3
#include <fmt/color.h>
4
#include <fmt/format.h>
5
#include <fmt/ranges.h>
6
7
#include <functional>
8
#include <unordered_map>
9
10
namespace
iguana {
11
17
class
Logger
18
{
19
20
friend
class
Object;
21
22
public
:
23
32
enum
Level
{
33
trace,
34
debug,
35
info,
36
quiet,
37
warn,
38
error,
39
silent
40
};
41
43
static
Level
const
DEFAULT_LEVEL
= info;
44
48
Logger
(std::string_view name =
"log"
,
Level
const
lev =
DEFAULT_LEVEL
,
bool
const
enable_style =
true
);
49
~Logger
() {}
50
54
void
SetLevel
(std::string_view lev);
55
59
void
SetLevel
(
Level
const
lev);
60
63
Level
GetLevel
();
64
66
void
EnableStyle
();
67
69
void
DisableStyle
();
70
75
static
std::string
Header
(std::string_view message,
int
const
width = 50);
76
78
template
<
typename
... VALUES>
79
void
Trace
(std::string_view message,
const
VALUES... vals)
const
{
Print
(trace, message, vals...); }
81
template
<
typename
... VALUES>
82
void
Debug
(std::string_view message,
const
VALUES... vals)
const
{
Print
(debug, message, vals...); }
84
template
<
typename
... VALUES>
85
void
Info
(std::string_view message,
const
VALUES... vals)
const
{
Print
(info, message, vals...); }
87
template
<
typename
... VALUES>
88
void
Warn
(std::string_view message,
const
VALUES... vals)
const
{
Print
(warn, message, vals...); }
90
template
<
typename
... VALUES>
91
void
Error
(std::string_view message,
const
VALUES... vals)
const
{
Print
(error, message, vals...); }
92
98
template
<
typename
... VALUES>
99
void
Print
(
Level
const
lev, std::string_view message,
const
VALUES... vals)
const
100
{
101
if
(lev >= m_level) {
102
if
(
auto
it{m_level_names.find(lev)}; it != m_level_names.end()) {
103
std::function<std::string(std::string)> style = [](std::string s) {
return
fmt::format(
"[{}]"
, s); };
104
if
(m_enable_style) {
105
switch
(lev) {
106
case
warn:
107
style = [](std::string s) {
return
fmt::format(
"[{}]"
, fmt::styled(s, fmt::emphasis::bold | fmt::fg(fmt::terminal_color::magenta))); };
108
break
;
109
case
error:
110
style = [](std::string s) {
return
fmt::format(
"[{}]"
, fmt::styled(s, fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red))); };
111
break
;
112
default
:
113
style = [](std::string s) {
return
fmt::format(
"[{}]"
, fmt::styled(s, fmt::emphasis::bold)); };
114
}
115
}
116
fmt::print(
117
lev >= warn ? stderr : stdout,
118
fmt::runtime(fmt::format(
"{} {} {}\n"
, style(it->second), style(m_name), message)),
119
vals...);
120
}
121
else
{
122
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
123
Error
(message, vals...);
124
}
125
}
126
}
127
128
private
:
129
131
std::string m_name;
132
134
Level
m_level;
135
137
std::unordered_map<Level, std::string> m_level_names;
138
140
bool
m_enable_style;
141
};
142
}
iguana::Logger::Info
void Info(std::string_view message, const VALUES... vals) const
Printout a log message at the info level.
Definition
Logger.h:85
iguana::Logger::Error
void Error(std::string_view message, const VALUES... vals) const
Printout a log message at the error level.
Definition
Logger.h:91
iguana::Logger::Level
Level
Definition
Logger.h:32
iguana::Logger::EnableStyle
void EnableStyle()
Enable styled log printouts, with color and emphasis.
iguana::Logger::Logger
Logger(std::string_view name="log", Level const lev=DEFAULT_LEVEL, bool const enable_style=true)
iguana::Logger::Trace
void Trace(std::string_view message, const VALUES... vals) const
Printout a log message at the trace level.
Definition
Logger.h:79
iguana::Logger::Debug
void Debug(std::string_view message, const VALUES... vals) const
Printout a log message at the debug level.
Definition
Logger.h:82
iguana::Logger::Print
void Print(Level const lev, std::string_view message, const VALUES... vals) const
Definition
Logger.h:99
iguana::Logger::Header
static std::string Header(std::string_view message, int const width=50)
iguana::Logger::SetLevel
void SetLevel(std::string_view lev)
iguana::Logger::Warn
void Warn(std::string_view message, const VALUES... vals) const
Printout a log message at the warn level.
Definition
Logger.h:88
iguana::Logger::GetLevel
Level GetLevel()
iguana::Logger::SetLevel
void SetLevel(Level const lev)
iguana::Logger::DEFAULT_LEVEL
static Level const DEFAULT_LEVEL
The default log level.
Definition
Logger.h:43
iguana::Logger::DisableStyle
void DisableStyle()
Disable styled log printout color and emphasis.
iguana_v1.1.0
src
iguana
services
Logger.h
Generated by
1.18.0