I tried to do this to redirect the clog output to a file:

ofstream ofs("file.log");
	clog.rdbuf(ofs.rdbuf());
	clog << "Logged." << endl;
	ofs.close();

It worked (the file contains the text), but the program segfaults. I read that you have to "restore" the original buffer, so I tried this

ofstream ofs("file.log");
	clog.rdbuf(ofs.rdbuf());
	clog << "Logged." << endl;
	ofs.close();
clog.rdbuf(clog.rdbuf());

But no change. Anyone know how to do this?

Thanks,
Dave

clog.rdbuf(clog.rdbuf()); doesn't do anything meaningful. You've already replaced the stream buffer, so the original one is gone unless you save it elsewhere:

ofstream ofs("file.log");

// Save the original stream buffer
streambuf *clog_save = clog.rdbuf();

clog.rdbuf(ofs.rdbuf());
clog << "Logged." << endl;
ofs.close();

// Restore the stream buffer
clog.rdbuf ( clog_save );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.