我有一个基于 ostream 的子类,它捕获我的程序的调试消息。
/** @brief Customized output stream with "tee" feature */
template <typename CharT, typename Traits = std::char_traits<CharT> >
class basic_tostream : public std::basic_ostream<CharT, Traits> {
public:
basic_tostream(std::basic_ostream<CharT, Traits> & o1, /**< main ostream */
std::basic_ostream<CharT, Traits> & o2 /**< teed ostream */)
: std::basic_ostream<CharT, Traits>(&tbuf), tbuf(o1.rdbuf(), o2.rdbuf())
{ /* empty */ }
private:
tee_outbuf<CharT, Traits> tbuf;
}; // end_class: basic_tostream
我如何使用这个类:
std::ofstream debugFile("debug.txt")
tostream tout(std::cout, debugFile);
/* computation */
tout << "message\n";
/* other computation */
问题:当应用程序正常退出时,该类工作正常。但是在崩溃的情况下(例如数组索引超出范围等),'tout' 确实将所有消息打印到控制台,但'debugFile' 不会捕获所有打印输出。
问题:那么,当应用程序崩溃时,如何正确地将 ostream 缓冲区刷新到输出文件?