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

用于格式化输出的线程安全流式日志记录器类 / Thread-safe stream logger class for formatted output More...

#include <thread_logger.hpp>

Public Member Functions

 thread_stream_logger_t (thread_logger_t &logger, Level level, toolbox::base::object_pool_t< std::stringstream > &pool)
 使用池构造新的线程流日志记录器对象 / Construct a new thread stream logger object using the pool
 
 ~thread_stream_logger_t ()
 销毁线程流日志记录器对象 / Destroy the thread stream logger object
 
 thread_stream_logger_t (const thread_stream_logger_t &)=delete
 
thread_stream_logger_toperator= (const thread_stream_logger_t &)=delete
 
 thread_stream_logger_t (thread_stream_logger_t &&)=delete
 
thread_stream_logger_toperator= (thread_stream_logger_t &&)=delete
 
auto str () -> std::string
 将记录的消息作为字符串获取 / Get the logged message as a string
 
template<typename T >
auto operator<< (const T &container) -> std::enable_if_t< is_container_v< T > &&!has_stream_operator_v< T >, thread_stream_logger_t & >
 重载容器的operator<< / Overload operator<< for containers
 
template<typename... Args>
auto operator<< (const std::tuple< Args... > &t) -> thread_stream_logger_t &
 重载元组的operator<< / Overload operator<< for tuples
 
auto red (const std::string &text) -> thread_stream_logger_t &
 以红色格式化文本 / Format text in red color
 
auto green (const std::string &text) -> thread_stream_logger_t &
 以绿色格式化文本 / Format text in green color
 
auto yellow (const std::string &text) -> thread_stream_logger_t &
 以黄色格式化文本 / Format text in yellow color
 
auto bold (const std::string &text) -> thread_stream_logger_t &
 以粗体格式化文本 / Format text in bold style
 
auto operator<< (const char *value) -> thread_stream_logger_t &
 重载C风格字符串的operator<< / Overload operator<< for C-style strings
 
template<typename T >
auto operator<< (const T &value) -> std::enable_if_t< has_stream_operator_v< T >, thread_stream_logger_t & >
 重载具有ostream operator<<的类型的operator<< / Overload operator<< for types that have an operator<< with an ostream
 
template<typename T >
auto operator<< (T &&value) -> std::enable_if_t<!has_stream_operator_v< T > &&has_ostream_method_v< T >, thread_stream_logger_t & >
 重载具有成员operator<<的类型的operator<< / Overload operator<< for types with member operator<<
 
auto operator<< (thread_stream_logger_t &logger) -> thread_stream_logger_t &
 重载用于组合日志记录器的operator<< / Overload operator<< for combining loggers
 
template<typename K , typename V >
auto operator<< (const std::map< K, V > &map) -> thread_stream_logger_t &
 重载std::map容器的operator<< / Overload operator<< for std::map containers
 
template<typename K , typename V >
auto operator<< (const std::unordered_map< K, V > &map) -> thread_stream_logger_t &
 重载std::unordered_map容器的operator<< / Overload operator<< for std::unordered_map containers
 

Detailed Description

用于格式化输出的线程安全流式日志记录器类 / Thread-safe stream logger class for formatted output

thread_stream_logger_t类提供了一个使用流运算符记录消息的线程安全机制。它支持各种数据类型,包括容器、元组和C风格字符串。日志记录器自动处理消息格式化并确保日志操作期间的线程安全。/ The thread_stream_logger_t class provides a thread-safe mechanism for logging messages using stream operators. It supports various data types including containers, tuples, and C-style strings. The logger automatically handles message formatting and ensures thread safety during logging operations.

Note
此类通常通过thread_logger_t接口及其相关宏使用。不建议直接实例化。/ This class is typically used through the thread_logger_t interface and its associated macros. Direct instantiation is not recommended.
See also
thread_logger_t
// 使用流式日志记录器的基本用法 / Basic usage with stream logger
LOG_INFO_S << "Processing " << items.size() << " items";
// 记录容器 / Logging containers
std::vector<int> values = {1, 2, 3};
LOG_DEBUG_S << "Values: " << values; // 输出: Values: [1, 2, 3] / Output:
Values: [1, 2, 3]
static auto instance() -> thread_logger_t &
获取日志记录器的单例实例 / Get the singleton instance of the logger
#define LOG_DEBUG_S
DEBUG级别流式日志的宏 / Macro for DEBUG level stream logging.
Definition thread_logger.hpp:1329
#define LOG_INFO_S
INFO级别流式日志的宏 / Macro for INFO level stream logging.
Definition thread_logger.hpp:1330

