News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

C++ TIps: IO - clarification on "what is clog"

Started by ben2ong2, October 06, 2006, 11:16:43 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (Furue Ryo)

I'm looking for a document for clog. I know it's an object of the class
i/ostream like cin, cout, and cerr, but I don't know its purpose and
usage. Could someone refer me to some document (preferably one from some
ftp site)?   Or, if it's easy to explain, I'd appreciate your posting
some explanation.


RESPONSE: You are not allowed to view links. Register or Login (Robert Martin), 23 Oct 95

clog and cerr both output to 'standard error'.  cerr is buffered, clog
is not.   Buffering is an efficiency issue.  It is much more efficient
to use the buffered form (cerr).  However, buffering means that the
output won't be written right away.  Thus, on a crash or other
abnormal exit, your error messages might be stuck in the buffer
instead of being written out.  clog allows the errors to be written
out right away.


RESPONSE: You are not allowed to view links. Register or Login (Steve Clamage), 23 Oct 95

Almost right. 'cerr' is unit-buffered, 'clog' is fully buffered. Unit-
buffered means that each individual output operation is  sent to the output
stream at its conclusion. Unbuffered means that each individual character
is immediately sent to the output stream. Unit-buffering is a good efficiency
compromise. Example:

   clog << "The answer is " << x;
or
   cerr << "The answer is " << x;
or
   unbuffered_stream << "The answer is " << x;

Suppose that 'x' should display as '1234', and that for some reason the
program aborts after inserting the '12' but before inserting the '34'.
clog would display nothing
cerr would display:                   The answer is
unbuffered_stream would display:      The answer is 12


RESPONSE: You are not allowed to view links. Register or Login (Allan Clarke), 25 Oct 95

Above are the sequences of discussion between you and Robert Martin
recently concerning clog. I understand Robert's explaination well
enough.

I understand your explaination of unit-buffering. What I don't understand
is that clog is fully buffered. How does this help in getting output
written right away (ie, as an aid in debugging)? In particular, it
seems that your example shows that for debugging output, it is better
to use cerr than clog (ie, you would at least see "The answer is").


RESPONSE: You are not allowed to view links. Register or Login (Steve Clamage), 25 Oct 95

I should have been more explicit that Robert got it backwards.

cerr is (almost) unbuffered, clog is fully buffered. If you need
to see output right away, you should use cerr. If you are
accumulating data to the standard error file and don't want
the overhead of frequent flushes, use clog.

You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login