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.

245 lines
6.8 KiB

13 years ago
13 years ago
13 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 Wayne Stambaugh <stambaughw@verizon.net>
  5. * Copyright (C) 1992-2013 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef _REPORTER_H_
  25. #define _REPORTER_H_
  26. #include <wx/string.h>
  27. /**
  28. * @file reporter.h
  29. * @author Wayne Stambaugh
  30. * @note A special thanks to Dick Hollenbeck who came up with the idea that inspired
  31. * me to write this.
  32. */
  33. class wxTextCtrl;
  34. class wxHtmlListbox;
  35. class WX_HTML_REPORT_PANEL;
  36. /**
  37. * Class REPORTER
  38. * is a pure virtual class used to derive REPORTER objects from.
  39. *
  40. * The purpose of the REPORTER object is to offer a way for a procedural function
  41. * to report multiple errors without having to:
  42. * <ul>
  43. * <li> know too much about the caller's UI, i.e. wx. </li>
  44. * <li> stop after the first error </li>
  45. * </ul>
  46. * the reporter has 4 severity levels (flags) tagging the messages:
  47. * - information
  48. * - warning
  49. * - error
  50. * - action (i.e. indication of changes - add component, change footprint, etc. )
  51. * They are indicators for the message formatting and displaying code,
  52. * filtering is not made here.
  53. */
  54. class REPORTER {
  55. public:
  56. /**
  57. * Severity of the reported messages.
  58. * Undefined are default status messages
  59. * Info are processing messages for which no action is taken
  60. * Action messages are items that modify the file(s) as expected
  61. * Warning messages are items that might be problematic but don't prevent
  62. * the process from completing
  63. * Error messages are items that prevent the process from completing
  64. */
  65. //
  66. enum SEVERITY {
  67. RPT_UNDEFINED = 0x0,
  68. RPT_INFO = 0x1,
  69. RPT_ACTION = 0x2,
  70. RPT_WARNING = 0x4,
  71. RPT_ERROR = 0x8
  72. };
  73. static constexpr int RPT_ALL = RPT_INFO | RPT_ACTION | RPT_WARNING | RPT_ERROR;
  74. /**
  75. * Location where the message is to be reported.
  76. * LOC_HEAD messages are printed before all others (typically intro messages)
  77. * LOC_BODY messages are printed in the middle
  78. * LOC_TAIL messages are printed after all others (typically status messages)
  79. */
  80. enum LOCATION {
  81. LOC_HEAD = 0,
  82. LOC_BODY,
  83. LOC_TAIL
  84. };
  85. /**
  86. * Function Report
  87. * is a pure virtual function to override in the derived object.
  88. *
  89. * @param aText is the string to report.
  90. * @param aSeverity is an indicator ( RPT_UNDEFINED, RPT_INFO, RPT_WARNING,
  91. * RPT_ERROR, RPT_ACTION ) used to filter and format messages
  92. */
  93. virtual REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) = 0;
  94. /**
  95. * Function ReportTail
  96. * Places the report at the end of the list, for objects that support report ordering
  97. */
  98. virtual REPORTER& ReportTail( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED )
  99. {
  100. return Report( aText, aSeverity );
  101. }
  102. /**
  103. * Function ReportHead
  104. * Places the report at the beginning of the list for objects that support ordering
  105. */
  106. virtual REPORTER& ReportHead( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED )
  107. {
  108. return Report( aText, aSeverity );
  109. }
  110. REPORTER& Report( const char* aText, SEVERITY aSeverity = RPT_UNDEFINED );
  111. REPORTER& operator <<( const wxString& aText ) { return Report( aText ); }
  112. REPORTER& operator <<( const wxChar* aText ) { return Report( wxString( aText ) ); }
  113. REPORTER& operator <<( wxChar aChar ) { return Report( wxString( aChar ) ); }
  114. REPORTER& operator <<( const char* aText ) { return Report( aText ); }
  115. /**
  116. * Function HasMessage
  117. * Returns true if the reporter client is non-empty.
  118. */
  119. virtual bool HasMessage() const = 0;
  120. };
  121. /**
  122. * Class WX_TEXT_CTRL_REPORTER
  123. * is wrapper for reporting to a wxTextCtrl object.
  124. */
  125. class WX_TEXT_CTRL_REPORTER : public REPORTER
  126. {
  127. wxTextCtrl* m_textCtrl;
  128. public:
  129. WX_TEXT_CTRL_REPORTER( wxTextCtrl* aTextCtrl ) :
  130. REPORTER(),
  131. m_textCtrl( aTextCtrl )
  132. {
  133. }
  134. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  135. bool HasMessage() const override;
  136. };
  137. /**
  138. * Class WX_STRING_REPORTER
  139. * is a wrapper for reporting to a wxString object.
  140. */
  141. class WX_STRING_REPORTER : public REPORTER
  142. {
  143. wxString* m_string;
  144. public:
  145. WX_STRING_REPORTER( wxString* aString ) :
  146. REPORTER(),
  147. m_string( aString )
  148. {
  149. }
  150. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  151. bool HasMessage() const override;
  152. };
  153. /**
  154. * Class WX_HTML_PANEL_REPORTER
  155. * is a wrapper for reporting to a wx HTML window
  156. */
  157. class WX_HTML_PANEL_REPORTER : public REPORTER
  158. {
  159. WX_HTML_REPORT_PANEL* m_panel;
  160. public:
  161. WX_HTML_PANEL_REPORTER( WX_HTML_REPORT_PANEL* aPanel ) :
  162. REPORTER(),
  163. m_panel( aPanel )
  164. {
  165. }
  166. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  167. REPORTER& ReportTail( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  168. REPORTER& ReportHead( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  169. bool HasMessage() const override;
  170. };
  171. /**
  172. * Class NULL_REPORTER
  173. *
  174. * A singleton reporter that reports to nowhere. Used as to simplify code by
  175. * avoiding the reportee to check for a non-NULL reporter object.
  176. */
  177. class NULL_REPORTER : public REPORTER
  178. {
  179. public:
  180. NULL_REPORTER()
  181. {
  182. }
  183. static REPORTER& GetInstance();
  184. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  185. bool HasMessage() const override { return false; }
  186. };
  187. /**
  188. * Class STDOUT_REPORTER
  189. *
  190. * Debug type reporter, forwarding messages to std::cout.
  191. */
  192. class STDOUT_REPORTER : public REPORTER
  193. {
  194. public:
  195. STDOUT_REPORTER()
  196. {
  197. }
  198. static REPORTER& GetInstance();
  199. REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_UNDEFINED ) override;
  200. bool HasMessage() const override { return false; }
  201. };
  202. #endif // _REPORTER_H_