Commit 617d41b1 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

Tracer: don't assemble tracing strings when null tracer is used.

Building the JSON/debug strings is fairly expensive, so let's avoid doing that
for every command if we are going to throw the result away immediately.
parent e79f8cbe
Pipeline #25310 passed with stage
in 25 minutes and 31 seconds
/***************************************************************************
* Copyright (C) 2006 by Tobias Koenig <tokoe@kde.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#ifndef AKONADI_NULLTRACER_H
#define AKONADI_NULLTRACER_H
#include "tracerinterface.h"
namespace Akonadi
{
namespace Server
{
/**
* A tracer which forwards all tracing information to /dev/null ;)
*/
class NullTracer : public TracerInterface
{
public:
~NullTracer() override = default;
void beginConnection(const QString &identifier, const QString &msg) override {
Q_UNUSED(identifier);
Q_UNUSED(msg);
}
void endConnection(const QString &identifier, const QString &msg) override {
Q_UNUSED(identifier);
Q_UNUSED(msg);
}
void connectionInput(const QString &identifier, const QByteArray &msg) override {
Q_UNUSED(identifier);
Q_UNUSED(msg);
}
void connectionOutput(const QString &identifier, const QByteArray &msg) override {
Q_UNUSED(identifier);
Q_UNUSED(msg);
}
void signal(const QString &signalName, const QString &msg) override {
Q_UNUSED(signalName);
Q_UNUSED(msg);
}
void warning(const QString &componentName, const QString &msg) override {
Q_UNUSED(componentName);
Q_UNUSED(msg);
}
void error(const QString &componentName, const QString &msg) override {
Q_UNUSED(componentName);
Q_UNUSED(msg);
}
};
} // namespace Server
} // namespace Akonadi
#endif
......@@ -25,7 +25,7 @@
#include "dbustracer.h"
#include "filetracer.h"
#include "nulltracer.h"
#include "akonadiserver_debug.h"
#include <private/standarddirs_p.h>
......@@ -49,71 +49,78 @@ Tracer::~Tracer() = default;
void Tracer::beginConnection(const QString &identifier, const QString &msg)
{
mMutex.lock();
mTracerBackend->beginConnection(identifier, msg);
mMutex.unlock();
QMutexLocker locker(&mMutex);
if (mTracerBackend) {
mTracerBackend->beginConnection(identifier, msg);
}
}
void Tracer::endConnection(const QString &identifier, const QString &msg)
{
mMutex.lock();
mTracerBackend->endConnection(identifier, msg);
mMutex.unlock();
QMutexLocker locker(&mMutex);
if (mTracerBackend) {
mTracerBackend->endConnection(identifier, msg);
}
}
void Tracer::connectionInput(const QString &identifier, const QByteArray &msg)
{
mMutex.lock();
mTracerBackend->connectionInput(identifier, msg);
mMutex.unlock();
QMutexLocker locker(&mMutex);
if (mTracerBackend) {
mTracerBackend->connectionInput(identifier, msg);
}
}
void Akonadi::Server::Tracer::connectionInput(const QString& identifier, qint64 tag, const Protocol::CommandPtr &cmd)
{
QByteArray msg;
if (mTracerBackend->connectionFormat() == TracerInterface::Json) {
QJsonObject json;
json[QStringLiteral("tag")] = tag;
Akonadi::Protocol::toJson(cmd.data(), json);
QJsonDocument doc(json);
msg = doc.toJson(QJsonDocument::Indented);
} else {
msg = QByteArray::number(tag) + ' ' + Protocol::debugString(cmd).toUtf8();
QMutexLocker locker(&mMutex);
if (mTracerBackend) {
if (mTracerBackend->connectionFormat() == TracerInterface::Json) {
QJsonObject json;
json[QStringLiteral("tag")] = tag;
Akonadi::Protocol::toJson(cmd.data(), json);
QJsonDocument doc(json);
mTracerBackend->connectionInput(identifier, doc.toJson(QJsonDocument::Indented));
} else {
mTracerBackend->connectionInput(identifier, QByteArray::number(tag) + ' ' + Protocol::debugString(cmd).toUtf8());
}
}
connectionInput(identifier, msg);
}
void Tracer::connectionOutput(const QString &identifier, const QByteArray &msg)
{
mMutex.lock();
mTracerBackend->connectionOutput(identifier, msg);
mMutex.unlock();
QMutexLocker locker(&mMutex);
if (mTracerBackend) {
mTracerBackend->connectionOutput(identifier, msg);
}
}
void Tracer::connectionOutput(const QString &identifier, qint64 tag, const Protocol::CommandPtr &cmd)
{
QByteArray msg;
if (mTracerBackend->connectionFormat() == TracerInterface::Json) {
QJsonObject json;
json[QStringLiteral("tag")] = tag;
Protocol::toJson(cmd.data(), json);
QJsonDocument doc(json);
msg = doc.toJson(QJsonDocument::Indented);
} else {
msg = QByteArray::number(tag) + ' ' + Protocol::debugString(cmd).toUtf8();
QMutexLocker locker(&mMutex);
if (mTracerBackend) {
if (mTracerBackend->connectionFormat() == TracerInterface::Json) {
QJsonObject json;
json[QStringLiteral("tag")] = tag;
Protocol::toJson(cmd.data(), json);
QJsonDocument doc(json);
mTracerBackend->connectionOutput(identifier, doc.toJson(QJsonDocument::Indented));
} else {
mTracerBackend->connectionOutput(identifier, QByteArray::number(tag) + ' ' + Protocol::debugString(cmd).toUtf8());
}
}
connectionOutput(identifier, msg);
}
void Tracer::signal(const QString &signalName, const QString &msg)
{
mMutex.lock();
mTracerBackend->signal(signalName, msg);
mMutex.unlock();
QMutexLocker locker(&mMutex);
if (mTracerBackend) {
mTracerBackend->signal(signalName, msg);
}
}
void Tracer::signal(const char *signalName, const QString &msg)
......@@ -123,16 +130,18 @@ void Tracer::signal(const char *signalName, const QString &msg)
void Tracer::warning(const QString &componentName, const QString &msg)
{
mMutex.lock();
mTracerBackend->warning(componentName, msg);
mMutex.unlock();
QMutexLocker locker(&mMutex);
if (mTracerBackend) {
mTracerBackend->warning(componentName, msg);
}
}
void Tracer::error(const QString &componentName, const QString &msg)
{
mMutex.lock();
mTracerBackend->error(componentName, msg);
mMutex.unlock();
QMutexLocker locker(&mMutex);
if (mTracerBackend) {
mTracerBackend->error(componentName, msg);
}
}
void Tracer::error(const char *componentName, const QString &msg)
......@@ -150,16 +159,19 @@ void Tracer::activateTracer(const QString &type)
{
QMutexLocker locker(&mMutex);
mSettings->setValue(QStringLiteral("Debug/Tracer"), type);
mSettings->sync();
if (type == QLatin1String("file")) {
const QString file = mSettings->value(QStringLiteral("Debug/File"), QStringLiteral("/dev/null")).toString();
mTracerBackend = std::make_unique<FileTracer>(file);
} else if (type == QLatin1String("dbus")) {
mTracerBackend = std::make_unique<DBusTracer>();
} else if (type == QLatin1String("null")) {
mTracerBackend = std::make_unique<NullTracer>();
mTracerBackend.reset();
} else {
mTracerBackend = std::make_unique<DBusTracer>();
qCCritical(AKONADISERVER_LOG) << "Unknown tracer type" << type;
mTracerBackend.reset();
return;
}
Q_ASSERT(mTracerBackend);
mSettings->setValue(QStringLiteral("Debug/Tracer"), type);
mSettings->sync();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment