cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
toolbox::logger::thread_logger_t Class Reference

具有多个日志级别和格式的线程安全日志类 / Thread-safe logging class with multiple logging levels and formats More...

#include <thread_logger.hpp>

Classes

class  thread_format_logger_t
 基于格式的日志记录器,用于printf风格的消息格式化 / Format-based logger for printf-style message formatting More...
 
class  thread_stream_logger_t
 用于格式化输出的线程安全流式日志记录器类 / Thread-safe stream logger class for formatted output More...
 

Public Types

enum class  Level : uint8_t {
  TRACE , DEBUG , INFO , WARN ,
  ERR , CRITICAL
}
 日志级别枚举 / Enumeration of logging levels More...
 

Public Member Functions

auto level () -> Level
 获取当前日志级别 / Get the current logging level
 
auto level_str () -> std::string
 获取当前日志级别的字符串表示 / Get the current logging level as a string
 
auto set_level (Level level) -> void
 设置日志级别 / Set the logging level
 
auto trace_f () -> thread_format_logger_t
 获取用于TRACE级别消息的格式化日志记录器 / Get a format logger for TRACE level messages
 
auto debug_f () -> thread_format_logger_t
 获取用于DEBUG级别消息的格式化日志记录器 / Get a format logger for DEBUG level messages
 
auto info_f () -> thread_format_logger_t
 获取用于INFO级别消息的格式化日志记录器 / Get a format logger for INFO level messages
 
auto warn_f () -> thread_format_logger_t
 获取用于WARN级别消息的格式化日志记录器 / Get a format logger for WARN level messages
 
auto error_f () -> thread_format_logger_t
 获取用于ERROR级别消息的格式化日志记录器 / Get a format logger for ERROR level messages
 
auto critical_f () -> thread_format_logger_t
 获取用于CRITICAL级别消息的格式化日志记录器 / Get a format logger for CRITICAL level messages
 
auto trace_s () -> thread_stream_logger_t
 获取用于TRACE级别消息的流式日志记录器 / Get a stream logger for TRACE level messages
 
auto debug_s () -> thread_stream_logger_t
 获取用于DEBUG级别消息的流式日志记录器 / Get a stream logger for DEBUG level messages
 
auto info_s () -> thread_stream_logger_t
 获取用于INFO级别消息的流式日志记录器 / Get a stream logger for INFO level messages
 
auto warn_s () -> thread_stream_logger_t
 获取用于WARN级别消息的流式日志记录器 / Get a stream logger for WARN level messages
 
auto error_s () -> thread_stream_logger_t
 获取用于ERROR级别消息的流式日志记录器 / Get a stream logger for ERROR level messages
 
auto critical_s () -> thread_stream_logger_t
 获取用于CRITICAL级别消息的流式日志记录器 / Get a stream logger for CRITICAL level messages
 

Static Public Member Functions

static auto instance () -> thread_logger_t &
 获取日志记录器的单例实例 / Get the singleton instance of the logger
 
static void shutdown ()
 显式关闭日志记录器实例的工作线程 / Explicitly shuts down the logger instance's worker thread
 

Detailed Description

具有多个日志级别和格式的线程安全日志类 / Thread-safe logging class with multiple logging levels and formats

thread_logger_t类提供了一个线程安全的日志机制,支持不同的日志级别(TRACE, DEBUG, INFO, WARN, ERR, CRITICAL)。它通过其嵌套类thread_stream_logger_t和thread_format_logger_t提供基于流和格式的日志功能。/ The thread_logger_t class provides a thread-safe logging mechanism with support for different logging levels (TRACE, DEBUG, INFO, WARN, ERR, CRITICAL). It offers both stream-based and format-based logging capabilities through its nested classes thread_stream_logger_t and thread_format_logger_t.

