Console Library
8.0.0
A header-only library that makes C++ simple
Toggle main menu visibility
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1
11
12
/*
13
Copyright (c) 2026 MrXie1109
14
15
Permission is hereby granted, free of charge, to any person obtaining a copy
16
of this software and associated documentation files (the "Software"), to deal
17
in the Software without restriction, including without limitation the rights
18
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
copies of the Software, and to permit persons to whom the Software is
20
furnished to do so, subject to the following conditions:
21
22
The above copyright notice and this permission notice shall be included in all
23
copies or substantial portions of the Software.
24
25
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
SOFTWARE.
32
*/
33
34
#pragma once
35
#include <cstdint>
36
#include <sstream>
37
38
#include "
../core/csexc.h
"
39
#include "
../io/colorful.h
"
40
#include "
../io/output.h
"
41
#include "
../text/strpp.h
"
42
#include "
./time.h
"
43
44
namespace
console
{
54
template
<
class
CharT =
char
,
class
Traits = std::
char
_traits<CharT>>
55
class
BasicLogging
{
56
public
:
57
using
string_type
= std::basic_string<CharT, Traits>;
58
68
enum class
Level
: int8_t { DEBUG, INFO, WARN, ERROR, FATAL };
69
70
private
:
71
BasicOutput<CharT, Traits>
output
;
72
bool
colorful
;
73
bool
settings
[5];
74
75
public
:
80
void
set
(
Level
minLevel) {
81
int8_t n = int8_t(minLevel);
82
for
(
int
i = 0; i < n; i++)
settings
[i] =
false
;
83
for
(
int
i = n; i < 5; i++)
settings
[i] =
true
;
84
}
85
94
void
set
(
bool
a,
bool
b,
bool
c,
bool
d,
bool
e) {
95
settings
[0] = a;
96
settings
[1] = b;
97
settings
[2] = c;
98
settings
[3] = d;
99
settings
[4] = e;
100
}
101
108
BasicLogging
(std::basic_ostream<CharT, Traits> &os = std::clog,
109
bool
cf =
false
,
110
Level
lvl =
Level::INFO
) :
111
output
(os,
string_type
{},
string_type
{1, CharT(
'\n'
)}, true),
112
colorful
(cf) {
113
set
(lvl);
114
}
115
121
template
<
class
... Args>
122
void
debug
(
const
Args &...args) {
123
if
(
settings
[0]) {
124
if
(
colorful
)
125
output
(color::BrightBlack,
126
CharT(
'['
),
127
datetime
(),
128
"] [DEBUG] - "
,
129
args...,
130
color::Reset);
131
else
132
output
(CharT(
'['
),
datetime
(),
"] [DEBUG] - "
, args...);
133
}
134
}
135
141
template
<
class
... Args>
142
void
info
(
const
Args &...args) {
143
if
(
settings
[1]) {
144
if
(
colorful
)
145
output
(color::BrightCyan,
146
CharT(
'['
),
147
datetime
(),
148
"] [.INFO] - "
,
149
args...,
150
color::Reset);
151
else
152
output
(CharT(
'['
),
datetime
(),
"] [.INFO] - "
, args...);
153
}
154
}
155
161
template
<
class
... Args>
162
void
warn
(
const
Args &...args) {
163
if
(
settings
[2]) {
164
if
(
colorful
)
165
output
(color::BrightYellow,
166
CharT(
'['
),
167
datetime
(),
168
"] [.WARN] - "
,
169
args...,
170
color::Reset);
171
else
172
output
(CharT(
'['
),
datetime
(),
"] [.WARN] - "
, args...);
173
}
174
}
175
181
template
<
class
... Args>
182
void
error
(
const
Args &...args) {
183
if
(
settings
[3]) {
184
if
(
colorful
)
185
output
(color::BrightRed,
186
CharT(
'['
),
187
datetime
(),
188
"] [ERROR] - "
,
189
args...,
190
color::Reset);
191
else
192
output
(CharT(
'['
),
datetime
(),
"] [ERROR] - "
, args...);
193
}
194
}
195
202
template
<
class
... Args>
203
void
fatal
(
const
Args &...args) {
204
std::string error_info(
to_string
(args...));
205
if
(
settings
[4]) {
206
if
(
colorful
)
207
output
(color::BrightMagenta,
208
CharT(
'['
),
209
datetime
(),
210
"] [FATAL] - "
,
211
error_info,
212
color::Reset);
213
else
214
output
(CharT(
'['
),
datetime
(),
"] [FATAL] - "
, error_info);
215
}
216
throw
FatalLogging
(
"Fatal Error: "
+ error_info);
217
}
218
};
219
221
using
Logging
=
BasicLogging<char>
;
222
224
using
WLogging
=
BasicLogging<wchar_t>
;
225
229
inline
BasicLogging<>
&
get_logger
() {
230
static
BasicLogging<>
instance(
231
std::clog,
true
,
BasicLogging<>::Level::INFO
);
232
return
instance;
233
}
234
235
static
BasicLogging<>
&
logger
236
=
get_logger
();
237
}
console::BasicLogging
日志记录器,支持多级别、颜色输出和自动时间戳。
Definition
logging.h:55
console::BasicLogging::info
void info(const Args &...args)
输出 INFO 级别日志。
Definition
logging.h:142
console::BasicLogging::warn
void warn(const Args &...args)
输出 WARN 级别日志。
Definition
logging.h:162
console::BasicLogging::set
void set(Level minLevel)
设置最低日志级别(低于该级别的日志将被忽略)。
Definition
logging.h:80
console::BasicLogging< char >::settings
bool settings[5]
Definition
logging.h:73
console::BasicLogging< char >::colorful
bool colorful
Definition
logging.h:72
console::BasicLogging::BasicLogging
BasicLogging(std::basic_ostream< CharT, Traits > &os=std::clog, bool cf=false, Level lvl=Level::INFO)
构造 BasicLogging 对象。
Definition
logging.h:108
console::BasicLogging::error
void error(const Args &...args)
输出 ERROR 级别日志。
Definition
logging.h:182
console::BasicLogging::string_type
std::basic_string< CharT, Traits > string_type
Definition
logging.h:57
console::BasicLogging< char >::output
BasicOutput< char, std::char_traits< char > > output
Definition
logging.h:71
console::BasicLogging::Level
Level
日志级别枚举。
Definition
logging.h:68
console::BasicLogging::Level::INFO
@ INFO
Definition
logging.h:68
console::BasicLogging::fatal
void fatal(const Args &...args)
输出 FATAL 级别日志并抛出 FatalLogging 异常。
Definition
logging.h:203
console::BasicLogging::debug
void debug(const Args &...args)
输出 DEBUG 级别日志。
Definition
logging.h:122
console::BasicLogging::set
void set(bool a, bool b, bool c, bool d, bool e)
单独设置每个级别的开关。
Definition
logging.h:94
console::BasicOutput
灵活的输出控制类,支持链式调用和多种参数。
Definition
output.h:420
console::FatalLogging::FatalLogging
FatalLogging(const std::string &msg)
构造 FatalLogging。
Definition
csexc.h:61
colorful.h
提供控制台颜色和样式修饰的 ANSI 转义序列常量。
csexc.h
定义 console 库使用的自定义异常类层次结构。
console::to_string
std::string to_string(Args &&...args)
将任意多个参数转换为字符串并拼接(无分隔符)。
Definition
strpp.h:664
console
本库所有组件所在的顶层命名空间。
console::WLogging
BasicLogging< wchar_t > WLogging
BasicLogging<wchar_t> 的类型别名。
Definition
logging.h:224
console::logger
static BasicLogging & logger
全局默认 logger 实例,启用颜色,级别 INFO。
Definition
logging.h:236
console::get_logger
BasicLogging & get_logger()
内部单例(char)。
Definition
logging.h:229
console::Logging
BasicLogging< char > Logging
BasicLogging<char> 的类型别名。
Definition
logging.h:221
console::datetime
std::string datetime(const std::string &fmt="%Y-%m-%d %H:%M:%S")
获取当前日期时间字符串。
Definition
time.h:348
output.h
提供 STL 容器的格式化输出和灵活的输出控制工具。
strpp.h
提供字符串处理工具函数和格式化类。
time.h
提供时间度量、计时、休眠和日期时间格式化功能。
include
util
logging.h
Generated on
for Console Library by
1.18.0