FMT 1.2.0
Forest management tools for forest planning
Loading...
Searching...
No Matches
FMTExceptionHandler.h
Go to the documentation of this file.
1/*
2Copyright (c) 2019 Gouvernement du Québec
3
4SPDX-License-Identifier: LiLiQ-R-1.1
5License-Filename: LICENSES/EN/LiLiQ-R11unicode.txt
6*/
7
8#ifndef FMTexceptionhandler_Hm_included
9#define FMTexceptionhandler_Hm_included
10
11
12#include "FMTLogger.h"
13#include <unordered_map>
14#include <boost/serialization/serialization.hpp>
15#include <boost/serialization/nvp.hpp>
16#include <boost/serialization/export.hpp>
17#include <memory>
18#include <set>
19#include <boost/thread.hpp>
20#if defined _MSC_VER
22#endif
23
24namespace Exception
25{
26 class FMTException;
27 class FMTWarning;
28 // DocString: FMTExceptionHandler
34 {
35 public:
36 // DocString: checkSignals()
40 void checkSignals() const;
41 // DocString: FMTExceptionHandler()
46 // DocString: ~FMTExceptionHandler()
50 virtual ~FMTExceptionHandler() = default;
51 // DocString: FMTExceptionHandler(const FMTExceptionHandler&)
57 // DocString: FMTExceptionHandler(const std::unique_ptr<Logging::FMTLogger>&)
62 FMTExceptionHandler(const std::unique_ptr<Logging::FMTLogger>& logger);
63 // DocString: FMTExceptionHandler::passInLogger
68 void passInLogger(const std::unique_ptr<Logging::FMTLogger>& logger);
69
70 // DocString: FMTExceptionHandler::getCplData
76 #if defined FMTWITHGDAL
77 // DocString: FMTExceptionHandler::handelCplError
84 virtual void handelCplError(int eErrClass, int nError, const char* pszErrorMsg);
85 #endif
86 // DocString: FMTExceptionHandler::operator=
92 FMTExceptionHandler& operator = (const FMTExceptionHandler& rhs);
93 // DocString: FMTExceptionHandler::throwNested
100 void throwNested(const std::exception& texception, int& level, bool rethrow = true);
101 // DocString: FMTExceptionHandler::printExceptions
110 virtual void printExceptions(std::string text,
111 const std::string& method, const int& line, const std::string& fil,
113 // DocString: FMTExceptionHandler::raise
125 virtual FMTException raise(FMTexc lexception, std::string text,
126 const std::string& method, const int& line, const std::string& file,
127 Core::FMTsection lsection = Core::FMTsection::Empty, bool throwit = true);
128 // DocString: FMTExceptionHandler::raiseFromCatch
138 virtual FMTException raiseFromCatch(std::string text,
139 const std::string& method, const int& line, const std::string& file,
141 // DocString: FMTExceptionHandler::raiseFromThreadCatch
150 void raiseFromThreadCatch(std::string text,
151 const std::string& method, const int& line, const std::string& file,
153 // DocString: FMTExceptionHandler::reRaiseIfThreadCrash
158 // DocString: FMTExceptionHandler::enableNestedExceptions
163 // DocString: FMTExceptionHandler::disableNestedExceptions
168 // DocString: setErrorsToWarnings()
173 void setErrorsToWarnings(const std::vector<Exception::FMTexc>& errors);
174 // DocString: setMaxWarningsBeforeSilenced()
179 void setMaxWarningsBeforeSilenced(const size_t& maxwarningcount);
180 // DocString: FMTExceptionHandler::clone
185 virtual std::unique_ptr <FMTExceptionHandler> clone() const = 0;
186 #if defined _MSC_VER
187 // DocString: FMTExceptionHandler::translateStructuralWIN32Exceptions
192 static void translateStructuralWIN32Exceptions(unsigned int p_u, EXCEPTION_POINTERS*);
193 #endif
194 // DocString: getErrorDescription(bool,FMTexc)
201 static std::string getErrorDescription(bool p_french, FMTexc p_type);
202 // DocString: getErrorsToIgnore
207 static std::vector<FMTexc> getErrorsToIgnore();
208 protected:
209 // DocString: FMTExceptionHandler::_exception
212 // DocString: FMTExceptionHandler::_errorcount
215 // DocString: FMTExceptionHandler::m_maxwarningsbeforesilenced
218 // DocString: FMTExceptionHandler::_logger
221 // DocString: FMTExceptionHandler::m_usenestedexceptions
224 // DocString: FMTExceptionHandler::m_mtx
226 //mutable std::recursive_mutex mtx;
227 mutable boost::recursive_mutex m_mtx;
228 // DocString: FMTExceptionHandler::m_errorstowarnings
230 std::vector<Exception::FMTexc>m_errorstowarnings;
231 // DocString: FMTExceptionHandler::registred_threads
233 std::set<boost::thread::id> m_registered_threads;
234 // DocString: FMTExceptionHandler::m_mainthreadid
236 static boost::thread::id m_mainthreadid;
237 // DocString: FMTExceptionHandler::m_crashedthreadid
239 static boost::thread::id m_crashedthreadid;
240 // DocString: FMTExceptionHandler::threadcrashexceptions
242 std::exception_ptr m_threadcrashexception;
243 // DocString: FMTExceptionHandler::_updateStatus
248 void _updateStatus(std::unique_ptr<FMTException>& p_exception);
249 // DocString: FMTExceptionHandler(FMTexc,Core::FMTsection,const std::string&,const std::string&,const std::string&,int)
259 static std::unique_ptr<FMTException> _createException(FMTexc p_exception, Core::FMTsection p_section, const std::string& p_message,
260 const std::string& p_method, const std::string& p_file, int p_line);
261 // DocString: FMTExceptionHandler::_needToRethrow
266 bool _needToRethrow() const;
267 // DocString: FMTExceptionHandler::_isMainThread()
272 bool _isMainThread() const;
273 // DocString: FMTExceptionHandler::_isThrowedOnThread()
278 bool _isThrowedOnThread() const;
279 // DocString: FMTExceptionHandler::_isThisThreadThrowed()
285 // DocString: FMTExceptionHandler::registerthread
290 // DocString: FMTExceptionHandler::isregistered
296 // DocString: FMTExceptionHandler::_gutsOfPrintExceptions
307 void _gutsOfPrintExceptions(std::string text,
308 const std::string& method, const int& line, const std::string& fil,
309 int& levelreference,Core::FMTsection lsection = Core::FMTsection::Empty,bool logfirstlevel = true);
310 // DocString: FMTExceptionHandler::_gutsOfExceptionLog
316 void _gutsOfExceptionLog(const std::exception& texception,const int& level);
317 // DocString: FMTExceptionHandler::_updateWarningCount
322 void _updateWarningCount(const FMTException& p_warning);
323 private:
324 // DocString: FMTExceptionHandler::serialize
331 friend class boost::serialization::access;
332 template<class Archive>
333 void serialize(Archive& ar, const unsigned int version)
334 {
335 ar& BOOST_SERIALIZATION_NVP(_exception);
336 ar& BOOST_SERIALIZATION_NVP(_logger);
337 ar& BOOST_SERIALIZATION_NVP(m_usenestedexceptions);
338 }
339 #if defined _MSC_VER
340 // DocString: FMTScopedSeTranslator::m_SeTranslator
342 static FMTScopedSeTranslator m_SeTranslator;
343 #endif
344 // DocString: FMTExceptionHandler::_specificwarningcount
346 std::unordered_map<FMTexc, size_t> m_specificwarningcount;
347
348 };
349
350}
351
352BOOST_CLASS_EXPORT_KEY(Exception::FMTExceptionHandler)
353
354#endif
#define FMTEXPORT
Definition: FMTutility.h:125
Base class used to handle errors thrown in FMT.
Definition: FMTExceptionHandler.h:34
static boost::thread::id m_mainthreadid
Main thread id.
Definition: FMTExceptionHandler.h:236
std::vector< Exception::FMTexc > m_errorstowarnings
If an error is in this list it's going to processed like a warning.
Definition: FMTExceptionHandler.h:230
static boost::thread::id m_crashedthreadid
The thread id of the crashed thread.
Definition: FMTExceptionHandler.h:239
Logging::FMTLogger * _logger
pointer to the logger used to print the warning / error.
Definition: FMTExceptionHandler.h:220
FMTExceptionHandler()
Default constructor for FMTExceptionHandler.
void _gutsOfPrintExceptions(std::string text, const std::string &method, const int &line, const std::string &fil, int &levelreference, Core::FMTsection lsection=Core::FMTsection::Empty, bool logfirstlevel=true)
The guts of printExceptions.
void setErrorsToWarnings(const std::vector< Exception::FMTexc > &errors)
Set a list of errors to be cast to warnings.
std::set< boost::thread::id > m_registered_threads
This is the level of the last FMTException thrown by the FMTExceptionHandler.
Definition: FMTExceptionHandler.h:233
void _registerWorkerThread()
Register a worker thread so the handler knows it is dealing with a slave thread and not the main one.
virtual FMTException raise(FMTexc lexception, std::string text, const std::string &method, const int &line, const std::string &file, Core::FMTsection lsection=Core::FMTsection::Empty, bool throwit=true)
Throw a FMTException based on the exception type, section, message and source location....
static std::vector< FMTexc > getErrorsToIgnore()
gives you a vector of error you can safely turn into warning using setErrorsToWarning return a vector...
boost::recursive_mutex m_mtx
Mutex for multi-threading.
Definition: FMTExceptionHandler.h:227
void checkSignals() const
Check signals in R and Python if on the main thread.
void raiseFromThreadCatch(std::string text, const std::string &method, const int &line, const std::string &file, Core::FMTsection lsection=Core::FMTsection::Empty)
Raise an exception from a catch body on a worker thread, catching all exceptions and printing them wi...
void _gutsOfExceptionLog(const std::exception &texception, const int &level)
The guts of logging exceptions.
void setMaxWarningsBeforeSilenced(const size_t &maxwarningcount)
Setter for the maximum number of warnings before they are silenced.
bool _isThreadRegistered() const
Return true if the thread is registered.
FMTExceptionHandler(const std::unique_ptr< Logging::FMTLogger > &logger)
Construct a FMTExceptionHandler with a logger.
void reRaiseIfThreadCrash()
Re-raise a multithread error if an exception was raised by any thread.
void disableNestedExceptions()
Disable the throwing of nested exceptions.
bool m_usenestedexceptions
If usenested = true then the handler will throw nested exceptions.
Definition: FMTExceptionHandler.h:223
bool _isThisThreadThrowed() const
Return true if this thread has thrown.
void _updateWarningCount(const FMTException &p_warning)
update the warning count and print if max not reached
virtual FMTExceptionHandler * getCplData()
Return a copy of the abstract handler for use in the FMTCPLErrorHandler function when using GDAL.
FMTExceptionHandler(const FMTExceptionHandler &rhs)
Copy constructor for FMTExceptionHandler.
int _errorcount
Keeps count of the number of error thrown.
Definition: FMTExceptionHandler.h:214
static std::string getErrorDescription(bool p_french, FMTexc p_type)
get descriptions of the potential errors
virtual void handelCplError(int eErrClass, int nError, const char *pszErrorMsg)
Callback called by GDAL for handling GDAL errors thrown.
virtual FMTException raiseFromCatch(std::string text, const std::string &method, const int &line, const std::string &file, Core::FMTsection lsection=Core::FMTsection::Empty)
Raise an exception from a catch body, determining if the exception is unhandled.
std::exception_ptr m_threadcrashexception
The exception throwed by the faulty thread...
Definition: FMTExceptionHandler.h:242
void throwNested(const std::exception &texception, int &level, bool rethrow=true)
Throw a nested exception.
void enableNestedExceptions()
Enable the throwing of nested exceptions.
virtual void printExceptions(std::string text, const std::string &method, const int &line, const std::string &fil, Core::FMTsection lsection=Core::FMTsection::Empty)
Print all nested exceptions starting with the first provided.
bool _needToRethrow() const
Return true if nested exceptions are used and the exception is a function error that must be rethrown...
bool _isMainThread() const
Return true if we are on the main thread.
void passInLogger(const std::unique_ptr< Logging::FMTLogger > &logger)
Pass a logger to the shared pointer of the handler for sharing.
virtual std::unique_ptr< FMTExceptionHandler > clone() const =0
clone the FMTExceptionHandler
size_t m_maxwarningsbeforesilenced
Number of time that the handler throw a certain warning before stop throwing it.
Definition: FMTExceptionHandler.h:217
virtual ~FMTExceptionHandler()=default
Default virtual destructor for FMTExceptionHandler.
static std::unique_ptr< FMTException > _createException(FMTexc p_exception, Core::FMTsection p_section, const std::string &p_message, const std::string &p_method, const std::string &p_file, int p_line)
Construct a FMTException from an exception type, a section, a message and the location where it occur...
void _updateStatus(std::unique_ptr< FMTException > &p_exception)
Update the status of the handler of the last exception type thrown and modify if it as to be ignored.
FMTexc _exception
This is the type of the last FMTException thrown by the FMTExceptionHandler.
Definition: FMTExceptionHandler.h:211
bool _isThrowedOnThread() const
Return true if an exception has been thrown on a thread.
Base exception class for FMT holding the type, section and message of a given exception.
Definition: FMTException.h:140
RAII helper that installs a structured exception translator for its scope on MSVC.
Definition: FMTScopedSeTranslator.h:26
Base class handling the level of information printed by FMT.
Definition: FMTLogger.h:37
FMTsection
Enumerator describing the sections of a Woodstock model.
Definition: FMTutility.h:32
@ Empty
Definition: FMTutility.h:44
Namespace for exceptions and exceptions handling (warnings and errors) thrown by FMT and all exceptio...
Definition: FMTAggregateRedefinition.h:11
FMTexc
Enumerator describing the exceptions thrown by FMT.
Definition: FMTException.h:45