Constructor & Destructor Documentation

◆ thread_stream_logger_t() [1/3]

toolbox::logger::thread_logger_t::thread_stream_logger_t::thread_stream_logger_t ( thread_logger_t logger,
Level  level,
toolbox::base::object_pool_t< std::stringstream > &  pool 
)

使用池构造新的线程流日志记录器对象 / Construct a new thread stream logger object using the pool

Parameters
logger对父线程日志记录器的引用 / Reference to the parent thread logger
level此流日志记录器的日志级别 / Logging level for this stream logger
pool对stringstream对象池的引用 / Reference to the stringstream object pool

◆ ~thread_stream_logger_t()

toolbox::logger::thread_logger_t::thread_stream_logger_t::~thread_stream_logger_t ( )

销毁线程流日志记录器对象 / Destroy the thread stream logger object

将最终消息入队。ss_ptr_析构函数自动将stringstream返回到池中。/ Enqueues the final message. The stringstream is automatically returned to the pool by the ss_ptr_ destructor.

◆ thread_stream_logger_t() [2/3]

toolbox::logger::thread_logger_t::thread_stream_logger_t::thread_stream_logger_t ( const thread_stream_logger_t )
delete

◆ thread_stream_logger_t() [3/3]

toolbox::logger::thread_logger_t::thread_stream_logger_t::thread_stream_logger_t ( thread_stream_logger_t &&  )
delete

Member Function Documentation

◆ bold()

auto toolbox::logger::thread_logger_t::thread_stream_logger_t::bold ( const std::string &  text) -> thread_stream_logger_t &

以粗体格式化文本 / Format text in bold style

Parameters
text要格式化的文本 / The text to format
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining

◆ green()

auto toolbox::logger::thread_logger_t::thread_stream_logger_t::green ( const std::string &  text) -> thread_stream_logger_t &

以绿色格式化文本 / Format text in green color

Parameters
text要格式化的文本 / The text to format
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining

◆ operator<<() [1/8]

auto toolbox::logger::thread_logger_t::thread_stream_logger_t::operator<< ( const char *  value) -> thread_stream_logger_t&
inline

重载C风格字符串的operator<< / Overload operator<< for C-style strings

此运算符允许记录C风格字符串(const char*)。字符串直接写入日志流。/ This operator allows logging of C-style strings (const char*). The string is directly written to the log stream.

Parameters
value要记录的C风格字符串 / The C-style string to log
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining
LOG_INFO_S << "C-style string: " << "Hello World";

◆ operator<<() [2/8]

template<typename K , typename V >
auto toolbox::logger::thread_logger_t::thread_stream_logger_t::operator<< ( const std::map< K, V > &  map) -> thread_stream_logger_t&
inline

重载std::map容器的operator<< / Overload operator<< for std::map containers

此运算符为std::map容器提供格式化输出,以可读格式显示键值对。输出类似于Python的字典表示。/ This operator provides formatted output for std::map containers, displaying key-value pairs in a readable format. The output is similar to Python's dictionary representation.

Template Parameters
Kmap的键类型 / The key type of the map
Vmap的值类型 / The value type of the map
Parameters
map要打印到日志流的map / The map to print to the log stream
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining
std::map<std::string, int> scores = {{"张三", 95}, {"李四", 88}};
LOG_DEBUG_S << "考试成绩: " << scores;
// 输出: 考试成绩: {张三: 95, 李四: 88} / Output: Exam scores: {张三: 95,
李四: 88}
std::map<int, std::string> ids = {{1001, "项目A"}, {1002, "项目B"}};
LOG_INFO_S << "项目列表: " << ids;
// 输出: 项目列表: {1001: 项目A, 1002: 项目B} / Output: Project list:
{1001: Project A, 1002: Project B}

