Initial import from the monolithic Qt.
[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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
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_qperformancetimer::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)
69 {
70     m_timer.start();
71 }
72
73 void QDeclarativeDebugTrace::addEvent(EventType t)
74 {
75     if (QDeclarativeDebugService::isDebuggingEnabled()) 
76         traceInstance()->addEventImpl(t);
77 }
78
79 void QDeclarativeDebugTrace::startRange(RangeType t)
80 {
81     if (QDeclarativeDebugService::isDebuggingEnabled()) 
82         traceInstance()->startRangeImpl(t);
83 }
84
85 void QDeclarativeDebugTrace::rangeData(RangeType t, const QString &data)
86 {
87     if (QDeclarativeDebugService::isDebuggingEnabled()) 
88         traceInstance()->rangeDataImpl(t, data);
89 }
90
91 void QDeclarativeDebugTrace::rangeData(RangeType t, const QUrl &data)
92 {
93     if (QDeclarativeDebugService::isDebuggingEnabled())
94         traceInstance()->rangeDataImpl(t, data);
95 }
96
97 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QString &fileName, int line)
98 {
99     if (QDeclarativeDebugService::isDebuggingEnabled())
100         traceInstance()->rangeLocationImpl(t, fileName, line);
101 }
102
103 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QUrl &fileName, int line)
104 {
105     if (QDeclarativeDebugService::isDebuggingEnabled())
106         traceInstance()->rangeLocationImpl(t, fileName, line);
107 }
108
109 void QDeclarativeDebugTrace::endRange(RangeType t)
110 {
111     if (QDeclarativeDebugService::isDebuggingEnabled()) 
112         traceInstance()->endRangeImpl(t);
113 }
114
115 void QDeclarativeDebugTrace::addEventImpl(EventType event)
116 {
117     if (status() != Enabled || !m_enabled)
118         return;
119
120     QDeclarativeDebugData ed = {m_timer.elapsed(), (int)Event, (int)event, QString(), -1};
121     processMessage(ed);
122 }
123
124 void QDeclarativeDebugTrace::startRangeImpl(RangeType range)
125 {
126     if (status() != Enabled || !m_enabled)
127         return;
128
129     QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeStart, (int)range, QString(), -1};
130     processMessage(rd);
131 }
132
133 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QString &rData)
134 {
135     if (status() != Enabled || !m_enabled)
136         return;
137
138     QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeData, (int)range, rData, -1};
139     processMessage(rd);
140 }
141
142 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QUrl &rData)
143 {
144     if (status() != Enabled || !m_enabled)
145         return;
146
147     QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeData, (int)range, rData.toString(QUrl::FormattingOption(0x100)), -1};
148     processMessage(rd);
149 }
150
151 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QString &fileName, int line)
152 {
153     if (status() != Enabled || !m_enabled)
154         return;
155
156     QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeLocation, (int)range, fileName, line};
157     processMessage(rd);
158 }
159
160 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QUrl &fileName, int line)
161 {
162     if (status() != Enabled || !m_enabled)
163         return;
164
165     QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeLocation, (int)range, fileName.toString(QUrl::FormattingOption(0x100)), line};
166     processMessage(rd);
167 }
168
169 void QDeclarativeDebugTrace::endRangeImpl(RangeType range)
170 {
171     if (status() != Enabled || !m_enabled)
172         return;
173
174     QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeEnd, (int)range, QString(), -1};
175     processMessage(rd);
176 }
177
178 /*
179     Either send the message directly, or queue up
180     a list of messages to send later (via sendMessages)
181 */
182 void QDeclarativeDebugTrace::processMessage(const QDeclarativeDebugData &message)
183 {
184     if (m_deferredSend)
185         m_data.append(message);
186     else
187         sendMessage(message.toByteArray());
188 }
189
190 /*
191     Send the messages queued up by processMessage
192 */
193 void QDeclarativeDebugTrace::sendMessages()
194 {
195     if (m_deferredSend) {
196         //### this is a suboptimal way to send batched messages
197         for (int i = 0; i < m_data.count(); ++i)
198             sendMessage(m_data.at(i).toByteArray());
199         m_data.clear();
200
201         //indicate completion
202         QByteArray data;
203         QDataStream ds(&data, QIODevice::WriteOnly);
204         ds << (qint64)-1 << (int)Complete;
205         sendMessage(data);
206     }
207 }
208
209 void QDeclarativeDebugTrace::messageReceived(const QByteArray &message)
210 {
211     QByteArray rwData = message;
212     QDataStream stream(&rwData, QIODevice::ReadOnly);
213
214     stream >> m_enabled;
215
216     if (!m_enabled)
217         sendMessages();
218 }