Qt 4.8
qabstracttestlogger.cpp
Go to the documentation of this file.
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 "QtTest/private/qabstracttestlogger_p.h"
43 #include "QtTest/private/qtestlog_p.h"
44 #include "QtTest/qtestassert.h"
45 
46 #include "QtCore/qbytearray.h"
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <stdarg.h>
51 
52 #ifndef Q_OS_WIN
53 #include <unistd.h>
54 #endif
55 
57 
58 namespace QTest
59 {
60  static FILE *stream = 0;
61 }
62 
63 void QAbstractTestLogger::outputString(const char *msg)
64 {
66 
67  ::fputs(msg, QTest::stream);
68  ::fflush(QTest::stream);
69 }
70 
72 {
74 
75 #if defined(Q_OS_WIN) || defined(Q_OS_INTEGRITY)
76  return true;
77 #else
78  static bool ttyoutput = isatty(fileno(QTest::stream));
79  return ttyoutput;
80 #endif
81 }
82 
83 
85 {
87 
88  const char *out = QTestLog::outputFileName();
89  if (!out) {
90  QTest::stream = stdout;
91  return;
92  }
93 #if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(Q_OS_WINCE)
94  if (::fopen_s(&QTest::stream, out, "wt")) {
95 #else
96  QTest::stream = ::fopen(out, "wt");
97  if (!QTest::stream) {
98 #endif
99  printf("Unable to open file for logging: %s", out);
100  ::exit(1);
101  }
102 }
103 
105 {
107  if (QTest::stream != stdout) {
108  fclose(QTest::stream);
109  } else {
110 #ifdef Q_OS_SYMBIAN
111  // Convenience sleep for Symbian and TRK. Without this sleep depending on the timing the
112  // user would not see the complete output because it is still pending in any of the buffers
113  // before arriving via the USB port on the development PC
114  User::AfterHighRes(2*1000*1000);
115 #endif
116  }
117  QTest::stream = 0;
118 }
119 
120 namespace QTest
121 {
122 
123 extern void filter_unprintable(char *str);
124 
129 int qt_asprintf(QTestCharBuffer *str, const char *format, ...)
130 {
131  static const int MAXSIZE = 1024*1024*2;
132 
133  Q_ASSERT(str);
134 
135  int size = str->size();
136 
137  va_list ap;
138  int res = 0;
139 
140  for (;;) {
141  va_start(ap, format);
142  res = qvsnprintf(str->data(), size, format, ap);
143  va_end(ap);
144  str->data()[size - 1] = '\0';
145  if (res >= 0 && res < size) {
146  // We succeeded
147  break;
148  }
149  // buffer wasn't big enough, try again.
150  // Note, we're assuming that a result of -1 is always due to running out of space.
151  size *= 2;
152  if (size > MAXSIZE) {
153  break;
154  }
155  if (!str->reset(size))
156  break; // out of memory - take what we have
157  }
158 
159  filter_unprintable(str->data());
160 
161  return res;
162 }
163 
164 }
165 
void filter_unprintable(char *str)
Definition: qtestcase.cpp:949
static void outputString(const char *msg)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
int qt_asprintf(QTestCharBuffer *str, const char *format,...)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static const char * outputFileName()
Definition: qtestlog.cpp:389
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static FILE * stream
Q_CORE_EXPORT int qvsnprintf(char *str, size_t n, const char *fmt, va_list ap)
#define QTEST_ASSERT(cond)
Definition: qtestassert.h:53
bool reset(int newSize)
The QTest namespace contains all the functions and declarations that are related to the QTestLib tool...