QTracer Class

The QTracer class provides an interface for handling trace events associated with a logging category. More...

Header: #include <QTracer>
qmake: QT += core
Since: Qt 5.2

Public Functions

QTracer()
virtual ~QTracer()
void addToCategory(QLoggingCategory & category)
virtual void end()
virtual void record(int data)
virtual void record(const char * data)
virtual void record(const QVariant & data)
virtual void start()

Detailed Description

The QTracer class provides an interface for handling trace events associated with a logging category.

QTracer objects are registered with logging categories. Multiple QTracer objects can be registered with the same category, and the same QTracer object can be registered with different categories.

If code containing qCTrace is executed, and the associated logging category is enabled for tracing, all QTracer objects that are registered with the category are notified.

QTracer objects

Member Function Documentation

QTracer::QTracer()

Constructs a tracer object.

Example:

class MyTracerBase : public QTracer
{
public:
    MyTracerBase() {
        enable = ::open(traceSwitch, O_WRONLY);
        marker = ::open(traceSink, O_WRONLY);
    }

    ~MyTracerBase() {
        ::close(enable);
        ::close(marker);
    }

protected:
    int enable;
    int marker;
};

QTracer::~QTracer() [virtual]

Destroys the tracer object.

void QTracer::addToCategory(QLoggingCategory & category)

Registers this tracer for the category.

The tracer will later be notified of messages of type QtTraceMsg, as long as that message type is enabled in the category.

Example:

QLoggingCategory theFooArea("foo");
QLoggingCategory theBarArea("bar");
QLoggingCategory theBazArea("baz");

namespace {
static struct Init
{
    Init() {
        tracer.addToCategory(theFooArea);
        tracer.addToCategory(theBarArea);
        logger.addToCategory(theBazArea);
    }

    MyTracer tracer;
    MyDataLogger logger;

} initializer;
}

void QTracer::end() [virtual]

This function is invoked when a tracing activity ends, typically from the destructor of a QTraceGuard object defined by qCTrace() or qCTraceGuard().

The base implementation does nothing. It is common for QTracer subclasses to override it to perform flushing of collected data.

See also qCTrace() and qCTraceGuard().

void QTracer::record(int data) [virtual]

This function is invoked during a tracing activity to pass integer data to the QTracer object.

Example:

class MyDataLogger : public MyTracerBase
{
public:
    MyDataLogger() {
        buf[0] = 0;
        pos = 0;
    }

    void record(int i) { pos += sprintf(buf + pos, "%d", i); }
    void record(const char *msg) { pos += sprintf(buf + pos, "%s", msg); }
    void end() { ::write(marker, buf, pos); pos = 0; }

private:
    char buf[100];
    int pos;
};

void QTracer::record(const char * data) [virtual]

This function is invoked during a tracing activity to pass string data to the QTracer object.

void QTracer::record(const QVariant & data) [virtual]

This function is invoked during a tracing activity to pass abitrary (non-integer, non-string) data to the QTracer object.

void QTracer::start() [virtual]

This function is invoked when a tracing activity starts, typically from the constructor of a QTraceGuard object defined by qCTrace() or qCTraceGuard().

The base implementation does nothing. QTracer subclasses are advised to override it if needed.

See also qCTrace() and qCTraceGuard().