From 572bd0ae0183477350ec002982ddd598d649f70b Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Sat, 20 May 2017 16:04:04 +0200 Subject: [PATCH] Exception extension to only report once --- src/Base/Exception.cpp | 117 ++++++++++++++++++++++------------------- src/Base/Exception.h | 6 ++- 2 files changed, 67 insertions(+), 56 deletions(-) diff --git a/src/Base/Exception.cpp b/src/Base/Exception.cpp index d8cb803401..b0092ed5f2 100644 --- a/src/Base/Exception.cpp +++ b/src/Base/Exception.cpp @@ -38,24 +38,24 @@ TYPESYSTEM_SOURCE(Base::Exception,Base::BaseClass); Exception::Exception(void) - : _line(0), _isTranslatable(false) +: _line(0), _isTranslatable(false), _isReported(false) { _sErrMsg = "FreeCAD Exception"; } Exception::Exception(const Exception &inst) : _sErrMsg(inst._sErrMsg), _file(inst._file), - _line(inst._line), _function(inst._function), _isTranslatable(inst._isTranslatable) + _line(inst._line), _function(inst._function), _isTranslatable(inst._isTranslatable), _isReported(inst._isReported) { } Exception::Exception(const char * sMessage) -: _sErrMsg(sMessage), _line(0), _isTranslatable(false) +: _sErrMsg(sMessage), _line(0), _isTranslatable(false), _isReported(false) { } Exception::Exception(const std::string& sMessage) -: _sErrMsg(sMessage), _line(0), _isTranslatable(false) +: _sErrMsg(sMessage), _line(0), _isTranslatable(false), _isReported(false) { } @@ -73,34 +73,37 @@ const char* Exception::what(void) const throw() return _sErrMsg.c_str(); } -void Exception::ReportException (void) const +void Exception::ReportException (void) { - std::string str = ""; - - if(!_sErrMsg.empty()) - str+= (_sErrMsg + " "); - - if(!_function.empty()) { - str+="In "; - str+=_function; - str+= " "; - } - - std::string _linestr = std::to_string(_line); - - if(!_file.empty() && !_linestr.empty()) { - // strip absolute path - std::size_t pos = _file.find("src"); - - if (pos!=std::string::npos) { - str+="in "; - str+= _file.substr(pos); - str+= ":"; - str+=_linestr; - } - } + if(!_isReported) { + std::string str = ""; - Console().Error("Exception (%s): %s \n",Console().Time(),str.c_str()); + if(!_sErrMsg.empty()) + str+= (_sErrMsg + " "); + + if(!_function.empty()) { + str+="In "; + str+=_function; + str+= " "; + } + + std::string _linestr = std::to_string(_line); + + if(!_file.empty() && !_linestr.empty()) { + // strip absolute path + std::size_t pos = _file.find("src"); + + if (pos!=std::string::npos) { + str+="in "; + str+= _file.substr(pos); + str+= ":"; + str+=_linestr; + } + } + + Console().Error("Exception (%s): %s \n",Console().Time(),str.c_str()); + _isReported = true; + } } PyObject * Exception::getPyObject(void) @@ -117,6 +120,7 @@ PyObject * Exception::getPyObject(void) edict.setItem("sfunction", Py::String(this->getFunction())); edict.setItem("swhat", Py::String(this->what())); edict.setItem("btranslatable", Py::Boolean(this->getTranslatable())); + edict.setItem("breported", Py::Boolean(this->_isReported)); return Py::new_reference_to(edict); } @@ -141,6 +145,8 @@ void Exception::setPyObject( PyObject * pydict) #endif if (edict.hasKey("btranslatable")) _isTranslatable = static_cast(Py::Boolean(edict.getItem("btranslatable"))); + if (edict.hasKey("breported")) + _isReported = static_cast(Py::Boolean(edict.getItem("breported"))); } } @@ -266,34 +272,37 @@ const char* FileException::what() const throw() return _sErrMsgAndFileName.c_str(); } -void FileException::ReportException (void) const +void FileException::ReportException (void) { - std::string str = ""; - - if(!_sErrMsgAndFileName.empty()) - str+= (_sErrMsgAndFileName + " "); - - if(!_function.empty()) { - str+="In "; - str+=_function; - str+= " "; - } - - std::string _linestr = std::to_string(_line); - - if(!_file.empty() && !_linestr.empty()) { - // strip absolute path - std::size_t pos = _file.find("src"); + if(!_isReported) { + std::string str = ""; - if (pos!=std::string::npos) { - str+="in "; - str+= _file.substr(pos); - str+= ":"; - str+=_linestr; + if(!_sErrMsgAndFileName.empty()) + str+= (_sErrMsgAndFileName + " "); + + if(!_function.empty()) { + str+="In "; + str+=_function; + str+= " "; } + + std::string _linestr = std::to_string(_line); + + if(!_file.empty() && !_linestr.empty()) { + // strip absolute path + std::size_t pos = _file.find("src"); + + if (pos!=std::string::npos) { + str+="in "; + str+= _file.substr(pos); + str+= ":"; + str+=_linestr; + } + } + + Console().Error("Exception (%s): %s \n",Console().Time(),str.c_str()); + _isReported = true; } - - Console().Error("Exception (%s): %s \n",Console().Time(),str.c_str()); } PyObject * FileException::getPyObject(void) diff --git a/src/Base/Exception.h b/src/Base/Exception.h index ef2c1e5d1d..a21b3f319d 100644 --- a/src/Base/Exception.h +++ b/src/Base/Exception.h @@ -98,7 +98,8 @@ public: virtual const char* what(void) const throw(); - virtual void ReportException (void) const; + /// Reports exception. It includes a mechanism to only report an exception once. + virtual void ReportException (void); inline void setMessage(const char * sMessage); inline void setMessage(const std::string& sMessage); @@ -138,6 +139,7 @@ protected: int _line; std::string _function; bool _isTranslatable; + bool _isReported; }; @@ -223,7 +225,7 @@ public: /// Description of the exception virtual const char* what() const throw(); /// Report generation - virtual void ReportException (void) const; + virtual void ReportException (void); /// Get file name for use with tranlatable message std::string getFileName() const; /// returns a Python dictionary containing the exception data