You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
6.9 KiB

3 years ago
  1. /**
  2. * @file reporter.cpp
  3. */
  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. #include <macros.h>
  28. #include <reporter.h>
  29. #include <widgets/wx_infobar.h>
  30. #include <widgets/wx_html_report_panel.h>
  31. #include <wx/log.h>
  32. #include <wx/textctrl.h>
  33. #include <wx/statusbr.h>
  34. REPORTER& REPORTER::Report( const char* aText, SEVERITY aSeverity )
  35. {
  36. Report( FROM_UTF8( aText ) );
  37. return *this;
  38. }
  39. REPORTER& WX_TEXT_CTRL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  40. {
  41. wxCHECK_MSG( m_textCtrl != nullptr, *this,
  42. wxT( "No wxTextCtrl object defined in WX_TEXT_CTRL_REPORTER." ) );
  43. m_textCtrl->AppendText( aText + wxS( "\n" ) );
  44. return *this;
  45. }
  46. bool WX_TEXT_CTRL_REPORTER::HasMessage() const
  47. {
  48. return !m_textCtrl->IsEmpty();
  49. }
  50. REPORTER& WX_STRING_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  51. {
  52. wxCHECK_MSG( m_string != nullptr, *this,
  53. wxT( "No wxString object defined in WX_STRING_REPORTER." ) );
  54. *m_string << aText << wxS( "\n" );
  55. return *this;
  56. }
  57. bool WX_STRING_REPORTER::HasMessage() const
  58. {
  59. return !m_string->IsEmpty();
  60. }
  61. REPORTER& WX_HTML_PANEL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  62. {
  63. wxCHECK_MSG( m_panel != nullptr, *this,
  64. wxT( "No WX_HTML_REPORT_PANEL object defined in WX_HTML_PANEL_REPORTER." ) );
  65. m_panel->Report( aText, aSeverity );
  66. return *this;
  67. }
  68. REPORTER& WX_HTML_PANEL_REPORTER::ReportTail( const wxString& aText, SEVERITY aSeverity )
  69. {
  70. wxCHECK_MSG( m_panel != nullptr, *this,
  71. wxT( "No WX_HTML_REPORT_PANEL object defined in WX_HTML_PANEL_REPORTER." ) );
  72. m_panel->Report( aText, aSeverity, LOC_TAIL );
  73. return *this;
  74. }
  75. REPORTER& WX_HTML_PANEL_REPORTER::ReportHead( 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. m_panel->Report( aText, aSeverity, LOC_HEAD );
  80. return *this;
  81. }
  82. bool WX_HTML_PANEL_REPORTER::HasMessage() const
  83. {
  84. return m_panel->Count( RPT_SEVERITY_ERROR | RPT_SEVERITY_WARNING ) > 0;
  85. }
  86. REPORTER& NULL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  87. {
  88. return *this;
  89. }
  90. REPORTER& NULL_REPORTER::GetInstance()
  91. {
  92. static REPORTER* s_nullReporter = nullptr;
  93. if( !s_nullReporter )
  94. s_nullReporter = new NULL_REPORTER();
  95. return *s_nullReporter;
  96. }
  97. REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
  98. {
  99. switch( aSeverity )
  100. {
  101. case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
  102. case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
  103. case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
  104. case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
  105. case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
  106. case RPT_SEVERITY_DEBUG: std::cout << "SEVERITY_DEBUG: "; break;
  107. case RPT_SEVERITY_EXCLUSION:
  108. case RPT_SEVERITY_IGNORE: break;
  109. }
  110. std::cout << aMsg << std::endl;
  111. return *this;
  112. }
  113. REPORTER& STDOUT_REPORTER::GetInstance()
  114. {
  115. static REPORTER* s_stdoutReporter = nullptr;
  116. if( !s_stdoutReporter )
  117. s_stdoutReporter = new STDOUT_REPORTER();
  118. return *s_stdoutReporter;
  119. }
  120. REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
  121. {
  122. switch( aSeverity )
  123. {
  124. case RPT_SEVERITY_ERROR: wxLogError( aMsg ); break;
  125. case RPT_SEVERITY_WARNING: wxLogWarning( aMsg ); break;
  126. case RPT_SEVERITY_UNDEFINED: wxLogMessage( aMsg ); break;
  127. case RPT_SEVERITY_INFO: wxLogInfo( aMsg ); break;
  128. case RPT_SEVERITY_ACTION: wxLogInfo( aMsg ); break;
  129. case RPT_SEVERITY_DEBUG: wxLogDebug( aMsg ); break;
  130. case RPT_SEVERITY_EXCLUSION: break;
  131. case RPT_SEVERITY_IGNORE: break;
  132. }
  133. return *this;
  134. }
  135. REPORTER& WXLOG_REPORTER::GetInstance()
  136. {
  137. static REPORTER* s_wxLogReporter = nullptr;
  138. if( !s_wxLogReporter )
  139. s_wxLogReporter = new WXLOG_REPORTER();
  140. return *s_wxLogReporter;
  141. }
  142. REPORTER& STATUSBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  143. {
  144. if( m_statusBar )
  145. m_statusBar->SetStatusText( aText, m_position );
  146. return *this;
  147. }
  148. bool STATUSBAR_REPORTER::HasMessage() const
  149. {
  150. if( m_statusBar )
  151. return m_statusBar->GetStatusText().IsEmpty();
  152. return false;
  153. }
  154. INFOBAR_REPORTER::~INFOBAR_REPORTER()
  155. {
  156. }
  157. REPORTER& INFOBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  158. {
  159. m_message.reset( new wxString( aText ) );
  160. m_severity = aSeverity;
  161. m_messageSet = true;
  162. return *this;
  163. }
  164. bool INFOBAR_REPORTER::HasMessage() const
  165. {
  166. return m_message && !m_message->IsEmpty();
  167. }
  168. void INFOBAR_REPORTER::Finalize()
  169. {
  170. // Don't do anything if no message was ever given
  171. if( !m_infoBar || !m_messageSet )
  172. return;
  173. // Short circuit if the message is empty and it is already hidden
  174. if( !HasMessage() && !m_infoBar->IsShown() )
  175. return;
  176. int icon = wxICON_NONE;
  177. switch( m_severity )
  178. {
  179. case RPT_SEVERITY_UNDEFINED: icon = wxICON_INFORMATION; break;
  180. case RPT_SEVERITY_INFO: icon = wxICON_INFORMATION; break;
  181. case RPT_SEVERITY_EXCLUSION: icon = wxICON_WARNING; break;
  182. case RPT_SEVERITY_ACTION: icon = wxICON_WARNING; break;
  183. case RPT_SEVERITY_WARNING: icon = wxICON_WARNING; break;
  184. case RPT_SEVERITY_ERROR: icon = wxICON_ERROR; break;
  185. case RPT_SEVERITY_IGNORE: icon = wxICON_INFORMATION; break;
  186. case RPT_SEVERITY_DEBUG: icon = wxICON_INFORMATION; break;
  187. }
  188. if( m_message->EndsWith( wxS( "\n" ) ) )
  189. *m_message = m_message->Left( m_message->Length() - 1 );
  190. if( HasMessage() )
  191. m_infoBar->QueueShowMessage( *m_message, icon );
  192. else
  193. m_infoBar->QueueDismiss();
  194. }