KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 <[email protected]>
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 <font/fontconfig.h>
32#include <string_utils.h>
33#include <widgets/kistatusbar.h>
34#include <widgets/wx_infobar.h>
35#include <wx/crt.h>
36#include <wx/log.h>
37#include <wx/textctrl.h>
38#include <wx/statusbr.h>
39#include <wx/tokenzr.h>
40#include <wx/weakref.h>
41
42
48static const wxChar traceReporter[] = wxT( "KICAD_REPORTER" );
49
50static std::mutex g_logReporterMutex;
51
52
54{
55public:
56 STATUSBAR_WARNING_REPORTER_IMPL( KISTATUSBAR* aStatusBar, const wxString& aSource ) :
57 m_statusBar( aStatusBar ),
58 m_source( aSource )
59 {
60 }
61
63 {
64 KISTATUSBAR* statusBar = m_statusBar.get();
65
66 if( statusBar && !statusBar->IsBeingDeleted() )
67 return statusBar;
68
69 return nullptr;
70 }
71
72 wxWeakRef<KISTATUSBAR> m_statusBar;
73 wxString m_source;
74};
75
76
77REPORTER& REPORTER::Report( const char* aText, SEVERITY aSeverity )
78{
79 Report( From_UTF8( aText ) );
80 return *this;
81}
82
83
84REPORTER& WX_TEXT_CTRL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
85{
86 REPORTER::Report( aText, aSeverity );
87
88 wxCHECK_MSG( m_textCtrl != nullptr, *this,
89 wxT( "No wxTextCtrl object defined in WX_TEXT_CTRL_REPORTER." ) );
90
91 m_textCtrl->AppendText( aText + wxS( "\n" ) );
92 return *this;
93}
94
95
96REPORTER& WX_STRING_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
97{
98 REPORTER::Report( aText, aSeverity );
99
100 m_string << aText << wxS( "\n" );
101 return *this;
102}
103
104
105const wxString& WX_STRING_REPORTER::GetMessages() const
106{
107 return m_string;
108}
109
110
112{
114 m_string.clear();
115}
116
117
118REPORTER& NULL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
119{
120 return REPORTER::Report( aText, aSeverity );
121}
122
123
125{
126 static REPORTER* s_nullReporter = nullptr;
127
128 if( !s_nullReporter )
129 s_nullReporter = new NULL_REPORTER();
130
131 return *s_nullReporter;
132}
133
134
135REPORTER& CLI_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
136{
137 REPORTER::Report( aMsg, aSeverity );
138
139 // Skip debug messages unless verbose mode is enabled
140 if( aSeverity == RPT_SEVERITY_DEBUG && !m_verbose )
141 return *this;
142
143 FILE* target = stdout;
144
145 if( aSeverity == RPT_SEVERITY_ERROR )
146 target = stderr;
147
148 if( aMsg.EndsWith( wxS( "\n" ) ) )
149 wxFprintf( target, aMsg );
150 else
151 wxFprintf( target, aMsg + wxS( "\n" ) );
152
153 // Needed after wxPrintf (or printf) to be sure the message is immediately printed
154 // (i.e. not stored in some i/o buffer)
155 fflush( target );
156
157 return *this;
158}
159
160
162{
163 static CLI_REPORTER s_cliReporter;
164
165 return s_cliReporter;
166}
167
168
169REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
170{
171 REPORTER::Report( aMsg, aSeverity );
172
173 switch( aSeverity )
174 {
175 case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
176 case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
177 case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
178 case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
179 case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
180 case RPT_SEVERITY_DEBUG: std::cout << "SEVERITY_DEBUG: "; break;
182 case RPT_SEVERITY_IGNORE: break;
183 }
184
185 std::cout << aMsg << std::endl;
186
187 return *this;
188}
189
190
192{
193 static REPORTER* s_stdoutReporter = nullptr;
194
195 if( !s_stdoutReporter )
196 s_stdoutReporter = new STDOUT_REPORTER();
197
198 return *s_stdoutReporter;
199}
200
201
202REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
203{
204 REPORTER::Report( aMsg, aSeverity );
205
206 switch( aSeverity )
207 {
208 case RPT_SEVERITY_ERROR: wxLogError( aMsg ); break;
209 case RPT_SEVERITY_WARNING: wxLogWarning( aMsg ); break;
210 case RPT_SEVERITY_UNDEFINED: wxLogMessage( aMsg ); break;
211 case RPT_SEVERITY_INFO: wxLogInfo( aMsg ); break;
212 case RPT_SEVERITY_ACTION: wxLogInfo( aMsg ); break;
213 case RPT_SEVERITY_DEBUG: wxLogTrace( traceReporter, aMsg ); break;
214 case RPT_SEVERITY_EXCLUSION: break;
215 case RPT_SEVERITY_IGNORE: break;
216 }
217
218 return *this;
219}
220
221
223{
224 static REPORTER* s_wxLogReporter = nullptr;
225 std::lock_guard lock( g_logReporterMutex );
226
227 if( !s_wxLogReporter )
228 s_wxLogReporter = new WXLOG_REPORTER();
229
230 return *s_wxLogReporter;
231}
232
233
234REPORTER& LOAD_INFO_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
235{
236 REPORTER::Report( aMsg, aSeverity );
237
238 REPORTER* target = m_redirectTarget;
239
240 if( !target )
241 target = &WXLOG_REPORTER::GetInstance();
242
243 target->Report( aMsg, aSeverity );
244
245 return *this;
246}
247
248
250{
251 static LOAD_INFO_REPORTER s_loadInfoReporter;
252 std::lock_guard lock( g_logReporterMutex );
253
254 return s_loadInfoReporter;
255}
256
257
259{
260 std::lock_guard lock( g_logReporterMutex );
261 m_redirectTarget = aReporter;
262}
263
264
266{
267 std::lock_guard lock( g_logReporterMutex );
268 return m_redirectTarget;
269}
270
271
273 m_reporter( LOAD_INFO_REPORTER::GetInstance() ),
274 m_previousReporter( m_reporter.GetRedirectTarget() )
275{
276 m_reporter.SetRedirectTarget( aReporter );
277}
278
279
284
285
291
292
297
298
299REPORTER& REDIRECT_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
300{
301 REPORTER::Report( aText, aSeverity );
302
303 if( m_redirectTarget )
304 m_redirectTarget->Report( aText, aSeverity );
305
306 return *this;
307}
308
309
310REPORTER& STATUSBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
311{
312 REPORTER::Report( aText, aSeverity );
313
314 if( m_statusBar )
315 m_statusBar->SetStatusText( aText, m_position );
316
317 return *this;
318}
319
320
322 const wxString& aSource ) :
323 m_impl( std::make_shared<STATUSBAR_WARNING_REPORTER_IMPL>( aStatusBar, aSource ) )
324{
325}
326
327
329
330
331REPORTER& STATUSBAR_WARNING_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
332{
333 REPORTER::Report( aText, aSeverity );
334
335 KISTATUSBAR* statusBar = m_impl ? m_impl->GetStatusBar() : nullptr;
336
337 if( !statusBar || aText.IsEmpty() )
338 return *this;
339
340 std::vector<LOAD_MESSAGE> messages;
341 wxStringTokenizer tokenizer( aText, wxS( "\n" ), wxTOKEN_STRTOK );
342 SEVERITY severity = aSeverity == RPT_SEVERITY_UNDEFINED ? RPT_SEVERITY_WARNING : aSeverity;
343
344 while( tokenizer.HasMoreTokens() )
345 {
346 LOAD_MESSAGE message;
347 message.message = tokenizer.GetNextToken();
348 message.severity = severity;
349 messages.emplace_back( std::move( message ) );
350 }
351
352 if( messages.empty() )
353 {
354 LOAD_MESSAGE message;
355 message.message = aText;
356 message.severity = severity;
357 messages.emplace_back( std::move( message ) );
358 }
359
360 statusBar->AddWarningMessages( m_impl->m_source, messages );
361 return *this;
362}
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:135
static CLI_REPORTER & GetInstance()
Definition reporter.cpp:161
bool m_verbose
Definition reporter.h:259
FONTCONFIG_REPORTER_SCOPE(REPORTER *aReporter)
Definition reporter.cpp:286
REPORTER * m_previousReporter
Definition reporter.h:342
void AddWarningMessages(const wxString &aSource, const wxString &aMessages)
Add warning/error messages (not thread-safe, use the std::vector<LOAD_MESSAGE> variant from other thr...
LOAD_INFO_REPORTER_SCOPE(REPORTER *aReporter)
Definition reporter.cpp:272
LOAD_INFO_REPORTER & m_reporter
Definition reporter.h:324
REPORTER * m_previousReporter
Definition reporter.h:325
REPORTER * GetRedirectTarget() const
Definition reporter.cpp:265
REPORTER * m_redirectTarget
Definition reporter.h:313
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:249
void SetRedirectTarget(REPORTER *aReporter)
Definition reporter.cpp:258
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:234
static REPORTER & GetInstance()
Definition reporter.cpp:124
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:118
REPORTER * m_redirectTarget
Definition reporter.h:353
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:299
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:75
virtual void Clear()
Definition reporter.h:155
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:104
REPORTER()
Definition reporter.h:77
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:310
wxStatusBar * m_statusBar
Definition reporter.h:372
wxWeakRef< KISTATUSBAR > m_statusBar
Definition reporter.cpp:72
KISTATUSBAR * GetStatusBar() const
Definition reporter.cpp:62
STATUSBAR_WARNING_REPORTER_IMPL(KISTATUSBAR *aStatusBar, const wxString &aSource)
Definition reporter.cpp:56
STATUSBAR_WARNING_REPORTER(KISTATUSBAR *aStatusBar, const wxString &aSource)
Definition reporter.cpp:321
~STATUSBAR_WARNING_REPORTER() override
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:331
std::shared_ptr< STATUSBAR_WARNING_REPORTER_IMPL > m_impl
Definition reporter.h:386
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:169
static REPORTER & GetInstance()
Definition reporter.cpp:191
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:202
static REPORTER & GetInstance()
Definition reporter.cpp:222
wxString m_string
Definition reporter.h:208
void Clear() override
Definition reporter.cpp:111
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:96
const wxString & GetMessages() const
Definition reporter.cpp:105
wxTextCtrl * m_textCtrl
Definition reporter.h:185
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:84
static void SetReporter(REPORTER *aReporter)
Set the reporter to use for reporting font substitution warnings.
static const wxChar traceReporter[]
Flag to enable reporter debugging output.
Definition reporter.cpp:48
This file contains miscellaneous commonly used macros and functions.
STL namespace.
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:50
wxString From_UTF8(const char *cstring)
KISTATUSBAR is a wxStatusBar suitable for Kicad manager.
Definition kistatusbar.h:54
wxString message
Definition kistatusbar.h:55
SEVERITY severity
Definition kistatusbar.h:56