qmlprofilerstatemanager.cpp 6.13 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Christiaan Janssen's avatar
Christiaan Janssen committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
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 12 13 14
** 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
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Christiaan Janssen's avatar
Christiaan Janssen committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
Christiaan Janssen's avatar
Christiaan Janssen committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Christiaan Janssen's avatar
Christiaan Janssen committed
29 30 31 32 33 34 35 36 37 38 39 40

#include "qmlprofilerstatemanager.h"

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

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

namespace QmlProfiler {
namespace Internal {

Christiaan Janssen's avatar
Christiaan Janssen committed
41
inline QString stringForState(int state) {
Christiaan Janssen's avatar
Christiaan Janssen committed
42
    switch (state) {
43 44 45 46 47 48 49 50
    case QmlProfilerStateManager::Idle: return QLatin1String("Idle");
    case QmlProfilerStateManager::AppStarting: return QLatin1String("AppStarting");
    case QmlProfilerStateManager::AppRunning: return QLatin1String("AppRunning");
    case QmlProfilerStateManager::AppStopRequested: return QLatin1String("AppStopRequested");
    case QmlProfilerStateManager::AppReadyToStop: return QLatin1String("AppReadyToStop");
    case QmlProfilerStateManager::AppStopped: return QLatin1String("AppStopped");
    case QmlProfilerStateManager::AppDying: return QLatin1String("AppDying");
    case QmlProfilerStateManager::AppKilled: return QLatin1String("AppKilled");
Christiaan Janssen's avatar
Christiaan Janssen committed
51 52 53 54 55
    default: break;
    }
    return QString();
}

Christiaan Janssen's avatar
Christiaan Janssen committed
56 57 58 59 60 61 62 63 64 65 66 67
class QmlProfilerStateManager::QmlProfilerStateManagerPrivate
{
public:
    QmlProfilerStateManagerPrivate(QmlProfilerStateManager *qq) : q(qq) {}
    ~QmlProfilerStateManagerPrivate() {}

    QmlProfilerStateManager *q;

