1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 "qdeclarativetrace_p.h"
44 #ifdef QML_ENABLE_TRACE
50 #ifdef QML_ENABLE_TRACE
52 QDeclarativeTrace::Pool QDeclarativeTrace::logPool;
53 QDeclarativeTrace::Entry *QDeclarativeTrace::first = 0;
54 QDeclarativeTrace::Entry *QDeclarativeTrace::last = 0;
56 static qint64 toNsecs(QDeclarativeTrace::TimeType time)
59 static mach_timebase_info_data_t info = {0,0};
61 mach_timebase_info(&info);
62 return time * info.numer / info.denom;
64 qint64 rv = time.tv_sec * 1000000000 + time.tv_nsec;
69 QDeclarativeTrace::Pool::Pool()
75 QDeclarativeTrace::Pool::~Pool()
78 sprintf(buffer, "qml.%d.log", ::getpid());
79 FILE *out = fopen(buffer, "w");
81 fprintf (stderr, "QML Log: Could not open %s\n", buffer);
84 fprintf (stderr, "QML Log: Writing log to %s\n", buffer);
87 QDeclarativeTrace::Entry *cur = QDeclarativeTrace::first;
91 qint64 firstTime = -1;
96 case QDeclarativeTrace::Entry::RangeStart: {
97 RangeStart *rs = static_cast<QDeclarativeTrace::RangeStart *>(cur);
99 qint64 nsecs = toNsecs(rs->time);
107 indent = QByteArray(depth * 4, ' ');
108 fprintf(out, "%s%s @%lld (%lld ns)\n", indent.constData(),
109 rs->description, nsecs, toNsecs(rs->end->time) - nsecs - firstTime);
111 case QDeclarativeTrace::Entry::RangeEnd:
113 indent = QByteArray(depth * 4, ' ');
115 case QDeclarativeTrace::Entry::Detail:
116 fprintf(out, "%s %s\n", indent.constData(),
117 static_cast<QDeclarativeTrace::Detail *>(cur)->description);
119 case QDeclarativeTrace::Entry::IntDetail:
120 fprintf(out, "%s %s: %d\n", indent.constData(),
121 static_cast<QDeclarativeTrace::Detail *>(cur)->description,
122 static_cast<QDeclarativeTrace::IntDetail *>(cur)->value);
124 case QDeclarativeTrace::Entry::StringDetail: {
125 QByteArray vLatin1 = static_cast<QDeclarativeTrace::StringDetail *>(cur)->value->toLatin1();
126 fprintf(out, "%s %s: %s\n", indent.constData(),
127 static_cast<QDeclarativeTrace::Detail *>(cur)->description,
128 vLatin1.constData());
130 case QDeclarativeTrace::Entry::UrlDetail: {
131 QByteArray vLatin1 = static_cast<QDeclarativeTrace::UrlDetail *>(cur)->value->toString().toLatin1();
132 fprintf(out, "%s %s: %s\n", indent.constData(),
133 static_cast<QDeclarativeTrace::Detail *>(cur)->description,
134 vLatin1.constData());
136 case QDeclarativeTrace::Entry::Event: {
137 Event *ev = static_cast<QDeclarativeTrace::Event *>(cur);
138 qint64 nsecs = toNsecs(ev->time) - firstTime;
139 fprintf(out, "%s + %s @%lld +%lld ns\n", indent.constData(),
140 ev->description, nsecs, nsecs - (toNsecs(ev->start->time) - firstTime));
142 case QDeclarativeTrace::Entry::Null: