如何重新定义cerr和clog两个tee到共享日志文件? [英] How to redefine both cerr and clog to both tee to a shared log file?

查看:179
本文介绍了如何重新定义cerr和clog两个tee到共享日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的一个相关问题显示了如何使用clog:

A related question here shows how to do this with just clog:

如何重新定义clog到三通到原始clog和日志文件?

现在的问题是如何同时为cerr这样做。有了上面的问题,cerr的输出不会出现在需要它的日志文件中。

The question now is how to also do this for cerr at the same time. With the above question, output to cerr does not end up in the log file where it is also needed.

目标是无论clog还是cerr结束在日志文件中一次,因此clog和cerr都需要转到共享日志文件。

The goal is that whatever goes to either clog or cerr ends up in the log file once, so both clog and cerr need to be teed to a shared log file.

推荐答案

std :: cout和std :: cerr到输出文件:

this code will redirect both std::cout and std::cerr to an output file :

// create an output stream
std::ofstream trace_log ( "/tmp/foo.log" );

// connect stream buffers
std::streambuf *coutbuf = std::cout.rdbuf();
std::cout.rdbuf(trace_log.rdbuf () );

std::streambuf *cerrbuf = std::cerr.rdbuf();
std::cerr.rdbuf(trace_log.rdbuf () );

// log 
std::cout << "cout here" << std::endl;
std::cerr << "cerr here" << std::endl;

// restore
std::cout.flush ();
std::cout.rdbuf(cerrbuf);

std::cerr.flush ();
std::cerr.rdbuf(cerrbuf);

这篇关于如何重新定义cerr和clog两个tee到共享日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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