我想写日志记录功能应该这样使用:记录功能<<
log(__FILE__) << "My message containing integer: " << 123 << " and double: " << 1.2;
这应该打印以下行,添加ENDL立即冲洗:
main.cpp中:我的消息包含整数:123和双:1.2
我的(简化的)尝试为函数的实现:
class Writer
{
public:
template<typename T>
Writer & operator<<(T t)
{
cout << t << endl;
cout.flush();
return (*this);
}
};
Writer log(const char* fileName)
{
cout << fileName << ": ";
return Writer();
}
int main(int argc, const char *argv[])
{
log(__FILE__) << "My message containing integer: " << 123 << "and double: " << 1.2;
return 0;
}
我的问题是,由于操作者的LR关联的< <输出为:含有整数我的消息::
的main.cpp
和双:
1.2
有没有办法如何实现该功能,或者是我的使用不可实现的要求?
理想情况下,我想使用纯C++ 03(即没有C++ 11功能,boost和非标准库)。
只是要清楚:什么时候需要刷新什么,什么应该在哪一行?例如:在你的第一个例子中,“double ...”部分在哪里? –
@BaummitAugen对不起,编辑,修复。 – bedrorom