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
56bool REPORTER::HasMessageOfSeverity( int aSeverityMask ) const
57{
58 wxFAIL_MSG( "HasMessageOfSeverity is not implemented in this reporter" );
59 return HasMessage();
60}
61
62
63REPORTER& WX_TEXT_CTRL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
64{
65 wxCHECK_MSG( m_textCtrl != nullptr, *this,
66 wxT( "No wxTextCtrl object defined in WX_TEXT_CTRL_REPORTER." ) );
67
68 m_textCtrl->AppendText( aText + wxS( "\n" ) );
69 return *this;
70}
71
72
74{
75 return !m_textCtrl->IsEmpty();
76}
77
78
79REPORTER& WX_STRING_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
80{
81 m_severityMask |= aSeverity;
82 m_string << aText << wxS( "\n" );
83 return *this;
84}
85
86
87const wxString& WX_STRING_REPORTER::GetMessages() const
88{
89 return m_string;
90}
91
92
94{
96 m_string.clear();
97}
98
99
101{
102 return !m_string.IsEmpty();
103}
104
105
106bool WX_STRING_REPORTER::HasMessageOfSeverity( int aSeverityMask ) const
107{
108 return ( m_severityMask & aSeverityMask ) != 0;
109}
110
111
112REPORTER& NULL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
113{
114 return *this;
115}
116
117
119{
120 static REPORTER* s_nullReporter = nullptr;
121
122 if( !s_nullReporter )
123 s_nullReporter = new NULL_REPORTER();
124
125 return *s_nullReporter;
126}
127
128
129REPORTER& CLI_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
130{
131 FILE* target = stdout;
132
133 if( aSeverity == RPT_SEVERITY_ERROR )
134 target = stderr;
135
136 if( aMsg.EndsWith( wxS( "\n" ) ) )
137 wxFprintf( target, aMsg );
138 else
139 wxFprintf( target, aMsg + wxS( "\n" ) );
140
141 return *this;
142}
143
144
146{
147 static CLI_REPORTER s_cliReporter;
148
149 return s_cliReporter;
150}
151
152
153REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
154{
155 switch( aSeverity )
156 {
157 case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
158 case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
159 case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
160 case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
161 case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
162 case RPT_SEVERITY_DEBUG: std::cout << "SEVERITY_DEBUG: "; break;
164 case RPT_SEVERITY_IGNORE: break;
165 }
166
167 std::cout << aMsg << std::endl;
168
169 return *this;
170}
171
172
174{
175 static REPORTER* s_stdoutReporter = nullptr;
176
177 if( !s_stdoutReporter )
178 s_stdoutReporter = new STDOUT_REPORTER();
179
180 return *s_stdoutReporter;
181}
182
183
184REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY 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 if( m_statusBar )
217 m_statusBar->SetStatusText( aText, m_position );
218
219 return *this;
220}
221
222
224{
225 if( m_statusBar )
226 return !m_statusBar->GetStatusText( m_position ).IsEmpty();
227
228 return false;
229}
Reporter forwarding messages to stdout or stderr as appropriate.
Definition: reporter.h:226
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:129
static REPORTER & GetInstance()
Definition: reporter.cpp:145
static REPORTER & GetInstance()
Definition: reporter.cpp:118
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:112
A pure virtual class used to derive REPORTER objects from.
Definition: reporter.h:72
virtual bool HasMessageOfSeverity(int aSeverityMask) const
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition: reporter.cpp:56
virtual bool HasMessage() const =0
Returns true if the reporter client is non-empty.
virtual REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED)=0
Report a string with a given severity.
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:303
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:223
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:153
static REPORTER & GetInstance()
Definition: reporter.cpp:173
REPORTER & Report(const wxString &aMsg, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:184
static REPORTER & GetInstance()
Definition: reporter.cpp:202
wxString m_string
Definition: reporter.h:192
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:100
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:79
bool HasMessageOfSeverity(int aSeverityMask) const override
Returns true if the reporter has one or more messages matching the specified severity mask.
Definition: reporter.cpp:106
const wxString & GetMessages() const
Definition: reporter.cpp:87
wxTextCtrl * m_textCtrl
Definition: reporter.h:163
bool HasMessage() const override
Returns true if the reporter client is non-empty.
Definition: reporter.cpp:73
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
Definition: reporter.cpp:63
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)