Commit 1a8407a3 authored by Daniel Vrátil's avatar Daniel Vrátil 🤖
Browse files

Slightly modernize StorageDebugger

parent 606a973d
......@@ -83,9 +83,6 @@ StorageDebugger *StorageDebugger::instance()
}
StorageDebugger::StorageDebugger()
: mFile(nullptr)
, mEnabled(false)
, mSequence(0)
{
qDBusRegisterMetaType<QList< QList<QVariant> > >();
qDBusRegisterMetaType<DbConnection>();
......@@ -95,13 +92,7 @@ StorageDebugger::StorageDebugger()
this, QDBusConnection::ExportAdaptors);
}
StorageDebugger::~StorageDebugger()
{
if (mFile) {
mFile->close();
delete mFile;
}
}
StorageDebugger::~StorageDebugger() = default;
void StorageDebugger::enableSQLDebugging(bool enable)
{
......@@ -110,8 +101,7 @@ void StorageDebugger::enableSQLDebugging(bool enable)
void StorageDebugger::writeToFile(const QString &file)
{
delete mFile;
mFile = new QFile(file);
mFile.reset(new QFile(file));
mFile->open(QIODevice::WriteOnly);
}
......@@ -197,22 +187,20 @@ QVector<DbConnection> StorageDebugger::connections() const
void StorageDebugger::queryExecuted(qint64 connectionId, const QSqlQuery &query, int duration)
{
QMutexLocker locker(&mMutex);
const qint64 seq = mSequence.fetchAndAddOrdered(1);
if (!mEnabled) {
return;
}
locker.unlock();
const qint64 seq = ++mSequence;
if (query.lastError().isValid()) {
Q_EMIT queryExecuted(seq, connectionId, QDateTime::currentMSecsSinceEpoch(),
duration, query.executedQuery(), query.boundValues(), 0,
QList< QList<QVariant> >(), query.lastError().text());
QList<QList<QVariant>>(), query.lastError().text());
return;
}
QSqlQuery q(query);
QList< QVariantList > result;
QList<QVariantList> result;
if (q.first()) {
const QSqlRecord record = q.record();
......@@ -237,20 +225,14 @@ void StorageDebugger::queryExecuted(qint64 connectionId, const QSqlQuery &query,
} while (q.next() && ++cnt < 1000);
}
int querySize;
if (query.isSelect()) {
querySize = query.size();
} else {
querySize = query.numRowsAffected();
}
const int querySize = query.isSelect() ? query.size() : query.numRowsAffected();
Q_EMIT queryExecuted(seq, connectionId, QDateTime::currentMSecsSinceEpoch(),
duration, query.executedQuery(), query.boundValues(),
querySize, result, QString());
if (mFile && mFile->isOpen()) {
QTextStream out(mFile);
out << query.executedQuery() << " " << duration << "ms " << "\n";
QTextStream out(mFile.get());
out << query.executedQuery() << " " << duration << "ms\n";
}
// Reset the query
......
......@@ -27,17 +27,8 @@
#include <QFile>
#include <QVector>
#ifdef QT5_BUILD
#include <QAtomicInteger>
#else
#include <QAtomicInt>
#endif
#ifdef Q_ATOMC_INT64_IS_SUPPORTED
#include <QAtomicInteger>
#else
#include <QAtomicInt>
#endif
#include <atomic>
#include <memory>
class QSqlQuery;
class QDBusArgument;
......@@ -65,7 +56,7 @@ class StorageDebugger : public QObject
public:
static StorageDebugger *instance();
~StorageDebugger();
~StorageDebugger() override;
void addConnection(qint64 id, const QString &name);
void removeConnection(qint64 id);
......@@ -81,9 +72,9 @@ public:
void queryExecuted(qint64 connectionId, const QSqlQuery &query, int duration);
void incSequence()
inline void incSequence()
{
mSequence.ref();
++mSequence;
}
void writeToFile(const QString &file);
......@@ -111,14 +102,10 @@ private:
static StorageDebugger *mSelf;
static QMutex mMutex;
QFile *mFile = nullptr;
std::unique_ptr<QFile> mFile;
bool mEnabled;
#ifdef Q_ATOMC_INT64_IS_SUPPORTED
QAtomicInteger<qint64> mSequence;
#else
QAtomicInt mSequence;
#endif
std::atomic_bool mEnabled = {false};
std::atomic_int64_t mSequence = {0};
QVector<DbConnection> mConnections;
};
......
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