◆ operator<<() [3/8]

template<typename... Args>
auto toolbox::logger::thread_logger_t::thread_stream_logger_t::operator<< ( const std::tuple< Args... > &  t) -> thread_stream_logger_t&
inline

重载元组的operator<< / Overload operator<< for tuples

此运算符允许记录std::tuple对象。元组元素以逗号分隔的列表形式打印,用圆括号括起来。/ This operator allows logging of std::tuple objects. The tuple elements are printed in a comma-separated list enclosed in parentheses.

Template Parameters
Args元组元素的类型 / Types of tuple elements
Parameters
t要记录的元组 / The tuple to log
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining
auto t = std::make_tuple(1, "test", 3.14);
LOG_DEBUG_S << "Tuple: " << t; // 输出: Tuple: (1, test, 3.14) / Output:
Tuple: (1, test, 3.14)

◆ operator<<() [4/8]

template<typename K , typename V >
auto toolbox::logger::thread_logger_t::thread_stream_logger_t::operator<< ( const std::unordered_map< K, V > &  map) -> thread_stream_logger_t&
inline

重载std::unordered_map容器的operator<< / Overload operator<< for std::unordered_map containers

此运算符为std::unordered_map容器提供格式化输出。输出格式类似于std::map,但由于容器的无序性质,元素顺序可能会变化。/ This operator provides formatted output for std::unordered_map containers. The output format is similar to std::map, but the order of elements may vary due to the unordered nature of the container.

Template Parameters
Kmap的键类型 / The key type of the map
Vmap的值类型 / The value type of the map
Parameters
map要打印到日志流的map / The map to print to the log stream
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining
std::unordered_map<std::string, double> prices = {{"苹果", 5.5},
{"香蕉", 3.8}}; LOG_INFO_S << "水果价格: " << prices;
// 输出: 水果价格: {苹果: 5.5, 香蕉: 3.8} (顺序可能变化) / Output: Fruit
prices: {苹果: 5.5, 香蕉: 3.8} (order may vary)
std::unordered_map<int, std::string> status = {{200, "成功"}, {404,
"未找到"}}; LOG_DEBUG_S << "状态码: " << status;
// 输出: 状态码: {200: 成功, 404: 未找到} (顺序可能变化) / Output: Status
codes: {200: Success, 404: Not Found} (order may vary)

◆ operator<<() [5/8]

template<typename T >
auto toolbox::logger::thread_logger_t::thread_stream_logger_t::operator<< ( const T &  container) -> std::enable_if_t<is_container_v<T> && !has_stream_operator_v<T>, thread_stream_logger_t&>
inline

重载容器的operator<< / Overload operator<< for containers

此运算符允许记录没有自己的流运算符的容器类型(如向量、列表)。容器元素以逗号分隔的列表形式打印,用方括号括起来。/ This operator allows logging of container types (e.g., vectors, lists) that don't have their own stream operator. The container elements are printed in a comma-separated list enclosed in square brackets.

Template Parameters
T容器类型(必须满足is_container_v) / Container type (must satisfy is_container_v)
Parameters
container要记录的容器 / The container to log
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining
std::vector<int> vec = {1, 2, 3};
LOG_INFO_S << "Vector: " << vec; // 输出: Vector: [1, 2, 3] / Output:
Vector: [1, 2, 3]

◆ operator<<() [6/8]

template<typename T >
auto toolbox::logger::thread_logger_t::thread_stream_logger_t::operator<< ( const T &  value) -> std::enable_if_t<has_stream_operator_v<T>, thread_stream_logger_t&>
inline

重载具有ostream operator<<的类型的operator<< / Overload operator<< for types that have an operator<< with an ostream

此运算符允许记录任何支持标准流插入运算符(operator<<)的类型。它在执行操作前检查日志级别,只有当当前级别足够时才记录。/ This operator allows logging of any type that supports the standard stream insertion operator (operator<<). It checks the logging level before performing the operation and only logs if the current level is sufficient.

