What is the point of clog?
Is it possible to redirect clog, cerr, cout, stdin, stdout, and/or stderr?
Yes. You want the rdbuf
function.
ofstream ofs("logfile");
cout.rdbuf(ofs.rdbuf());
cout << "Goes to file." << endl;
Is the only difference between clog and cerr the buffering?
As far as I know, yes.
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".
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).
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]
How to color the output stream of std::cout, but not of std::cerr and std::clog?
After a few days of tries I found a pretty suitable solution for this problem. I simply created a functor able to apply changes directly to the std::ostream
object, to be used in this way:
functor( std::cout ) << "Modified output stream";
Such an implementation si a bit long and can be found here.
Is it necessary to reset rdbuf of cout, cerr, and clog if they have been changed to be redirected to a file?
Citing from The C++ Standard Library - A tutorial and reference 2nd edition by Nicolai Josuttis,
Ch. 15.12.13, Redirecting Standard Streams pp. 822
...
std::cout.rdbuf (file.rdbuf());
Caution! The object
file
is local and is destroyed at the end of the block. This also destroys the corresponding stream buffer. This differs from the “normal” streams because file streams allocate their stream buffer objects at construction time and destroy them on destruction. Thus, in this example,cout
can no longer be used for writing. In fact, it cannot even be destroyed safely at program termination. Thus, the old buffer should always be saved and restored later!
So the answer seems to be Yes, and even though I don't have a standard quote, the author above is quite an expert.
Why doesn't my change to clog stick?
I assume you want to create a stack variable of IerrLog. You need to change
IerrLog someLog ();
to
IerrLog someLog;
Your original statement will be interpreted by the compiler as a declaration of function someLog() which takes no arguments and returns an IerrLog.
You should also create your file as a member variable and not on the stack.
Related Topics
How to Use Sendinput Function C++
When Is It Safe to Call This-> in Constructor and Destructor
Check If One String Is a Prefix of Another
Platform-Independent Guid Generation in C++
How to Convert Rgb -> Yuv -> Rgb (Both Ways)
Static_Assert on Initializer_List::Size()
C Preprocessor MACro Specialisation Based on an Argument
Debug and Release Library Linking with Cmake (Visual Studio)
Using Emit VS Calling a Signal as If It's a Regular Function in Qt
How to Fix: /Usr/Lib/Libstdc++.So.6: Version 'Glibcxx_3.4.15' Not Found
C++ Object Created with New, Destroyed with Free(); How Bad Is This