QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
LogManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QFuture>
4#include <QtCore/QHash>
5#include <QtCore/QList>
6#include <QtCore/QObject>
7#include <QtCore/QSet>
8#include <QtCore/QString>
9#include <QtCore/QTimer>
10#include <QtQmlIntegration/QtQmlIntegration>
11
12#include "LogEntry.h"
13
14class QJSEngine;
15class QQmlEngine;
16class LogModel;
17class QGCFileWriter;
18
19class LogManager : public QObject
20{
21 Q_OBJECT
22 QML_ELEMENT
23 QML_SINGLETON
24
25 Q_MOC_INCLUDE("LogModel.h")
26
27 Q_PROPERTY(LogModel* model READ model CONSTANT)
28 Q_PROPERTY(bool hasError READ hasError NOTIFY hasErrorChanged)
29 Q_PROPERTY(QString lastError READ lastError NOTIFY lastErrorChanged)
30
31public:
33
34 static LogManager* instance();
35 static LogManager* create(QQmlEngine* qmlEngine, QJSEngine* jsEngine);
36 static void installHandler();
37 static void applyEnvironmentLogLevel();
38
39 void init();
40
41 [[nodiscard]] LogModel* model() { return _model; }
42
43 [[nodiscard]] bool hasError() const { return _ioError; }
44
45 [[nodiscard]] QString lastError() const { return _lastError; }
46
47 void setLogDirectory(const QString& path);
48
49 [[nodiscard]] QString logDirectory() const { return _logDirectory; }
50
51 Q_INVOKABLE void writeMessages(const QString& destFile);
52 Q_INVOKABLE void clearError();
53 Q_INVOKABLE void flush();
54
55 static void setCaptureEnabled(bool enabled);
56 static void clearCapturedMessages();
57 [[nodiscard]] static QList<LogEntry> capturedMessages(const QString& category = {});
58 [[nodiscard]] static bool hasCapturedMessage(const QString& category, LogEntry::Level level);
59 [[nodiscard]] static bool hasCapturedWarning(const QString& category);
60 [[nodiscard]] static bool hasCapturedCritical(const QString& category);
61 [[nodiscard]] static bool hasCapturedUncategorizedMessage();
62 static void captureIfEnabled(QtMsgType type, const QMessageLogContext& context, const QString& msg);
63
64signals:
68 void writeFinished(bool success);
69
70private slots:
71 void _handleEntry(const LogEntry& entry);
72 void _flushToDisk();
73
74private:
75 explicit LogManager(QObject* parent = nullptr);
76
77 static void msgHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg);
78 void log(QtMsgType type, const QMessageLogContext& context, const QString& message);
79 static LogEntry buildEntry(QtMsgType type, const QMessageLogContext& context, const QString& message);
80
81 void _dispatchToSinks(const LogEntry& entry);
82 void _replayEarlyEntries();
83 void _setDiskLoggingEnabled(bool enabled);
84 void _rotateLogs();
85 void _setIoError(const QString& message);
86 void _exportEntries(QList<LogEntry> entries, const QString& destFile);
87 const QString& _internCategory(const QString& category);
88
89 // Rate limiting infrastructure — currently disabled (_rateLimitingEnabled = false)
90 struct RateBucket
91 {
92 qint64 lastRefillMs = 0;
93 int tokens = 0;
94 int suppressed = 0;
95 };
96
97 bool _rateLimitCheck(const LogEntry& entry);
98 void _emitSuppressedSummary(const QString& category, int count);
99
100 LogModel* _model = nullptr;
101 QGCFileWriter* _fileWriter = nullptr;
102
103 QFuture<void> _exportFuture;
104 QTimer _flushTimer;
105 QList<LogEntry> _pendingDiskWrites;
106 QSet<QString> _internedCategories;
107 QHash<QString, RateBucket> _rateBuckets;
108 QString _logDirectory;
109 bool _ioError = false;
110 QString _lastError;
111 bool _initialized = false;
112 bool _diskLoggingEnabled = false;
113 bool _rateLimitingEnabled = false;
114 int _maxLogFileSize = 10 * 1024 * 1024;
115 int _maxBackupFiles = 5;
116
117 static constexpr int kFlushIntervalMSecs = 1000;
118 static constexpr int kRateTokensPerSecond = 100;
119 static constexpr int kRateMaxTokens = 200;
120};
QString lastError() const
Definition LogManager.h:45
static void applyEnvironmentLogLevel()
LogModel * model()
Definition LogManager.h:41
void writeStarted()
Q_INVOKABLE void clearError()
static bool hasCapturedMessage(const QString &category, LogEntry::Level level)
static void clearCapturedMessages()
static bool hasCapturedCritical(const QString &category)
Q_INVOKABLE void flush()
static bool hasCapturedWarning(const QString &category)
Q_INVOKABLE void writeMessages(const QString &destFile)
static LogManager * create(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
Definition LogManager.cc:74
void lastErrorChanged()
static bool hasCapturedUncategorizedMessage()
void writeFinished(bool success)
static QList< LogEntry > capturedMessages(const QString &category={})
QString logDirectory() const
Definition LogManager.h:49
void setLogDirectory(const QString &path)
void init()
static void installHandler()
static LogManager * instance()
Definition LogManager.cc:69
void hasErrorChanged()
static void captureIfEnabled(QtMsgType type, const QMessageLogContext &context, const QString &msg)
bool hasError() const
Definition LogManager.h:43
static void setCaptureEnabled(bool enabled)