message_error.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef PLANCK_MESSAGE_ERROR_H
00035 #define PLANCK_MESSAGE_ERROR_H
00036
00037 #include <exception>
00038 #include <iostream>
00039 #include <string>
00040
00041 #if defined (PLANCK_STACKTRACE)
00042 #include <execinfo.h>
00043 #endif
00044
00045 inline void show_stackframe()
00046 {
00047 #if defined (PLANCK_STACKTRACE)
00048 void *trace[16];
00049 int trace_size = backtrace(trace, 16);
00050 char **messages = backtrace_symbols(trace, trace_size);
00051 std::cerr << "[bt] Execution path:" << std::endl;
00052 for (int i=0; i<trace_size; ++i)
00053 std::cerr << "[bt] " << messages[i] << std::endl;
00054 #endif
00055 }
00056
00057
00058 class Message_error
00059 {
00060 private:
00061 std::string msg;
00062
00063 public:
00064 Message_error()
00065 : msg (std::string("Unspecified error"))
00066 { std::cerr<<msg<<std::endl; show_stackframe(); }
00067
00068 explicit Message_error(const std::string &message)
00069 : msg (message) { std::cerr<<msg<<std::endl; show_stackframe(); }
00070
00071 virtual const char* what() const
00072 { return msg.c_str(); }
00073
00074 virtual ~Message_error() {}
00075 };
00076
00077 #if defined (PLANCK_CHECKS)
00078
00079 #define PLANCK_DIAGNOSIS_BEGIN try {
00080 #define PLANCK_DIAGNOSIS_END \
00081 } \
00082 catch (Message_error &e) \
00083 { std::cerr << "Planck exception: " << e.what() << std::endl; throw; } \
00084 catch (std::exception &e) \
00085 { std::cerr << "std::exception: " << e.what() << std::endl; throw; } \
00086 catch (...) \
00087 { std::cerr << "Unknown exception" << std::endl; throw; }
00088
00089 #else
00090
00091 #define PLANCK_DIAGNOSIS_BEGIN
00092 #define PLANCK_DIAGNOSIS_END
00093
00094 #endif
00095
00096 #endif