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 <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
56REPORTER& WX_TEXT_CTRL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
57{
58 REPORTER::Report( aText, aSeverity );
59
60 wxCHECK_MSG( m_textCtrl != nullptr, *this,
61 wxT( "No wxTextCtrl object defined in WX_TEXT_CTRL_REPORTER." ) );
62
63 m_textCtrl->AppendText( aText + wxS( "\n" ) );
64 return *this;
65}
66
67
69{
70 return !m_textCtrl->IsEmpty();
71}
72
73
74REPORTER& WX_STRING_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
75{
76 REPORTER::Report( aText, aSeverity );
77
78 m_string << aText << wxS( "\n" );
79 return *this;
80}
81
82
83const wxString& WX_STRING_REPORTER::GetMessages() const
84{
85 return m_string;
86}
87
88
90{
92 m_string.clear();
93}
94
95
97{
98 return !m_string.IsEmpty();
99}
100
101
102REPORTER& NULL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
103{
104 return REPORTER::Report( aText, aSeverity );
105}
106
107
109{
110 static REPORTER* s_nullReporter = nullptr;
111
112 if( !s_nullReporter )
113 s_nullReporter = new NULL_REPORTER();
114
115 return *s_nullReporter;
116}
117
118
119REPORTER& CLI_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
120{
121 REPORTER::Report( aMsg, aSeverity );
122
123 FILE* target = stdout;
124
125 if( aSeverity == RPT_SEVERITY_ERROR )
126 target = stderr;
127
128 if( aMsg.EndsWith( wxS( "\n" ) ) )
129 wxFprintf( target, aMsg );
130 else
131 wxFprintf( target, aMsg + wxS( "\n" ) );
132
133 // Needed after wxPrintf (or printf) to be sure the message is immediately printed
134 // (i.e. not stored in some i/o buffer)
135 fflush( target );
136
137 return *this;
138}
139
140
142{
143 static CLI_REPORTER s_cliReporter;
144
145 return s_cliReporter;
146}
147
148
149REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
150{
151 REPORTER::Report( aMsg, aSeverity );
152
153 switch( aSeverity )
154 {
155 case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
156 case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
157 case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
158 case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
159 case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
160 case RPT_SEVERITY_DEBUG: std::cout << "SEVERITY_DEBUG: "; break;
162 case RPT_SEVERITY_IGNORE: break;
163 }
164
165 std::cout << aMsg << std::endl;
166
167 return *this;
168}
169
170
172{
173 static REPORTER* s_stdoutReporter = nullptr;
174
175 if( !s_stdoutReporter )
176 s_stdoutReporter = new STDOUT_REPORTER();
177
178 return *s_stdoutReporter;
179}
180
181
182REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
183{
184 REPORTER::Report( aMsg, aSeverity );
185
186 switch( aSeverity )
187 {
188 case RPT_SEVERITY_ERROR: wxLogError( aMsg ); break;
189 case RPT_SEVERITY_WARNING: wxLogWarning( aMsg ); break;
190 case RPT_SEVERITY_UNDEFINED: wxLogMessage( aMsg ); break;
191 case RPT_SEVERITY_INFO: wxLogInfo( aMsg ); break;
192 case RPT_SEVERITY_ACTION: wxLogInfo( aMsg ); break;
193 case RPT_SEVERITY_DEBUG: wxLogTrace( traceReporter, aMsg ); break;
194 case RPT_SEVERITY_EXCLUSION: break;
195 case RPT_SEVERITY_IGNORE: break;
196 }
197
198 return *this;
199}
200
201
203{
204 static REPORTER* s_wxLogReporter = nullptr;
205 std::lock_guard lock( g_logReporterMutex );
206
207 if( !s_wxLogReporter )
208 s_wxLogReporter = new WXLOG_REPORTER();
209
210 return *s_wxLogReporter;
211}
212
213
214REPORTER& STATUSBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
215{
216 REPORTER::Report( aText, aSeverity );
217
218 if( m_statusBar )
219 m_statusBar->SetStatusText( aText, m_position );
220
221 return *this;
222}
223
224
226{
227 if( m_statusBar )
228 return !m_statusBar->GetStatusText( m_position ).IsEmpty();
229
230 return false;
231}
Reporter forwarding messages to stdout or stderr as appropriate.
Definition: reporter.h:238
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:119
static REPORTER & GetInstance()
Definition: reporter.cpp:141
static REPORTER & GetInstance()
Definition: reporter.cpp:108
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:102
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:73
virtual void Clear()
Definition: reporter.h:150
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)
Report a string with a given severity.
Definition: reporter.h:102
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:214
wxStatusBar * m_statusBar
Definition: reporter.h:308
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:225
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:149
static REPORTER & GetInstance()
Definition: reporter.cpp:171
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:182
static REPORTER & GetInstance()
Definition: reporter.cpp:202
wxString m_string
Definition: reporter.h:207
void Clear() override
Definition: reporter.cpp:89
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:96
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:74
const wxString & GetMessages() const
Definition: reporter.cpp:83
wxTextCtrl * m_textCtrl
Definition: reporter.h:182
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:68
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:56
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)