Akonadi

tracer.cpp
1/***************************************************************************
2 * SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org> *
3 * *
4 * SPDX-License-Identifier: LGPL-2.0-or-later *
5 ***************************************************************************/
6#include "tracer.h"
7
8#include <QSettings>
9#include <QString>
10
11#include "traceradaptor.h"
12
13#include "akonadiserver_debug.h"
14#include "dbustracer.h"
15#include "filetracer.h"
16
17#include "private/standarddirs_p.h"
18
19// #define DEFAULT_TRACER QLatin1StringView( "dbus" )
20#define DEFAULT_TRACER QStringLiteral("null")
21
22using namespace Akonadi;
23using namespace Akonadi::Server;
24
25Tracer::Tracer()
26 : mSettings(std::make_unique<QSettings>(Akonadi::StandardDirs::serverConfigFile(), QSettings::IniFormat))
27{
28 activateTracer(currentTracer());
29
30 new TracerAdaptor(this);
31
33}
34
35Tracer::~Tracer() = default;
36
37void Tracer::beginConnection(const QString &identifier, const QString &msg)
38{
39 QMutexLocker locker(&mMutex);
40 if (mTracerBackend) {
41 mTracerBackend->beginConnection(identifier, msg);
42 }
43}
44
45void Tracer::endConnection(const QString &identifier, const QString &msg)
46{
47 QMutexLocker locker(&mMutex);
48 if (mTracerBackend) {
49 mTracerBackend->endConnection(identifier, msg);
50 }
51}
52
53void Tracer::connectionInput(const QString &identifier, const QByteArray &msg)
54{
55 QMutexLocker locker(&mMutex);
56 if (mTracerBackend) {
57 mTracerBackend->connectionInput(identifier, msg);
58 }
59}
60
61void Akonadi::Server::Tracer::connectionInput(const QString &identifier, qint64 tag, const Protocol::CommandPtr &cmd)
62{
63 QMutexLocker locker(&mMutex);
64 if (mTracerBackend) {
65 if (mTracerBackend->connectionFormat() == TracerInterface::Json) {
66 QJsonObject json;
67 json[QStringLiteral("tag")] = tag;
68 Akonadi::Protocol::toJson(cmd.data(), json);
69
70 QJsonDocument doc(json);
71
72 mTracerBackend->connectionInput(identifier, doc.toJson(QJsonDocument::Indented));
73 } else {
74 mTracerBackend->connectionInput(identifier, QByteArray::number(tag) + ' ' + Protocol::debugString(cmd).toUtf8());
75 }
76 }
77}
78
79void Tracer::connectionOutput(const QString &identifier, const QByteArray &msg)
80{
81 QMutexLocker locker(&mMutex);
82 if (mTracerBackend) {
83 mTracerBackend->connectionOutput(identifier, msg);
84 }
85}
86
87void Tracer::connectionOutput(const QString &identifier, qint64 tag, const Protocol::CommandPtr &cmd)
88{
89 QMutexLocker locker(&mMutex);
90 if (mTracerBackend) {
91 if (mTracerBackend->connectionFormat() == TracerInterface::Json) {
92 QJsonObject json;
93 json[QStringLiteral("tag")] = tag;
94 Protocol::toJson(cmd.data(), json);
95 QJsonDocument doc(json);
96
97 mTracerBackend->connectionOutput(identifier, doc.toJson(QJsonDocument::Indented));
98 } else {
99 mTracerBackend->connectionOutput(identifier, QByteArray::number(tag) + ' ' + Protocol::debugString(cmd).toUtf8());
100 }
101 }
102}
103
104void Tracer::signal(const QString &signalName, const QString &msg)
105{
106 QMutexLocker locker(&mMutex);
107 if (mTracerBackend) {
108 mTracerBackend->signal(signalName, msg);
109 }
110}
111
112void Tracer::signal(const char *signalName, const QString &msg)
113{
114 signal(QLatin1StringView(signalName), msg);
115}
116
117void Tracer::warning(const QString &componentName, const QString &msg)
118{
119 QMutexLocker locker(&mMutex);
120 if (mTracerBackend) {
121 mTracerBackend->warning(componentName, msg);
122 }
123}
124
125void Tracer::error(const QString &componentName, const QString &msg)
126{
127 QMutexLocker locker(&mMutex);
128 if (mTracerBackend) {
129 mTracerBackend->error(componentName, msg);
130 }
131}
132
133void Tracer::error(const char *componentName, const QString &msg)
134{
135 error(QLatin1StringView(componentName), msg);
136}
137
139{
140 QMutexLocker locker(&mMutex);
141 return mSettings->value(QStringLiteral("Debug/Tracer"), DEFAULT_TRACER).toString();
142}
143
145{
146 QMutexLocker locker(&mMutex);
147
148 if (type == QLatin1StringView("file")) {
149 const QString file = mSettings->value(QStringLiteral("Debug/File"), QStringLiteral("/dev/null")).toString();
150 mTracerBackend = std::make_unique<FileTracer>(file);
151 } else if (type == QLatin1StringView("dbus")) {
152 mTracerBackend = std::make_unique<DBusTracer>();
153 } else if (type == QLatin1StringView("null")) {
154 mTracerBackend.reset();
155 } else {
156 qCCritical(AKONADISERVER_LOG) << "Unknown tracer type" << type;
157 mTracerBackend.reset();
158 return;
159 }
160
161 mSettings->setValue(QStringLiteral("Debug/Tracer"), type);
162 mSettings->sync();
163}
164
165#include "moc_tracer.cpp"
void activateTracer(const QString &type)
Activates the given tracer type.
Definition tracer.cpp:144
void connectionInput(const QString &identifier, const QByteArray &msg) override
This method is called whenever the akonadi server retrieves some data from the outside.
Definition tracer.cpp:53
void signal(const QString &signalName, const QString &msg) override
This method is called whenever a dbus signal is emitted on the bus.
Definition tracer.cpp:104
void beginConnection(const QString &identifier, const QString &msg) override
This method is called whenever a new data (imap) connection to the akonadi server is established.
Definition tracer.cpp:37
void endConnection(const QString &identifier, const QString &msg) override
This method is called whenever a data (imap) connection to akonadi server is closed.
Definition tracer.cpp:45
void warning(const QString &componentName, const QString &msg) override
This method is called whenever a component wants to output a warning.
Definition tracer.cpp:117
void error(const QString &componentName, const QString &msg) override
This method is called whenever a component wants to output an error.
Definition tracer.cpp:125
QString currentTracer() const
Returns the currently activated tracer type.
Definition tracer.cpp:138
~Tracer() override
Destroys the global tracer instance.
Helper integration between Akonadi and Qt.
QByteArray number(double n, char format, int precision)
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
QDBusConnection sessionBus()
T * data() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 19 2024 11:58:37 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.