Merge branch 'qtquick2' of scm.dev.nokia.troll.no:qt/qtdeclarative-staging into qtquick2
[profile/ivi/qtdeclarative.git] / src / declarative / debugger / qdeclarativedebugtrace.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativedebugtrace_p.h"
43
44 #include <QtCore/qdatastream.h>
45 #include <QtCore/qurl.h>
46 #include <QtCore/qtimer.h>
47
48 Q_GLOBAL_STATIC(QDeclarativeDebugTrace, traceInstance);
49
50 // convert to a QByteArray that can be sent to the debug client
51 // use of QDataStream can skew results if m_deferredSend == false
52 //     (see tst_qdeclarativedebugtrace::trace() benchmark)
53 QByteArray QDeclarativeDebugData::toByteArray() const
54 {
55     QByteArray data;
56     //### using QDataStream is relatively expensive
57     QDataStream ds(&data, QIODevice::WriteOnly);
58     ds << time << messageType << detailType;
59     if (messageType == (int)QDeclarativeDebugTrace::RangeData)
60         ds << detailData;
61     if (messageType == (int)QDeclarativeDebugTrace::RangeLocation)
62         ds << detailData << line;
63     return data;
64 }
65
66 QDeclarativeDebugTrace::QDeclarativeDebugTrace()
67 : QDeclarativeDebugService(QLatin1String("CanvasFrameRate")),
68   m_enabled(false), m_deferredSend(true), m_messageReceived(false)
69 {
70     m_timer.start();
71     if (status() == Enabled) {
72         // wait for first message indicating whether to trace or not
73         while (!m_messageReceived)
74             waitForMessage();
75     }
76 }
77
78 void QDeclarativeDebugTrace::addEvent(EventType t)
79 {
80     if (QDeclarativeDebugService::isDebuggingEnabled()) 
81         traceInstance()->addEventImpl(t);
82 }
83
84 void QDeclarativeDebugTrace::startRange(RangeType t)
85 {
86     if (QDeclarativeDebugService::isDebuggingEnabled()) 
87         traceInstance()->startRangeImpl(t);
88 }
89
90 void QDeclarativeDebugTrace::rangeData(RangeType t, const QString &data)
91 {
92     if (QDeclarativeDebugService::isDebuggingEnabled()) 
93         traceInstance()->rangeDataImpl(t, data);
94 }
95
96 void QDeclarativeDebugTrace::rangeData(RangeType t, const QUrl &data)
97 {
98     if (QDeclarativeDebugService::isDebuggingEnabled())
99         traceInstance()->rangeDataImpl(t, data);
100 }
101
102 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QString &fileName, int line)
103 {
104     if (QDeclarativeDebugService::isDebuggingEnabled())
105         traceInstance()->rangeLocationImpl(t, fileName, line);
106 }
107
108 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QUrl &fileName, int line)
109 {
110     if (QDeclarativeDebugService::isDebuggingEnabled())
111         traceInstance()->rangeLocationImpl(t, fileName, line);
112 }
113
114 void QDeclarativeDebugTrace::endRange(RangeType t)
115 {
116     if (QDeclarativeDebugService::isDebuggingEnabled()) 
117         traceInstance()->endRangeImpl(t);
118 }
119
120 void QDeclarativeDebugTrace::addEventImpl(EventType event)
121 {
122     if (status() != Enabled || !m_enabled)
123         return;
124
125     QDeclarativeDebugData ed = {m_timer.nsecsElapsed(), (int)Event, (int)event, QString(), -1};
126     processMessage(ed);
127 }
128
129 void QDeclarativeDebugTrace::startRangeImpl(RangeType range)
130 {
131     if (status() != Enabled || !m_enabled)
132         return;
133
134     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeStart, (int)range, QString(), -1};
135     processMessage(rd);
136 }
137
138 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QString &rData)
139 {
140     if (status() != Enabled || !m_enabled)
141         return;
142
143     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeData, (int)range, rData, -1};
144     processMessage(rd);
145 }
146
147 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QUrl &rData)
148 {
149     if (status() != Enabled || !m_enabled)
150         return;
151
152     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeData, (int)range, rData.toString(QUrl::FormattingOption(0x100)), -1};
153     processMessage(rd);
154 }
155
156 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QString &fileName, int line)
157 {
158     if (status() != Enabled || !m_enabled)
159         return;
160
161     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeLocation, (int)range, fileName, line};
162     processMessage(rd);
163 }
164
165 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QUrl &fileName, int line)
166 {
167     if (status() != Enabled || !m_enabled)
168         return;
169
170     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeLocation, (int)range, fileName.toString(QUrl::FormattingOption(0x100)), line};
171     processMessage(rd);
172 }
173
174 void QDeclarativeDebugTrace::endRangeImpl(RangeType range)
175 {
176     if (status() != Enabled || !m_enabled)
177         return;
178
179     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeEnd, (int)range, QString(), -1};
180     processMessage(rd);
181 }
182
183 /*
184     Either send the message directly, or queue up
185     a list of messages to send later (via sendMessages)
186 */
187 void QDeclarativeDebugTrace::processMessage(const QDeclarativeDebugData &message)
188 {
189     if (m_deferredSend)
190         m_data.append(message);
191     else
192         sendMessage(message.toByteArray());
193 }
194
195 /*
196     Send the messages queued up by processMessage
197 */
198 void QDeclarativeDebugTrace::sendMessages()
199 {
200     if (m_deferredSend) {
201         //### this is a suboptimal way to send batched messages
202         for (int i = 0; i < m_data.count(); ++i)
203             sendMessage(m_data.at(i).toByteArray());
204         m_data.clear();
205
206         //indicate completion
207         QByteArray data;
208         QDataStream ds(&data, QIODevice::WriteOnly);
209         ds << (qint64)-1 << (int)Complete;
210         sendMessage(data);
211     }
212 }
213
214 void QDeclarativeDebugTrace::messageReceived(const QByteArray &message)
215 {
216     QByteArray rwData = message;
217     QDataStream stream(&rwData, QIODevice::ReadOnly);
218
219     stream >> m_enabled;
220
221     m_messageReceived = true;
222
223     if (!m_enabled)
224         sendMessages();
225 }