Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I write a logger class with cout style interface (logger << "Error: " << val << endl;)

I want to create a logger class such that with a functionality like this:

Logger log;
log << "Error: " << value << "seen" << endl;

This should print me a custom formatted message. E.g. "12-09-2009 11:22:33 Error 5 seen"

My simple class currently looks like this:

class Logger {
    private:
        ostringstream oss;
    public:
        template <typename T>
        Logger& operator<<(T a);
}

template <typename T>
Logger& Logger::operator<<(T a) {
    oss << a;
    return *this;
}

void functionTest(void) {
    Logger log;
    log << "Error: " << 5 << " seen";
}

This will cause oss to correctly have the buffer "Error: 5 seen". But I dont know what other function I need to write/modify so that something prints on the screen. Does anyone know how to get this to work or is there another way to design this class to have my functionality work?

like image 212
Dheeraj Agrawal Avatar asked Feb 10 '10 09:02

Dheeraj Agrawal


1 Answers

Behind every std::ostream is a streambuf. It cab be retrieved and set via std::stream::rdbuf(). In particular, it can be wrapped - you can provide a streambuf object that post-processes the streamed text. (post-processing means you can't distinguish std::cout << 123; from std::cout << "123"; )

In your particular case, the postprocessing is fairly simple. At the start of every line you want to insert some bytes. This merely means that you should keep track of whether you've already output the prefix for the current line. If not, do so and set the flag. And whenever you see a newline, reset it. Your streambuf wrapper has just a single bool worth of state.

like image 194
MSalters Avatar answered Oct 20 '22 14:10

MSalters