qmlprofilerstatemanager.cpp 5.55 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Christiaan Janssen's avatar
Christiaan Janssen committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Christiaan Janssen's avatar
Christiaan Janssen committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Christiaan Janssen's avatar
Christiaan Janssen committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Christiaan Janssen's avatar
Christiaan Janssen committed
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
Christiaan Janssen's avatar
Christiaan Janssen committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
Christiaan Janssen's avatar
Christiaan Janssen committed
25 26 27 28 29 30 31 32 33 34 35

#include "qmlprofilerstatemanager.h"

#include <QDebug>
#include <utils/qtcassert.h>

// uncomment for printing the state changes to debug output
//#define _DEBUG_PROFILERSTATE_

namespace QmlProfiler {

Christiaan Janssen's avatar
Christiaan Janssen committed
36
inline QString stringForState(int state) {
Christiaan Janssen's avatar
Christiaan Janssen committed
37
    switch (state) {
38 39 40 41
    case QmlProfilerStateManager::Idle: return QLatin1String("Idle");
    case QmlProfilerStateManager::AppRunning: return QLatin1String("AppRunning");
    case QmlProfilerStateManager::AppStopRequested: return QLatin1String("AppStopRequested");
    case QmlProfilerStateManager::AppDying: return QLatin1String("AppDying");
Christiaan Janssen's avatar
Christiaan Janssen committed
42 43 44 45 46
    default: break;
    }
    return QString();
}

Christiaan Janssen's avatar
Christiaan Janssen committed
47 48 49 50 51 52 53 54 55 56 57
class QmlProfilerStateManager::QmlProfilerStateManagerPrivate
{
public:
    QmlProfilerStateManagerPrivate(QmlProfilerStateManager *qq) : q(qq) {}
    ~QmlProfilerStateManagerPrivate() {}

    QmlProfilerStateManager *q;

    QmlProfilerStateManager::QmlProfilerState m_currentState;
    bool m_clientRecording;
    bool m_serverRecording;
58 59
    quint64 m_requestedFeatures;
    quint64 m_recordedFeatures;
Christiaan Janssen's avatar
Christiaan Janssen committed
60
};
Christiaan Janssen's avatar
Christiaan Janssen committed
61
QmlProfilerStateManager::QmlProfilerStateManager(QObject *parent) :
Christiaan Janssen's avatar
Christiaan Janssen committed
62
    QObject(parent),d(new QmlProfilerStateManagerPrivate(this))
Christiaan Janssen's avatar
Christiaan Janssen committed
63
{
Christiaan Janssen's avatar
Christiaan Janssen committed
64 65 66
    d->m_currentState = Idle;
    d->m_clientRecording = true;
    d->m_serverRecording = false;
67 68
    d->m_requestedFeatures = 0;
    d->m_recordedFeatures = 0;
Christiaan Janssen's avatar
Christiaan Janssen committed
69 70 71 72
}

QmlProfilerStateManager::~QmlProfilerStateManager()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
73
    delete d;
Christiaan Janssen's avatar
Christiaan Janssen committed
74 75
}

Christiaan Janssen's avatar
Christiaan Janssen committed
76
QmlProfilerStateManager::QmlProfilerState QmlProfilerStateManager::currentState()
Christiaan Janssen's avatar
Christiaan Janssen committed
77
{
Christiaan Janssen's avatar
Christiaan Janssen committed
78
    return d->m_currentState;
Christiaan Janssen's avatar
Christiaan Janssen committed
79 80
}

Christiaan Janssen's avatar
Christiaan Janssen committed
81
bool QmlProfilerStateManager::clientRecording()
Christiaan Janssen's avatar
Christiaan Janssen committed
82
{
Christiaan Janssen's avatar
Christiaan Janssen committed
83
    return d->m_clientRecording;
Christiaan Janssen's avatar
Christiaan Janssen committed
84 85
}

Christiaan Janssen's avatar
Christiaan Janssen committed
86
bool QmlProfilerStateManager::serverRecording()
Christiaan Janssen's avatar
Christiaan Janssen committed
87
{
Christiaan Janssen's avatar
Christiaan Janssen committed
88
    return d->m_serverRecording;
Christiaan Janssen's avatar
Christiaan Janssen committed
89 90
}

91
quint64 QmlProfilerStateManager::requestedFeatures() const
92
{
93 94 95 96 97 98
    return d->m_requestedFeatures;
}

quint64 QmlProfilerStateManager::recordedFeatures() const
{
    return d->m_recordedFeatures;
99 100
}

