我正在尝试实现我自己的qDebug()
风格的调试-输出流,这基本上就是我到目前为止所拥有的:
struct debug
{
#if defined(DEBUG)
template<typename T>
std::ostream& operator<<(T const& a) const
{
std::cout << a;
return std::cout;
}
#else
template<typename T>
debug const& operator<<(T const&) const
{
return *this;
}
/* must handle manipulators (endl) separately:
* manipulators are functions that take a stream& as argument and return a
* stream&
*/
debug const& operator<<(std::ostream& (*manip)(std::ostream&)) const
{
// do nothing with the manipulator
return *this;
}
#endif
};
典型用法:
debug() << "stuff" << "more stuff" << std::endl;
但我希望不必添加std::endl;
我的问题基本上是,我如何判断operator<<
的返回类型何时不会被另一个operator<<
使用(并附加endl
)?
要实现这样的操作,我能想到的唯一方法是创建一个与qDebug()
创建的每个临时对象相关联的打印内容列表,然后打印所有内容,以及~debug()
中的尾随换行符(我可以做一些聪明的事情,比如插入空格),但显然这并不理想,因为我不能保证临时对象在作用域结束之前会被销毁(或者我可以吗?)。
发布于 2010-02-02 05:00:58
Qt使用类似于@Evan的方法。有关实现的详细信息,请参阅a version of qdebug.h,但它们将所有内容流式传输到底层文本流,然后在销毁由qDebug()返回的临时QDebug对象时刷新流和结束行。
发布于 2010-02-02 04:22:38
下面这样的代码就可以了:
struct debug {
debug() {
}
~debug() {
std::cerr << m_SS.str() << std::endl;
}
public:
// accepts just about anything
template<class T>
debug &operator<<(const T &x) {
m_SS << x;
return *this;
}
private:
std::ostringstream m_SS;
};
这应该会让你做这样的事情:
debug() << "hello world";
我使用了这样的模式结合锁来提供类似流的日志记录系统,它可以保证日志条目是自动写入的。
注释:未经测试的代码,但应该可以工作:-)
发布于 2010-02-02 04:17:16
当你这样写的时候,这是典型的用法:
debug() << "stuff" << "more stuff" << std::endl;
您是否确实计划在每次使用调试对象时都构造一个调试对象?如果是这样,您应该能够通过让debug析构函数添加新行来获得您想要的行为:
~debug()
{
*this << std::endl;
... the rest of your destructor ...
}
这确实意味着你不能做这样的事情:
// this won't output "line1" and "line2" on separate lines
debug d;
d << "line1";
d << "line2";
https://stackoverflow.com/questions/2179623
复制相似问题