Template Parameters
T要记录的值的类型,必须定义了operator<< / The type of value to log, must have operator<< defined
Parameters
value要打印到日志流的值 / The value to print to the log stream
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining
// 记录基本类型 / Logging basic types
LOG_INFO_S << 42 << " " << 3.14 << " " << true;
// 输出: 42 3.14 true / Output: 42 3.14 true
// 记录自定义类型 / Logging custom types
struct Point { int x, y; };
std::ostream& operator<<(std::ostream& os, const Point& p) {
return os << "(" << p.x << "," << p.y << ")";
}
LOG_DEBUG_S << Point{1, 2};
// 输出: (1,2) / Output: (1,2)
auto operator<<(const T &container) -> std::enable_if_t< is_container_v< T > &&!has_stream_operator_v< T >, thread_stream_logger_t & >
重载容器的operator<< / Overload operator<< for containers
Definition thread_logger.hpp:530

◆ operator<<() [7/8]

template<typename T >
auto toolbox::logger::thread_logger_t::thread_stream_logger_t::operator<< ( T &&  value) -> std::enable_if_t<!has_stream_operator_v<T> && has_ostream_method_v<T>, thread_stream_logger_t&>
inline

重载具有成员operator<<的类型的operator<< / Overload operator<< for types with member operator<<

此运算符处理将operator<<实现为成员函数而不是自由函数的类型。对于不能修改全局命名空间来添加流运算符的类型特别有用。/ This operator handles types that implement operator<< as a member function rather than a free function. It's particularly useful for types that can't modify the global namespace to add stream operators.

Template Parameters
T要记录的值的类型,必须有成员operator<< / The type of value to log, must have member operator<<
Parameters
value要打印到日志流的值 / The value to print to the log stream
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining
struct CustomLogger {
std::ostream& operator<<(std::ostream& os) const {
return os << "Custom logger output";
}
};
LOG_INFO_S << CustomLogger();
// 输出: Custom logger output / Output: Custom logger output

◆ operator<<() [8/8]

auto toolbox::logger::thread_logger_t::thread_stream_logger_t::operator<< ( thread_stream_logger_t logger) -> thread_stream_logger_t&
inline

重载用于组合日志记录器的operator<< / Overload operator<< for combining loggers

此运算符允许将一个日志记录器的内容组合到另一个中。在保持相同日志级别的同时从多个来源构建复杂日志消息时很有用。/ This operator allows combining the contents of one logger into another. It's useful for building complex log messages from multiple sources while maintaining the same logging level.

Parameters
logger要追加其内容的其他日志记录器 / The other logger whose contents to append
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining
auto& logger1 = LOG_INFO_S << "状态: " << "正常";
auto& logger2 = LOG_DEBUG_S << " 详细信息: " << "所有系统运行中";
logger1 << logger2; // 仅在INFO级别启用时记录 / Only logs if INFO level
is enabled
// 输出: 状态: 正常 详细信息: 所有系统运行中 / Output: Status: Normal
Details: All systems running

◆ operator=() [1/2]

thread_stream_logger_t & toolbox::logger::thread_logger_t::thread_stream_logger_t::operator= ( const thread_stream_logger_t )
delete

◆ operator=() [2/2]

thread_stream_logger_t & toolbox::logger::thread_logger_t::thread_stream_logger_t::operator= ( thread_stream_logger_t &&  )
delete

◆ red()

auto toolbox::logger::thread_logger_t::thread_stream_logger_t::red ( const std::string &  text) -> thread_stream_logger_t &

以红色格式化文本 / Format text in red color

Parameters
text要格式化的文本 / The text to format
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining

◆ str()

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

将记录的消息作为字符串获取 / Get the logged message as a string

Returns
记录的消息作为字符串 / The logged message as a string

◆ yellow()

auto toolbox::logger::thread_logger_t::thread_stream_logger_t::yellow ( const std::string &  text) -> thread_stream_logger_t &

以黄色格式化文本 / Format text in yellow color

Parameters
text要格式化的文本 / The text to format
Returns
对此日志记录器的引用,用于链式调用 / Reference to this logger for chaining

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