1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qdeclarativedebugtrace_p.h"
44 #include <QtCore/qdatastream.h>
45 #include <QtCore/qurl.h>
46 #include <QtCore/qtimer.h>
48 Q_GLOBAL_STATIC(QDeclarativeDebugTrace, traceInstance);
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
56 //### using QDataStream is relatively expensive
57 QDataStream ds(&data, QIODevice::WriteOnly);
58 ds << time << messageType << detailType;
59 if (messageType == (int)QDeclarativeDebugTrace::RangeData)
61 if (messageType == (int)QDeclarativeDebugTrace::RangeLocation)
62 ds << detailData << line;
66 QDeclarativeDebugTrace::QDeclarativeDebugTrace()
67 : QDeclarativeDebugService(QLatin1String("CanvasFrameRate")),
68 m_enabled(false), m_deferredSend(true)
73 void QDeclarativeDebugTrace::addEvent(EventType t)
75 if (QDeclarativeDebugService::isDebuggingEnabled())
76 traceInstance()->addEventImpl(t);
79 void QDeclarativeDebugTrace::startRange(RangeType t)
81 if (QDeclarativeDebugService::isDebuggingEnabled())
82 traceInstance()->startRangeImpl(t);
85 void QDeclarativeDebugTrace::rangeData(RangeType t, const QString &data)
87 if (QDeclarativeDebugService::isDebuggingEnabled())
88 traceInstance()->rangeDataImpl(t, data);
91 void QDeclarativeDebugTrace::rangeData(RangeType t, const QUrl &data)
93 if (QDeclarativeDebugService::isDebuggingEnabled())
94 traceInstance()->rangeDataImpl(t, data);
97 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QString &fileName, int line)
99 if (QDeclarativeDebugService::isDebuggingEnabled())
100 traceInstance()->rangeLocationImpl(t, fileName, line);
103 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QUrl &fileName, int line)
105 if (QDeclarativeDebugService::isDebuggingEnabled())
106 traceInstance()->rangeLocationImpl(t, fileName, line);
109 void QDeclarativeDebugTrace::endRange(RangeType t)
111 if (QDeclarativeDebugService::isDebuggingEnabled())
112 traceInstance()->endRangeImpl(t);
115 void QDeclarativeDebugTrace::addEventImpl(EventType event)
117 if (status() != Enabled || !m_enabled)
120 QDeclarativeDebugData ed = {m_timer.elapsed(), (int)Event, (int)event, QString(), -1};
124 void QDeclarativeDebugTrace::startRangeImpl(RangeType range)
126 if (status() != Enabled || !m_enabled)
129 QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeStart, (int)range, QString(), -1};
133 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QString &rData)
135 if (status() != Enabled || !m_enabled)
138 QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeData, (int)range, rData, -1};
142 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QUrl &rData)
144 if (status() != Enabled || !m_enabled)
147 QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeData, (int)range, rData.toString(QUrl::FormattingOption(0x100)), -1};
151 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QString &fileName, int line)
153 if (status() != Enabled || !m_enabled)
156 QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeLocation, (int)range, fileName, line};
160 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QUrl &fileName, int line)
162 if (status() != Enabled || !m_enabled)
165 QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeLocation, (int)range, fileName.toString(QUrl::FormattingOption(0x100)), line};
169 void QDeclarativeDebugTrace::endRangeImpl(RangeType range)
171 if (status() != Enabled || !m_enabled)
174 QDeclarativeDebugData rd = {m_timer.elapsed(), (int)RangeEnd, (int)range, QString(), -1};
179 Either send the message directly, or queue up
180 a list of messages to send later (via sendMessages)
182 void QDeclarativeDebugTrace::processMessage(const QDeclarativeDebugData &message)
185 m_data.append(message);
187 sendMessage(message.toByteArray());
191 Send the messages queued up by processMessage
193 void QDeclarativeDebugTrace::sendMessages()
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());
201 //indicate completion
203 QDataStream ds(&data, QIODevice::WriteOnly);
204 ds << (qint64)-1 << (int)Complete;
209 void QDeclarativeDebugTrace::messageReceived(const QByteArray &message)
211 QByteArray rwData = message;
212 QDataStream stream(&rwData, QIODevice::ReadOnly);