KiCad PCB EDA Suite
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 <stambaughw@gmail.com>
8  * Copyright (C) 2013-2021 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 <macros.h>
29 #include <reporter.h>
30 #include <widgets/infobar.h>
31 #include <wx_html_report_panel.h>
32 #include <wx/log.h>
33 #include <wx/textctrl.h>
34 #include <wx/statusbr.h>
35 
36 REPORTER& REPORTER::Report( const char* aText, SEVERITY aSeverity )
37 {
38  Report( FROM_UTF8( aText ) );
39  return *this;
40 }
41 
42 
43 REPORTER& WX_TEXT_CTRL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
44 {
45  wxCHECK_MSG( m_textCtrl != nullptr, *this,
46  wxT( "No wxTextCtrl object defined in WX_TEXT_CTRL_REPORTER." ) );
47 
48  m_textCtrl->AppendText( aText + wxS( "\n" ) );
49  return *this;
50 }
51 
52 
54 {
55  return !m_textCtrl->IsEmpty();
56 }
57 
58 
59 REPORTER& WX_STRING_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
60 {
61  wxCHECK_MSG( m_string != nullptr, *this,
62  wxT( "No wxString object defined in WX_STRING_REPORTER." ) );
63 
64  *m_string << aText << wxS( "\n" );
65  return *this;
66 }
67 
68 
70 {
71  return !m_string->IsEmpty();
72 }
73 
74 
75 REPORTER& WX_HTML_PANEL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
76 {
77  wxCHECK_MSG( m_panel != nullptr, *this,
78  wxT( "No WX_HTML_REPORT_PANEL object defined in WX_HTML_PANEL_REPORTER." ) );
79 
80  m_panel->Report( aText, aSeverity );
81  return *this;
82 }
83 
84 
85 REPORTER& WX_HTML_PANEL_REPORTER::ReportTail( const wxString& aText, SEVERITY aSeverity )
86 {
87  wxCHECK_MSG( m_panel != nullptr, *this,
88  wxT( "No WX_HTML_REPORT_PANEL object defined in WX_HTML_PANEL_REPORTER." ) );
89 
90  m_panel->Report( aText, aSeverity, LOC_TAIL );
91  return *this;
92 }
93 
94 
95 REPORTER& WX_HTML_PANEL_REPORTER::ReportHead( const wxString& aText, SEVERITY aSeverity )
96 {
97  wxCHECK_MSG( m_panel != nullptr, *this,
98  wxT( "No WX_HTML_REPORT_PANEL object defined in WX_HTML_PANEL_REPORTER." ) );
99 
100  m_panel->Report( aText, aSeverity, LOC_HEAD );
101  return *this;
102 }
103 
104 
106 {
108 }
109 
110 
111 REPORTER& NULL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
112 {
113  return *this;
114 }
115 
116 
118 {
119  static REPORTER* s_nullReporter = nullptr;
120 
121  if( !s_nullReporter )
122  s_nullReporter = new NULL_REPORTER();
123 
124  return *s_nullReporter;
125 }
126 
127 
128 REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
129 {
130  switch( aSeverity )
131  {
132  case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
133  case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
134  case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
135  case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
136  case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
138  case RPT_SEVERITY_IGNORE: break;
139  }
140 
141  std::cout << aMsg << std::endl;
142 
143  return *this;
144 }
145 
146 
148 {
149  static REPORTER* s_stdoutReporter = nullptr;
150 
151  if( !s_stdoutReporter )
152  s_stdoutReporter = new STDOUT_REPORTER();
153 
154  return *s_stdoutReporter;
155 }
156 
157 
158 REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
159 {
160  switch( aSeverity )
161  {
162  case RPT_SEVERITY_ERROR: wxLogError( aMsg ); break;
163  case RPT_SEVERITY_WARNING: wxLogWarning( aMsg ); break;
164  case RPT_SEVERITY_UNDEFINED: wxLogMessage( aMsg ); break;
165  case RPT_SEVERITY_INFO: wxLogInfo( aMsg ); break;
166  case RPT_SEVERITY_ACTION: wxLogInfo( aMsg ); break;
167  case RPT_SEVERITY_EXCLUSION: break;
168  case RPT_SEVERITY_IGNORE: break;
169  }
170 
171  return *this;
172 }
173 
174 
176 {
177  static REPORTER* s_wxLogReporter = nullptr;
178 
179  if( !s_wxLogReporter )
180  s_wxLogReporter = new WXLOG_REPORTER();
181 
182  return *s_wxLogReporter;
183 }
184 
185 
186 REPORTER& STATUSBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
187 {
188  if( m_statusBar )
189  m_statusBar->SetStatusText( aText, m_position );
190 
191  return *this;
192 }
193 
194 
196 {
197  if( m_statusBar )
198  return m_statusBar->GetStatusText().IsEmpty();
199 
200  return false;
201 }
202 
203 
205 {
206 }
207 
208 
209 REPORTER& INFOBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
210 {
211  m_message.reset( new wxString( aText ) );
212  m_severity = aSeverity;
213  m_messageSet = true;
214 
215  return *this;
216 }
217 
218 
220 {
221  return m_message && !m_message->IsEmpty();
222 }
223 
224 
226 {
227  // Don't do anything if no message was ever given
228  if( !m_infoBar || !m_messageSet )
229  return;
230 
231  // Short circuit if the message is empty and it is already hidden
232  if( !HasMessage() && !m_infoBar->IsShown() )
233  return;
234 
235  int icon = wxICON_NONE;
236 
237  switch( m_severity )
238  {
239  case RPT_SEVERITY_UNDEFINED: icon = wxICON_INFORMATION; break;
240  case RPT_SEVERITY_INFO: icon = wxICON_INFORMATION; break;
241  case RPT_SEVERITY_EXCLUSION: icon = wxICON_WARNING; break;
242  case RPT_SEVERITY_ACTION: icon = wxICON_WARNING; break;
243  case RPT_SEVERITY_WARNING: icon = wxICON_WARNING; break;
244  case RPT_SEVERITY_ERROR: icon = wxICON_ERROR; break;
245  case RPT_SEVERITY_IGNORE: icon = wxICON_INFORMATION; break;
246  }
247 
248  if( m_message->EndsWith( "\n" ) )
249  *m_message = m_message->Left( m_message->Length() - 1 );
250 
251  if( HasMessage() )
253  else
255 }
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:219
wxTextCtrl * m_textCtrl
Definition: reporter.h:156
static REPORTER & GetInstance()
Definition: reporter.cpp:175
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:158
static wxString FROM_UTF8(const char *cstring)
Convert a UTF8 encoded C string to a wxString for all wxWidgets build modes.
Definition: macros.h:110
REPORTER & ReportTail(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Places the report at the end of the list, for objects that support report ordering.
Definition: reporter.cpp:85
REPORTER & ReportHead(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Places the report at the beginning of the list for objects that support ordering.
Definition: reporter.cpp:95
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:105
WX_HTML_REPORT_PANEL * m_panel
Definition: reporter.h:213
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:75
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:70
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:69
This file contains miscellaneous commonly used macros and functions.
SEVERITY m_severity
Definition: reporter.h:342
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:128
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:43
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:111
int Count(int severityMask)
sets the frame label
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:186
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:209
void Finalize()
Update the infobar with the reported text.
Definition: reporter.cpp:225
SEVERITY
void Report(const wxString &aText, SEVERITY aSeverity, REPORTER::LOCATION aLocation=REPORTER::LOC_BODY)
Reports the string.
void QueueShowMessage(const wxString &aMessage, int aFlags=wxICON_INFORMATION)
Send the infobar an event telling it to show a message.
Definition: infobar.cpp:109
static REPORTER & GetInstance()
Definition: reporter.cpp:147
virtual ~INFOBAR_REPORTER()
Definition: reporter.cpp:204
void QueueDismiss()
Send the infobar an event telling it to hide itself.
Definition: infobar.cpp:120
WX_INFOBAR * m_infoBar
Definition: reporter.h:340
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:59
wxString * m_string
Definition: reporter.h:181
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:53
static REPORTER & GetInstance()
Definition: reporter.cpp:117
wxStatusBar * m_statusBar
Definition: reporter.h:301
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:195
std::unique_ptr< wxString > m_message
Definition: reporter.h:341