Iguana
1.2.1
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
18
class
Logger
19
{
20
21
friend
class
Object;
22
23
public
:
24
37
enum
Level
{
38
trace,
39
debug,
40
info,
41
quiet,
42
warn,
43
error,
44
silent
45
};
46
48
static
Level
const
DEFAULT_LEVEL
= info;
49
53
Logger
(std::string_view name =
"log"
,
Level
const
lev =
DEFAULT_LEVEL
,
bool
const
enable_style =
true
);
54
~Logger
() {}
55
59
void
SetLevel
(std::string_view lev);
60
64
void
SetLevel
(
Level
const
lev);
65
68
Level
GetLevel
();
69
72
std::string
GetLevelName
();
73
75
void
EnableStyle
();
76
78
void
DisableStyle
();
79
84
static
std::string
Header
(std::string_view message,
int
const
width = 50);
85
87
template
<
typename
... VALUES>
88
void
Trace
(std::string_view message,
const
VALUES... vals)
const
{
Print
(trace, message, vals...); }
90
template
<
typename
... VALUES>
91
void
Debug
(std::string_view message,
const
VALUES... vals)
const
{
Print
(debug, message, vals...); }
93
template
<
typename
... VALUES>
94
void
Info
(std::string_view message,
const
VALUES... vals)
const
{
Print
(info, message, vals...); }
96
template
<
typename
... VALUES>
97
void
Warn
(std::string_view message,
const
VALUES... vals)
const
{
Print
(warn, message, vals...); }
99
template
<
typename
... VALUES>
100
void
Error
(std::string_view message,
const
VALUES... vals)
const
{
Print
(error, message, vals...); }
101
107
template
<
typename
... VALUES>
108
void
Print
(
Level
const
lev, std::string_view message,
const
VALUES... vals)
const
109
{
110
if
(lev >= m_level) {
111
if
(
auto
it{m_level_names.find(lev)}; it != m_level_names.end()) {
112
std::function<std::string(std::string)> style = [](std::string s) {
return
fmt::format(
"[{}]"
, s); };
113
if
(m_enable_style) {
114
switch
(lev) {
115
case
warn:
116
style = [](std::string s) {
return
fmt::format(
"[{}]"
, fmt::styled(s, fmt::emphasis::bold | fmt::fg(fmt::terminal_color::magenta))); };
117
break
;
118
case
error:
119
style = [](std::string s) {
return
fmt::format(
"[{}]"
, fmt::styled(s, fmt::emphasis::bold | fmt::fg(fmt::terminal_color::red))); };
120
break
;
121
default
:
122
style = [](std::string s) {
return
fmt::format(
"[{}]"
, fmt::styled(s, fmt::emphasis::bold)); };
123
}
124
}
125
fmt::print(
126
lev >= warn ? stderr : stdout,
127
fmt::runtime(fmt::format(
"{} {} {}\n"
, style(it->second), style(m_name), message)),
128
vals...);
129
}
130
else
{
131
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
132
Error
(message, vals...);
133
}
134
}
135
}
136
137
private
:
138
140
std::string m_name;
141
143
Level
m_level;
144
146
std::unordered_map<Level, std::string> m_level_names;
147
149
bool
m_enable_style;
150
};
151
}
iguana::Logger::Info
void Info(std::string_view message, const VALUES... vals) const
Printout a log message at the info level.
Definition
Logger.h:94
iguana::Logger::Error
void Error(std::string_view message, const VALUES... vals) const
Printout a log message at the error level.
Definition
Logger.h:100
iguana::Logger::Level
Level
Definition
Logger.h:37
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:88
iguana::Logger::Debug
void Debug(std::string_view message, const VALUES... vals) const
Printout a log message at the debug level.
Definition
Logger.h:91
iguana::Logger::Print
void Print(Level const lev, std::string_view message, const VALUES... vals) const
Definition
Logger.h:108
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:97
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:48
iguana::Logger::DisableStyle
void DisableStyle()
Disable styled log printout color and emphasis.
iguana::Logger::GetLevelName
std::string GetLevelName()
iguana_v1.2.1
src
iguana
services
Logger.h
Generated by
1.18.0