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/wx_infobar.h>
34#include <wx/crt.h>
35#include <wx/log.h>
36#include <wx/textctrl.h>
37#include <wx/statusbr.h>
38
39
45static const wxChar traceReporter[] = wxT( "KICAD_REPORTER" );
46
47static std::mutex g_logReporterMutex;
48
49
50REPORTER& REPORTER::Report( const char* aText, SEVERITY aSeverity )
51{
52 Report( From_UTF8( aText ) );
53 return *this;
54}
55
56
57REPORTER& WX_TEXT_CTRL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
58{
59 REPORTER::Report( aText, aSeverity );
60
61 wxCHECK_MSG( m_textCtrl != nullptr, *this,
62 wxT( "No wxTextCtrl object defined in WX_TEXT_CTRL_REPORTER." ) );
63
64 m_textCtrl->AppendText( aText + wxS( "\n" ) );
65 return *this;
66}
67
68
69REPORTER& WX_STRING_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
70{
71 REPORTER::Report( aText, aSeverity );
72
73 m_string << aText << wxS( "\n" );
74 return *this;
75}
76
77
78const wxString& WX_STRING_REPORTER::GetMessages() const
79{
80 return m_string;
81}
82
83
85{
87 m_string.clear();
88}
89
90
91REPORTER& NULL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
92{
93 return REPORTER::Report( aText, aSeverity );
94}
95
96
98{
99 static REPORTER* s_nullReporter = nullptr;
100
101 if( !s_nullReporter )
102 s_nullReporter = new NULL_REPORTER();
103
104 return *s_nullReporter;
105}
106
107
108REPORTER& CLI_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
109{
110 REPORTER::Report( aMsg, aSeverity );
111
112 FILE* target = stdout;
113
114 if( aSeverity == RPT_SEVERITY_ERROR )
115 target = stderr;
116
117 if( aMsg.EndsWith( wxS( "\n" ) ) )
118 wxFprintf( target, aMsg );
119 else
120 wxFprintf( target, aMsg + wxS( "\n" ) );
121
122 // Needed after wxPrintf (or printf) to be sure the message is immediately printed
123 // (i.e. not stored in some i/o buffer)
124 fflush( target );
125
126 return *this;
127}
128
129
131{
132 static CLI_REPORTER s_cliReporter;
133
134 return s_cliReporter;
135}
136
137
138REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
139{
140 REPORTER::Report( aMsg, aSeverity );
141
142 switch( aSeverity )
143 {
144 case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
145 case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
146 case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
147 case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
148 case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
149 case RPT_SEVERITY_DEBUG: std::cout << "SEVERITY_DEBUG: "; break;
151 case RPT_SEVERITY_IGNORE: break;
152 }
153
154 std::cout << aMsg << std::endl;
155
156 return *this;
157}
158
159
161{
162 static REPORTER* s_stdoutReporter = nullptr;
163
164 if( !s_stdoutReporter )
165 s_stdoutReporter = new STDOUT_REPORTER();
166
167 return *s_stdoutReporter;
168}
169
170
171REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
172{
173 REPORTER::Report( aMsg, aSeverity );
174
175 switch( aSeverity )
176 {
177 case RPT_SEVERITY_ERROR: wxLogError( aMsg ); break;
178 case RPT_SEVERITY_WARNING: wxLogWarning( aMsg ); break;
179 case RPT_SEVERITY_UNDEFINED: wxLogMessage( aMsg ); break;
180 case RPT_SEVERITY_INFO: wxLogInfo( aMsg ); break;
181 case RPT_SEVERITY_ACTION: wxLogInfo( aMsg ); break;
182 case RPT_SEVERITY_DEBUG: wxLogTrace( traceReporter, aMsg ); break;
183 case RPT_SEVERITY_EXCLUSION: break;
184 case RPT_SEVERITY_IGNORE: break;
185 }
186
187 return *this;
188}
189
190
192{
193 static REPORTER* s_wxLogReporter = nullptr;
194 std::lock_guard lock( g_logReporterMutex );
195
196 if( !s_wxLogReporter )
197 s_wxLogReporter = new WXLOG_REPORTER();
198
199 return *s_wxLogReporter;
200}
201
202
203REPORTER& LOAD_INFO_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
204{
205 REPORTER::Report( aMsg, aSeverity );
206
207 REPORTER* target = m_redirectTarget;
208
209 if( !target )
210 target = &WXLOG_REPORTER::GetInstance();
211
212 target->Report( aMsg, aSeverity );
213
214 return *this;
215}
216
217
219{
220 static LOAD_INFO_REPORTER s_loadInfoReporter;
221 std::lock_guard lock( g_logReporterMutex );
222
223 return s_loadInfoReporter;
224}
225
226
228{
229 std::lock_guard lock( g_logReporterMutex );
230 m_redirectTarget = aReporter;
231}
232
233
235{
236 std::lock_guard lock( g_logReporterMutex );
237 return m_redirectTarget;
238}
239
240
242 m_reporter( LOAD_INFO_REPORTER::GetInstance() ),
243 m_previousReporter( m_reporter.GetRedirectTarget() )
244{
245 m_reporter.SetRedirectTarget( aReporter );
246}
247
248
253
254
260
261
266
267
268REPORTER& REDIRECT_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
269{
270 REPORTER::Report( aText, aSeverity );
271
272 if( m_redirectTarget )
273 m_redirectTarget->Report( aText, aSeverity );
274
275 return *this;
276}
277
278
279REPORTER& STATUSBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
280{
281 REPORTER::Report( aText, aSeverity );
282
283 if( m_statusBar )
284 m_statusBar->SetStatusText( aText, m_position );
285
286 return *this;
287}
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:108
static REPORTER & GetInstance()
Definition reporter.cpp:130
FONTCONFIG_REPORTER_SCOPE(REPORTER *aReporter)
Definition reporter.cpp:255
REPORTER * m_previousReporter
Definition reporter.h:328
LOAD_INFO_REPORTER_SCOPE(REPORTER *aReporter)
Definition reporter.cpp:241
LOAD_INFO_REPORTER & m_reporter
Definition reporter.h:310
REPORTER * m_previousReporter
Definition reporter.h:311
REPORTER * GetRedirectTarget() const
Definition reporter.cpp:234
REPORTER * m_redirectTarget
Definition reporter.h:299
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:218
void SetRedirectTarget(REPORTER *aReporter)
Definition reporter.cpp:227
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:203
static REPORTER & GetInstance()
Definition reporter.cpp:97
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:91
REPORTER * m_redirectTarget
Definition reporter.h:339
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:268
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual void Clear()
Definition reporter.h:153
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition reporter.h:102
REPORTER()
Definition reporter.h:75
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:279
wxStatusBar * m_statusBar
Definition reporter.h:358
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:138
static REPORTER & GetInstance()
Definition reporter.cpp:160
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:171
static REPORTER & GetInstance()
Definition reporter.cpp:191
wxString m_string
Definition reporter.h:206
void Clear() override
Definition reporter.cpp:84
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:69
const wxString & GetMessages() const
Definition reporter.cpp:78
wxTextCtrl * m_textCtrl
Definition reporter.h:183
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:57
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:45
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:47
wxString From_UTF8(const char *cstring)