Scope
ScopeException.cpp
1 #include "StdAfx.h"
2 #include "ScopeException.h"
3 #include "FPGAException.h"
4 #include "controllers/ScopeLogger.h"
5 
6 namespace scope {
7 
8 void ScopeExceptionHandler(const std::string& _origin, const bool& _log, const bool& _showmessagebox, const bool& _trace, const bool& _rethrow) {
9  try {
10  // Rethrow the exception to catch it in this function
11  throw;
12  }
13  catch (const ScopeException& ex) {
14  std::string msg = "A Scope exception happened in " + _origin + ":\r\n" + ex.what();
15  __ScopeExceptionHandlerHelper(msg, _log, _showmessagebox, _trace);
16  if (_rethrow) throw;
17  }
18  catch (const FPGAException& ex) {
19  std::string msg = "An FPGA exception happened in " + _origin + ":\r\n" + "Status code: " + std::to_string(static_cast<long long>(ex.fpga_status)) + " Message: " + ex.what();
20  __ScopeExceptionHandlerHelper(msg, _log, _showmessagebox, _trace);
21  if (_rethrow) throw;
22  }
23  catch (const boost::property_tree::ptree_bad_path& bp) {
24  std::string msg = "A property tree exception happened in " + _origin + ":\r\n";
25  msg += "The parameter \"" + bp.path<boost::property_tree::wpath>().dump() + "\" could not be found in the xml file.";
26  __ScopeExceptionHandlerHelper(msg, _log, _showmessagebox, _trace);
27  if (_rethrow) throw;
28  }
29  catch (const std::bad_cast& exbc) {
30  std::string msg = "A bad cast exception happened in " + _origin + ":\r\n" + exbc.what() + "\r\n\r\n Most probably ScopeDefines.h is misconfigured. Exit immediately.";
31  __ScopeExceptionHandlerHelper(msg, _log, _showmessagebox, _trace);
32  if (_rethrow) throw;
33  }
34  catch (const std::bad_alloc& exba) {
35  std::string msg = "A bad allocation exception happened int " + _origin + ":\r\n" + exba.what() + "\r\n\r\n Most probably there is not enough memory to allocate for Scope. Exit immediately.";
36  __ScopeExceptionHandlerHelper(msg, _log, _showmessagebox, _trace);
37  if (_rethrow) throw;
38  }
39  catch (const std::exception& exstd) {
40  std::string msg = "A STL exception happened in " + _origin + ":\r\n" + exstd.what();
41  __ScopeExceptionHandlerHelper(msg, _log, _showmessagebox, _trace);
42  if (_rethrow) throw;
43  }
44  catch (const _com_error& comerr) {
45  std::string msg = "A COM error happened in " + std::string(CW2A(comerr.Source())) + "\r\n\r\n" + std::string(CW2A(comerr.Description()));
46  __ScopeExceptionHandlerHelper(msg, _log, _showmessagebox, _trace);
47  }
48  catch (...) {
49  std::string msg = "An unkown exception happened in " + _origin + "\r\nThis is serious. Exit Scope immediately.";
50  __ScopeExceptionHandlerHelper(msg, _log, _showmessagebox, _trace);
51  if (_rethrow) throw;
52  }
53 }
54 
55 void __ScopeExceptionHandlerHelper(const std::string& _msg, const bool& _log, const bool& _showmessagebox, const bool& _trace) {
56  if ( _trace ) {
57  DBOUT(CA2W(_msg.c_str()));
58  }
59 
60  if ( _log ) {
61  ScopeLogger scope_logger;
62  scope_logger.Log(std::wstring(CA2W(_msg.c_str())), log_error);
63  }
64 
65  if ( _showmessagebox )
66  ::MessageBoxA(NULL, _msg.c_str(), "ScopeException", MB_OK | MB_ICONWARNING | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
67 }
68 
69 void __ScopeExceptionHandlerHelperW(const std::wstring& _msg, const bool& _log, const bool& _showmessagebox, const bool& _trace) {
70  if ( _trace ) {
71  DBOUT(_msg.c_str());
72  }
73 
74  if ( _log ) {
75  ScopeLogger scope_logger;
76  scope_logger.Log(_msg, log_error);
77  }
78 
79  if ( _showmessagebox )
80  ::MessageBoxW(NULL, _msg.c_str(), L"ScopeException", MB_OK | MB_ICONWARNING | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
81 }
82 
83 }
Simple exception class for Scope.
Definition: ScopeException.h:9
An exception for FPGA stuff.
Definition: FPGAException.h:9
const int32_t fpga_status
the FPGA status
Definition: FPGAException.h:27
void __ScopeExceptionHandlerHelper(const std::string &_msg, const bool &_log, const bool &_showmessagebox, const bool &_trace)
Helper function that actually handles the logging to the ScopeLogger.
A logger class to log various messages and user comments.
Definition: ScopeLogger.h:24
void __ScopeExceptionHandlerHelperW(const std::wstring &_msg, const bool &_log, const bool &_showmessagebox, const bool &_trace)
Helper function that actually handles the logging to the ScopeLogger (wide string version) ...
This is the include file for standard system include files, or project specific include files that ar...
void Log(const std::wstring &message, const log_message_type &msgtype)
Logs a message.
Definition: ScopeLogger.cpp:26
#define DBOUT(s)
A debug output to the debug console.
Definition: helpers.h:153
void ScopeExceptionHandler(const std::string &_origin, const bool &_log, const bool &_showmessagebox, const bool &_trace, const bool &_rethrow)
Handles all exceptions and does nice logging.