QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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 "moc_qgsdbquerylog.cpp"
18#include "qgsapplication.h"
19#include "qgslogger.h"
20
21#include <QDateTime>
22
23//
24// QgsDatabaseQueryLogEntry
25//
26
27QAtomicInt QgsDatabaseQueryLogEntry::sQueryId = 0;
28
30 : queryId( ++sQueryId )
31 , query( query )
32 , startedTime( QDateTime::currentMSecsSinceEpoch() )
33{}
34
35
36//
37// QgsDatabaseQueryLog
38//
39
40bool QgsDatabaseQueryLog::sEnabled = false;
41
43 : QObject( parent )
44{
45
46}
47
49{
50 if ( !sEnabled )
51 return;
52
53 QMetaObject::invokeMethod( QgsApplication::databaseQueryLog(), "queryStartedPrivate", Qt::QueuedConnection, Q_ARG( QgsDatabaseQueryLogEntry, query ) );
54}
55
57{
58 if ( !sEnabled )
59 return;
60
61 // record time of completion
62 QgsDatabaseQueryLogEntry finishedQuery = query;
63 finishedQuery.finishedTime = QDateTime::currentMSecsSinceEpoch();
64
65 QMetaObject::invokeMethod( QgsApplication::databaseQueryLog(), "queryFinishedPrivate", Qt::QueuedConnection, Q_ARG( QgsDatabaseQueryLogEntry, finishedQuery ) );
66}
67
69{
70 QgsDebugMsgLevel( query.query, 2 );
71 emit queryStarted( query );
72}
73
75{
76 QgsDebugMsgLevel( query.query, 2 );
77 emit queryFinished( query );
78}
79
static QgsDatabaseQueryLog * databaseQueryLog()
Returns the database query log.
Encapsulates a logged database query.
quint64 finishedTime
Time when the query finished (in milliseconds since epoch), if available.
QString query
The logged database query (e.g. the SQL query)
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