source: trunk/src/testlib/qplaintestlogger.cpp@ 504

Last change on this file since 504 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 15.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtTest module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "QtTest/private/qtestresult_p.h"
43#include "QtTest/qtestassert.h"
44#include "QtTest/private/qtestlog_p.h"
45#include "QtTest/private/qplaintestlogger_p.h"
46#include "QtTest/private/qbenchmark_p.h"
47
48#include <stdarg.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52
53#ifdef Q_OS_WIN
54#include "windows.h"
55#endif
56
57#ifdef Q_OS_WINCE
58#include <QtCore/QString>
59#endif
60
61#include <QtCore/QByteArray>
62#include <QtCore/qmath.h>
63
64QT_BEGIN_NAMESPACE
65
66namespace QTest {
67
68#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
69
70 static CRITICAL_SECTION outputCriticalSection;
71 static HANDLE hConsole = INVALID_HANDLE_VALUE;
72 static WORD consoleAttributes = 0;
73
74 static const char *qWinColoredMsg(int prefix, int color, const char *msg)
75 {
76 if (!hConsole)
77 return msg;
78
79 WORD attr = consoleAttributes & ~(FOREGROUND_GREEN | FOREGROUND_BLUE
80 | FOREGROUND_RED | FOREGROUND_INTENSITY);
81 if (prefix)
82 attr |= FOREGROUND_INTENSITY;
83 if (color == 32)
84 attr |= FOREGROUND_GREEN;
85 if (color == 36)
86 attr |= FOREGROUND_BLUE | FOREGROUND_GREEN;
87 if (color == 31)
88 attr |= FOREGROUND_RED | FOREGROUND_INTENSITY;
89 if (color == 37)
90 attr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
91 if (color == 33)
92 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
93 SetConsoleTextAttribute(hConsole, attr);
94 printf(msg);
95 SetConsoleTextAttribute(hConsole, consoleAttributes);
96 return "";
97 }
98
99# define COLORED_MSG(prefix, color, msg) colored ? qWinColoredMsg(prefix, color, msg) : msg
100#else
101# define COLORED_MSG(prefix, color, msg) colored && QAbstractTestLogger::isTtyOutput() ? "\033["#prefix";"#color"m" msg "\033[0m" : msg
102#endif
103
104 static const char *incidentType2String(QAbstractTestLogger::IncidentTypes type)
105 {
106 static bool colored = (!qgetenv("QTEST_COLORED").isEmpty());
107 switch (type) {
108 case QAbstractTestLogger::Pass:
109 return COLORED_MSG(0, 32, "PASS "); //green
110 case QAbstractTestLogger::XFail:
111 return COLORED_MSG(1, 32, "XFAIL "); //light green
112 case QAbstractTestLogger::Fail:
113 return COLORED_MSG(0, 31, "FAIL! "); //red
114 case QAbstractTestLogger::XPass:
115 return COLORED_MSG(0, 31, "XPASS "); //red, too
116 }
117 return "??????";
118 }
119
120 static const char *benchmarkResult2String()
121 {
122 static bool colored = (!qgetenv("QTEST_COLORED").isEmpty());
123 return COLORED_MSG(0, 36, "RESULT "); // cyan
124 }
125
126 static const char *messageType2String(QAbstractTestLogger::MessageTypes type)
127 {
128 static bool colored = (!qgetenv("QTEST_COLORED").isEmpty());
129 switch (type) {
130 case QAbstractTestLogger::Skip:
131 return COLORED_MSG(0, 37, "SKIP "); //white
132 case QAbstractTestLogger::Warn:
133 return COLORED_MSG(0, 33, "WARNING"); // yellow
134 case QAbstractTestLogger::QWarning:
135 return COLORED_MSG(1, 33, "QWARN ");
136 case QAbstractTestLogger::QDebug:
137 return COLORED_MSG(1, 33, "QDEBUG ");
138 case QAbstractTestLogger::QSystem:
139 return COLORED_MSG(1, 33, "QSYSTEM");
140 case QAbstractTestLogger::QFatal:
141 return COLORED_MSG(0, 31, "QFATAL "); // red
142 case QAbstractTestLogger::Info:
143 return "INFO "; // no coloring
144 }
145 return "??????";
146 }
147
148 static void outputMessage(const char *str)
149 {
150#if defined(Q_OS_WINCE)
151 int length = strlen(str);
152 for (int pos = 0; pos < length; pos +=255) {
153 QString uniText = QString::fromLatin1(str + pos, 255);
154 OutputDebugStringW((const LPCWSTR) uniText.utf16());
155 }
156 if (QTestLog::outputFileName())
157#elif defined(Q_OS_WIN)
158 EnterCriticalSection(&outputCriticalSection);
159 // OutputDebugString is not threadsafe
160 OutputDebugStringA(str);
161 LeaveCriticalSection(&outputCriticalSection);
162#endif
163 QAbstractTestLogger::outputString(str);
164 }
165
166 static void printMessage(const char *type, const char *msg, const char *file = 0, int line = 0)
167 {