Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
11/*
12Copyright (c) 2026 MrXie1109
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#include <cstdint>
35#include <sstream>
36#include "output.h"
37#include "colorful.h"
38#include "time.h"
39#include "strpp.h"
40#include "csexc.h"
41#include "literals.h"
42
43namespace console
44{
46 * @class Logging
47 * @brief 日志记录器,支持多级别、颜色输出和自动时间戳。
48 * @details 使用 Output 类进行格式化输出,可设置最低日志级别或单独控制每个级别的开关。
49 * FATAL 级别会抛出 fatal_logging 异常。
50 */
51 class Logging
52 {
53 public:
61 * @var FATAL 致命错误(会抛出异常)
62 */
63 enum class Level : int8_t
64 {
70 };
71
72 private:
73 Output output;
74 bool colorful;
75 bool settings[5];
76
77 public:
82 void set(Level minLevel)
83 {
84 int8_t n = int8_t(minLevel);
85 for (int i = 0; i < n; i++)
86 settings[i] = false;
87 for (int i = n; i < 5; i++)
88 settings[i] = true;
89 }
90
91 /**
92 * @brief 单独设置每个级别的开关。
93 * @param a DEBUG 开关
94 * @param b INFO 开关
95 * @param c WARN 开关
96 * @param d ERROR 开关
97 * @param e FATAL 开关
98 */
99 void set(bool a, bool b, bool c, bool d, bool e)
100 {
101 settings[0] = a;
102 settings[1] = b;
103 settings[2] = c;
104 settings[3] = d;
105 settings[4] = e;
106 }
107
110 * @param os 输出流,默认为 std::cout。
111 * @param cf 是否启用颜色,默认 false。
112 * @param lvl 最低日志级别,默认 INFO。
113 */
114 Logging(std::ostream &os = std::clog,
115 bool cf = false, Level lvl = Level::INFO)
116 : output(os, "", "\n", true),
117 colorful(cf) { set(lvl); }
118
122
124 template <class... Args>
125 void debug(const Args &...args)
126 {
127 if (settings[0])
128 {
129 if (colorful)
130 output(color::BrightBlack,
131 '[', datetime(), "] [DEBUG] - ",
132 args..., color::Reset);
133 else
134 output('[', datetime(), "] [DEBUG] - ", args...);
135 }
136 }
137
141
143 template <class... Args>
144 void info(const Args &...args)
145 {
146 if (settings[1])
147 {
148 if (colorful)
150 '[', datetime(), "] [.INFO] - ",
151 args..., color::Reset);
152 else
153 output('[', datetime(), "] [.INFO] - ", args...);
154 }
155 }
156
162 template <class... Args>
163 void warn(const Args &...args)
164 {
165 if (settings[2])
166 {
167 if (colorful)
168 output(color::BrightYellow,
169 '[', datetime(), "] [.WARN] - ",
170 args..., color::Reset);
171 else
172 output('[', datetime(), "] [.WARN] - ", args...);
173 }
174 }
175
181 template <class... Args>
182 void error(const Args &...args)
183 {
184 if (settings[3])
185 {
186 if (colorful)
187 output(color::BrightRed,
188 '[', datetime(), "] [ERROR] - ",
189 args..., color::Reset);
190 else
191 output('[', datetime(), "] [ERROR] - ", args...);
192 }
193 }
194
201 template <class... Args>
202 void fatal(const Args &...args)
203 {
204 std::string error_info(uni_to_str(args...));
205 if (settings[4])
206 {
207 if (colorful)
209 '[', datetime(), "] [FATAL] - ",
210 error_info, color::Reset);
211 else
212 output('[', datetime(), "] [FATAL] - ", error_info);
213 }
214 throw fatal_logging("Fatal Error: " + error_info);
215 }
216 } logger(std::clog, true, Logging::Level::INFO);
217}
void warn(const Args &...args)
输出 WARN 级别日志。
Definition logging.h:163
Logging(std::ostream &os=std::clog, bool cf=false, Level lvl=Level::INFO)
构造 Logging 对象。
Definition logging.h:114
void error(const Args &...args)
输出 ERROR 级别日志。
Definition logging.h:182
void info(const Args &...args)
输出 INFO 级别日志。
Definition logging.h:144
void fatal(const Args &...args)
输出 FATAL 级别日志并抛出 fatal_logging 异常。
Definition logging.h:202
void debug(const Args &...args)
输出 DEBUG 级别日志。
Definition logging.h:125
void set(Level minLevel)
设置最低日志级别(低于该级别的日志将被忽略)。
Definition logging.h:82
Level
日志级别枚举。
Definition logging.h:64
@ FATAL
Definition logging.h:69
@ WARN
Definition logging.h:67
@ INFO
Definition logging.h:66
@ ERROR
Definition logging.h:68
@ DEBUG
Definition logging.h:65
void set(bool a, bool b, bool c, bool d, bool e)
单独设置每个级别的开关。
Definition logging.h:99
表示致命日志错误,通常会导致程序终止。
Definition csexc.h:59
提供控制台颜色和样式修饰的 ANSI 转义序列常量。
定义 console 库使用的自定义异常类层次结构。
std::string uni_to_str(Args &&...args)
将任意多个参数转换为字符串并拼接(无分隔符)。
Definition strpp.h:281
提供自定义字面量运算符,包括字符串字面量、时间字面量和格式化字符串字面量。
const char * BrightRed
亮红色前景
Definition colorful.h:62
const char * BrightBlack
亮黑色(灰)前景
Definition colorful.h:60
const char * BrightYellow
亮黄色前景
Definition colorful.h:66
const char * BrightCyan
亮青色前景
Definition colorful.h:72
const char * BrightMagenta
亮品红前景
Definition colorful.h:70
const char * Reset
重置所有颜色/样式
Definition colorful.h:76
本库所有组件所在的顶层命名空间。
std::string datetime(const std::string &fmt="%Y-%m-%d %H:%M:%S")
获取当前日期时间字符串。
Definition time.h:166
class console::Logging logger(std::clog, true, Logging::Level::INFO)
全局默认 logger 实例,启用颜色,级别 INFO。
提供 STL 容器的格式化输出和灵活的输出控制工具。
提供字符串处理工具函数和格式化类。
提供时间度量、计时、休眠和日期时间格式化功能。