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 return *this;
134}
135
136
138{
139 static CLI_REPORTER s_cliReporter;
140
141 return s_cliReporter;
142}
143
144
145REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
146{
147 REPORTER::Report( aMsg, aSeverity );
148
149 switch( aSeverity )
150 {
151 case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
152 case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
153 case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
154 case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
155 case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
156 case RPT_SEVERITY_DEBUG: std::cout << "SEVERITY_DEBUG: "; break;
158 case RPT_SEVERITY_IGNORE: break;
159 }
160
161 std::cout << aMsg << std::endl;
162
163 return *this;
164}
165
166
168{
169 static REPORTER* s_stdoutReporter = nullptr;
170
171 if( !s_stdoutReporter )
172 s_stdoutReporter = new STDOUT_REPORTER();
173
174 return *s_stdoutReporter;
175}
176
177
178REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
179{
180 REPORTER::Report( aMsg, aSeverity );
181
182 switch( aSeverity )
183 {
184 case RPT_SEVERITY_ERROR: wxLogError( aMsg ); break;
185 case RPT_SEVERITY_WARNING: wxLogWarning( aMsg ); break;
186 case RPT_SEVERITY_UNDEFINED: wxLogMessage( aMsg ); break;
187 case RPT_SEVERITY_INFO: wxLogInfo( aMsg ); break;
188 case RPT_SEVERITY_ACTION: wxLogInfo( aMsg ); break;
189 case RPT_SEVERITY_DEBUG: wxLogTrace( traceReporter, aMsg ); break;
190 case RPT_SEVERITY_EXCLUSION: break;
191 case RPT_SEVERITY_IGNORE: break;
192 }
193
194 return *this;
195}
196
197
199{
200 static REPORTER* s_wxLogReporter = nullptr;
201 std::lock_guard lock( g_logReporterMutex );
202
203 if( !s_wxLogReporter )
204 s_wxLogReporter = new WXLOG_REPORTER();
205
206 return *s_wxLogReporter;
207}
208
209
210REPORTER& STATUSBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
211{
212 REPORTER::Report( aText, aSeverity );
213
214 if( m_statusBar )
215 m_statusBar->SetStatusText( aText, m_position );
216
217 return *this;
218}
219
220
222{
223 if( m_statusBar )
224 return !m_statusBar->GetStatusText( m_position ).IsEmpty();
225
226 return false;
227}
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:137
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:210
wxStatusBar * m_statusBar
Definition: reporter.h:308
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:221
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:145
static REPORTER & GetInstance()
Definition: reporter.cpp:167
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:178
static REPORTER & GetInstance()
Definition: reporter.cpp:198
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)