1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qtestbasicstreamer.h"
43#include "qtestlogger_p.h"
44#include "qtestelement.h"
45#include "qtestelementattribute.h"
46#include "QtTest/private/qtestlog_p.h"
47#include "qtestassert.h"
48
49#include <stdio.h>
50#include <stdlib.h>
51
52#ifndef Q_OS_WIN
53#include <unistd.h>
54#endif
55
56QT_BEGIN_NAMESPACE
57
58namespace QTest
59{
60 static FILE *stream = 0;
61}
62
63QTestBasicStreamer::QTestBasicStreamer()
64 :testLogger(0)
65{
66}
67
68QTestBasicStreamer::~QTestBasicStreamer()
69{}
70
71void QTestBasicStreamer::formatStart(const QTestElement *element, QTestCharBuffer *formatted) const
72{
73 if(!element || !formatted )
74 return;
75 formatted->data()[0] = '\0';
76}
77
78void QTestBasicStreamer::formatEnd(const QTestElement *element, QTestCharBuffer *formatted) const
79{
80 if(!element || !formatted )
81 return;
82 formatted->data()[0] = '\0';
83}
84
85void QTestBasicStreamer::formatBeforeAttributes(const QTestElement *element, QTestCharBuffer *formatted) const
86{
87 if(!element || !formatted )
88 return;
89 formatted->data()[0] = '\0';
90}
91
92void QTestBasicStreamer::formatAfterAttributes(const QTestElement *element, QTestCharBuffer *formatted) const
93{
94 if(!element || !formatted )
95 return;
96 formatted->data()[0] = '\0';
97}
98
99void QTestBasicStreamer::formatAttributes(const QTestElement *, const QTestElementAttribute *attribute, QTestCharBuffer *formatted) const
100{
101 if(!attribute || !formatted )
102 return;
103 formatted->data()[0] = '\0';
104}
105
106void QTestBasicStreamer::output(QTestElement *element) const
107{
108 if(!element)
109 return;
110
111 outputElements(element);
112}
113
114void QTestBasicStreamer::outputElements(QTestElement *element, bool) const
115{
116 QTestCharBuffer buf;
117 bool hasChildren;
118 /*
119 Elements are in reverse order of occurrence, so start from the end and work
120 our way backwards.
121 */
122 while (element && element->nextElement()) {
123 element = element->nextElement();
124 }
125 while (element) {
126 hasChildren = element->childElements();
127
128 formatStart(element, &buf);
129 outputString(buf.data());
130
131 formatBeforeAttributes(element, &buf);
132 outputString(buf.data());
133
134 outputElementAttributes(element, element->attributes());
135
136 formatAfterAttributes(element, &buf);
137 outputString(buf.data());
138
139 if(hasChildren)
140 outputElements(element->childElements(), true);
141
142 formatEnd(element, &buf);
143 outputString(buf.data());
144
145 element = element->previousElement();
146 }
147}
148
149void QTestBasicStreamer::outputElementAttributes(const QTestElement* element, QTestElementAttribute *attribute) const
150{
151 QTestCharBuffer buf;
152 while(attribute){
153 formatAttributes(element, attribute, &buf);
154 outputString(buf.data());
155 attribute = attribute->nextElement();
156 }
157}
158
159void QTestBasicStreamer::outputString(const char *msg) const
160{
161 QTEST_ASSERT(QTest::stream);
162
163 ::fputs(msg, QTest::stream);
164 ::fflush(QTest::stream);
165}
166
167void QTestBasicStreamer::startStreaming()
168{
169 QTEST_ASSERT(!QTest::stream);
170
171 const char *out = QTestLog::outputFileName();
172 if (!out) {
173 QTest::stream = stdout;
174 return;
175 }
176 #if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(Q_OS_WINCE)
177 if (::fopen_s(&QTest::stream, out, "wt")) {
178 #else
179 QTest::stream = ::fopen(out, "wt");
180 if (!QTest::stream) {
181 #endif
182 printf("Unable to open file for logging: %s", out);
183 ::exit(1);
184 }
185}
186
187bool QTestBasicStreamer::isTtyOutput()
188{
189 QTEST_ASSERT(QTest::stream);
190
191#if defined(Q_OS_WIN) || defined(Q_OS_INTEGRITY)
192 return true;
193#else
194 static bool ttyoutput = isatty(fileno(QTest::stream));
195 return ttyoutput;
196#endif
197}
198
199void QTestBasicStreamer::stopStreaming()
200{
201 QTEST_ASSERT(QTest::stream);
202 if (QTest::stream != stdout)
203 fclose(QTest::stream);
204
205 QTest::stream = 0;
206}
207
208void QTestBasicStreamer::setLogger(const QTestLogger *tstLogger)
209{
210 testLogger = tstLogger;
211}
212
213const QTestLogger *QTestBasicStreamer::logger() const
214{
215 return testLogger;
216}
217
218QT_END_NAMESPACE
219
220