gpt4 book ai didi

C++ 运算符 << 重载

转载 作者:行者123 更新时间:2023-11-30 04:05:03 24 4
gpt4 key购买 nike

我有一个重载 << 运算符的第 3 方(日志记录)类。使用此记录器类的客户端代码可以通过调用其中一个预定义的宏来使用它。例如:

//logs can be filtered based on this module id string
LOGGER_INFO("MODULE_ID_STR") << "Logging at info level";

我想扩展此功能,其中使用此第 3 方记录器的类/模块不必每次都包含模块 ID 字符串。含义 - 客户端代码应该设置一次模块 ID 字符串,然后才能执行此操作:

cLogger.INFO << "Logging at info level"; 

上面的调用应该在内部使用之前注册的注册模块 id 字符串,然后使用它来进行实际的第 3 方日志调用。因此,通过为每个日志级别重载 << 运算符,可以在 C++ 中优雅地完成此操作。

一些额外的细节......我开始这样做:

这是扩展第 3 方记录器功能的类:

class LoggerEx
{
public:
LoggerEx(const std::string &moduleToLog)
{
m_ModuleID = moduleToLog;
};
virtual ~LoggerEx() {};

class Debug
{
//overload the << operator (how to write this..??)
LOGGER_INFO(m_ModuleID) << "Logging at info level";
};

class Info
{
//overload the << operator
};
//Note that there could be more such levels
// (INFO, WARN, ERROR, TRACE, FATAL, etc).

public:
Debug DEBUG;
Info INFO;

protected:
std::string m_ModuleID

};

一些使用记录器类的客户端代码应该被允许这样做......

class Xyz
{
public:
Xyz() : l("Xyz")
{}
void doSomething()
{
l.DEBUG << "Doing something";
}
protected:
Logger l;
};

另一个客户端类...

class Mno
{
public:
Xyz() : l("Mno")
{}

void processSomething()
{
l.INFO << "Process something";
}
protected:
Logger l;
};

由于原始记录器支持多种数据类型(int、float、chars、std::string),上面的方法是否可行,或者是否有任何其他想法/解决方案可以在 C++ 中更优雅地完成此操作而无需编写完整的将包装器(或复制代码)吹到记录器?

谢谢...

最佳答案

这实际上比人们想象的要难,主要是因为在典型的日志库中,LOGGER_INFO宏或它的等价物不仅仅是给你一个流。这是来自 Boost 的典型宏:

#define BOOST_LOG_STREAM_WITH_PARAMS_INTERNAL(logger, rec_var, params_seq)\
for (::boost::log::record rec_var = (logger).open_record((BOOST_PP_SEQ_ENUM(params_seq))); !!rec_var;)\
::boost::log::aux::make_record_pump((logger), rec_var).stream()

快速浏览一下这段代码就会发现它创建了一个新的 record , 创建一个 pump , 得到 stream从这个泵,和你的<< "log text here" << " more log stuff"调用实际上在该流上运行。当 pump 和 record 被破坏时,在语句的末尾,消息实际上被推送到一个日志条目中,当你想到它时这是有道理的 - 你会期望 LOGGER_INFO(m_ModuleID) << "Logging at info level" << "more text";生成一个日志条目而不是两个。

这样一个天真的实现就像

class LoggerEx
{
public:
LoggerEx(const std::string &moduleToLog) : Debug(moduleToLog)
{ }
~LoggerEx() {}

class Debug
{
private:
std::string m_ModuleID;
public:
Debug(const std::string &module) : m_ModuleID(module) {}
template <typename T>
const Debug & operator << (const T& thing_to_log) const {
LOGGER_INFO(m_ModuleID) << thing_to_log;
return *this;
}
};

public:
Debug DEBUG;
};

仅当您只使用 << 时才有效在您的日志记录代码中每个语句一次。

绕过它的一种可能方法是使用内部流来存储正在制作的日志条目:

class LoggerEx
{
public:
LoggerEx(const std::string &moduleToLog) : m_module(moduleToLog)
{ }
~LoggerEx() {}

class Debug
{
private:
std::string m_ModuleID;
std::stringstream m_ss;
public:
Debug(const std::string &module) : m_ModuleID(module) {}
Debug(const Debug &other) : m_ModuleID(other.m_ModuleID) {}
~Debug() {
std::string str = m_ss.str();
if(!str.empty())
LOGGER_INFO(m_ModuleID) << str;
}
template <typename T>
Debug & operator << (const T& thing_to_log) {
m_ss << thing_to_log;
return *this;
}
};

public:
Debug DEBUG() { return Debug(m_module);}
private:
std::string m_module;
};

它会被称为

l.DEBUG() << "Some stuff " << some_number << " some more stuff";

想法是 DEBUG()调用产生一个临时对象;你的operator <<调用该临时对象将内容写入 stringstream ,在行尾,当临时对象被破坏时,stringstream 中的东西被推送到日志库。

关于C++ 运算符 << 重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23481554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com