Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
14 KiB

  1. /******************************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. MPC_trace.h
  5. Abstract:
  6. This file contains the declaration of Tracing Macros for the MPC project.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 05/08/99
  9. created
  10. ******************************************************************************/
  11. #if !defined(__INCLUDED___MPC___TRACE_H___)
  12. #define __INCLUDED___MPC___TRACE_H___
  13. /////////////////////////////////////////////////////////////////////////
  14. //
  15. // We don't want tracing for RETAIL bits.
  16. //
  17. #ifndef DEBUG
  18. #undef NOTRACE
  19. #define NOTRACE
  20. #endif
  21. #include <dbgtrace.h>
  22. #include <traceids.h>
  23. #define __MPC_PROTECT(x) try { x;} catch(...) {}
  24. #define __MPC_PROTECT_HR(hr,x) try { x;} catch(...) { hr = HRESULT_FROM_WIN32(ERROR_EXCEPTION_IN_SERVICE); }
  25. #define __MPC_TRY_BEGIN() try {
  26. #define __MPC_TRY_CATCHALL(hr) } catch(...) { hr = HRESULT_FROM_WIN32(ERROR_EXCEPTION_IN_SERVICE); }
  27. #ifndef NOTRACE
  28. #define __MPC_TRACE_INIT() InitAsyncTrace()
  29. #define __MPC_TRACE_TERM() TermAsyncTrace()
  30. #define __MPC_FUNC_ENTRY(id,x) __MPC_TraceEntry __te(id,x,__FILE__);
  31. #define __MPC_FUNC_LEAVE { __te.Leave( __LINE__ ); goto __func_cleanup; }
  32. #define __MPC_FUNC_CLEANUP __func_cleanup: __te.Cleanup()
  33. #define __MPC_FUNC_EXIT(x) { __te.Return( __LINE__ ); return x; }
  34. #define __MPC_TRACE_HRESULT(hr) __te.FormatError(hr,__LINE__)
  35. #define __MPC_TRACE_FATAL !(__dwEnabledTraces & FATAL_TRACE_MASK) ? \
  36. (void)0 : \
  37. SetAsyncTraceParams( __te.pszFileName, __LINE__, __te.pszFunctionName, FATAL_TRACE_MASK ) && \
  38. PreAsyncTrace
  39. #define __MPC_TRACE_ERROR !(__dwEnabledTraces & ERROR_TRACE_MASK) ? \
  40. (void)0 : \
  41. SetAsyncTraceParams( __te.pszFileName, __LINE__, __te.pszFunctionName, ERROR_TRACE_MASK ) && \
  42. PreAsyncTrace
  43. #define __MPC_TRACE_DEBUG !(__dwEnabledTraces & DEBUG_TRACE_MASK) ? \
  44. (void)0 : \
  45. SetAsyncTraceParams( __te.pszFileName, __LINE__, __te.pszFunctionName, DEBUG_TRACE_MASK ) && \
  46. PreAsyncTrace
  47. #define __MPC_TRACE_STATE !(__dwEnabledTraces & STATE_TRACE_MASK) ? \
  48. (void)0 : \
  49. SetAsyncTraceParams( __te.pszFileName, __LINE__, __te.pszFunctionName, STATE_TRACE_MASK ) && \
  50. PreAsyncTrace
  51. #define __MPC_TRACE_FUNCT !(__dwEnabledTraces & FUNCT_TRACE_MASK) ? \
  52. (void)0 : \
  53. SetAsyncTraceParams( pszFileName, __LINE__, pszFunctionName, FUNCT_TRACE_MASK ) && \
  54. PreAsyncTrace
  55. class __MPC_TraceEntry
  56. {
  57. public:
  58. LPARAM id;
  59. LPCSTR pszFunctionName;
  60. LPCSTR pszFileName;
  61. __inline __MPC_TraceEntry( LPARAM id, LPCSTR pszFunc, LPCSTR pszFile )
  62. {
  63. this->id = id;
  64. this->pszFunctionName = pszFunc;
  65. this->pszFileName = pszFile;
  66. __MPC_TRACE_FUNCT( id, "%s : Entering", (LPSTR)pszFunctionName );
  67. }
  68. __inline ~__MPC_TraceEntry()
  69. {
  70. __MPC_TRACE_FUNCT( id, "%s : Exiting", pszFunctionName );
  71. }
  72. __inline void Leave( DWORD line )
  73. {
  74. __MPC_TRACE_FUNCT( id, "%s : Leaving from line %d", pszFunctionName, line );
  75. }
  76. __inline void Return( DWORD line )
  77. {
  78. __MPC_TRACE_FUNCT( id, "%s : Returning from line %d", pszFunctionName, line );
  79. }
  80. void FormatError( HRESULT hr, DWORD line )
  81. {
  82. __MPC_TraceEntry& __te = *this;
  83. if(SUCCEEDED(hr)) return; // Not logging on success.
  84. if(HRESULT_FACILITY(hr) == FACILITY_WIN32)
  85. {
  86. CHAR rgMsgBuf[512];
  87. if(::FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM |
  88. FORMAT_MESSAGE_IGNORE_INSERTS,
  89. NULL,
  90. HRESULT_CODE(hr),
  91. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  92. rgMsgBuf,
  93. (sizeof(rgMsgBuf)/sizeof(*rgMsgBuf))-1,
  94. NULL ))
  95. {
  96. __MPC_TRACE_ERROR( id, "Got unexpected WIN32 error at line %d: %08x %s", line, hr, rgMsgBuf );
  97. return;
  98. }
  99. }
  100. __MPC_TRACE_ERROR( id, "Got unexpected HRESULT failure at line %d: %08x", line, hr );
  101. }
  102. __inline void Cleanup()
  103. {
  104. __MPC_TRACE_FUNCT( id, "%s : Cleaning up", pszFunctionName );
  105. }
  106. };
  107. #else
  108. #define __MPC_TRACE_INIT()
  109. #define __MPC_TRACE_TERM()
  110. #define __MPC_FUNC_ENTRY(id,x)
  111. #define __MPC_FUNC_LEAVE goto __func_cleanup
  112. #define __MPC_FUNC_CLEANUP __func_cleanup:
  113. #define __MPC_FUNC_EXIT(x) return x
  114. #define __MPC_TRACE_HRESULT(hr) (void)0
  115. #define __MPC_TRACE_FATAL 1 ? (void)0 : PreAsyncTrace
  116. #define __MPC_TRACE_ERROR 1 ? (void)0 : PreAsyncTrace
  117. #define __MPC_TRACE_DEBUG 1 ? (void)0 : PreAsyncTrace
  118. #define __MPC_TRACE_STATE 1 ? (void)0 : PreAsyncTrace
  119. #define __MPC_TRACE_FUNCT 1 ? (void)0 : PreAsyncTrace
  120. #endif
  121. #define __MPC_EXIT_IF_METHOD_FAILS(hr,x) { if(FAILED(hr=x)) { __MPC_TRACE_HRESULT(hr); __MPC_FUNC_LEAVE; } }
  122. #define __MPC_EXIT_IF_SYSCALL_FAILS(hr,res,x) { if(ERROR_SUCCESS!=(res=x)) { __MPC_SET_WIN32_ERROR_AND_EXIT(hr, res); } }
  123. #define __MPC_EXIT_IF_CALL_RETURNS_THISVALUE(hr,x,y) { if((x)==y) { __MPC_SET_WIN32_ERROR_AND_EXIT(hr, ::GetLastError()); } }
  124. #define __MPC_EXIT_IF_CALL_RETURNS_FALSE(hr,x) __MPC_EXIT_IF_CALL_RETURNS_THISVALUE(hr,x,FALSE)
  125. #define __MPC_EXIT_IF_CALL_RETURNS_NULL(hr,x) __MPC_EXIT_IF_CALL_RETURNS_THISVALUE(hr,x,NULL)
  126. #define __MPC_EXIT_IF_CALL_RETURNS_ZERO(hr,x) __MPC_EXIT_IF_CALL_RETURNS_THISVALUE(hr,x,0)
  127. #define __MPC_EXIT_IF_ALLOC_FAILS(hr,var,x) { if((var = (x)) == NULL) { __MPC_SET_ERROR_AND_EXIT(hr, E_OUTOFMEMORY ); } }
  128. #define __MPC_EXIT_IF_INVALID_HANDLE(hr,var,x) { if((var = (x)) == INVALID_HANDLE_VALUE) { __MPC_SET_WIN32_ERROR_AND_EXIT(hr, ::GetLastError() ); } }
  129. #define __MPC_EXIT_IF_INVALID_HANDLE__CLEAN(hr,var,x) { if((var = (x)) == INVALID_HANDLE_VALUE) { var = NULL; __MPC_SET_WIN32_ERROR_AND_EXIT(hr, ::GetLastError() ); } }
  130. #define __MPC_SET_ERROR_AND_EXIT(hr, err) { hr = err; __MPC_TRACE_HRESULT(hr); __MPC_FUNC_LEAVE; }
  131. #define __MPC_SET_WIN32_ERROR_AND_EXIT(hr, res) __MPC_SET_ERROR_AND_EXIT(hr, HRESULT_FROM_WIN32(res))
  132. ////////////////////////////////////////////////////////////////////////////////
  133. ////////////////////////////////////////////////////////////////////////////////
  134. #define __MPC_PARAMCHECK_BEGIN(hr) \
  135. { \
  136. HRESULT& hrOuter = hr; \
  137. HRESULT hrInner = S_OK;
  138. #define __MPC_PARAMCHECK_POINTER(ptr) \
  139. { \
  140. _ASSERTE(ptr != NULL); \
  141. if(ptr == NULL) \
  142. { \
  143. hrInner = E_POINTER; \
  144. } \
  145. }
  146. #define __MPC_PARAMCHECK_POINTER_AND_SET(ptr,val) \
  147. { \
  148. _ASSERTE(ptr != NULL); \
  149. if(ptr == NULL) \
  150. { \
  151. hrInner = E_POINTER; \
  152. } \
  153. else \
  154. { \
  155. *ptr = val; \
  156. } \
  157. }
  158. #define __MPC_PARAMCHECK_NOTNULL(ptr) \
  159. { \
  160. _ASSERTE(ptr != NULL); \
  161. if(ptr == NULL) \
  162. { \
  163. hrInner = E_INVALIDARG; \
  164. } \
  165. }
  166. #define __MPC_PARAMCHECK_STRING_NOT_EMPTY(ptr) \
  167. { \
  168. _ASSERTE(ptr != NULL && ptr[0] != 0); \
  169. if(ptr == NULL || ptr[0] == 0) \
  170. { \
  171. hrInner = E_INVALIDARG; \
  172. } \
  173. }
  174. #define __MPC_PARAMCHECK_END() \
  175. { __MPC_EXIT_IF_METHOD_FAILS(hrOuter,hrInner); } \
  176. }
  177. ////////////////////////////////////////////////////////////////////////////////
  178. ////////////////////////////////////////////////////////////////////////////////
  179. #define __MPC_BEGIN_PROPERTY_GET0(id,func,hr,pVal) \
  180. __MPC_FUNC_ENTRY( id, func ); \
  181. \
  182. HRESULT hr; \
  183. MPC::SmartLock<_ThreadModel> lock( this ); \
  184. \
  185. __MPC_PARAMCHECK_BEGIN(hr) \
  186. __MPC_PARAMCHECK_NOTNULL(pVal); \
  187. __MPC_PARAMCHECK_END(); \
  188. {
  189. #define __MPC_BEGIN_PROPERTY_GET0__NOLOCK(id,func,hr,pVal) \
  190. __MPC_FUNC_ENTRY( id, func ); \
  191. \
  192. HRESULT hr; \
  193. \
  194. __MPC_PARAMCHECK_BEGIN(hr) \
  195. __MPC_PARAMCHECK_NOTNULL(pVal); \
  196. __MPC_PARAMCHECK_END(); \
  197. {
  198. ////////////////////
  199. #define __MPC_BEGIN_PROPERTY_GET(id,func,hr,pVal) \
  200. __MPC_FUNC_ENTRY( id, func ); \
  201. \
  202. HRESULT hr; \
  203. MPC::SmartLock<_ThreadModel> lock( this ); \
  204. \
  205. __MPC_PARAMCHECK_BEGIN(hr) \
  206. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,NULL); \
  207. __MPC_PARAMCHECK_END(); \
  208. {
  209. #define __MPC_BEGIN_PROPERTY_GET__NOLOCK(id,func,hr,pVal) \
  210. __MPC_FUNC_ENTRY( id, func ); \
  211. \
  212. HRESULT hr; \
  213. \
  214. __MPC_PARAMCHECK_BEGIN(hr) \
  215. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,NULL); \
  216. __MPC_PARAMCHECK_END(); \
  217. {
  218. ////////////////////
  219. #define __MPC_BEGIN_PROPERTY_GET2(id,func,hr,pVal,value) \
  220. __MPC_FUNC_ENTRY( id, func ); \
  221. \
  222. HRESULT hr; \
  223. MPC::SmartLock<_ThreadModel> lock( this ); \
  224. \
  225. __MPC_PARAMCHECK_BEGIN(hr) \
  226. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,value); \
  227. __MPC_PARAMCHECK_END(); \
  228. {
  229. #define __MPC_BEGIN_PROPERTY_GET2__NOLOCK(id,func,hr,pVal,value) \
  230. __MPC_FUNC_ENTRY(id,func); \
  231. \
  232. HRESULT hr; \
  233. \
  234. __MPC_PARAMCHECK_BEGIN(hr) \
  235. __MPC_PARAMCHECK_POINTER_AND_SET(pVal,value); \
  236. __MPC_PARAMCHECK_END(); \
  237. {
  238. ////////////////////
  239. #define __MPC_BEGIN_PROPERTY_PUT(id,func,hr) \
  240. __MPC_FUNC_ENTRY(id,func); \
  241. \
  242. HRESULT hr; \
  243. MPC::SmartLock<_ThreadModel> lock( this ); \
  244. \
  245. __MPC_PARAMCHECK_BEGIN(hr) \
  246. __MPC_PARAMCHECK_END(); \
  247. {
  248. #define __MPC_BEGIN_PROPERTY_PUT__NOLOCK(id,func,hr) \
  249. __MPC_FUNC_ENTRY(id,func); \
  250. \
  251. HRESULT hr; \
  252. \
  253. __MPC_PARAMCHECK_BEGIN(hr) \
  254. __MPC_PARAMCHECK_END(); \
  255. {
  256. #define __MPC_END_PROPERTY(hr) \
  257. } \
  258. hr = S_OK; \
  259. \
  260. __MPC_FUNC_CLEANUP; \
  261. __MPC_FUNC_EXIT(hr)
  262. /////////////////////////////////////////////////////////////////////////
  263. #endif // !defined(__INCLUDED___MPC___TRACE_H___)