QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgsdbquerylog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdbquerylog.cpp
3 ------------
4 Date : October 2021
5 Copyright : (C) 2021 by Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsdbquerylog.h"
17#include "qgsapplication.h"
18#include <QDateTime>
19
20//
21// QgsDatabaseQueryLogEntry
22//
23
24QAtomicInt QgsDatabaseQueryLogEntry::sQueryId = 0;
25
27 : queryId( ++sQueryId )
28 , query( query )
29 , startedTime( QDateTime::currentMSecsSinceEpoch() )
30{}
31
32
33//
34// QgsDatabaseQueryLog
35//
36
37bool QgsDatabaseQueryLog::sEnabled = false;
38
40 : QObject( parent )
41{
42
43}
44
46{
47 if ( !sEnabled )
48 return;
49
50 QMetaObject::invokeMethod( QgsApplication::databaseQueryLog(), "queryStartedPrivate", Qt::QueuedConnection, Q_ARG( QgsDatabaseQueryLogEntry, query ) );
51}
52
54{
55 if ( !sEnabled )
56 return;
57
58 // record time of completion
59 QgsDatabaseQueryLogEntry finishedQuery = query;
60 finishedQuery.finishedTime = QDateTime::currentMSecsSinceEpoch();
61
62 QMetaObject::invokeMethod( QgsApplication::databaseQueryLog(), "queryFinishedPrivate", Qt::QueuedConnection, Q_ARG( QgsDatabaseQueryLogEntry, finishedQuery ) );
63}
64
66{
67 QgsDebugMsgLevel( query.query, 2 );
68 emit queryStarted( query );
69}
70
72{
73 QgsDebugMsgLevel( query.query, 2 );
74 emit queryFinished( query );
75}
76
static QgsDatabaseQueryLog * databaseQueryLog()
Returns the database query log.
Encapsulates a logged database query.
Definition: qgsdbquerylog.h:32
quint64 finishedTime
Time when the query finished (in milliseconds since epoch), if available.
Definition: qgsdbquerylog.h:66
QString query
The logged database query (e.g. the SQL query)
Definition: qgsdbquerylog.h:54
QgsDatabaseQueryLogEntry(const QString &query=QString())
Constructor for QgsDatabaseQueryLogEntry.
static void log(const QgsDatabaseQueryLogEntry &query)
Logs a database query as starting.
void queryFinished(const QgsDatabaseQueryLogEntry &query)
Emitted whenever a database query has finished executing.
static void finished(const QgsDatabaseQueryLogEntry &query)
Records that the database query has finished.
void queryStarted(const QgsDatabaseQueryLogEntry &query)
Emitted whenever a database query is started.
void queryFinishedPrivate(const QgsDatabaseQueryLogEntry &query)
Internal slot for logging queries as finished.
void queryStartedPrivate(const QgsDatabaseQueryLogEntry &query)
Internal slot for logging queries as start.
QgsDatabaseQueryLog(QObject *parent=nullptr)
Creates a new query log.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39