Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1
11
12/*
13Copyright (c) 2026 MrXie1109
14
15Permission is hereby granted, free of charge, to any person obtaining a copy
16of this software and associated documentation files (the "Software"), to deal
17in the Software without restriction, including without limitation the rights
18to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19copies of the Software, and to permit persons to whom the Software is
20furnished to do so, subject to the following conditions:
21
22The above copyright notice and this permission notice shall be included in all
23copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31SOFTWARE.
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
44namespace console {
54 template <class CharT = char, class Traits = std::char_traits<CharT>>
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:
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
222
225
230 static BasicLogging<> instance(
231 std::clog, true, BasicLogging<>::Level::INFO);
232 return instance;
233 }
234
236 = get_logger();
237}
日志记录器,支持多级别、颜色输出和自动时间戳。
Definition logging.h:55
void info(const Args &...args)
输出 INFO 级别日志。
Definition logging.h:142
void warn(const Args &...args)
输出 WARN 级别日志。
Definition logging.h:162
void set(Level minLevel)
设置最低日志级别(低于该级别的日志将被忽略)。
Definition logging.h:80
bool settings[5]
Definition logging.h:73
bool colorful
Definition logging.h:72
BasicLogging(std::basic_ostream< CharT, Traits > &os=std::clog, bool cf=false, Level lvl=Level::INFO)
构造 BasicLogging 对象。
Definition logging.h:108
void error(const Args &...args)
输出 ERROR 级别日志。
Definition logging.h:182
std::basic_string< CharT, Traits > string_type
Definition logging.h:57
BasicOutput< char, std::char_traits< char > > output
Definition logging.h:71
Level
日志级别枚举。
Definition logging.h:68
@ INFO
Definition logging.h:68
void fatal(const Args &...args)
输出 FATAL 级别日志并抛出 FatalLogging 异常。
Definition logging.h:203
void debug(const Args &...args)
输出 DEBUG 级别日志。
Definition logging.h:122
void set(bool a, bool b, bool c, bool d, bool e)
单独设置每个级别的开关。
Definition logging.h:94
灵活的输出控制类,支持链式调用和多种参数。
Definition output.h:420
FatalLogging(const std::string &msg)
构造 FatalLogging。
Definition csexc.h:61
提供控制台颜色和样式修饰的 ANSI 转义序列常量。
定义 console 库使用的自定义异常类层次结构。
std::string to_string(Args &&...args)
将任意多个参数转换为字符串并拼接(无分隔符)。
Definition strpp.h:664
本库所有组件所在的顶层命名空间。
BasicLogging< wchar_t > WLogging
BasicLogging<wchar_t> 的类型别名。
Definition logging.h:224
static BasicLogging & logger
全局默认 logger 实例,启用颜色,级别 INFO。
Definition logging.h:236
BasicLogging & get_logger()
内部单例(char)。
Definition logging.h:229
BasicLogging< char > Logging
BasicLogging<char> 的类型别名。
Definition logging.h:221
std::string datetime(const std::string &fmt="%Y-%m-%d %H:%M:%S")
获取当前日期时间字符串。
Definition time.h:348
提供 STL 容器的格式化输出和灵活的输出控制工具。
提供字符串处理工具函数和格式化类。
提供时间度量、计时、休眠和日期时间格式化功能。