<分区>
我的代码中有一个基本的调试消息,它打印一条关于调用什么函数的消息。
#ifdef _DEBUG
std::clog << "message etc" << std::endl;
#endif
如何重定向输出以将消息发送到文本文件?
<分区>
我的代码中有一个基本的调试消息,它打印一条关于调用什么函数的消息。
#ifdef _DEBUG
std::clog << "message etc" << std::endl;
#endif
如何重定向输出以将消息发送到文本文件?
最佳答案
您可以设置与使用文件保存数据的clog
相关的缓冲区。
这是一个演示该概念的简单程序。
#include <iostream>
#include <fstream>
int main()
{
std::ofstream out("test.txt");
// Get the rdbuf of clog.
// We need it to reset the value before exiting.
auto old_rdbuf = std::clog.rdbuf();
// Set the rdbuf of clog.
std::clog.rdbuf(out.rdbuf());
// Write to clog.
// The output should go to test.txt.
std::clog << "Test, Test, Test.\n";
// Reset the rdbuf of clog.
std::clog.rdbuf(old_rdbuf);
return 0;
}
关于c++ - 将源中的 std::clog 移动到输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34618916/