00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00034
00035
00036 #ifndef _MBODEBUGTRACE_H_
00037 #define _MBODEBUGTRACE_H_
00038
00039 #include "Mbo.h"
00040 #include "arg_info.h"
00041 #include <stdexcept>
00042
00043 namespace mbo
00044 {
00045
00046 std::string MBO_API MboGetErrorText(DWORD dwLastError);
00047
00048 std::string MBO_API vstrprintf(const char *szFormat, va_list vargs);
00049 std::string MBO_API strprintf(const char *szFormat, ...);
00050
00051 typedef void (*OutputDebugFunc)(const std::string&,void*);
00052
00058 void MBO_API OutputDebugTrace(int nSkip = 2);
00059
00065 std::string MBO_API CreateDebugTrace(int nSkip = 2);
00066
00075 int MBO_API FilterExceptionAndTraceInfoEx(
00076 unsigned int code,
00077 const struct _EXCEPTION_POINTERS *ep,
00078 const char *szFile,
00079 int nLine,
00080 const char *szFormat,
00081 ...
00082 );
00083
00084 #define FilterExceptionAndTraceInfo0(szFormat) FilterExceptionAndTraceInfoEx(GetExceptionCode(), GetExceptionInformation(), __FILE__, __LINE__, szFormat)
00085 #define FilterExceptionAndTraceInfo1(szFormat,arg1) FilterExceptionAndTraceInfoEx(GetExceptionCode(), GetExceptionInformation(), __FILE__, __LINE__, szFormat, arg1)
00086 #define FilterExceptionAndTraceInfo2(szFormat,arg1,arg2) FilterExceptionAndTraceInfoEx(GetExceptionCode(), GetExceptionInformation(), __FILE__, __LINE__, szFormat, arg1, arg2)
00087 #define FilterExceptionAndTraceInfo3(szFormat,arg1,arg2,arg3) FilterExceptionAndTraceInfoEx(GetExceptionCode(), GetExceptionInformation(), __FILE__, __LINE__, szFormat, arg1, arg2, arg3)
00088 #define FilterExceptionAndTraceInfo4(szFormat,arg1,arg2,arg3,arg4) FilterExceptionAndTraceInfoEx(GetExceptionCode(), GetExceptionInformation(), __FILE__, __LINE__, szFormat, arg1, arg2, arg3, arg4)
00089
00097 int MBO_API FilterExceptionAndTraceEx(
00098 unsigned int code,
00099 const struct _EXCEPTION_POINTERS *ep,
00100 const char *szFile,
00101 int nLine
00102 );
00103
00104 #define FilterExceptionAndTrace() FilterExceptionAndTraceEx(GetExceptionCode(), GetExceptionInformation(), __FILE__, __LINE__)
00105
00108 class mbo_stack_trace
00109 {
00110 public:
00113 mbo_stack_trace()
00114 : m_strStackTrace(CreateDebugTrace(3))
00115 {
00116 }
00117
00121 explicit mbo_stack_trace(int nSkip)
00122 : m_strStackTrace(CreateDebugTrace(nSkip))
00123 {
00124 }
00125
00128 const std::string& get_stack_trace() const
00129 {
00130 return m_strStackTrace;
00131 }
00132
00133 protected:
00134 const std::string m_strStackTrace;
00135 };
00136
00141 template<class base_exception, typename _str_type = std::string&>
00142 class mbo_exception: public base_exception, public mbo_stack_trace
00143 {
00144 public:
00145
00146 typedef typename copy_arg_info<_str_type>::argument_t str_arg;
00147 typedef typename copy_arg_info<_str_type>::variable_t str_var;
00148
00151 static const char* get_exception_name()
00152 {
00153 const char *szName = typeid(base_exception).name();
00154
00155 if (!strncmp(szName, "class ", sizeof("class ")-1))
00156 {
00157 return szName + sizeof("class ") - 1;
00158 }
00159 else
00160 {
00161 return szName;
00162 }
00163 }
00164
00167 mbo_exception()
00168 : base_exception(str_arg(get_exception_name()))
00169 , mbo_stack_trace(4)
00170 {
00171 OutputDebugString(what());
00172 OutputDebugString("\n");
00173 OutputDebugString(get_stack_trace().c_str());
00174 }
00175
00178 explicit mbo_exception(str_arg msg)
00179 : base_exception(str_arg((std::string(get_exception_name()) + ": " + msg).c_str()))
00180 , mbo_stack_trace(4)
00181 {
00182 OutputDebugString(what());
00183 OutputDebugString("\n");
00184 OutputDebugString(get_stack_trace().c_str());
00185 }
00186 };
00187
00188 typedef mbo_exception<std::exception,char*> exception;
00189 typedef mbo_exception<std::bad_alloc,char*> bad_alloc;
00190 typedef mbo_exception<std::domain_error> domain_error;
00191 typedef mbo_exception<std::invalid_argument> invalid_argument;
00192 typedef mbo_exception<std::length_error> length_error;
00193 typedef mbo_exception<std::logic_error> logic_error;
00194 typedef mbo_exception<std::out_of_range> out_of_range;
00195 typedef mbo_exception<std::overflow_error> overflow_error;
00196 typedef mbo_exception<std::range_error> range_error;
00197 typedef mbo_exception<std::runtime_error> runtime_error;
00198 typedef mbo_exception<std::underflow_error> underflow_error;
00199
00200 }
00201
00202 #endif