83fa145f9135fd4cf58d6cc95994ac82e2abdaee
[profile/ivi/qtdeclarative.git] / src / declarative / qml / ftw / qdeclarativetrace_p.h
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 #ifndef QDECLARATIVETRACE_P_H
43 #define QDECLARATIVETRACE_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <QtCore/qglobal.h>
57 #include <private/qdeclarativepool_p.h>
58
59 // #define QML_ENABLE_TRACE
60
61 #if defined(QML_ENABLE_TRACE) && defined(Q_OS_MAC)
62 #include <mach/mach_time.h>
63 #endif
64
65 QT_BEGIN_NAMESPACE
66
67 class QUrl;
68 class QDeclarativeTrace
69 {
70 public:
71     inline QDeclarativeTrace(const char *desc);
72     inline ~QDeclarativeTrace();
73
74     inline void addDetail(const char *);
75     inline void addDetail(const char *, int);
76     inline void addDetail(const char *, const QString &);
77     inline void addDetail(const char *, const QUrl &);
78
79     inline void event(const char *desc);
80
81 #ifdef QML_ENABLE_TRACE
82
83 #ifdef Q_OS_MAC
84     typedef uint64_t TimeType;
85 #else
86     typedef timespec TimeType;
87 #endif
88
89     struct Entry : public QDeclarativePool::POD {
90         enum Type { Null, RangeStart, RangeEnd, Detail, IntDetail, StringDetail, UrlDetail, Event };
91         inline Entry();
92         inline Entry(Type);
93         Type type;
94         Entry *next;
95     };
96     struct RangeEnd : public Entry {
97         inline RangeEnd();
98         TimeType time;
99     };
100     struct RangeStart : public Entry {
101         inline RangeStart();
102         const char *description;
103         TimeType time;
104         QDeclarativeTrace::RangeEnd *end;
105     };
106     struct Detail : public Entry {
107         inline Detail();
108         inline Detail(Type t);
109         const char *description;
110     };
111     struct IntDetail : public Detail {
112         inline IntDetail();
113         int value;
114     };
115     struct StringDetail : public Detail {
116         inline StringDetail();
117         QString *value;
118     };
119     struct UrlDetail : public Detail {
120         inline UrlDetail();
121         QUrl *value;
122     };
123     struct Event : public Entry {
124         inline Event();
125         const char *description;
126         TimeType time;
127         QDeclarativeTrace::RangeStart *start;
128     };
129
130     struct Pool : public QDeclarativePool {
131         Pool();
132         ~Pool();
133     };
134
135     static Pool logPool;
136     static Entry *first;
137     static Entry *last;
138
139 private:
140     RangeStart *start;
141
142     static TimeType gettime() {
143 #ifdef Q_OS_MAC
144         return mach_absolute_time();
145 #else
146         TimeType ts;
147         clock_gettime(CLOCK_MONOTONIC, &ts);
148         return ts;
149 #endif
150     }
151 #endif
152 };
153
154 #ifdef QML_ENABLE_TRACE
155 QDeclarativeTrace::Entry::Entry()
156 : type(Null), next(0)
157 {
158 }
159
160 QDeclarativeTrace::Entry::Entry(Type type)
161 : type(type), next(0)
162 {
163     QDeclarativeTrace::last->next = this;
164     QDeclarativeTrace::last = this;
165 }
166
167 QDeclarativeTrace::RangeEnd::RangeEnd()
168 : QDeclarativeTrace::Entry(QDeclarativeTrace::Entry::RangeEnd),
169   time(gettime())
170 {
171 }
172
173 QDeclarativeTrace::RangeStart::RangeStart()
174 : QDeclarativeTrace::Entry(QDeclarativeTrace::Entry::RangeStart),
175   description(0), time(gettime())
176 {
177 }
178
179 QDeclarativeTrace::Detail::Detail()
180 : QDeclarativeTrace::Entry(QDeclarativeTrace::Entry::Detail),
181   description(0)
182 {
183 }
184
185 QDeclarativeTrace::Detail::Detail(Type type)
186 : QDeclarativeTrace::Entry(type), description(0)
187 {
188 }
189
190 QDeclarativeTrace::IntDetail::IntDetail()
191 : QDeclarativeTrace::Detail(QDeclarativeTrace::Entry::IntDetail),
192   value(0)
193 {
194 }
195
196 QDeclarativeTrace::StringDetail::StringDetail()
197 : QDeclarativeTrace::Detail(QDeclarativeTrace::Entry::StringDetail),
198   value(0)
199 {
200 }
201
202 QDeclarativeTrace::UrlDetail::UrlDetail()
203 : QDeclarativeTrace::Detail(QDeclarativeTrace::Entry::UrlDetail),
204   value(0)
205 {
206 }
207
208 QDeclarativeTrace::Event::Event()
209 : QDeclarativeTrace::Entry(QDeclarativeTrace::Entry::Event),
210   description(0), time(gettime()), start(0)
211 {
212 }
213 #endif
214
215 QDeclarativeTrace::QDeclarativeTrace(const char *desc)
216 {
217 #ifdef QML_ENABLE_TRACE
218     RangeStart *e = logPool.New<RangeStart>();
219     e->description = desc;
220     e->end = 0;
221     start = e;
222 #else
223     Q_UNUSED(desc);
224 #endif
225 }
226
227 QDeclarativeTrace::~QDeclarativeTrace()
228 {
229 #ifdef QML_ENABLE_TRACE
230     RangeEnd *e = logPool.New<RangeEnd>();
231     start->end = e;
232 #endif
233 }
234
235 void QDeclarativeTrace::addDetail(const char *desc)
236 {
237 #ifdef QML_ENABLE_TRACE
238     Detail *e = logPool.New<Detail>();
239     e->description = desc;
240 #else
241     Q_UNUSED(desc);
242 #endif
243 }
244
245 void QDeclarativeTrace::addDetail(const char *desc, int v)
246 {
247 #ifdef QML_ENABLE_TRACE
248     IntDetail *e = logPool.New<IntDetail>();
249     e->description = desc;
250     e->value = v;
251 #else
252     Q_UNUSED(desc);
253     Q_UNUSED(v);
254 #endif
255 }
256
257 void QDeclarativeTrace::addDetail(const char *desc, const QString &v)
258 {
259 #ifdef QML_ENABLE_TRACE
260     StringDetail *e = logPool.New<StringDetail>();
261     e->description = desc;
262     e->value = logPool.NewString(v);
263 #else
264     Q_UNUSED(desc);
265     Q_UNUSED(v);
266 #endif
267 }
268
269 void QDeclarativeTrace::addDetail(const char *desc, const QUrl &v)
270 {
271 #ifdef QML_ENABLE_TRACE
272     UrlDetail *e = logPool.New<UrlDetail>();
273     e->description = desc;
274     e->value = logPool.NewUrl(v);
275 #else
276     Q_UNUSED(desc);
277     Q_UNUSED(v);
278 #endif
279 }
280
281 void QDeclarativeTrace::event(const char *desc)
282 {
283 #ifdef QML_ENABLE_TRACE
284     Event *e = logPool.New<Event>();
285     e->start = start;
286     e->description = desc;
287 #else
288     Q_UNUSED(desc);
289 #endif
290 }
291
292 QT_END_NAMESPACE
293
294 #endif // QDECLARATIVETRACE_P_H