正在将源代码中的std::clog移动到输出文件
Moving std::clog in source to output file
我的代码中有一个基本的调试消息,它打印一条关于调用什么函数的消息。
#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;
}
如何重定向输出以将消息发送到文本文件?
就重定向的含义而言,它实际上在一定程度上取决于您的shell语法。根据这个参考,std::clog
通常与std::cerr
:结合
全局对象
std::clog
和std::wclog
控制输出到实现定义类型的流缓冲区(从std::streambuf
派生),该缓冲区与标准C输出流stderr相关联,但与std::cerr/std::wcerr
不同,这些流不会自动刷新,也不会自动与cout绑定()。
例如,在bash中,你会做一些类似的事情
$ program 2> Logs.txt
关于以编程方式重定向,您可以按照R Sahu的回答中提到的或在当前标记的副本中解释的方式进行重定向。
相关文章:
- 将对象移动到std::shared_ptr
- 何时在引用或唯一指针上使用移动语义
- 如何从具有移动语义的类对象中生成共享指针
- 将shared_ptr移动到<StructA>shared_ptr<变体<结构A、结构 B>>
- C / C++ 移位/偏移/向左或向右移动位图?
- MSVC将仅移动结构参数解释为指针
- 自定义先决条件对移动分配运算符有效吗
- 返回值优化:显式移动还是隐式
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 可以使用移动语义更改或改进此C++代码吗?
- 使lambda不可复制/不可移动
- c++在使用指针时移动语义
- 将QGraphicsItem的移动区域限制在多边形区域内
- SendInput()鼠标移动计算
- 按值 C++ 返回时进行双倍移动
- 移动二维数组中的字符
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- 安全到标准:移动会员?
- 正在将源代码中的std::clog移动到输出文件