Akonadi

tracer.cpp
1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2006 Tobias Koenig <[email protected]> *
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 QLatin1String( "dbus" )
20 #define DEFAULT_TRACER QStringLiteral("null")
21 
22 using namespace Akonadi;
23 using namespace Akonadi::Server;
24 
25 Tracer::Tracer()
26  : mSettings(std::make_unique<QSettings>(Akonadi::StandardDirs::serverConfigFile(), QSettings::IniFormat))
27 {
28  activateTracer(currentTracer());
29 
30  new TracerAdaptor(this);
31 
33 }
34 
35 Tracer::~Tracer() = default;
36 
37 void Tracer::beginConnection(const QString &identifier, const QString &msg)
38 {
39  QMutexLocker locker(&mMutex);
40  if (mTracerBackend) {
41  mTracerBackend->beginConnection(identifier, msg);
42  }
43 }
44 
45 void Tracer::endConnection(const QString &identifier, const QString &msg)
46 {
47  QMutexLocker locker(&mMutex);
48  if (mTracerBackend) {
49  mTracerBackend->endConnection(identifier, msg);
50  }
51 }
52 
53 void Tracer::connectionInput(const QString &identifier, const QByteArray &msg)
54 {
55  QMutexLocker locker(&mMutex);
56  if (mTracerBackend) {
57  mTracerBackend->connectionInput(identifier, msg);
58  }
59 }
60 
61 void 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 
79 void Tracer::connectionOutput(const QString &identifier, const QByteArray &msg)
80 {
81  QMutexLocker locker(&mMutex);
82  if (mTracerBackend) {
83  mTracerBackend->connectionOutput(identifier, msg);
84  }
85 }
86 
87 void 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 
104 void Tracer::signal(const QString &signalName, const QString &msg)
105 {
106  QMutexLocker locker(&mMutex);
107  if (mTracerBackend) {
108  mTracerBackend->signal(signalName, msg);
109  }
110 }
111 
112 void Tracer::signal(const char *signalName, const QString &msg)
113 {
114  signal(QLatin1String(signalName), msg);
115 }
116 
117 void Tracer::warning(const QString &componentName, const QString &msg)
118 {
119  QMutexLocker locker(&mMutex);
120  if (mTracerBackend) {
121  mTracerBackend->warning(componentName, msg);
122  }
123 }
124 
125 void Tracer::error(const QString &componentName, const QString &msg)
126 {
127  QMutexLocker locker(&mMutex);
128  if (mTracerBackend) {
129  mTracerBackend->error(componentName, msg);
130  }
131 }
132 
133 void Tracer::error(const char *componentName, const QString &msg)
134 {
135  error(QLatin1String(componentName), msg);
136 }
137 
138 QString Tracer::currentTracer() const
139 {
140  QMutexLocker locker(&mMutex);
141  return mSettings->value(QStringLiteral("Debug/Tracer"), DEFAULT_TRACER).toString();
142 }
143 
144 void Tracer::activateTracer(const QString &type)
145 {
146  QMutexLocker locker(&mMutex);
147 
148  if (type == QLatin1String("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 == QLatin1String("dbus")) {
152  mTracerBackend = std::make_unique<DBusTracer>();
153  } else if (type == QLatin1String("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 }
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
T * data() const const
QByteArray number(int n, int base)
bool registerObject(const QString &path, QObject *object, QDBusConnection::RegisterOptions options)
QDBusConnection sessionBus()
A glue between Qt and the standard library.
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.