1497750b350851b1f981c22a14d679e72538e651
[profile/ivi/qtdeclarative.git] / src / declarative / qml / ftw / qdeclarativetrace.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 "qdeclarativetrace_p.h"
43
44 #ifdef QML_ENABLE_TRACE
45 #include <stdio.h>
46 #endif
47
48 QT_BEGIN_NAMESPACE
49
50 #ifdef QML_ENABLE_TRACE
51
52 QDeclarativeTrace::Pool QDeclarativeTrace::logPool;
53 QDeclarativeTrace::Entry *QDeclarativeTrace::first = 0;
54 QDeclarativeTrace::Entry *QDeclarativeTrace::last = 0;
55
56 static qint64 toNsecs(QDeclarativeTrace::TimeType time)
57 {
58 #ifdef Q_OS_MAC
59     static mach_timebase_info_data_t info = {0,0};
60     if (info.denom == 0)
61         mach_timebase_info(&info);
62     return time * info.numer / info.denom;
63 #else
64     qint64 rv = time.tv_sec * 1000000000 + time.tv_nsec;
65     return rv;
66 #endif
67 }
68
69 QDeclarativeTrace::Pool::Pool()
70 {
71     first = New<Entry>();
72     last = first;
73 }
74
75 QDeclarativeTrace::Pool::~Pool()
76 {
77     char buffer[128];
78     sprintf(buffer, "qml.%d.log", ::getpid());
79     FILE *out = fopen(buffer, "w");
80     if (!out) {
81         fprintf (stderr, "QML Log: Could not open %s\n", buffer);
82         return;
83     } else {
84         fprintf (stderr, "QML Log: Writing log to %s\n", buffer);
85     }
86
87     QDeclarativeTrace::Entry *cur = QDeclarativeTrace::first;
88     QByteArray indent;
89     int depth = -1;
90
91     qint64 firstTime = -1;
92
93     while (cur) {
94
95         switch (cur->type) {
96         case QDeclarativeTrace::Entry::RangeStart: {
97             RangeStart *rs = static_cast<QDeclarativeTrace::RangeStart *>(cur);
98
99             qint64 nsecs = toNsecs(rs->time);
100
101             if (firstTime == -1)
102                 firstTime = nsecs;
103
104             nsecs -= firstTime;
105
106             depth++;
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);
110             } break;
111         case QDeclarativeTrace::Entry::RangeEnd:
112             depth--;
113             indent = QByteArray(depth * 4, ' ');
114             break;
115         case QDeclarativeTrace::Entry::Detail:
116             fprintf(out, "%s  %s\n", indent.constData(),
117                     static_cast<QDeclarativeTrace::Detail *>(cur)->description);
118             break;
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);
123             break;
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());
129             } break;
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());
135             } break;
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));
141             } break;
142         case QDeclarativeTrace::Entry::Null:
143         default:
144             break;
145         }
146         cur = cur->next;
147     }
148     fclose(out);
149 }
150
151 #endif
152
153 QT_END_NAMESPACE
154