Note
此类实现了单例模式,并且对于来自多个线程的并发日志操作是线程安全的 / This class implements the singleton pattern and is thread-safe for concurrent logging operations from multiple threads
See also
thread_stream_logger_t
thread_format_logger_t
// 使用流式日志记录器的基本用法 / Basic usage with stream logger
LOG_INFO_S << "Application started with version " << version;
// 使用格式化日志记录器的基本用法 / Basic usage with format logger
LOG_DEBUG_F("Processing {} items in batch {}", item_count, batch_id);
@ INFO
一般操作消息 / General operational messages
static auto instance() -> thread_logger_t &
获取日志记录器的单例实例 / Get the singleton instance of the logger
#define LOG_INFO_S
INFO级别流式日志的宏 / Macro for INFO level stream logging.
Definition thread_logger.hpp:1330
#define LOG_DEBUG_F
DEBUG级别格式化日志的宏 / Macro for DEBUG level format logging.
Definition thread_logger.hpp:1322

Member Enumeration Documentation

◆ Level

enum class toolbox::logger::thread_logger_t::Level : uint8_t
strong

日志级别枚举 / Enumeration of logging levels

定义了日志消息的严重程度级别,从最不严重(TRACE)到最严重(CRITICAL)。低于当前日志级别的消息将被忽略。/ Defines the severity levels for log messages, ordered from least severe (TRACE) to most severe (CRITICAL). Messages with a level below the current logging level will be ignored.

Enumerator
TRACE 

用于调试的详细跟踪信息 / Detailed trace information for debugging

DEBUG 

用于开发的调试级别信息 / Debug-level information for development

INFO 

一般操作消息 / General operational messages

WARN 

需要注意的警告条件 / Warning conditions that might need attention

ERR 

需要调查的错误条件 / Error conditions that need investigation (Renamed from ERROR)

CRITICAL 

需要立即采取行动的关键条件 / Critical conditions requiring immediate action

Member Function Documentation

◆ critical_f()

auto toolbox::logger::thread_logger_t::critical_f ( ) -> thread_format_logger_t
inline

获取用于CRITICAL级别消息的格式化日志记录器 / Get a format logger for CRITICAL level messages

Returns
配置为CRITICAL级别的thread_format_logger_t实例 / A thread_format_logger_t instance configured for CRITICAL level
LOG_CRITICAL_F("系统因严重错误关闭: {}", error);
// 输出: 系统因严重错误关闭: 内存耗尽 / Output: System shutdown due to
critical error: Out of memory
#define LOG_CRITICAL_F
CRITICAL级别格式化日志的宏 / Macro for CRITICAL level format logging.
Definition thread_logger.hpp:1326

◆ critical_s()

auto toolbox::logger::thread_logger_t::critical_s ( ) -> thread_stream_logger_t
inline

获取用于CRITICAL级别消息的流式日志记录器 / Get a stream logger for CRITICAL level messages

Returns
配置为CRITICAL级别的thread_stream_logger_t实例 / A thread_stream_logger_t instance configured for CRITICAL level
// 记录严重系统错误 / Log critical system error
LOG_CRITICAL_S << "系统组件" << component << "发生严重错误,正在关闭系统";
// 记录资源耗尽 / Log resource exhaustion
LOG_CRITICAL_S << "内存耗尽: 已用" << used_mem << "MB, 总计" << total_mem
<< "MB";
#define LOG_CRITICAL_S
CRITICAL级别流式日志的宏 / Macro for CRITICAL level stream logging.
Definition thread_logger.hpp:1333

◆ debug_f()

auto toolbox::logger::thread_logger_t::debug_f ( ) -> thread_format_logger_t
inline

获取用于DEBUG级别消息的格式化日志记录器 / Get a format logger for DEBUG level messages

Returns
配置为DEBUG级别的thread_format_logger_t实例 / A thread_format_logger_t instance configured for DEBUG level
LOG_DEBUG_F("已接收 {} 字节数据", data_size);
// 输出: 已接收 1024 字节数据 / Output: Received 1024 bytes of data

◆ debug_s()

auto toolbox::logger::thread_logger_t::debug_s ( ) -> thread_stream_logger_t
inline

