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 "dbustracer.h"
14 #include "filetracer.h"
15 #include "akonadiserver_debug.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 
105 void Tracer::signal(const QString &signalName, const QString &msg)
106 {
107  QMutexLocker locker(&mMutex);
108  if (mTracerBackend) {
109  mTracerBackend->signal(signalName, msg);
110  }
111 }
112 
113 void Tracer::signal(const char *signalName, const QString &msg)
114 {
115  signal(QLatin1String(signalName), msg);
116 }
117 
118 void Tracer::warning(const QString &componentName, const QString &msg)
119 {
120  QMutexLocker locker(&mMutex);
121  if (mTracerBackend) {
122  mTracerBackend->warning(componentName, msg);
123  }
124 }
125 
126 void Tracer::error(const QString &componentName, const QString &msg)
127 {
128  QMutexLocker locker(&mMutex);
129  if (mTracerBackend) {
130  mTracerBackend->error(componentName, msg);
131  }
132 }
133 
134 void Tracer::error(const char *componentName, const QString &msg)
135 {
136  error(QLatin1String(componentName), msg);
137 }
138 
139 QString Tracer::currentTracer() const
140 {
141  QMutexLocker locker(&mMutex);
142  return mSettings->value(QStringLiteral("Debug/Tracer"), DEFAULT_TRACER).toString();
143 }
144 
145 void Tracer::activateTracer(const QString &type)
146 {
147  QMutexLocker locker(&mMutex);
148 
149  if (type == QLatin1String("file")) {
150  const QString file = mSettings->value(QStringLiteral("Debug/File"), QStringLiteral("/dev/null")).toString();
151  mTracerBackend = std::make_unique<FileTracer>(file);
152  } else if (type == QLatin1String("dbus")) {
153  mTracerBackend = std::make_unique<DBusTracer>();
154  } else if (type == QLatin1String("null")) {
155  mTracerBackend.reset();
156  } else {
157  qCCritical(AKONADISERVER_LOG) << "Unknown tracer type" << type;
158  mTracerBackend.reset();
159  return;
160  }
161 
162  mSettings->setValue(QStringLiteral("Debug/Tracer"), type);
163  mSettings->sync();
164 }
bool registerObject(const QString &path, QObject *object, QDBusConnection::RegisterOptions options)
QDBusConnection sessionBus()
A glue between Qt and the standard library.
T * data() const const
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
QByteArray number(int n, int base)
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:15:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.