Clone wiki

dbg / custom_log_sink

Setting up a custom log sink

By default dbg will write all output, for tracebacks etc, to stderr.

If a different log target is required, a new dbg::logsink implementation can be created and installed with dbg::set_logsink(), as shown below:

#include <dbg/log.hpp>

struct my_logsink : dbg::logsink
    virtual void write(const char *text, std::size_t n)
        // write n chars from text to the desired target
        // ...

int main()
    my_logsink sink;
    dbg::logsink &old_sink = dbg::set_logsink(sink);

    // ...

The sink object you use should exist for the entire duration of the application, or until it is replaced with another sink.

In multithreaded applications, you must ensure that the write() method is written in such a way as to be callable simultaneously from multiple threads.