    QmlProfilerStateManager::QmlProfilerState m_currentState;
    bool m_clientRecording;
    bool m_serverRecording;
};
Christiaan Janssen's avatar
Christiaan Janssen committed
68
QmlProfilerStateManager::QmlProfilerStateManager(QObject *parent) :
Christiaan Janssen's avatar
Christiaan Janssen committed
69
    QObject(parent),d(new QmlProfilerStateManagerPrivate(this))
Christiaan Janssen's avatar
Christiaan Janssen committed
70
{
Christiaan Janssen's avatar
Christiaan Janssen committed
71 72 73
    d->m_currentState = Idle;
    d->m_clientRecording = true;
    d->m_serverRecording = false;
Christiaan Janssen's avatar
Christiaan Janssen committed
74 75 76 77
}

QmlProfilerStateManager::~QmlProfilerStateManager()
{
Christiaan Janssen's avatar
Christiaan Janssen committed
78
    delete d;
Christiaan Janssen's avatar
Christiaan Janssen committed
79 80
}

Christiaan Janssen's avatar
Christiaan Janssen committed
81
QmlProfilerStateManager::QmlProfilerState QmlProfilerStateManager::currentState()
Christiaan Janssen's avatar
Christiaan Janssen committed
82
{
Christiaan Janssen's avatar
Christiaan Janssen committed
83
    return d->m_currentState;
Christiaan Janssen's avatar
Christiaan Janssen committed
84 85
}

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

Christiaan Janssen's avatar
Christiaan Janssen committed
91
bool QmlProfilerStateManager::serverRecording()
Christiaan Janssen's avatar
Christiaan Janssen committed
92
{
Christiaan Janssen's avatar
Christiaan Janssen committed
93
    return d->m_serverRecording;
Christiaan Janssen's avatar
Christiaan Janssen committed
94 95
}

Christiaan Janssen's avatar
Christiaan Janssen committed
96
QString QmlProfilerStateManager::currentStateAsString()
Christiaan Janssen's avatar
Christiaan Janssen committed
97
{
Christiaan Janssen's avatar
Christiaan Janssen committed
98
    return stringForState(d->m_currentState);
Christiaan Janssen's avatar
Christiaan Janssen committed
99 100 101 102 103
}

void QmlProfilerStateManager::setCurrentState(QmlProfilerState newState)
{
#ifdef _DEBUG_PROFILERSTATE_
Christiaan Janssen's avatar
Christiaan Janssen committed
104
    qDebug() << "Profiler state change request from" << stringForState(d->m_currentState) << "to" << stringForState(newState);
Christiaan Janssen's avatar
Christiaan Janssen committed
105
#endif
Christiaan Janssen's avatar
Christiaan Janssen committed
106
    QTC_ASSERT(d->m_currentState != newState, /**/);
Christiaan Janssen's avatar
Christiaan Janssen committed
107 108
    switch (newState) {
    case Idle:
Christiaan Janssen's avatar
Christiaan Janssen committed
109 110 111 112
        QTC_ASSERT(d->m_currentState == AppStarting ||
                   d->m_currentState == AppStopped ||
                   d->m_currentState == AppKilled,
                   qDebug() << "from" << stringForState(d->m_currentState));
Christiaan Janssen's avatar
Christiaan Janssen committed
113 114
        break;
    case AppStarting:
Christiaan Janssen's avatar
Christiaan Janssen committed
115 116
        QTC_ASSERT(d->m_currentState == Idle,
                   qDebug() << "from" << stringForState(d->m_currentState));
Christiaan Janssen's avatar
Christiaan Janssen committed
117 118
        break;
    case AppRunning:
Christiaan Janssen's avatar
Christiaan Janssen committed
119 120
        QTC_ASSERT(d->m_currentState == AppStarting,
                   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 126
        break;
    case AppReadyToStop:
Christiaan Janssen's avatar
Christiaan Janssen committed
127 128
        QTC_ASSERT(d->m_currentState == AppStopRequested,
                   qDebug() << "from" << stringForState(d->m_currentState));
Christiaan Janssen's avatar
Christiaan Janssen committed
129 130
        break;
    case AppStopped:
Christiaan Janssen's avatar
Christiaan Janssen committed
131 132 133
        QTC_ASSERT(d->m_currentState == AppReadyToStop ||
                   d->m_currentState == AppDying,
                   qDebug() << "from" << stringForState(d->m_currentState));
Christiaan Janssen's avatar
Christiaan Janssen committed
134
        break;
135
    case AppDying:
Christiaan Janssen's avatar
Christiaan Janssen committed
136 137
        QTC_ASSERT(d->m_currentState == AppRunning,
                   qDebug() << "from" << stringForState(d->m_currentState));
Christiaan Janssen's avatar
Christiaan Janssen committed
138
        break;
139
    case AppKilled:
Christiaan Janssen's avatar
Christiaan Janssen committed
140 141
        QTC_ASSERT(d->m_currentState == AppDying,
                   qDebug() << "from" << stringForState(d->m_currentState));
142
        break;
Friedemann Kleint's avatar
Friedemann Kleint committed
143 144 145 146
    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
147 148 149
        break;
    }

Christiaan Janssen's avatar
Christiaan Janssen committed
150
    d->m_currentState = newState;
Christiaan Janssen's avatar
Christiaan Janssen committed
151 152 153 154 155 156
    emit stateChanged();
}

void QmlProfilerStateManager::setClientRecording(bool recording)
{
#ifdef _DEBUG_PROFILERSTATE_
Christiaan Janssen's avatar
Christiaan Janssen committed
157
    qDebug() << "Setting client recording flag from" << d->m_serverRecording << "to" << recording;
Christiaan Janssen's avatar
Christiaan Janssen committed
158
#endif
Christiaan Janssen's avatar
Christiaan Janssen committed
159 160
    if (d->m_clientRecording != recording) {
        d->m_clientRecording = recording;
Christiaan Janssen's avatar
Christiaan Janssen committed
161 162 163 164 165 166 167
        emit clientRecordingChanged();
    }
}

void QmlProfilerStateManager::setServerRecording(bool recording)
{
#ifdef _DEBUG_PROFILERSTATE_
Christiaan Janssen's avatar
Christiaan Janssen committed
168
    qDebug() << "Setting server recording flag from" << d->m_serverRecording << "to" << recording;
Christiaan Janssen's avatar
Christiaan Janssen committed
169
#endif
Christiaan Janssen's avatar
Christiaan Janssen committed
170 171
    if (d->m_serverRecording != recording) {
        d->m_serverRecording = recording;
Christiaan Janssen's avatar
Christiaan Janssen committed
172 173 174 175
        emit serverRecordingChanged();
    }
}

Christiaan Janssen's avatar
Christiaan Janssen committed
176 177
}
}