kicad-source/include/reporter.h

313 lines
7.8 KiB
C
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 Wayne Stambaugh <stambaughw@gmail.com>
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
2016-01-12 11:33:33 -05:00
#ifndef _REPORTER_H_
#define _REPORTER_H_
#include <memory>
#include <map>
2020-10-24 10:45:37 -04:00
#include <eda_units.h>
#include <widgets/report_severity.h>
#include <kicommon.h>
2016-08-11 14:41:07 +02:00
2016-01-12 11:33:33 -05:00
/**
* @file reporter.h
* @author Wayne Stambaugh
* @note A special thanks to Dick Hollenbeck who came up with the idea that inspired
* me to write this.
* @warning Do not add any dependencies to wxWidgets (or any other third party UI library )
* to the REPORTER object. All wxWidgets objects should be defined by pointer or
* reference and forward declared so that using reporters in low level KiCad objects
* will not require pulling in wxWidgets to building them.
2016-01-12 11:33:33 -05:00
*/
class wxString;
class wxStatusBar;
class wxTextCtrl;
class WX_HTML_REPORT_PANEL;
class WX_INFOBAR;
/**
2020-12-21 10:17:52 -05:00
* A pure virtual class used to derive REPORTER objects from.
*
2013-06-13 06:43:29 -05:00
* The purpose of the REPORTER object is to offer a way for a procedural function
* to report multiple errors without having to:
* <ul>
* <li> know too much about the caller's UI, i.e. wx. </li>
* <li> stop after the first error </li>
2013-06-13 06:43:29 -05:00
* </ul>
* the reporter has 4 severity levels (flags) tagging the messages:
2020-12-21 10:17:52 -05:00
* - information
* - warning
* - error
* - action (i.e. indication of changes - add component, change footprint, etc. )
*
* They are indicators for the message formatting and displaying code,
* filtering is not made here.
*/
class KICOMMON_API REPORTER
2020-12-21 10:17:52 -05:00
{
public:
/**
* Location where the message is to be reported.
* LOC_HEAD messages are printed before all others (typically intro messages)
* LOC_BODY messages are printed in the middle
* LOC_TAIL messages are printed after all others (typically status messages)
*/
enum LOCATION {
LOC_HEAD = 0,
LOC_BODY,
LOC_TAIL
};
/**
2020-12-21 10:17:52 -05:00
* Report a string with a given severity.
*
* @param aText is the string to report.
2020-12-21 10:17:52 -05:00
* @param aSeverity is an indicator ( RPT_UNDEFINED, RPT_INFO, RPT_WARNING, RPT_ERROR,
* RPT_ACTION ) used to filter and format messages
*/
2020-12-21 10:17:52 -05:00
virtual REPORTER& Report( const wxString& aText,
SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) = 0;
/**
* Places the report at the end of the list, for objects that support report ordering
*/
2020-12-21 10:17:52 -05:00
virtual REPORTER& ReportTail( const wxString& aText,
SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED )
{
return Report( aText, aSeverity );
}
/**
2020-12-21 10:17:52 -05:00
* Places the report at the beginning of the list for objects that support ordering.
*/
2020-12-21 10:17:52 -05:00
virtual REPORTER& ReportHead( const wxString& aText,
SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED )
{
return Report( aText, aSeverity );
}
REPORTER& Report( const char* aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED );
REPORTER& operator <<( const wxString& aText ) { return Report( aText ); }
/**
* Returns true if the reporter client is non-empty.
*/
virtual bool HasMessage() const = 0;
/**
* Returns true if the reporter has one or more messages matching the specified
* severity mask.
*/
virtual bool HasMessageOfSeverity( int aSeverityMask ) const;
2020-09-10 19:28:00 +01:00
virtual EDA_UNITS GetUnits() const
{
2025-03-01 20:24:37 +00:00
return EDA_UNITS::MM;
2020-09-10 19:28:00 +01:00
}
virtual ~REPORTER()
{
}
};
/**
2020-12-21 10:17:52 -05:00
* A wrapper for reporting to a wxTextCtrl object.
*/
class KICOMMON_API WX_TEXT_CTRL_REPORTER : public REPORTER
{
public:
WX_TEXT_CTRL_REPORTER( wxTextCtrl* aTextCtrl ) :
REPORTER(),
m_textCtrl( aTextCtrl )
{
}
virtual ~WX_TEXT_CTRL_REPORTER()
{
}
2020-12-21 10:17:52 -05:00
REPORTER& Report( const wxString& aText,
SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
bool HasMessage() const override;
private:
wxTextCtrl* m_textCtrl;
};
/**
2020-12-21 10:17:52 -05:00
* A wrapper for reporting to a wxString object.
*/
class KICOMMON_API WX_STRING_REPORTER : public REPORTER
{
public:
WX_STRING_REPORTER() :
REPORTER(),
m_severityMask( 0 )
{
}
virtual ~WX_STRING_REPORTER()
{
}
REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
bool HasMessage() const override;
bool HasMessageOfSeverity( int aSeverityMask ) const override;
const wxString& GetMessages() const;
void Clear();
private:
wxString m_string;
int m_severityMask;
};
/**
2020-12-21 10:17:52 -05:00
* A singleton reporter that reports to nowhere.
*
2020-12-21 10:17:52 -05:00
* Used as to simplify code by avoiding the reportee to check for a non-NULL reporter object.
*/
class KICOMMON_API NULL_REPORTER : public REPORTER
{
public:
NULL_REPORTER()
{
}
virtual ~NULL_REPORTER()
{
}
static REPORTER& GetInstance();
2020-12-21 10:17:52 -05:00
REPORTER& Report( const wxString& aText,
SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
bool HasMessage() const override { return false; }
};
/**
* Reporter forwarding messages to stdout or stderr as appropriate
*/
class KICOMMON_API CLI_REPORTER : public REPORTER
{
public:
CLI_REPORTER()
{
}
virtual ~CLI_REPORTER()
{
}
static REPORTER& GetInstance();
REPORTER& Report( const wxString& aMsg, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
bool HasMessage() const override { return false; }
bool HasMessageOfSeverity( int aSeverityMask ) const override;
private:
std::map<SEVERITY, bool> m_hasMessageMap;
};
/**
* Debug type reporter, forwarding messages to std::cout.
*/
class KICOMMON_API STDOUT_REPORTER : public REPORTER
{
public:
STDOUT_REPORTER()
{
}
virtual ~STDOUT_REPORTER()
{
}
static REPORTER& GetInstance();
REPORTER& Report( const wxString& aMsg, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
bool HasMessage() const override { return false; }
};
class KICOMMON_API WXLOG_REPORTER : public REPORTER
{
public:
WXLOG_REPORTER()
{
}
virtual ~WXLOG_REPORTER()
{
}
static REPORTER& GetInstance();
REPORTER& Report( const wxString& aMsg, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
bool HasMessage() const override { return false; }
};
/**
2020-12-21 10:17:52 -05:00
* A wrapper for reporting to a specific text location in a statusbar.
*/
class KICOMMON_API STATUSBAR_REPORTER : public REPORTER
{
public:
STATUSBAR_REPORTER( wxStatusBar* aStatusBar, int aPosition = 0 )
2020-12-21 10:17:52 -05:00
: REPORTER(),
m_statusBar( aStatusBar ),
m_position( aPosition )
{
}
REPORTER& Report( const wxString& aText, SEVERITY aSeverity = RPT_SEVERITY_UNDEFINED ) override;
bool HasMessage() const override;
2020-12-21 10:17:52 -05:00
private:
wxStatusBar* m_statusBar;
int m_position;
};
#endif // _REPORTER_H_