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.

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