Working with std::cerr and std::clog
Separate diagnostics and logs from program output using cerr and clog streams.
Working with std::cerr and std::clog is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Three Output Streams
The standard library exposes three predefined output streams:
- std::cout — buffered standard output
- std::cerr — unbuffered standard error
- std::clog — buffered standard error
std::cerr: Errors and Warnings
std::cerr writes to stderr and is unbuffered — output appears immediately, even if the program crashes next.
std::cerr << "Error: file not found\n";std::clog: Buffered Logs
std::clog also writes to stderr but is buffered like cout. Use it for routine logs where slight latency is fine.
std::clog << "[info] processing record 42\n";Why Two Streams to stderr?
Errors need to appear immediately so the user sees them before a crash. Logs are high volume — buffering them keeps performance acceptable.
Redirecting Output
On the command line, stdout and stderr go to separate channels. Redirect them independently.
# Send stdout to a file, stderr to the terminal
./app > out.txt
# Send stderr to a file too
./app 2> err.txt
# Send both to one file
./app > all.txt 2>&1Diagnostic vs Data Output
By convention, machine-readable data goes to stdout; human diagnostics go to stderr. This lets pipelines work cleanly.
Tying Streams Together
cin is tied to cout by default — reading from cin flushes cout first. cerr is similarly tied to cout.
Custom Logging with Levels
Wrap clog in helper functions for log levels — INFO, WARN, ERROR. This keeps log messages consistent.
void warn(const std::string& msg) {
std::clog << "[WARN] " << msg << "\n";
}
void error(const std::string& msg) {
std::cerr << "[ERROR] " << msg << "\n";
}Performance: cerr Is Slow
Because cerr flushes on every operation, repeated writes are slow. For heavy logging prefer clog or a dedicated logging library.
Flushing clog Manually
If you want clog output to appear immediately for debugging, flush it manually.
std::clog << "checkpoint" << std::flush;Writing to a File Stream
The same patterns work with file streams. Open a log file with std::ofstream and stream into it like clog.
std::ofstream logFile("app.log");
logFile << "Application started\n";Quick Check
Which standard stream is automatically flushed after every write?
Recap
Use std::cout for normal output, std::cerr for urgent unbuffered errors, and std::clog for buffered logs. Redirect them independently on the shell with > and 2>.
Frequently asked questions
Is the “Working with std::cerr and std::clog” lesson free?
Yes — the full text of “Working with std::cerr and std::clog” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “Working with std::cerr and std::clog”?
Separate diagnostics and logs from program output using cerr and clog streams. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Working with std::cerr and std::clog” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Using std::cin and std::cout Properly
- Formatting Output with iomanip
- Reading Input Safely with getline
- Working with std::cerr and std::clog