I know that there is no concept of threads in current C++, but this article is saying:
A typesafe, threadsafe, portable
logging mechanism…..
The
fprintf()function is threadsafe,
so even if this log is used from
different threads, the output lines
won’t be scrambled.
What about cout, cerr and clog?
I think this question is applicable to all kind of stream types in C++ also, like fstream and stringstream.
The article makes a claim about the POSIX standard for the
fprintfAPI. It says nothing about C++ streams. And this is quite correct, as there are no such guarantees on those stream.Note that although the logging class in that article uses C++ stream syntax, it does this via a
std::ostringstreamobject that is created and destroyed for every logging event, and so is not shared between threads. It usesfprintfto actually write the content to the console.The Microsoft C library makes some claims to be POSIX compliant, and so the code in the article probably is quite widely portable (as many other popular operating systems are POSIX compliant). But this doesn’t mean that standard C++ streams are thread-safe.