What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?
stdout
and stderr
are different streams, even though they both refer to console output by default. Redirecting (piping) one of them (e.g. program.exe >out.txt
) would not affect the other.
Generally, stdout
should be used for actual program output, while all information and error messages should be printed to stderr
, so that if the user redirects output to a file, information messages are still printed on the screen and not to the output file.
The question regarding cerr cout and clog
Buffered output is typically much faster than unbuffered. So if you wanted to write a vast amount of data quickly to a log (but didn't care if it actually ended up there), you would use clog rather than cerr.
And all streams can normally be redirected, assuming a vaguely competent operating system, but this is outwith the C++ standard, which has no such concept as "redirection".
How are iostream objects cin, cout, cerr, and clog implemented?
There's no compiler magic.
IIRC, the standard implementation is to define a global constant object in the header. In each translation unit this header is included, one such object is created. Its constructor increments a counter, its destructor decrements it. When incrementing from 0 to 1, the the console stream objects are created, when decrementing from 1 to 0, they are destroyed.
What does the c mean in cout, cin, cerr and clog?
The "c" stands for "character" because iostreams map values to and from byte (char) representations. [Bjarne Stroustrup's C++ Style and Technique FAQ]
Are standard output streams in C++ thread-safe (cout, cerr, clog)?
The article makes a claim about the POSIX standard for the fprintf
API. 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::ostringstream
object that is created and destroyed for every logging event, and so is not shared between threads. It uses fprintf
to 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.
Is `clog` buffered?
It's not a bug, clog
is not supposed to be buffered, in fact the standard does not associate clog
with cout
in any way nor does it state tha clog
should be buffered.
It does state that cerr
and clog
are both associated with stderr
, so if there was to be some similarity in the behavior it should be between clog
and cerr
, which seems to be what you are experiencing.
As stderr
is defined to be unbuffered it would be logic that clog
is also unbuffered, but as this behavior isn't specified, it is not guaranteed to be always the case.
cout
is associated with stdout
, and its buffering settings are deppendent on the environment. Usually, for UNIX systems, it's line buffered.
29.4.3 Narrow stream objects [narrow.stream.objects]
ostream cout;
3 - The object
cout
controls output to a stream buffer associated with the objectstdout
, declared in<cstdio>
(29.12.1).
ostream cerr;
4 - The object cerr controls output to a stream buffer associated with the object
stderr
, declared in<cstdio>
(29.12.1).[...]
ostream clog;
6 - The object
clog
controls output to a stream buffer associated with the objectstderr
, declared in<cstdio>
(29.12.1).
Outputting to a stdout and a file with cout, cerr, clog, or even user defined ostreams
Your requirement change the normal behavior of cout, which makes the codes confusing.
Why not use another class to do this.Like this :
#include <fstream>
#include <iostream>
class GeneralOStream
{
public:
GeneralOStream(std::ostream& stdStream, std::ostream& fileStream)
:_stdStream(stdStream)
, _fileStream(fileStream)
{
}
template<typename T>
friend GeneralOStream& operator << (GeneralOStream& stream, const T& t);
private:
std::ostream& _stdStream;
std::ostream& _fileStream;
};
template<typename T>
GeneralOStream& operator << (GeneralOStream& stream, const T& t)
{
stream._stdStream << t;
stream._fileStream << t;
return stream;
}
int main()
{
std::ofstream file("out.txt");
GeneralOStream gos(std::cout, file);
std::cout << "Hellow world!" << std::endl; //This outputs to console and file
}
Related Topics
Why Do String Literals (Char*) in C++ Have to Be Constants
_Iterator_Debug_Level Value '0' Doesn't Match Value '2'
Cast Vector<T> to Vector<Const T>
What Is "Strip" (Gcc Application) Used For
How to Compile for Windows Xp with Visual Studio 2012
Why Are Forward Declarations Necessary
Is There a Limit of Stack Size of a Process in Linux
Convert a Unicode String in C++ to Upper Case
How to Extract the Mantissa of a Double
Tcp/Ip Connection on a Specific Interface
How to Check If Window Is "Always on Top"
Why Does My Program Hang When Opening a Mkfifo-Ed Pipe
Hooking Directx Endscene from an Injected Dll
Embedded C++:To Use Stl or Not
How to Generate Random Numbers in C++
Dijkstra Shortest Path with Vertexlist = Lists in Boost Graph