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 // Skip debug messages unless verbose mode is enabled
113 if( aSeverity == RPT_SEVERITY_DEBUG && !m_verbose )
114 return *this;
115
116 FILE* target = stdout;
117
118 if( aSeverity == RPT_SEVERITY_ERROR )
119 target = stderr;
120
121 if( aMsg.EndsWith( wxS( "\n" ) ) )
122 wxFprintf( target, aMsg );
123 else
124 wxFprintf( target, aMsg + wxS( "\n" ) );
125
126 // Needed after wxPrintf (or printf) to be sure the message is immediately printed
127 // (i.e. not stored in some i/o buffer)
128 fflush( target );
129
130 return *this;
131}
132
133
135{
136 static CLI_REPORTER s_cliReporter;
137
138 return s_cliReporter;
139}
140
141
142REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
143{
144 REPORTER::Report( aMsg, aSeverity );
145
146 switch( aSeverity )
147 {
148 case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
149 case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
150 case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
151 case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
152 case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
153 case RPT_SEVERITY_DEBUG: std::cout << "SEVERITY_DEBUG: "; break;
155 case RPT_SEVERITY_IGNORE: break;
156 }
157
158 std::cout << aMsg << std::endl;
159
160 return *this;
161}
162
163
165{
166 static REPORTER* s_stdoutReporter = nullptr;
167
168 if( !s_stdoutReporter )
169 s_stdoutReporter = new STDOUT_REPORTER();
170
171 return *s_stdoutReporter;
172}
173
174
175REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
176{
177 REPORTER::Report( aMsg, aSeverity );
178
179 switch( aSeverity )
180 {
181 case RPT_SEVERITY_ERROR: wxLogError( aMsg ); break;
182 case RPT_SEVERITY_WARNING: wxLogWarning( aMsg ); break;
183 case RPT_SEVERITY_UNDEFINED: wxLogMessage( aMsg ); break;
184 case RPT_SEVERITY_INFO: wxLogInfo( aMsg ); break;
185 case RPT_SEVERITY_ACTION: wxLogInfo( aMsg ); break;
186 case RPT_SEVERITY_DEBUG: wxLogTrace( traceReporter, aMsg ); break;
187 case RPT_SEVERITY_EXCLUSION: break;
188 case RPT_SEVERITY_IGNORE: break;
189 }
190
191 return *this;
192}
193
194
196{
197 static REPORTER* s_wxLogReporter = nullptr;
198 std::lock_guard lock( g_logReporterMutex );
199
200 if( !s_wxLogReporter )
201 s_wxLogReporter = new WXLOG_REPORTER();
202
203 return *s_wxLogReporter;
204}
205
206
207REPORTER& LOAD_INFO_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
208{
209 REPORTER::Report( aMsg, aSeverity );
210
211 REPORTER* target = m_redirectTarget;
212
213 if( !target )
214 target = &WXLOG_REPORTER::GetInstance();
215
216 target->Report( aMsg, aSeverity );
217
218 return *this;
219}
220
221
223{
224 static LOAD_INFO_REPORTER s_loadInfoReporter;
225 std::lock_guard lock( g_logReporterMutex );
226
227 return s_loadInfoReporter;
228}
229
230
232{
233 std::lock_guard lock( g_logReporterMutex );
234 m_redirectTarget = aReporter;
235}
236
237
239{
240 std::lock_guard lock( g_logReporterMutex );
241 return m_redirectTarget;
242}
243
244
246 m_reporter( LOAD_INFO_REPORTER::GetInstance() ),
247 m_previousReporter( m_reporter.GetRedirectTarget() )
248{
249 m_reporter.SetRedirectTarget( aReporter );
250}
251
252
257
258
264
265
270
271
272REPORTER& REDIRECT_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
273{
274 REPORTER::Report( aText, aSeverity );
275
276 if( m_redirectTarget )
277 m_redirectTarget->Report( aText, aSeverity );
278
279 return *this;
280}
281
282
283REPORTER& STATUSBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
284{
285 REPORTER::Report( aText, aSeverity );
286
287 if( m_statusBar )
288 m_statusBar->SetStatusText( aText, m_position );
289
290 return *this;
291}
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:108
static CLI_REPORTER & GetInstance()
Definition reporter.cpp:134
bool m_verbose
Definition reporter.h:257
FONTCONFIG_REPORTER_SCOPE(REPORTER *aReporter)
Definition reporter.cpp:259
REPORTER * m_previousReporter
Definition reporter.h:340
LOAD_INFO_REPORTER_SCOPE(REPORTER *aReporter)
Definition reporter.cpp:245
LOAD_INFO_REPORTER & m_reporter
Definition reporter.h:322
REPORTER * m_previousReporter
Definition reporter.h:323
REPORTER * GetRedirectTarget() const
Definition reporter.cpp:238
REPORTER * m_redirectTarget
Definition reporter.h:311
static LOAD_INFO_REPORTER & GetInstance()
Definition reporter.cpp:222
void SetRedirectTarget(REPORTER *aReporter)
Definition reporter.cpp:231
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:207
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:351
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:272
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:283
wxStatusBar * m_statusBar
Definition reporter.h:370
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:142
static REPORTER & GetInstance()
Definition reporter.cpp:164
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition reporter.cpp:175
static REPORTER & GetInstance()
Definition reporter.cpp:195
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)