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.

224 lines
5.5 KiB

  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, 2024 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 <wx/crt.h>
  32. #include <wx/log.h>
  33. #include <wx/textctrl.h>
  34. #include <wx/statusbr.h>
  35. /**
  36. * Flag to enable reporter debugging output.
  37. *
  38. * @ingroup trace_env_vars
  39. */
  40. static const wxChar traceReporter[] = wxT( "KICAD_REPORTER" );
  41. REPORTER& REPORTER::Report( const char* aText, SEVERITY aSeverity )
  42. {
  43. Report( From_UTF8( aText ) );
  44. return *this;
  45. }
  46. bool REPORTER::HasMessageOfSeverity( int aSeverityMask ) const
  47. {
  48. wxFAIL_MSG( "HasMessageOfSeverity is not implemented in this reporter" );
  49. return HasMessage();
  50. }
  51. REPORTER& WX_TEXT_CTRL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  52. {
  53. wxCHECK_MSG( m_textCtrl != nullptr, *this,
  54. wxT( "No wxTextCtrl object defined in WX_TEXT_CTRL_REPORTER." ) );
  55. m_textCtrl->AppendText( aText + wxS( "\n" ) );
  56. return *this;
  57. }
  58. bool WX_TEXT_CTRL_REPORTER::HasMessage() const
  59. {
  60. return !m_textCtrl->IsEmpty();
  61. }
  62. REPORTER& WX_STRING_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  63. {
  64. m_severityMask |= aSeverity;
  65. m_string << aText << wxS( "\n" );
  66. return *this;
  67. }
  68. const wxString& WX_STRING_REPORTER::GetMessages() const
  69. {
  70. return m_string;
  71. }
  72. void WX_STRING_REPORTER::Clear()
  73. {
  74. m_severityMask = 0;
  75. m_string.clear();
  76. }
  77. bool WX_STRING_REPORTER::HasMessage() const
  78. {
  79. return !m_string.IsEmpty();
  80. }
  81. bool WX_STRING_REPORTER::HasMessageOfSeverity( int aSeverityMask ) const
  82. {
  83. return ( m_severityMask & aSeverityMask ) != 0;
  84. }
  85. REPORTER& NULL_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  86. {
  87. return *this;
  88. }
  89. REPORTER& NULL_REPORTER::GetInstance()
  90. {
  91. static REPORTER* s_nullReporter = nullptr;
  92. if( !s_nullReporter )
  93. s_nullReporter = new NULL_REPORTER();
  94. return *s_nullReporter;
  95. }
  96. REPORTER& CLI_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
  97. {
  98. FILE* target = stdout;
  99. if( aSeverity == RPT_SEVERITY_ERROR )
  100. target = stderr;
  101. if( aMsg.EndsWith( wxS( "\n" ) ) )
  102. wxFprintf( target, aMsg );
  103. else
  104. wxFprintf( target, aMsg + wxS( "\n" ) );
  105. return *this;
  106. }
  107. REPORTER& CLI_REPORTER::GetInstance()
  108. {
  109. static CLI_REPORTER s_cliReporter;
  110. return s_cliReporter;
  111. }
  112. REPORTER& STDOUT_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
  113. {
  114. switch( aSeverity )
  115. {
  116. case RPT_SEVERITY_UNDEFINED: std::cout << "SEVERITY_UNDEFINED: "; break;
  117. case RPT_SEVERITY_INFO: std::cout << "SEVERITY_INFO: "; break;
  118. case RPT_SEVERITY_WARNING: std::cout << "SEVERITY_WARNING: "; break;
  119. case RPT_SEVERITY_ERROR: std::cout << "SEVERITY_ERROR: "; break;
  120. case RPT_SEVERITY_ACTION: std::cout << "SEVERITY_ACTION: "; break;
  121. case RPT_SEVERITY_DEBUG: std::cout << "SEVERITY_DEBUG: "; break;
  122. case RPT_SEVERITY_EXCLUSION:
  123. case RPT_SEVERITY_IGNORE: break;
  124. }
  125. std::cout << aMsg << std::endl;
  126. return *this;
  127. }
  128. REPORTER& STDOUT_REPORTER::GetInstance()
  129. {
  130. static REPORTER* s_stdoutReporter = nullptr;
  131. if( !s_stdoutReporter )
  132. s_stdoutReporter = new STDOUT_REPORTER();
  133. return *s_stdoutReporter;
  134. }
  135. REPORTER& WXLOG_REPORTER::Report( const wxString& aMsg, SEVERITY aSeverity )
  136. {
  137. switch( aSeverity )
  138. {
  139. case RPT_SEVERITY_ERROR: wxLogError( aMsg ); break;
  140. case RPT_SEVERITY_WARNING: wxLogWarning( aMsg ); break;
  141. case RPT_SEVERITY_UNDEFINED: wxLogMessage( aMsg ); break;
  142. case RPT_SEVERITY_INFO: wxLogInfo( aMsg ); break;
  143. case RPT_SEVERITY_ACTION: wxLogInfo( aMsg ); break;
  144. case RPT_SEVERITY_DEBUG: wxLogTrace( traceReporter, aMsg ); break;
  145. case RPT_SEVERITY_EXCLUSION: break;
  146. case RPT_SEVERITY_IGNORE: break;
  147. }
  148. return *this;
  149. }
  150. REPORTER& WXLOG_REPORTER::GetInstance()
  151. {
  152. static REPORTER* s_wxLogReporter = nullptr;
  153. if( !s_wxLogReporter )
  154. s_wxLogReporter = new WXLOG_REPORTER();
  155. return *s_wxLogReporter;
  156. }
  157. REPORTER& STATUSBAR_REPORTER::Report( const wxString& aText, SEVERITY aSeverity )
  158. {
  159. if( m_statusBar )
  160. m_statusBar->SetStatusText( aText, m_position );
  161. return *this;
  162. }
  163. bool STATUSBAR_REPORTER::HasMessage() const
  164. {
  165. if( m_statusBar )
  166. return !m_statusBar->GetStatusText( m_position ).IsEmpty();
  167. return false;
  168. }