| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef _JP_TRACER_H__ |
| 17 |
#define _JP_TRACER_H__ |
| 18 |
#include <string> |
| 19 |
#include <sstream> |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifdef JP_TRACING_ENABLE |
| 24 |
#define JP_TRACE_IN_C(...) \ |
| 25 |
JPypeTracer _trace(__VA_ARGS__); try { |
| 26 |
#define JP_TRACE_OUT_C } \ |
| 27 |
catch(...) { _trace.gotError(JP_STACKINFO()); throw; } |
| 28 |
#define JP_TRACE_IN(...) \ |
| 29 |
JPypeTracer _trace(__VA_ARGS__); \ |
| 30 |
try { do {} while (0) |
| 31 |
#define JP_TRACE_OUT \ |
| 32 |
} \ |
| 33 |
catch(...) { _trace.gotError(JP_STACKINFO()); throw; } |
| 34 |
#define JP_TRACE(...) JPTracer::trace(__VA_ARGS__) |
| 35 |
#define JP_TRACE_LOCKS(...) JPypeTracer::traceLocks(__VA_ARGS__) |
| 36 |
#define JP_TRACE_PY(m, obj) JPypeTracer::tracePythonObject(m, obj) |
| 37 |
#define JP_TRACE_JAVA(m, obj) JPypeTracer::traceJavaObject(m, obj) |
| 38 |
#else |
| 39 |
#ifndef JP_INSTRUMENTATION |
| 40 |
#define JP_TRACE_IN(...) try { do {} while (0) |
| 41 |
#endif |
| 42 |
#define JP_TRACE_OUT } catch (JPypeException &ex) { ex.from(JP_STACKINFO()); throw; } |
| 43 |
#define JP_TRACE(...) |
| 44 |
#define JP_TRACE_LOCKS(...) |
| 45 |
#define JP_TRACE_PY(m, obj) |
| 46 |
#define JP_TRACE_JAVA(m, obj) |
| 47 |
#endif |
| 48 |
|
| 49 |
|
| 50 |
#define JP_ENABLE_TRACE_PY |
| 51 |
|
| 52 |
class JPypeTracer |
| 53 |
{ |
| 54 |
private: |
| 55 |
string m_Name; |
| 56 |
bool m_Error; |
| 57 |
JPypeTracer *m_Last; |
| 58 |
|
| 59 |
public: |
| 60 |
|
| 61 |
explicit JPypeTracer(const char *name, void *ref = nullptr); |
| 62 |
~JPypeTracer(); |
| 63 |
|
| 64 |
void gotError(const JPStackInfo& info) |
| 65 |
{ |
| 66 |
m_Error = true; |
| 67 |
try |
| 68 |
{ |
| 69 |
throw; |
| 70 |
} catch (JPypeException& ex) |
| 71 |
{ |
| 72 |
ex.from(info); |
| 73 |
throw; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
static void traceJavaObject(const char *msg, const void* obj); |
| 78 |
static void tracePythonObject(const char *msg, PyObject *ref); |
| 79 |
static void traceLocks(const string& msg, void *ref); |
| 80 |
|
| 81 |
static void trace1(const char *src, const char *msg); |
| 82 |
static void trace2(const char *msg1, const char *msg2); |
| 83 |
private: |
| 84 |
static void traceIn(const char *msg, void *ref); |
| 85 |
static void traceOut(const char *msg, bool error); |
| 86 |
} ; |
| 87 |
|
| 88 |
extern "C" int _PyJPModule_trace; |
| 89 |
namespace JPTracer |
| 90 |
{ |
| 91 |
|
| 92 |
template <class T> |
| 93 |
inline void trace(const T& msg) |
| 94 |
{ |
| 95 |
if ((_PyJPModule_trace & 1) == 0) |
| 96 |
return; |
| 97 |
std::stringstream str; |
| 98 |
str << msg; |
| 99 |
JPypeTracer::trace1(nullptr, str.str().c_str()); |
| 100 |
} |
| 101 |
|
| 102 |
inline void trace(const char *msg) |
| 103 |
{ |
| 104 |
if ((_PyJPModule_trace & 1) == 0) |
| 105 |
return; |
| 106 |
JPypeTracer::trace1(nullptr, msg); |
| 107 |
} |
| 108 |
|
| 109 |
template <class T1, class T2> |
| 110 |
inline void trace(const T1& msg1, const T2 & msg2) |
| 111 |
{ |
| 112 |
if ((_PyJPModule_trace & 1) == 0) |
| 113 |
return; |
| 114 |
std::stringstream str; |
| 115 |
str << msg1 << " " << msg2; |
| 116 |
JPypeTracer::trace1(nullptr, str.str().c_str()); |
| 117 |
} |
| 118 |
|
| 119 |
inline void trace(const char *msg1, const char *msg2) |
| 120 |
{ |
| 121 |
if ((_PyJPModule_trace & 1) == 0) |
| 122 |
return; |
| 123 |
JPypeTracer::trace2(msg1, msg2); |
| 124 |
} |
| 125 |
|
| 126 |
template <class T1, class T2, class T3> |
| 127 |
inline void trace(const T1& msg1, const T2& msg2, const T3 & msg3) |
| 128 |
{ |
| 129 |
if ((_PyJPModule_trace & 1) == 0) |
| 130 |
return; |
| 131 |
std::stringstream str; |
| 132 |
str << msg1 << " " << msg2 << " " << msg3; |
| 133 |
JPypeTracer::trace1(nullptr, str.str().c_str()); |
| 134 |
} |
| 135 |
|
| 136 |
template <class T1, class T2, class T3, class T4> |
| 137 |
inline void trace(const T1& msg1, const T2& msg2, const T3& msg3, const T4 & msg4) |
| 138 |
{ |
| 139 |
if ((_PyJPModule_trace & 1) == 0) |
| 140 |
return; |
| 141 |
std::stringstream str; |
| 142 |
str << msg1 << " " << msg2 << " " << msg3 << " " << msg4; |
| 143 |
JPypeTracer::trace1(nullptr, str.str().c_str()); |
| 144 |
} |
| 145 |
|
| 146 |
template <class T1, class T2, class T3, class T4, class T5> |
| 147 |
inline void trace(const T1& msg1, const T2& msg2, const T3& msg3, const T4& msg4, const T5 & msg5) |
| 148 |
{ |
| 149 |
if ((_PyJPModule_trace & 1) == 0) |
| 150 |
return; |
| 151 |
std::stringstream str; |
| 152 |
str << msg1 << " " << msg2 << " " << msg3 << " " << msg4 << " " << msg5; |
| 153 |
JPypeTracer::trace1(nullptr, str.str().c_str()); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#endif |