| 1 |
// Copyright © 2016 Canonical Ltd. |
| 2 |
// Author: Loïc Molinari <[email protected]> |
| 3 |
// |
| 4 |
// This file is part of Lomiri UI Toolkit. |
| 5 |
// |
| 6 |
// Lomiri UI Toolkit is free software: you can redistribute it and/or modify it |
| 7 |
// under the terms of the GNU Lesser General Public License as published by the |
| 8 |
// Free Software Foundation; version 3. |
| 9 |
// |
| 10 |
// Lomiri UI Toolkit is distributed in the hope that it will be useful, but |
| 11 |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 13 |
// for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with Lomiri UI Toolkit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
#ifndef APPLICATIONMONITOR_H |
| 19 |
#define APPLICATIONMONITOR_H |
| 20 |
|
| 21 |
#include <QtCore/QList> |
| 22 |
|
| 23 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 24 |
#include <LomiriMetrics-Qt6/logger.h> |
| 25 |
#include <LomiriMetrics-Qt6/events.h> |
| 26 |
#include <LomiriMetrics-Qt6/lomirimetricsglobal.h> |
| 27 |
#else |
| 28 |
#include <LomiriMetrics/logger.h> |
| 29 |
#include <LomiriMetrics/events.h> |
| 30 |
#include <LomiriMetrics/lomirimetricsglobal.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
class UMApplicationMonitorPrivate; |
| 34 |
|
| 35 |
// Monitor a QtQuick application by automatically tracking QtQuick windows and |
| 36 |
// process metrics. The metrics gathered can be logged and displayed by an |
| 37 |
// overlay rendered on top of each frame. |
| 38 |
class LOMIRI_METRICS_EXPORT UMApplicationMonitor : public QObject |
| 39 |
{ |
| 40 |
Q_OBJECT |
| 41 |
|
| 42 |
public: |
| 43 |
enum LoggingFilter { |
| 44 |
// Allow process events logging. |
| 45 |
ProcessEvent = (1 << 0), |
| 46 |
// Allow window events logging. |
| 47 |
WindowEvent = (1 << 1), |
| 48 |
// Allow frame events logging. |
| 49 |
FrameEvent = (1 << 2), |
| 50 |
// Allow generic events logging. |
| 51 |
GenericEvent = (1 << 3), |
| 52 |
// Allow all events logging. |
| 53 |
AllEvents = (ProcessEvent | WindowEvent | FrameEvent | GenericEvent) |
| 54 |
}; |
| 55 |
Q_DECLARE_FLAGS(LoggingFilters, LoggingFilter) |
| 56 |
|
| 57 |
enum Event { |
| 58 |
// Application defined event indicating that the initialisation is done |
| 59 |
// and the UI ready. It can be used by tools to measure the time needed |
| 60 |
// to start up an application. |
| 61 |
UserInterfaceReady = 0 |
| 62 |
}; |
| 63 |
|
| 64 |
// Get the unique UMApplicationMonitor instance. A QGuiApplication instance |
| 65 |
// must be running. |
| 66 |
static UMApplicationMonitor* instance() { return self ? self : new UMApplicationMonitor; } |
| 67 |
|
| 68 |
// Render an overlay of real-time metrics on top of each QtQuick frame. |
| 69 |
void setOverlay(bool overlay); |
| 70 |
bool overlay(); |
| 71 |
|
| 72 |
// Log the events with the installed loggers. |
| 73 |
void setLogging(bool logging); |
| 74 |
bool logging(); |
| 75 |
|
| 76 |
// Set the logging filter. All events are logged by default. |
| 77 |
void setLoggingFilter(LoggingFilters filter); |
| 78 |
LoggingFilters loggingFilter(); |
| 79 |
|
| 80 |
// Set the loggers. Empty by default, max number of loggers is 8. |
| 81 |
QList<UMLogger*> loggers(); |
| 82 |
bool installLogger(UMLogger* logger); |
| 83 |
bool removeLogger(UMLogger* logger, bool free = true); |
| 84 |
void clearLoggers(bool free = true); |
| 85 |
|
| 86 |
// Generic event system allowing to log application specific |
| 87 |
// events. registerGenericEvent() returns a unique integer id to be used as |
| 88 |
// first argument to logGenericEvent(). logGenericEvent() logs a generic |
| 89 |
// event with a dedicated id, a null-terminated string describing the event |
| 90 |
// and the string size with the null-terminating character. The maximum |
| 91 |
// string size (with the null-terminating character) is defined in |
| 92 |
// UMGenericEvent::maxStringSize. Does not log and returns false if logging |
| 93 |
// is disabled or if the logging filter does not contain GenericEvent. |
| 94 |
quint32 registerGenericEvent(); |
| 95 |
bool logGenericEvent(quint32 id, const char* string, quint32 size); |
| 96 |
|
| 97 |
// Log events predefined by the application monitor. Relies on the generic |
| 98 |
// event system. |
| 99 |
bool logEvent(Event event); |
| 100 |
|
| 101 |
// Set the time in milliseconds between two updates of events of a given |
| 102 |
// type. -1 to disable updates. Only UMEvent::Process is accepted so far as |
| 103 |
// event type, default value is 1000. Note that when the overlay is enabled, |
| 104 |
// a process update triggers a frame update. |
| 105 |
void setUpdateInterval(UMEvent::Type type, int interval); |
| 106 |
int updateInterval(UMEvent::Type type); |
| 107 |
|
| 108 |
Q_SIGNALS: |
| 109 |
void overlayChanged(); |
| 110 |
void loggingChanged(); |
| 111 |
void loggingFilterChanged(); |
| 112 |
void loggersChanged(); |
| 113 |
void updateIntervalChanged(UMEvent::Type type); |
| 114 |
|
| 115 |
private Q_SLOTS: |
| 116 |
void closeDown(); |
| 117 |
void processTimeout(); |
| 118 |
|
| 119 |
private: |
| 120 |
static UMApplicationMonitor* self; |
| 121 |
|
| 122 |
UMApplicationMonitor(); |
| 123 |
~UMApplicationMonitor(); |
| 124 |
Q_DISABLE_COPY(UMApplicationMonitor) |
| 125 |
|
| 126 |
bool eventFilter(QObject* object, QEvent* event) Q_DECL_OVERRIDE; |
| 127 |
|
| 128 |
UMApplicationMonitorPrivate* const d_ptr; |
| 129 |
Q_DECLARE_PRIVATE(UMApplicationMonitor) |
| 130 |
}; |
| 131 |
|
| 132 |
#endif // APPLICATIONMONITOR_H |