Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ stream as a member variable

I've got a C++ class which I would like to hold a stream used for logging.

The stream should be able to be set (and possibly reset) after the construction of the object.

It should be possible to set the stream as std::cout, or as a file stream to log to a file, or as a stringstream which does nothing more than ignore the data (a /dev/null of sorts). In any case, it should be an ostream type object, which the creator of the object can reset at any time. The class itself is oblivious to the concrete stream type.

I could accomplish this with a pointer to an ostream, but then the syntax becomes a little annoying, having to use the deref operator:

(*m_log) << "message";

rather than

m_log << "message";

But I can't use references, as the stream object needs to be possibly reset after the object has been initialized.

Is there an elegant way to achieve this, i.e., avoid using pointers, but still be able to reset after construction?

like image 315
Madeleine P. Vincent Avatar asked Dec 09 '11 13:12

Madeleine P. Vincent


2 Answers

You can reset streams: see it live on https://ideone.com/Ci4eo

#include <fstream>
#include <iostream>
#include <string>

struct Logger
{
    Logger(std::ostream& os) : m_log(os.rdbuf()) { }

    std::streambuf* reset(std::ostream& os) 
    {
        return m_log.rdbuf(os.rdbuf());
    }

    template <typename T> friend Logger& operator<<(Logger& os, const T& t)
    { os.m_log << t; return os; }

    friend Logger& operator<<(Logger& os, std::ostream& ( *pf )(std::ostream&))
    { os.m_log << pf; return os; }

  private:
    std::ostream m_log;
};

int main(int argc, const char *argv[])
{
    Logger logto(std::cout);

    logto << "Hello world" << std::endl;

    logto.reset(std::cerr);
    logto << "Error world" << std::endl;

    return 0;
}
like image 142
sehe Avatar answered Nov 09 '22 07:11

sehe


Why trouble yourself?

class foo{
public:
  // ..
private:
  std::ostream& log() const{ return *m_log; }
  mutable std::ostream* m_log;
};

And just use log() << "blah\n"; instead.

like image 41
Xeo Avatar answered Nov 09 '22 06:11

Xeo