class mola::LogHub

Overview

Process-wide fan-out of the log messages of all MOLA modules to any number of consumers (“sinks”), e.g. the GUI console window or ROS 2 /rosout.

Every hooked mrpt::system::COutputLogger gets exactly one forwarding callback, which only refers to this (never destroyed) singleton, so loggers may outlive any consumer, and consumers may come and go at any time.

Usage from a consumer module:

  • add_sink() once, keeping the returned handle alive while the sink is wanted. Releasing it unregisters the sink.

  • attach_modules() periodically (e.g. every few seconds from spinOnce()), to hook modules (and their ExecutableBase::child_loggers()) created later, and to apply the current sink levels.

Sink functions are called synchronously from whichever thread logs, so they must be fast and must not block (e.g. just push into a queue). They may be invoked once more right after their handle is released, so they must only capture state they co-own (e.g. a shared_ptr). Messages logged from within a sink function are not forwarded (no recursion), and exceptions thrown by a sink are ignored.

The callback verbosity threshold of each hooked logger is set to the lowest level among the live sinks, but never above the logger’s own console level, so callbacks registered by modules themselves keep receiving at least what the logger prints. Hence, a module must not set that threshold itself (mrpt::system::COutputLogger::setVerbosityLevelForCallbacks()) while hooked.

Thread-safe hooking of loggers that are concurrently logging requires MRPT >= 3.1.0.

#include <LogHub.h>

class LogHub
{
public:
    // typedefs

    typedef std::function<void(const LogRecord&)> SinkFunction;

    // classes

    class Sink;

    // methods

    static LogHub& Instance();
    std::shared_ptr<Sink> add_sink(mrpt::system::VerbosityLevel minLevel, SinkFunction f);
    void attach_modules(ExecutableBase& self);
    void attach_logger(mrpt::system::COutputLogger& logger, const std::string& source);
};

Methods

static LogHub& Instance()

The process-wide instance. It is never destroyed.

std::shared_ptr<Sink> add_sink(mrpt::system::VerbosityLevel minLevel, SinkFunction f)

Registers a new sink, receiving messages with level >= minLevel. Messages logged before this call are not delivered.

void attach_modules(ExecutableBase& self)

Hooks self and all modules reachable from it via its name server, plus their ExecutableBase::child_loggers(), and updates their callback verbosity thresholds. Cheap for already-hooked loggers.

void attach_logger(
    mrpt::system::COutputLogger& logger,
    const std::string& source
    )

Hooks a single logger, with source as the LogRecord::source of its messages, and updates its callback verbosity threshold. Hooking the same logger again is a no-op, apart from updating the threshold.