3#include <QtCore/QDateTime>
4#include <QtCore/QElapsedTimer>
7#include <QtCore/QJsonObject>
9#include <QtCore/QObject>
11#include <QtCore/QString>
12#include <QtCore/QTextStream>
17class QAbstractTransition;
85 qint64 stateDurationMs = 0;
89 QString toString(
bool colored =
false,
bool showTiming =
true,
bool indent =
true)
const;
90 QJsonObject toJson()
const;
171 void log(
LogLevel level,
const QString& message,
const QJsonObject& context = QJsonObject());
174 void logEvent(
LogEvent event,
const QString& message,
const QJsonObject& context = QJsonObject());
187 void _onMachineStarted();
188 void _onMachineStopped();
189 void _onStateEntered();
190 void _onStateExited();
193 void _log(
const LogEntry& entry);
194 void _connectToState(QAbstractState* state);
195 QString _colorize(
const QString& text,
const QString& colorCode)
const;
196 QString _eventColor(
LogEvent event)
const;
197 LogLevel _effectiveLogLevel(
const QString& stateName)
const;
198 QString _determineTransitionReason()
const;
201 bool _enabled =
false;
205 bool _coloredOutput =
false;
206 bool _logTimings =
true;
207 bool _logIndent =
true;
208 bool _logTransitionReasons =
true;
210 QSet<QString> _excludedStates;
211 QHash<QString, LogLevel> _stateLogLevels;
213 QFile* _logFile =
nullptr;
214 QTextStream* _logStream =
nullptr;
217 QList<LogEntry> _crashLog;
218 int _crashLogMaxEntries = 0;
220 QElapsedTimer _machineTimer;
221 QHash<QString, qint64> _stateEntryTimes;
222 QString _previousState;
223 int _currentDepth = 0;
225 QHash<LogEvent, int> _eventCounts;
226 QList<QMetaObject::Connection> _connections;
229Q_DECLARE_OPERATORS_FOR_FLAGS(StateMachineLogger::LogEvents)
QGroundControl specific state machine with enhanced error handling.
void setEnabled(bool enabled)
void excludeState(const QString &stateName)
Exclude specific states from logging.
std::function< void(const LogEntry &)> LogHandler
void setLogLevel(LogLevel level)
void enableCrashLog(int maxEntries)
Enable crash log buffer (circular buffer for post-mortem analysis)
LogEvents logFilter() const
void setStateLogLevel(const QString &stateName, LogLevel level)
Set log level override for a specific state.
void setLogTimings(bool enabled)
Enable timing annotations.
bool coloredOutput() const
void log(LogLevel level, const QString &message, const QJsonObject &context=QJsonObject())
Log a custom message.
void setLogIndent(bool enabled)
Enable hierarchy indentation.
void setLogHandler(LogHandler handler)
Set a custom log handler.
void includeState(const QString &stateName)
void setLogFilter(LogEvents events)
Set which events to log (default: all)
LogLevel logLevel() const
bool logTransitionReasons() const
void setLogTransitionReasons(bool enabled)
Log transition reasons (signal name, timeout, etc.)
void logEvent(LogEvent event, const QString &message, const QJsonObject &context=QJsonObject())
Log with specific event type.
LogLevel
Log verbosity levels.
@ Normal
Errors + state changes.
@ Trace
Everything including internal details.
@ Verbose
Normal + signals/transitions.
QString dumpCrashLog() const
void clearStateLogLevel(const QString &stateName)
void resetStats()
Reset statistics.
LogEvent
Types of log events.
void setColoredOutput(bool enabled)
Enable colored console output.
bool setLogFile(const QString &filePath)
Log to a file (in addition to console)
QHash< LogEvent, int > eventCounts() const
Get count of entries by event type.
QList< LogEntry > crashLogEntries() const
~StateMachineLogger() override