
Pre-defined stream
Controls output to a stream buffer associated with the object stderr declared in <cstdio>.
None
#include <iostream> extern ostream clog; ostream clog;
The object clog controls output to a stream buffer associated with the object stderr declared in <cstdio>. The difference between clog and cerr is that clog is buffered but cerr is not. Therefore, commands like clog << "ERROR !!"; and fprintf(stderr,"ERROR !!"); are not synchronized.
The formatting is done through member functions or manipulators. See cout or basic_ostream for details.
//
// clog example
//
#include<iostream>
#include<fstream>
void main ( )
{
using namespace std;
// open the file "file_name.txt"
// for reading
ifstream in("file_name.txt");
// output the all file to stdout
if ( in )
cout << in.rdbuf();
else
// if the ifstream object is in a bad state
// output an error message to stderr
clog << "Error while opening the file" << endl;
}
clog can be used to redirect some of the errors to another recipient. For example, you might want to redirect them to a file named my_err:
ofstream out("my_err");
if ( out )
clog.rdbuf(out.rdbuf());
else
cerr << "Error while opening the file" << endl;
Then when you are doing something like clog << "error number x"; the error message is output to the file my_err. Obviously, you can use the same scheme to redirect clog to other devices.
If your compiler does not support namespaces, then you do not need the using declaration for std.
basic_ostream(3C++), iostream(3C++), basic_filebuf(3C++), cout(3C++), cin(3C++), cerr(3C++), wcin(3C++), wcout(3C++), wcerr(3C++), wclog(3C++), iomanip(3C++), ios_base(3C++), basic_ios(3C++),
Working Paper for Draft Proposed International Standard for Information Systems--Programming Language C++, Section 27.3.1
ANSI X3J16/ISO WG21 Joint C++ Committee