Christiaan Janssen's avatar
Christiaan Janssen committed
101
QString QmlProfilerStateManager::currentStateAsString()
Christiaan Janssen's avatar
Christiaan Janssen committed
102
{
Christiaan Janssen's avatar
Christiaan Janssen committed
103
    return stringForState(d->m_currentState);
Christiaan Janssen's avatar
Christiaan Janssen committed
104 105 106 107 108
}

void QmlProfilerStateManager::setCurrentState(QmlProfilerState newState)
{
#ifdef _DEBUG_PROFILERSTATE_
Christiaan Janssen's avatar
Christiaan Janssen committed
109
    qDebug() << "Profiler state change request from" << stringForState(d->m_currentState) << "to" << stringForState(newState);
Christiaan Janssen's avatar
Christiaan Janssen committed
110
#endif
Christiaan Janssen's avatar
Christiaan Janssen committed
111
    QTC_ASSERT(d->m_currentState != newState, /**/);
Christiaan Janssen's avatar
Christiaan Janssen committed
112 113
    switch (newState) {
    case Idle:
114 115
        QTC_ASSERT(d->m_currentState == AppStopRequested ||
                   d->m_currentState == AppDying,
Christiaan Janssen's avatar
Christiaan Janssen committed
116
                   qDebug() << "from" << stringForState(d->m_currentState));
Christiaan Janssen's avatar
Christiaan Janssen committed
117 118
        break;
    case AppRunning:
119
        QTC_ASSERT(d->m_currentState == Idle,
Christiaan Janssen's avatar
Christiaan Janssen committed
120
                   qDebug() << "from" << stringForState(d->m_currentState));
Christiaan Janssen's avatar
Christiaan Janssen committed
121 122
        break;
    case AppStopRequested:
Christiaan Janssen's avatar
Christiaan Janssen committed
123 124
        QTC_ASSERT(d->m_currentState == AppRunning,
                   qDebug() << "from" << stringForState(d->m_currentState));
Christiaan Janssen's avatar
Christiaan Janssen committed
125
        break;
126
    case AppDying:
Christiaan Janssen's avatar
Christiaan Janssen committed
127 128
        QTC_ASSERT(d->m_currentState == AppRunning,
                   qDebug() << "from" << stringForState(d->m_currentState));
Christiaan Janssen's avatar
Christiaan Janssen committed
129
        break;
Friedemann Kleint's avatar
Friedemann Kleint committed
130 131 132 133
    default: {
        const QString message = QString::fromLatin1("Switching to unknown state in %1:%2").arg(QString::fromLatin1(__FILE__), QString::number(__LINE__));
        qWarning("%s", qPrintable(message));
    }
Christiaan Janssen's avatar
Christiaan Janssen committed
134 135 136
        break;
    }

Christiaan Janssen's avatar
Christiaan Janssen committed
137
    d->m_currentState = newState;
Christiaan Janssen's avatar
Christiaan Janssen committed
138 139 140 141 142 143
    emit stateChanged();
}

void QmlProfilerStateManager::setClientRecording(bool recording)
{
#ifdef _DEBUG_PROFILERSTATE_
Ulf Hermann's avatar
Ulf Hermann committed
144
    qDebug() << "Setting client recording flag from" << d->m_clientRecording << "to" << recording;
Christiaan Janssen's avatar
Christiaan Janssen committed
145
#endif
Christiaan Janssen's avatar
Christiaan Janssen committed
146 147
    if (d->m_clientRecording != recording) {
        d->m_clientRecording = recording;
148
        emit clientRecordingChanged(recording);
Christiaan Janssen's avatar
Christiaan Janssen committed
149 150 151 152 153 154
    }
}

void QmlProfilerStateManager::setServerRecording(bool recording)
{
#ifdef _DEBUG_PROFILERSTATE_
Christiaan Janssen's avatar
Christiaan Janssen committed
155
    qDebug() << "Setting server recording flag from" << d->m_serverRecording << "to" << recording;
Christiaan Janssen's avatar
Christiaan Janssen committed
156
#endif
Christiaan Janssen's avatar
Christiaan Janssen committed
157 158
    if (d->m_serverRecording != recording) {
        d->m_serverRecording = recording;
159
        emit serverRecordingChanged(recording);
Christiaan Janssen's avatar
Christiaan Janssen committed
160 161 162
    }
}

163 164 165 166 167 168 169 170 171
void QmlProfilerStateManager::setRequestedFeatures(quint64 features)
{
    if (d->m_requestedFeatures != features) {
        d->m_requestedFeatures = features;
        emit requestedFeaturesChanged(features);
    }
}

void QmlProfilerStateManager::setRecordedFeatures(quint64 features)
172
{
173 174 175
    if (d->m_recordedFeatures != features) {
        d->m_recordedFeatures = features;
        emit recordedFeaturesChanged(features);
176 177 178
    }
}

179
} // namespace QmlProfiler