获取用于DEBUG级别消息的流式日志记录器 / Get a stream logger for DEBUG level messages

Returns
配置为DEBUG级别的thread_stream_logger_t实例 / A thread_stream_logger_t instance configured for DEBUG level
LOG_DEBUG_S << "正在处理 " << items.size() << " 个项目";
// 输出: 正在处理 5 个项目 / Output: Processing 5 items
#define LOG_DEBUG_S
DEBUG级别流式日志的宏 / Macro for DEBUG level stream logging.
Definition thread_logger.hpp:1329

◆ error_f()

auto toolbox::logger::thread_logger_t::error_f ( ) -> thread_format_logger_t
inline

获取用于ERROR级别消息的格式化日志记录器 / Get a format logger for ERROR level messages

Returns
配置为ERROR级别的thread_format_logger_t实例 / A thread_format_logger_t instance configured for ERROR level
LOG_ERROR_F("请求处理失败: {}", error_message);
// 输出: 请求处理失败: 连接超时 / Output: Failed to process request:
Connection timeout
#define LOG_ERROR_F
ERROR级别格式化日志的宏 / Macro for ERROR level format logging.
Definition thread_logger.hpp:1325

◆ error_s()

auto toolbox::logger::thread_logger_t::error_s ( ) -> thread_stream_logger_t
inline

获取用于ERROR级别消息的流式日志记录器 / Get a stream logger for ERROR level messages

Returns
配置为ERROR级别的thread_stream_logger_t实例 / A thread_stream_logger_t instance configured for ERROR level
// 记录文件打开错误 / Log file open error
LOG_ERROR_S << "无法打开文件: " << filename << ", 错误码: " << errno;
// 记录数据库错误 / Log database error
LOG_ERROR_S << "数据库查询失败: " << sql << ", 错误信息: " <<
db.lastError();
#define LOG_ERROR_S
ERROR级别流式日志的宏 / Macro for ERROR level stream logging.
Definition thread_logger.hpp:1332

◆ info_f()

auto toolbox::logger::thread_logger_t::info_f ( ) -> thread_format_logger_t
inline

获取用于INFO级别消息的格式化日志记录器 / Get a format logger for INFO level messages

Returns
配置为INFO级别的thread_format_logger_t实例 / A thread_format_logger_t instance configured for INFO level
LOG_INFO_F("应用程序启动成功,版本: {}", version);
// 输出: 应用程序启动成功,版本: 1.0.0 / Output: Application started
successfully, version: 1.0.0
#define LOG_INFO_F
INFO级别格式化日志的宏 / Macro for INFO level format logging.
Definition thread_logger.hpp:1323

◆ info_s()

auto toolbox::logger::thread_logger_t::info_s ( ) -> thread_stream_logger_t
inline

获取用于INFO级别消息的流式日志记录器 / Get a stream logger for INFO level messages

Returns
配置为INFO级别的thread_stream_logger_t实例 / A thread_stream_logger_t instance configured for INFO level
LOG_INFO_S << "应用程序启动,版本: " << version << " 配置: " << config_file;
// 输出: 应用程序启动,版本: 2.0.0 配置: config.json / Output: Application
started with version 2.0.0 config: config.json

◆ instance()

static auto toolbox::logger::thread_logger_t::instance ( ) -> thread_logger_t &
static

获取日志记录器的单例实例 / Get the singleton instance of the logger

使用动态分配并确保实例在程序退出前一直存在,以减轻静态析构顺序问题。初始化是线程安全的。/ Uses dynamic allocation and ensures the instance persists until program exit to mitigate issues with static destruction order. Thread-safe initialization.

Returns
对线程安全日志记录器实例的引用 / Reference to the thread-safe logger instance

◆ level()

auto toolbox::logger::thread_logger_t::level ( ) -> Level
inline

获取当前日志级别 / Get the current logging level

Returns
当前日志级别作为Level枚举值 / The current logging level as a Level enum value
if (logger.level() <= thread_logger_t::Level::DEBUG) {
// 执行调试特定操作 / Perform debug-specific operations
}

