1
/*
2
 * This file is part of the API Extractor project.
3
 *
4
 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
5
 *
6
 * Contact: PySide team <contact@pyside.org>
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * version 2 as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
 * 02110-1301 USA
21
 *
22
 */
23
24
#include "reporthandler.h"
25
#include "typesystem.h"
26
#include "typedatabase.h"
27
#include <QtCore/QSet>
28
#include <cstring>
29
#include <cstdarg>
30
#include <cstdio>
31
32
#if _WINDOWS || NOCOLOR
33
    #define COLOR_END ""
34
    #define COLOR_WHITE ""
35
    #define COLOR_YELLOW ""
36
    #define COLOR_GREEN ""
37
#else
38
    #define COLOR_END "\033[0m"
39
    #define COLOR_WHITE "\033[1;37m"
40
    #define COLOR_YELLOW "\033[1;33m"
41
    #define COLOR_GREEN "\033[0;32m"
42
#endif
43
44
static bool m_silent = false;
45
static int m_warningCount = 0;
46
static int m_suppressedCount = 0;
47
static QString m_context;
48
static ReportHandler::DebugLevel m_debugLevel = ReportHandler::NoDebug;
49
static QSet<QString> m_reportedWarnings;
50
static QString m_progressBuffer;
51
static int m_step_size = 0;
52
static int m_step = -1;
53
static int m_step_warning = 0;
54
55
static void printProgress()
56
{
57
    std::printf("%s", m_progressBuffer.toAscii().data());
58
    std::fflush(stdout);
59
    m_progressBuffer.clear();
60
}
61
62
static void printWarnings()
63
{
64
    if (m_reportedWarnings.size() > 0) {
65
        m_progressBuffer += "\t";
66
        foreach(QString msg, m_reportedWarnings)
67
            m_progressBuffer += msg + "\n\t";
68
        m_progressBuffer += "\n\n";
69
        m_reportedWarnings.clear();
70
        printProgress();
71
    }
72
}
73
74
ReportHandler::DebugLevel ReportHandler::debugLevel()
75
{
76
    return m_debugLevel;
77
}
78
79
void ReportHandler::setDebugLevel(ReportHandler::DebugLevel level)
80
{
81
    m_debugLevel = level;
82
}
83
84
void ReportHandler::setContext(const QString& context)
85
{
86
    m_context = context;
87
}
88
89
int ReportHandler::suppressedCount()
90
{
91
    return m_suppressedCount;
92
}
93
94
int ReportHandler::warningCount()
95
{
96
    return m_warningCount;
97
}
98
99
void ReportHandler::setProgressReference(int max)
100
{
101
    m_step_size = max;
102
    m_step = -1;
103
}
104
105
bool ReportHandler::isSilent()
106
{
107
    return m_silent;
108
}
109
110
void ReportHandler::setSilent(bool silent)
111
{
112
    m_silent = silent;
113
}
114
115
void ReportHandler::warning(const QString &text)
116
{
117
    if (m_silent)
118
        return;
119
120
// Context is useless!
121
//     QString warningText = QString("\r" COLOR_YELLOW "WARNING(%1)" COLOR_END " :: %2").arg(m_context).arg(text);
122
    TypeDatabase *db = TypeDatabase::instance();
123
    if (db && db->isSuppressedWarning(text)) {
124
        ++m_suppressedCount;
125
    } else if (!m_reportedWarnings.contains(text)) {
126
        ++m_warningCount;
127
        ++m_step_warning;
128
        m_reportedWarnings << text;
129
    }
130
}
131
132
void ReportHandler::progress(const QString& str, ...)
133
{
134
    if (m_silent)
135
        return;
136
137
    if (m_step == -1) {
138
        QTextStream buf(&m_progressBuffer);
139
        buf.setFieldWidth(45);
140
        buf.setFieldAlignment(QTextStream::AlignLeft);
141
        buf << str;
142
        printProgress();
143
        m_step = 0;
144
    }
145
    m_step++;
146
    if (m_step >= m_step_size) {
147
        if (m_step_warning == 0) {
148
            m_progressBuffer = "[" COLOR_GREEN "OK" COLOR_END "]\n";
149
        } else {
150
            m_progressBuffer = "[" COLOR_YELLOW "WARNING" COLOR_END "]\n";
151
        }
152
        printProgress();
153
        m_step_warning = 0;
154
    }
155
}
156
157
void ReportHandler::flush()
158
{
159
    if (!m_silent)
160
        printWarnings();
161
}
162
163
void ReportHandler::debug(DebugLevel level, const QString &text)
164
{
165
    if (m_debugLevel == NoDebug)
166
        return;
167
168
    if (level <= m_debugLevel) {
169
        std::printf("\r" COLOR_GREEN "DEBUG" COLOR_END " :: %-70s\n", qPrintable(text));
170
        printProgress();
171
    }
172
}