I\'ve a working logger class, which outputs some text into a richtextbox (Win32, C++). Problem is, i always end up using it like this:
stringstream ss;
ss
An elegant solution that also solves the flushing issues is the following:
#include
#include
#include
#include
class Logger
{
using Stream = std::ostringstream;
using Buffer_p = std::unique_ptr>;
public:
void log(const std::string& cmd) {
std::cout << "INFO: " << cmd << std::endl;
}
Buffer_p log() {
return Buffer_p(new Stream, [&](Stream* st) {
log(st->str());
});
}
};
#define LOG(instance) *(instance.log())
int main()
{
Logger logger;
LOG(logger) << "e.g. Log a number: " << 3;
return 0;
}