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.

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