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

Last change on this file since 755 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 16.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtTest module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia 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#if defined(Q_OS_SYMBIAN)
58#include <e32debug.h>
59#endif
60
61#ifdef Q_OS_WINCE
62#include <QtCore/QString>
63#endif
64
65#include <QtCore/QByteArray>
66#include <QtCore/qmath.h>
67
68QT_BEGIN_NAMESPACE
69
70namespace QTest {
71
72#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
73
74 static CRITICAL_SECTION outputCriticalSection;
75 static HANDLE hConsole = INVALID_HANDLE_VALUE;
76 static WORD consoleAttributes = 0;
77
78 static const char *qWinColoredMsg(int prefix, int color, const char *msg)
79 {
80 if (!hConsole)
81 return msg;
82
83 WORD attr = consoleAttributes & ~(FOREGROUND_GREEN | FOREGROUND_BLUE
84 | FOREGROUND_RED | FOREGROUND_INTENSITY);
85 if (prefix)
86 attr |= FOREGROUND_INTENSITY;
87 if (color == 32)
88 attr |= FOREGROUND_GREEN;
89 if (color == 36)
90 attr |= FOREGROUND_BLUE | FOREGROUND_GREEN;
91 if (color == 31)
92 attr |= FOREGROUND_RED | FOREGROUND_INTENSITY;
93 if (color == 37)
94 attr |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
95 if (color == 33)
96 attr |= FOREGROUND_RED | FOREGROUND_GREEN;
97 SetConsoleTextAttribute(hConsole, attr);
98 printf(msg);
99 SetConsoleTextAttribute(hConsole, consoleAttributes);
100 return "";
101 }
102
103# define COLORED_MSG(prefix, color, msg) colored ? qWinColoredMsg(prefix, color, msg) : msg
104#else
105# define COLORED_MSG(prefix, color, msg) colored && QAbstractTestLogger::isTtyOutput() ? "\033["#prefix";"#color"m" msg "\033[0m" : msg
106#endif
107
108 static const char *incidentType2String(QAbstractTestLogger::IncidentTypes type)
109 {
110 static bool colored = (!qgetenv("QTEST_COLORED").isEmpty());
111 switch (type) {
112 case QAbstractTestLogger::Pass:
113 return COLORED_MSG(0, 32, "PASS "); //green
114 case QAbstractTestLogger::XFail:
115 return COLORED_MSG(1, 32, "XFAIL "); //light green
116 case QAbstractTestLogger::Fail:
117 return COLORED_MSG(0, 31, "FAIL! "); //red
118 case QAbstractTestLogger::XPass:
119 return COLORED_MSG(0, 31, "XPASS "); //red, too
120 }
121 return "??????";
122 }
123
124 static const char *benchmarkResult2String()
125 {