std::clog,std::wclog(3)
π std::clog, std::wclog π
okay bestie, so we're in C++ Standard Library territory today and honestly this one is lowkey a sleeper hit. welcome to the <iostream> girls πΈ
β¨ Synopsis β¨
first things first, where do these icons live? they're defined in header <iostream> π
extern std::ostream clog; (1)
extern std::wostream wclog; (2)
so what are they actually doing? the global objects std::clog and std::wclog control output to a stream buffer of implementation-defined type (derived from std::streambuf), and that buffer is associated with the standard C output stream stderr. like, it's giving error-stream energy π«Ά
but here's the plot twist β unlike std::cerr/std::wcerr, these streams are not automatically flushed, and cout is not automatically tie()'d with these streams. no auto-flush, no tie-ing, no cap. it's giving "i'll get there when i get there" π
these objects are guaranteed to be initialized during or before the first time an object of type std::ios_base::Init is constructed, and they're available for use in the constructors and destructors of static objects with ordered initialization β as long as <iostream> is included before the object is defined. so yes, you can use them in static ctor/dtor land, slay π
and one more thing for the thread girlies: unless std::ios_base::sync_with_stdio(false) has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted output. multi-thread safe, periodt π₯
π Notes π
the 'c' in the name refers to "character" (stroustrup.com FAQ). so clog means "character log" and wclog means "wide character log". that's it, that's the lore π
π¦ Example π¦
run this code, bestie, i promise it's cute:
#include <iostream>
struct Foo
{
int n;
Foo()
{
std::clog << "static constructor\n";
}
~Foo()
{
std::clog << "static destructor\n";
}
};
Foo f; // static object
int main()
{
std::clog << "main function\n";
}
β Output β
static constructor
main function
static destructor
look at that ordering π static ctor first, then main, then static dtor β the way it just works. iconic.
π See also π
Initβ initializes standard stream objects (public member class ofstd::ios_base)cerrβ writes to the standard C error streamstderr, unbuffered (yes, unbuffered, no chill π )wcerrβ (global object)coutβ writes to the standard C output streamstdoutwcoutβ (global object)stdinβ expression of typeFILE*associated with the input streamstdoutβ expression of typeFILE*associated with the output streamstderrβ expression of typeFILE*associated with the error output stream (macro constant)
okay that's the whole page, bestie, you ate that π this was brought to you by http://cppreference.com, version 2024.06.10, and the manual page for std::clog,std::wclog(3) β go forth and log your little heart out, mwah ππ§β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms