QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QGCLogging.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QFile>
4#include <QtCore/QList>
5#include <QtCore/QLoggingCategory>
6#include <QtCore/QString>
7#include <QtCore/QStringListModel>
8#include <QtCore/QTimer>
9
10Q_DECLARE_LOGGING_CATEGORY(QGCLoggingLog)
11
12
14{
15 QtMsgType type = QtDebugMsg;
16 QString category;
17 QString message;
18};
19
20class QGCLogging : public QStringListModel
21{
22 Q_OBJECT
23
24public:
25 explicit QGCLogging(QObject *parent = nullptr);
27
29 static QGCLogging *instance();
30
32 static void installHandler();
33
35 Q_INVOKABLE void writeMessages(const QString &destFile);
36
38 void log(const QString &message);
39
40 // ========================================================================
41 // Test Log Capture
42 // ========================================================================
43
47 static void setCaptureEnabled(bool enabled);
48
50 static void clearCapturedMessages();
51
54 [[nodiscard]] static QList<CapturedLogMessage> capturedMessages(const QString &category = {});
55
58 [[nodiscard]] static bool hasCapturedMessage(const QString &category, QtMsgType type);
59
61 [[nodiscard]] static bool hasCapturedWarning(const QString &category);
62
64 [[nodiscard]] static bool hasCapturedCritical(const QString &category);
65
67 [[nodiscard]] static bool hasCapturedUncategorizedMessage();
68
71 static void captureIfEnabled(QtMsgType type, const QMessageLogContext &context, const QString &msg);
72
73signals:
75 void emitLog(const QString &message);
76
79
81 void writeFinished(bool success);
82
83private slots:
85 void _threadsafeLog(const QString &message);
86
88 void _flushToDisk();
89
90private:
91 void _rotateLogs();
92
93 QFile _logFile;
94 QTimer _flushTimer;
95 QStringList _pendingDiskWrites;
96 bool _ioError = false;
97
98 static constexpr int kMaxLogFileSize = 10LL * 1024 * 1024;
99 static constexpr int kMaxBackupFiles = 5;
100 static constexpr int kFlushIntervalMSecs = 1000;
101};
Q_DECLARE_LOGGING_CATEGORY(AndroidSerialLog)
static void setCaptureEnabled(bool enabled)
Definition QGCLogging.cc:92
static bool hasCapturedMessage(const QString &category, QtMsgType type)
void log(const QString &message)
Enqueue a log message (thread-safe)
void writeMessages(const QString &destFile)
Write current log messages to a file asynchronously.
static bool hasCapturedCritical(const QString &category)
Convenience: captured critical in category?
static QList< CapturedLogMessage > capturedMessages(const QString &category={})
static QGCLogging * instance()
Get the singleton instance.
Definition QGCLogging.cc:50
static void installHandler()
Install Qt message handler to route logs through this class.
Definition QGCLogging.cc:79
static void clearCapturedMessages()
Discard all previously captured messages.
Definition QGCLogging.cc:97
void writeFinished(bool success)
Emitted when file write finishes (success flag)
static void captureIfEnabled(QtMsgType type, const QMessageLogContext &context, const QString &msg)
void emitLog(const QString &message)
Emitted when a log message is enqueued.
static bool hasCapturedWarning(const QString &category)
Convenience: captured warning in category?
void writeStarted()
Emitted when file write starts.
static bool hasCapturedUncategorizedMessage()
Return true if any uncategorized message was captured (e.g. raw qDebug/qWarning).
A single captured log message for test introspection.
Definition QGCLogging.h:14