Exception extension to only report once

This commit is contained in:
Abdullah Tahiri
2017-05-20 16:04:04 +02:00
committed by wmayer
parent 4ad3f9993d
commit 572bd0ae01
2 changed files with 67 additions and 56 deletions

View File

@@ -38,24 +38,24 @@ TYPESYSTEM_SOURCE(Base::Exception,Base::BaseClass);
Exception::Exception(void) Exception::Exception(void)
: _line(0), _isTranslatable(false) : _line(0), _isTranslatable(false), _isReported(false)
{ {
_sErrMsg = "FreeCAD Exception"; _sErrMsg = "FreeCAD Exception";
} }
Exception::Exception(const Exception &inst) Exception::Exception(const Exception &inst)
: _sErrMsg(inst._sErrMsg), _file(inst._file), : _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) 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) 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(); return _sErrMsg.c_str();
} }
void Exception::ReportException (void) const void Exception::ReportException (void)
{ {
std::string str = ""; if(!_isReported) {
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;
}
}
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) PyObject * Exception::getPyObject(void)
@@ -117,6 +120,7 @@ PyObject * Exception::getPyObject(void)
edict.setItem("sfunction", Py::String(this->getFunction())); edict.setItem("sfunction", Py::String(this->getFunction()));
edict.setItem("swhat", Py::String(this->what())); edict.setItem("swhat", Py::String(this->what()));
edict.setItem("btranslatable", Py::Boolean(this->getTranslatable())); edict.setItem("btranslatable", Py::Boolean(this->getTranslatable()));
edict.setItem("breported", Py::Boolean(this->_isReported));
return Py::new_reference_to(edict); return Py::new_reference_to(edict);
} }
@@ -141,6 +145,8 @@ void Exception::setPyObject( PyObject * pydict)
#endif #endif
if (edict.hasKey("btranslatable")) if (edict.hasKey("btranslatable"))
_isTranslatable = static_cast<bool>(Py::Boolean(edict.getItem("btranslatable"))); _isTranslatable = static_cast<bool>(Py::Boolean(edict.getItem("btranslatable")));
if (edict.hasKey("breported"))
_isReported = static_cast<bool>(Py::Boolean(edict.getItem("breported")));
} }
} }
@@ -266,34 +272,37 @@ const char* FileException::what() const throw()
return _sErrMsgAndFileName.c_str(); return _sErrMsgAndFileName.c_str();
} }
void FileException::ReportException (void) const void FileException::ReportException (void)
{ {
std::string str = ""; if(!_isReported) {
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 (pos!=std::string::npos) { if(!_sErrMsgAndFileName.empty())
str+="in "; str+= (_sErrMsgAndFileName + " ");
str+= _file.substr(pos);
str+= ":"; if(!_function.empty()) {
str+=_linestr; 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) PyObject * FileException::getPyObject(void)

View File

@@ -98,7 +98,8 @@ public:
virtual const char* what(void) const throw(); 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 char * sMessage);
inline void setMessage(const std::string& sMessage); inline void setMessage(const std::string& sMessage);
@@ -138,6 +139,7 @@ protected:
int _line; int _line;
std::string _function; std::string _function;
bool _isTranslatable; bool _isTranslatable;
bool _isReported;
}; };
@@ -223,7 +225,7 @@ public:
/// Description of the exception /// Description of the exception
virtual const char* what() const throw(); virtual const char* what() const throw();
/// Report generation /// Report generation
virtual void ReportException (void) const; virtual void ReportException (void);
/// Get file name for use with tranlatable message /// Get file name for use with tranlatable message
std::string getFileName() const; std::string getFileName() const;
/// returns a Python dictionary containing the exception data /// returns a Python dictionary containing the exception data