Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
Sonde::LogEventSource Class Reference

Captures Qt log output (qDebug, qInfo, qWarning, qCritical, qCDebug, ...) as events named "log". More...

#include <logeventsource.h>

Inheritance diagram for Sonde::LogEventSource:
Sonde::EventSource

Public Member Functions

 LogEventSource (QObject *parent=nullptr)
 Installs the message handler unless another instance already did.
 ~LogEventSource () override
 Restores the previous message handler (if this instance is active).
bool isActive () const
 Whether this instance installed the message handler.
Public Member Functions inherited from Sonde::EventSource
 EventSource (QString name, QString description={ }, QObject *parent=nullptr)
 Creates a source called name (used in filters and resource URIs), described by description in the events tools' descriptions.
 ~EventSource () override
 Destroys the source; the server forgets it.
QString name () const
 The source's name.
QString description () const
 Human-readable description shown in tool descriptions.
void emitEvent (LogLevel level, const QString &message, const QString &category={ }, const QJsonObject &data={ })
 Emits an event.
void eventOccurred (const Sonde::Event &event)
 An event was emitted (sequence is still 0; the server assigns it).

Static Public Member Functions

static void captureCategory (const QByteArray &category, QtMsgType lowest=QtDebugMsg)
 Turns on category, at lowest and above, for Sonde's log capture only.

Private Attributes

PrivatePtr< LogEventSourcePrivate > d
 Implementation.

Detailed Description

Captures Qt log output (qDebug, qInfo, qWarning, qCritical, qCDebug, ...) as events named "log".

It installs a Qt message handler. The previously installed handler keeps receiving every message, so normal console output is unaffected, and it is restored when the source is destroyed. If another handler was installed after this one, that handler is left in place (this one then just passes messages on). Only one instance can be active at a time; further instances are inactive (see isActive()).

Category filtering follows the normal Qt logging rules (QT_LOGGING_RULES, QLoggingCategory::setFilterRules), since disabled categories never reach the message handler. Messages are queued to the source's thread, so they appear once the event loop runs.

Member Function Documentation

◆ captureCategory()

void Sonde::LogEventSource::captureCategory ( const QByteArray & category,
QtMsgType lowest = QtDebugMsg )
static

Turns on category, at lowest and above, for Sonde's log capture only.

Its messages become events like any other, but they reach the application's previous message handler (the console, usually) only at the levels the application's own logging rules enable. So Sonde can listen to a category that is off by default, such as "qt.qml.binding.removal", without changing the application's output.

It works through a category filter (QLoggingCategory::installFilter()) that asks the previous filter first, so the application's rules (QT_LOGGING_RULES, qtlogging.ini, setFilterRules()) and its own filter stay in force. The category is only turned on while a LogEventSource is active. Process-wide; calling it again for the same category changes lowest.


The documentation for this class was generated from the following files: