Question

I need to output messages to both console and log files. After googling, i learnt "teebuf" concept which basically create a custom class inherited from basic_streambuf. This work fine but how to clean up the redirected buffer properly. I mean how to implement RAII for "tee" buffer so i don't need to bother it every time when i need to quit the program.

Remark: currently using boost library is not a option for me.

Code Segment

int main() {
    std::streambuf* cout_sbuf = std::cout.rdbuf();
    std::ofstream   fout(fileOutDebug);
    teeoutbuf       teeout(std::cout.rdbuf(), fout.rdbuf());
    std::cout.rdbuf(&teeout);

    // some code ...
    if (failed) {
        std::cout.rdbuf(cout_sbuf); // <-- i once comment this and it gives error
        return -1;
    }

    // some more code ...
    std::cout.rdbuf(cout_sbuf); // <-- i once comment this and it gives error
    return 0;
}

Code Segment (My trial implementation, but fail)

template < typename CharT, typename Traits = std::char_traits<CharT>
> class basic_tostream : std::basic_ostream<CharT, Traits>
{
    public:
        basic_tostream(std::basic_ostream<CharT, Traits> & o1,
                       std::basic_ostream<CharT, Traits> & o2)
    : std::basic_ostream<CharT, Traits>(&tbuf), 
      tbuf(o1.rdbuf(), o2.rdbuf()) {}
        void print(char* msg);
    private:
    basic_teebuf<CharT, Traits> tbuf; // internal buffer (tee-version)
};
typedef basic_tostream<char> tostream;

int main() {
    std::ofstream fout(fileOutDebug);
    tostream tee(std::cout, fout, verbose);
    tee << "test 1\n"; // <-- compile error
    tee.print("sometext"); // <-- comment above and it run fine, both file and console written
}

Error message: 'std::basic_ostream' is an inaccessible base of 'basic_tostream'

Was it helpful?

Solution

You should use:

template < typename CharT, typename Traits = std::char_traits<CharT> >
class basic_tostream : public std::basic_ostream<CharT, Traits>

instead of:

template < typename CharT, typename Traits = std::char_traits<CharT> >
class basic_tostream : std::basic_ostream<CharT, Traits>

Key difference being public. That's what the 'std::basic_ostream' is an inaccessible base of 'basic_tostream' error message is about.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top