Re: Thread save access to cout?
Your thoughts about locking the output during parameter evaluation is
true and may be an issue. Somehow the whole chain should be streamed
to a local stringstream first, and then written to the console at
once.
Exactly.
The std::endl could possibly used as an end-of-uninterruptable
output (but not a single '\n').
No. This might never come, again resulting in a deadlock. Furthermore
std:endl is a constant identical to '\n'. You can't distinguish them.
However, you could do some thread-static line buffering and forward the
entire message not before you get '\n'. This also avoids the problem
with the evaluation of function within synchronized context.
But you may still get mince when the message body contains '\n', and you
get no output at all until everything is terminated by '\n'.
Hmm, I have t rethink that problem carefully ....
The problem is that the existing project is big and a rework of every
single output statement is a lot of work. Otherwise, the convenience
of doing these chained outputs should still be possible.
Use an appropriate logging library.
However, you will never be able to deal automatically with statement
sequences like:
cout << "Message head";
for (whatever)
{ cout << "some item info";
}
cout << std::endl;
Furthermore you might think about trace levels. Note that populating the
stringstream with the required information might be much more expensive
than the output itself. So taking back the trace level by discarding
some messages might not give the intended performance boost. You hav to
check the trace level /before/ you start to evaluate the message body.
This implies that the evaluation of the message must not have any side
effects, otherwise the application will behave differently depending on
the trace level.
Marcel