std::clog,std::wclog(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 of std::ios_base)
  • cerr β€” writes to the standard C error stream stderr, unbuffered (yes, unbuffered, no chill πŸ’…)
  • wcerr β€” (global object)
  • cout β€” writes to the standard C output stream stdout
  • wcout β€” (global object)
  • stdin β€” expression of type FILE* associated with the input stream
  • stdout β€” expression of type FILE* associated with the output stream
  • stderr β€” expression of type FILE* 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