QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
LogStore.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QFuture>
4#include <QtCore/QList>
5#include <QtCore/QMutex>
6#include <QtCore/QObject>
7#include <QtCore/QString>
8#include <QtCore/QWaitCondition>
9#include <QtQmlIntegration/QtQmlIntegration>
10#include <atomic>
11#include <vector>
12
13#include "LogEntry.h"
14
15class QSqlQuery;
16class QThread;
17
18class LogStore : public QObject
19{
20 Q_OBJECT
21 QML_ELEMENT
22 QML_UNCREATABLE("")
23
24 Q_PROPERTY(bool isOpen READ isOpen NOTIFY isOpenChanged)
25 Q_PROPERTY(QString databasePath READ databasePath NOTIFY databasePathChanged)
26 Q_PROPERTY(qint64 entryCount READ entryCount NOTIFY entryCountChanged)
27 Q_PROPERTY(QString sessionId READ sessionId CONSTANT)
28
29public:
30 explicit LogStore(QObject* parent = nullptr);
31 ~LogStore() override;
32
33 void open(const QString& dbPath);
34 void close();
35
36 bool isOpen() const { return _isOpen.load(std::memory_order_relaxed); }
37
38 QString databasePath() const;
39
40 qint64 entryCount() const { return _entryCount.load(std::memory_order_relaxed); }
41
42 QString sessionId() const { return _sessionId; }
43
44 void append(LogEntry entry);
45 void flush();
46
47 struct QueryParams
48 {
49 QString sessionId;
50 QDateTime fromTime;
51 QDateTime toTime;
52 int minLevel = LogEntry::Debug;
53 QString category;
54 QString textFilter;
55 int limit = 10000;
56 int offset = 0;
57 };
58
59 Q_INVOKABLE QList<LogEntry> query(const QueryParams& params) const;
60 Q_INVOKABLE QStringList sessions() const;
61 Q_INVOKABLE qint64 sessionEntryCount(const QString& sessionId) const;
62 Q_INVOKABLE bool deleteSession(const QString& sessionId);
63 Q_INVOKABLE void exportSession(const QString& sessionId, const QString& destFile, int format = 0);
64
65signals:
69 void errorOccurred(const QString& message);
70 void exportFinished(bool success);
71
72private:
74
75 void _workerLoop();
76 void _startWorker();
77 void _stopWorker();
78 static void _bindAndExec(QSqlQuery& q, const QString& sessionId, const LogEntry& entry);
79
80 mutable QMutex _mutex;
81 QWaitCondition _condition;
82 std::vector<LogEntry> _pendingWrites;
83
84 QString _dbPath;
85 const QString _sessionId;
86 QThread* _thread = nullptr;
87
88 QString _writeConnName;
89
90 QFuture<void> _exportFuture;
91 std::atomic<bool> _isOpen{false};
92 std::atomic<bool> _quit{false};
93 std::atomic<qint64> _entryCount{0};
94 static constexpr int kBatchSize = 500;
95 static constexpr int kFlushIntervalMs = 2000;
96};
void entryCountChanged()
void exportFinished(bool success)
void databasePathChanged()
void errorOccurred(const QString &message)
void isOpenChanged()