Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::clog: the standard logging stream

std::clog is the global narrow output stream associated with C stderr, without a required unitbuf setting.

Version: C++23

Syntax

namespace std {
    extern ostream clog;
}

std::clog from <iostream> is a global std::ostream that writes to a stream buffer associated with C stderr. It suits diagnostic messages that may be buffered; unlike std::cerr, the standard does not set unitbuf for it or tie it to std::cout.

Insertions use the normal stream-formatting rules, and manipulators and flags persist for later writes. When a log record must arrive before a crash or control transfer, call flush; a newline alone does not guarantee flushing of the C++ stream.

Device errors set failbit or badbit, so check important writes. Concurrent messages can interleave characters, requiring synchronization around complete records. Changing rdbuf() or sync_with_stdio(false) affects process-global stream state; the example restores the original buffer.

Examples

#include <iostream>
#include <sstream>

int main() {
    std::ostringstream sink;
    auto* original = std::clog.rdbuf(sink.rdbuf());
    std::clog << "severity=" << 3;
    std::clog.rdbuf(original);
    std::cout << sink.str() << '\n';
}

Result

severity=3

std::clog → severity=3

Sources