我有一个基于ostream的子类,它捕获我的程序的调试消息。如何在应用程序崩溃时刷新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缓冲区刷新为输出文件?
你是对的。 'endl'输出一个换行符并刷新流。 – chris