◆ level_str()

auto toolbox::logger::thread_logger_t::level_str ( ) -> std::string
inline

获取当前日志级别的字符串表示 / Get the current logging level as a string

Returns
当前日志级别的人类可读字符串 / The current logging level as a human-readable string
std::cout << "Current log level: " << logger.level_str() << std::endl;

◆ set_level()

auto toolbox::logger::thread_logger_t::set_level ( Level  level) -> void
inline

设置日志级别 / Set the logging level

Parameters
level要设置的新日志级别 / The new logging level to set

◆ shutdown()

static void toolbox::logger::thread_logger_t::shutdown ( )
static

显式关闭日志记录器实例的工作线程 / Explicitly shuts down the logger instance's worker thread

停止后台工作线程。在程序终止前应调用一次以确保干净关闭,特别是在测试框架等复杂场景中。/ Stops the background worker thread. Should be called once before program termination to ensure clean shutdown, especially in complex scenarios like test frameworks.

int main() {
// 使用日志记录器 / Use logger
LOG_INFO_S << "程序启动";
// 程序结束前关闭日志 / Shutdown logger before program ends
return 0;
}
static void shutdown()
显式关闭日志记录器实例的工作线程 / Explicitly shuts down the logger instance's worker thread

◆ trace_f()

auto toolbox::logger::thread_logger_t::trace_f ( ) -> thread_format_logger_t
inline

获取用于TRACE级别消息的格式化日志记录器 / Get a format logger for TRACE level messages

Returns
配置为TRACE级别的thread_format_logger_t实例 / A thread_format_logger_t instance configured for TRACE level
LOG_TRACE_F("正在处理项目 {}", item_id);
// 输出: 正在处理项目 123 / Output: Processing item 123
#define LOG_TRACE_F
TRACE级别格式化日志的宏 / Macro for TRACE level format logging.
Definition thread_logger.hpp:1321

◆ trace_s()

auto toolbox::logger::thread_logger_t::trace_s ( ) -> thread_stream_logger_t
inline

获取用于TRACE级别消息的流式日志记录器 / Get a stream logger for TRACE level messages

Returns
配置为TRACE级别的thread_stream_logger_t实例 / A thread_stream_logger_t instance configured for TRACE level
LOG_TRACE_S << "进入函数 " << __func__ << " 参数: " << param;
// 输出: 进入函数 process_data 参数: 42 / Output: Entering function
process_data param: 42
#define LOG_TRACE_S
TRACE级别流式日志的宏 / Macro for TRACE level stream logging.
Definition thread_logger.hpp:1328

◆ warn_f()

auto toolbox::logger::thread_logger_t::warn_f ( ) -> thread_format_logger_t
inline

获取用于WARN级别消息的格式化日志记录器 / Get a format logger for WARN level messages

Returns
配置为WARN级别的thread_format_logger_t实例 / A thread_format_logger_t instance configured for WARN level
LOG_WARN_F("磁盘空间不足: 剩余{}MB", free_space);
// 输出: 磁盘空间不足: 剩余100MB / Output: Disk space is low: 100MB
remaining
#define LOG_WARN_F
WARN级别格式化日志的宏 / Macro for WARN level format logging.
Definition thread_logger.hpp:1324

◆ warn_s()

auto toolbox::logger::thread_logger_t::warn_s ( ) -> thread_stream_logger_t
inline

获取用于WARN级别消息的流式日志记录器 / Get a stream logger for WARN level messages

Returns
配置为WARN级别的thread_stream_logger_t实例 / A thread_stream_logger_t instance configured for WARN level
LOG_WARN_S << "意外的值: " << value << " (预期: " << expected << ")";
// 输出: 意外的值: 404 (预期: 200) / Output: Unexpected value: 404
(expected: 200)
#define LOG_WARN_S
WARN级别流式日志的宏 / Macro for WARN level stream logging.
Definition thread_logger.hpp:1331

The documentation for this class was generated from the following file: