将std :: clog在源文件中移动到输出文件 [英] Moving std::clog in source to output file

查看:612
本文介绍了将std :: clog在源文件中移动到输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中有一个基本的调试消息,打印一个消息,告诉我们调用什么函数。

I have a basic debug message in my code that prints a message as to what function is called.

#ifdef _DEBUG
     std::clog << "message etc" << std::endl;
#endif

如何重定向输出以将消息发送到文本文件?

How do I redirect the output to send the message to a textfile?

推荐答案

可以设置与使用文件的 clog

You can set the buffer associated with clog that uses a file to save its data to.

这是一个简单的程序,演示了这个概念。

Here's a simple program that demonstrates the concept.

#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;
}

这篇关于将std :: clog在源文件中移动到输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