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 ** 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.
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.
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.
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.
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_qdeclarativedebugtrace::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), m_messageReceived(false)
71 if (status() == Enabled) {
72 // wait for first message indicating whether to trace or not
73 while (!m_messageReceived)
78 void QDeclarativeDebugTrace::addEvent(EventType t)
80 if (QDeclarativeDebugService::isDebuggingEnabled())
81 traceInstance()->addEventImpl(t);
84 void QDeclarativeDebugTrace::startRange(RangeType t)
86 if (QDeclarativeDebugService::isDebuggingEnabled())
87 traceInstance()->startRangeImpl(t);
90 void QDeclarativeDebugTrace::rangeData(RangeType t, const QString &data)
92 if (QDeclarativeDebugService::isDebuggingEnabled())
93 traceInstance()->rangeDataImpl(t, data);
96 void QDeclarativeDebugTrace::rangeData(RangeType t, const QUrl &data)
98 if (QDeclarativeDebugService::isDebuggingEnabled())
99 traceInstance()->rangeDataImpl(t, data);
102 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QString &fileName, int line)
104 if (QDeclarativeDebugService::isDebuggingEnabled())
105 traceInstance()->rangeLocationImpl(t, fileName, line);
108 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QUrl &fileName, int line)
110 if (QDeclarativeDebugService::isDebuggingEnabled())
111 traceInstance()->rangeLocationImpl(t, fileName, line);
114 void QDeclarativeDebugTrace::endRange(RangeType t)
116 if (QDeclarativeDebugService::isDebuggingEnabled())
117 traceInstance()->endRangeImpl(t);
120 void QDeclarativeDebugTrace::addEventImpl(EventType event)
122 if (status() != Enabled || !m_enabled)
125 QDeclarativeDebugData ed = {m_timer.nsecsElapsed(), (int)Event, (int)event, QString(), -1};
129 void QDeclarativeDebugTrace::startRangeImpl(RangeType range)
131 if (status() != Enabled || !m_enabled)
134 QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeStart, (int)range, QString(), -1};
138 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QString &rData)
140 if (status() != Enabled || !m_enabled)
143 QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeData, (int)range, rData, -1};
147 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QUrl &rData)
149 if (status() != Enabled || !m_enabled)
152 QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeData, (int)range, rData.toString(QUrl::FormattingOption(0x100)), -1};
156 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QString &fileName, int line)
158 if (status() != Enabled || !m_enabled)
161 QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeLocation, (int)range, fileName, line};
165 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QUrl &fileName, int line)
167 if (status() != Enabled || !m_enabled)
170 QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeLocation, (int)range, fileName.toString(QUrl::FormattingOption(0x100)), line};
174 void QDeclarativeDebugTrace::endRangeImpl(RangeType range)
176 if (status() != Enabled || !m_enabled)
179 QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeEnd, (int)range, QString(), -1};
184 Either send the message directly, or queue up
185 a list of messages to send later (via sendMessages)
187 void QDeclarativeDebugTrace::processMessage(const QDeclarativeDebugData &message)
190 m_data.append(message);
192 sendMessage(message.toByteArray());
196 Send the messages queued up by processMessage
198 void QDeclarativeDebugTrace::sendMessages()
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());
206 //indicate completion
208 QDataStream ds(&data, QIODevice::WriteOnly);
209 ds << (qint64)-1 << (int)Complete;
214 void QDeclarativeDebugTrace::messageReceived(const QByteArray &message)
216 QByteArray rwData = message;
217 QDataStream stream(&rwData, QIODevice::ReadOnly);
221 m_messageReceived = true;