0

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

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Narue
0

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 );
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.