KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
reporter.cpp
Go to the documentation of this file.
1
4/*
5 * This program source code file is part of KiCad, a free EDA CAD application.
6 *
7 * Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>
8 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, you may find one here:
22 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23 * or you may search the http://www.gnu.org website for the version 2 license,
24 * or you may write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28#include <mutex>
29#include <macros.h>
30#include <reporter.h>
31#include <string_utils.h>
32#include <widgets/wx_infobar.h>
33#include <wx/crt.h>
34#include <wx/log.h>
35#include <wx/textctrl.h>
36#include <wx/statusbr.h>
37
38
44static const wxChar traceReporter[] = wxT( "KICAD_REPORTER" );
45
46static std::mutex g_logReporterMutex;
47
48
49REPORTER& REPORTER::Report( const char* aText, SEVERITY aSeverity )
50{
51 Report( From_UTF8( aText ) );
52 return *this;
53}
54
55
56bool REPORTER::HasMessageOfSeverity( int aSeverityMask ) const
57{
58 wxFAIL_MSG( "HasMessageOfSeverity is not implemented in this reporter" );
59 return HasMessage();
60}
61
62
63REPORTER& WX_TEXT_CTRL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
64{
65 wxCHECK_MSG( m_textCtrl != nullptr, *this,
66 wxT( "No wxTextCtrl object defined in WX_TEXT_CTRL_REPORTER." ) );
67
68 m_textCtrl->AppendText( aText + wxS( "\n" ) );
69 return *this;
70}
71
72
74{
75 return !m_textCtrl->IsEmpty();
76}
77
78
79REPORTER& WX_STRING_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
80{
81 m_severityMask |= aSeverity;
82 m_string << aText << wxS( "\n" );
83 return *this;
84}
85
86
87const wxString& WX_STRING_REPORTER::GetMessages() const
88{
89 return m_string;
90}
91
92
94{
96 m_string.clear();
97}
98
99
101{
102 return !m_string.IsEmpty();
103}
104
105
106bool WX_STRING_REPORTER::HasMessageOfSeverity( int aSeverityMask ) const
107{
108 return ( m_severityMask & aSeverityMask ) != 0;
109}
110
111
112REPORTER& NULL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
113{
114 return *this;
115}
116
117
119{
120 static REPORTER* s_nullReporter = nullptr;
121
122 if( !s_nullReporter )
123 s_nullReporter = new NULL_REPORTER();
124
125 return *s_nullReporter;
126}
127
128
129REPORTER& CLI_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
130{
131 FILE* target = stdout;
132
133 if( aSeverity == RPT_SEVERITY_ERROR )
134 target = stderr;
135
136 if( aMsg.EndsWith( wxS( "\n" ) ) )
137 wxFprintf( target, aMsg );
138 else
139 wxFprintf( target, aMsg + wxS( "\n" ) );
140
141 m_hasMessageMap[aSeverity] = true;
142
143 return *this;
144}
145
146
147bool CLI_REPORTER::HasMessageOfSeverity( int aSeverityMask ) const
148{
149 for( const auto& [severity, flag] : m_hasMessageMap )
150 {
151 if( ( aSeverityMask & severity ) > 0 && flag )
152 return true;
153 }
154
155 return false;
156}
157
158
160{
161 static CLI_REPORTER s_cliReporter;
162
163 return s_cliReporter;
164}
165
166
167REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
168{
169 switch( aSeverity )
170 {
171 case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
172 case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
173 case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
174 case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
175 case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
176 case RPT_SEVERITY_DEBUG: std::cout << "SEVERITY_DEBUG: "; break;
178 case RPT_SEVERITY_IGNORE: break;
179 }
180
181 std::cout << aMsg << std::endl;
182
183 return *this;
184}
185
186
188{
189 static REPORTER* s_stdoutReporter = nullptr;
190
191 if( !s_stdoutReporter )
192 s_stdoutReporter = new STDOUT_REPORTER();
193
194 return *s_stdoutReporter;
195}
196
197
198REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
199{
200 switch( aSeverity )
201 {
202 case RPT_SEVERITY_ERROR: wxLogError( aMsg ); break;
203 case RPT_SEVERITY_WARNING: wxLogWarning( aMsg ); break;
204 case RPT_SEVERITY_UNDEFINED: wxLogMessage( aMsg ); break;
205 case RPT_SEVERITY_INFO: wxLogInfo( aMsg ); break;
206 case RPT_SEVERITY_ACTION: wxLogInfo( aMsg ); break;
207 case RPT_SEVERITY_DEBUG: wxLogTrace( traceReporter, aMsg ); break;
208 case RPT_SEVERITY_EXCLUSION: break;
209 case RPT_SEVERITY_IGNORE: break;
210 }
211
212 return *this;
213}
214
215
217{
218 static REPORTER* s_wxLogReporter = nullptr;
219 std::lock_guard lock( g_logReporterMutex );
220
221 if( !s_wxLogReporter )
222 s_wxLogReporter = new WXLOG_REPORTER();
223
224 return *s_wxLogReporter;
225}
226
227
228REPORTER& STATUSBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
229{
230 if( m_statusBar )
231 m_statusBar->SetStatusText( aText, m_position );
232
233 return *this;
234}
235
236
238{
239 if( m_statusBar )
240 return !m_statusBar->GetStatusText( m_position ).IsEmpty();
241
242 return false;
243}
Reporter forwarding messages to stdout or stderr as appropriate.
Definition: reporter.h:227
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:129
std::map< SEVERITY, bool > m_hasMessageMap
Definition: reporter.h:245
bool HasMessageOfSeverity(int aSeverityMask) const override
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition: reporter.cpp:147
static REPORTER & GetInstance()
Definition: reporter.cpp:159
static REPORTER & GetInstance()
Definition: reporter.cpp:118
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:112
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:73
virtual bool HasMessageOfSeverity(int aSeverityMask) const
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition: reporter.cpp:56
virtual bool HasMessage() const =0
Returns true if the reporter client is non-empty.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:228
wxStatusBar * m_statusBar
Definition: reporter.h:308
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:237
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:167
static REPORTER & GetInstance()
Definition: reporter.cpp:187
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:198
static REPORTER & GetInstance()
Definition: reporter.cpp:216
wxString m_string
Definition: reporter.h:193
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:100
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:79
bool HasMessageOfSeverity(int aSeverityMask) const override
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition: reporter.cpp:106
const wxString & GetMessages() const
Definition: reporter.cpp:87
wxTextCtrl * m_textCtrl
Definition: reporter.h:164
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:73
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:63
static const wxChar traceReporter[]
Flag to enable reporter debugging output.
Definition: reporter.cpp:44
This file contains miscellaneous commonly used macros and functions.
SEVERITY
@ RPT_SEVERITY_WARNING
@ RPT_SEVERITY_ERROR
@ RPT_SEVERITY_UNDEFINED
@ RPT_SEVERITY_EXCLUSION
@ RPT_SEVERITY_IGNORE
@ RPT_SEVERITY_DEBUG
@ RPT_SEVERITY_INFO
@ RPT_SEVERITY_ACTION
static std::mutex g_logReporterMutex
Definition: reporter.cpp:46
wxString From_UTF8(const char *cstring)