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.

414 lines
11 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. RsTrace.h
  5. Abstract:
  6. Simple tracing functionality for components that cannot use standard
  7. WsbTrace in RsCommon.dll
  8. Author:
  9. Rohde Wakefield [rohde] 20-Feb-1998
  10. Revision History:
  11. --*/
  12. #pragma once
  13. #ifndef _RSTRACE_H
  14. #define _RSTRACE_H
  15. #define RsBoolAsString(b) ((b) ? "TRUE" : "FALSE" )
  16. #ifdef TRACE
  17. #undef TRACE
  18. #endif
  19. #define TRACE if( CRsFuncTrace::m_TraceEnabled ) CRsFuncTrace::Trace
  20. #define TRACEFN( __FuncName ) CRsFuncTrace __FnTrace( __FuncName );
  21. #define TRACEFNHR( __FuncName ) HRESULT hrRet = S_OK; CRsFuncTraceHr __FnTrace( __FuncName, &hrRet );
  22. #define TRACEFNDW( __FuncName ) DWORD dwRet = 0; CRsFuncTraceDw __FnTrace( __FuncName, &dwRet );
  23. #define TRACEFNLONG( __FuncName ) LONG lRet = 0; CRsFuncTraceLong __FnTrace( __FuncName, &lRet );
  24. #define TRACEFNSHORT( __FuncName ) SHORT sRet = 0; CRsFuncTraceShort __FnTrace( __FuncName, &sRet );
  25. #define TRACEFNBOOL( __FuncName ) BOOL boolRet = FALSE; CRsFuncTraceBool __FnTrace( __FuncName, &boolRet );
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CRsRegKey - A minimal subset of ATL's CRegKey class
  28. class CRsRegKey
  29. {
  30. public:
  31. CRsRegKey() {m_hKey = NULL;}
  32. ~CRsRegKey() {Close();}
  33. // Attributes
  34. public:
  35. operator HKEY() const {return m_hKey;}
  36. HKEY m_hKey;
  37. // Operations
  38. public:
  39. LONG QueryValue(DWORD& dwValue, LPCTSTR lpszValueName)
  40. {
  41. DWORD dwType = NULL;
  42. DWORD dwCount = sizeof(DWORD);
  43. LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
  44. (LPBYTE)&dwValue, &dwCount);
  45. #if 0 // we check for sometimes non-existent values
  46. _ASSERTE((lRes!=ERROR_SUCCESS) || (dwType == REG_DWORD));
  47. _ASSERTE((lRes!=ERROR_SUCCESS) || (dwCount == sizeof(DWORD)));
  48. #endif
  49. return lRes;
  50. }
  51. LONG QueryValue(LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount)
  52. {
  53. _ASSERTE(pdwCount != NULL);
  54. DWORD dwType = NULL;
  55. LONG lRes = RegQueryValueEx(m_hKey, (LPTSTR)lpszValueName, NULL, &dwType,
  56. (LPBYTE)szValue, pdwCount);
  57. #if 0 // we check for sometimes non-existent values
  58. _ASSERTE((lRes!=ERROR_SUCCESS) || (dwType == REG_SZ) ||
  59. (dwType == REG_MULTI_SZ) || (dwType == REG_EXPAND_SZ));
  60. #endif
  61. return lRes;
  62. }
  63. LONG SetValue(DWORD dwValue, LPCTSTR lpszValueName)
  64. {
  65. _ASSERTE(m_hKey != NULL);
  66. return RegSetValueEx(m_hKey, lpszValueName, NULL, REG_DWORD,
  67. (BYTE * const)&dwValue, sizeof(DWORD));
  68. }
  69. LONG DeleteValue(LPCTSTR lpszValue)
  70. {
  71. ATLASSERT(m_hKey != NULL);
  72. return RegDeleteValue(m_hKey, (LPTSTR)lpszValue);
  73. }
  74. LONG Open(HKEY hKeyParent, LPCTSTR lpszKeyName,
  75. REGSAM samDesired = KEY_ALL_ACCESS)
  76. {
  77. _ASSERTE(hKeyParent != NULL);
  78. HKEY hKey = NULL;
  79. LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyName, 0, samDesired, &hKey);
  80. if (lRes == ERROR_SUCCESS)
  81. {
  82. lRes = Close();
  83. _ASSERTE(lRes == ERROR_SUCCESS);
  84. m_hKey = hKey;
  85. }
  86. return lRes;
  87. }
  88. LONG Close()
  89. {
  90. LONG lRes = ERROR_SUCCESS;
  91. if (m_hKey != NULL)
  92. {
  93. lRes = RegCloseKey(m_hKey);
  94. m_hKey = NULL;
  95. }
  96. return lRes;
  97. }
  98. };
  99. //
  100. // Base class for function tracing. Core tracing behavior.
  101. //
  102. class CRsFuncTraceBase
  103. {
  104. public:
  105. CRsFuncTraceBase( const char * FuncName ) : m_FuncName( FuncName )
  106. {
  107. m_IndentLevel++;
  108. }
  109. ~CRsFuncTraceBase( void )
  110. {
  111. m_IndentLevel--;
  112. }
  113. static void TraceInner( const _TCHAR * Fmt, ... )
  114. {
  115. va_list list;
  116. va_start( list, Fmt );
  117. TraceV( 1, Fmt, list );
  118. va_end( list );
  119. }
  120. static void TraceOuter( const _TCHAR * Fmt, ... )
  121. {
  122. va_list list;
  123. va_start( list, Fmt );
  124. TraceV( -1, Fmt, list );
  125. va_end( list );
  126. }
  127. static void Trace( const _TCHAR * Fmt, ... )
  128. {
  129. va_list list;
  130. va_start( list, Fmt );
  131. TraceV( 0, Fmt, list );
  132. va_end( list );
  133. }
  134. static void TraceV( LONG IndentMod, const _TCHAR * Fmt, va_list List )
  135. {
  136. _TCHAR buf[1024];
  137. LONG charIndent = max( 0, ( m_IndentLevel + IndentMod ) * m_IndentChars );
  138. for( LONG i = 0; i < charIndent; i++ ) {
  139. *(buf + i) = _T(' ');
  140. }
  141. _vstprintf( buf + charIndent, Fmt, List );
  142. OutputDebugString( buf );
  143. OutputDebugString( _T("\n") );
  144. }
  145. static BOOL CheckRegEnabled( _TCHAR * Module )
  146. {
  147. BOOL retval = FALSE;
  148. CRsRegKey keySoftware, keyCompany, keyModule, keyModule2;
  149. if( ERROR_SUCCESS == keySoftware.Open( HKEY_LOCAL_MACHINE, _T("Software") ) &&
  150. ERROR_SUCCESS == keyCompany.Open( keySoftware, _T("Microsoft") ) &&
  151. ERROR_SUCCESS == keyModule.Open( keyCompany, _T("RemoteStorage") ) &&
  152. ERROR_SUCCESS == keyModule2.Open( keyModule, Module ) ) {
  153. DWORD dw;
  154. if( ERROR_SUCCESS == keyModule2.QueryValue( dw, _T("Trace") ) ) {
  155. if( dw != 0 ) {
  156. retval = TRUE;
  157. }
  158. } else {
  159. TCHAR buf[128];
  160. dw = 128;
  161. if( ERROR_SUCCESS == keyModule2.QueryValue( buf, _T("Trace"), &dw ) ) {
  162. if( ( dw > 0 ) && ( _T('0') != buf[0] ) ) {
  163. retval = TRUE;
  164. }
  165. }
  166. }
  167. }
  168. return( retval );
  169. }
  170. protected:
  171. const char * m_FuncName;
  172. private:
  173. static LONG m_IndentLevel;
  174. static const LONG m_IndentChars;
  175. public:
  176. static BOOL m_TraceEnabled;
  177. };
  178. //
  179. // Trace Functions w/o any result data printed
  180. //
  181. class CRsFuncTrace : public CRsFuncTraceBase
  182. {
  183. public:
  184. CRsFuncTrace( const char * FuncName ) :
  185. CRsFuncTraceBase( FuncName )
  186. {
  187. if( m_TraceEnabled )
  188. TraceOuter( m_TraceInFmt, m_FuncName );
  189. }
  190. ~CRsFuncTrace( void )
  191. {
  192. if( m_TraceEnabled )
  193. TraceOuter( m_TraceOutFmt, m_FuncName );
  194. }
  195. private:
  196. static const _TCHAR * m_TraceInFmt;
  197. static const _TCHAR * m_TraceOutFmt;
  198. };
  199. //
  200. // Trace Functions with HRESULT
  201. //
  202. class CRsFuncTraceHr : public CRsFuncTraceBase
  203. {
  204. public:
  205. CRsFuncTraceHr( const char * FuncName, const HRESULT * pHr ) :
  206. CRsFuncTraceBase( FuncName ), m_pHr( pHr )
  207. {
  208. if( m_TraceEnabled )
  209. TraceOuter( m_TraceInFmt, m_FuncName );
  210. }
  211. ~CRsFuncTraceHr( void )
  212. {
  213. if( m_TraceEnabled )
  214. TraceOuter( m_TraceOutFmt, m_FuncName, *m_pHr );
  215. }
  216. private:
  217. const HRESULT * m_pHr;
  218. static const _TCHAR * m_TraceInFmt;
  219. static const _TCHAR * m_TraceOutFmt;
  220. };
  221. //
  222. // Trace Functions with DWORD return
  223. //
  224. class CRsFuncTraceDw : public CRsFuncTraceBase
  225. {
  226. public:
  227. CRsFuncTraceDw( const char * FuncName, const DWORD * pDw ) :
  228. CRsFuncTraceBase( FuncName ), m_pDw( pDw )
  229. {
  230. if( m_TraceEnabled )
  231. TraceOuter( m_TraceInFmt, m_FuncName );
  232. }
  233. ~CRsFuncTraceDw( void )
  234. {
  235. if( m_TraceEnabled )
  236. TraceOuter( m_TraceOutFmt, m_FuncName, *m_pDw, *m_pDw );
  237. }
  238. private:
  239. const DWORD * m_pDw;
  240. static const _TCHAR * m_TraceInFmt;
  241. static const _TCHAR * m_TraceOutFmt;
  242. };
  243. //
  244. // Trace Functions with LONG return
  245. //
  246. class CRsFuncTraceLong : public CRsFuncTraceBase
  247. {
  248. public:
  249. CRsFuncTraceLong( const char * FuncName, const LONG * pLong ) :
  250. CRsFuncTraceBase( FuncName ), m_pLong( pLong )
  251. {
  252. if( m_TraceEnabled )
  253. TraceOuter( m_TraceInFmt, m_FuncName );
  254. }
  255. ~CRsFuncTraceLong( void )
  256. {
  257. if( m_TraceEnabled )
  258. TraceOuter( m_TraceOutFmt, m_FuncName, *m_pLong );
  259. }
  260. private:
  261. const LONG * m_pLong;
  262. static const _TCHAR * m_TraceInFmt;
  263. static const _TCHAR * m_TraceOutFmt;
  264. };
  265. //
  266. // Trace Functions with SHORT return
  267. //
  268. class CRsFuncTraceShort : public CRsFuncTraceBase
  269. {
  270. public:
  271. CRsFuncTraceShort( const char * FuncName, const SHORT * pShort ) :
  272. CRsFuncTraceBase( FuncName ), m_pShort( pShort )
  273. {
  274. if( m_TraceEnabled )
  275. TraceOuter( m_TraceInFmt, m_FuncName );
  276. }
  277. ~CRsFuncTraceShort( void )
  278. {
  279. if( m_TraceEnabled )
  280. TraceOuter( m_TraceOutFmt, m_FuncName, *m_pShort );
  281. }
  282. private:
  283. const SHORT * m_pShort;
  284. static const _TCHAR * m_TraceInFmt;
  285. static const _TCHAR * m_TraceOutFmt;
  286. };
  287. //
  288. // Trace Functions with BOOL return
  289. //
  290. class CRsFuncTraceBool : public CRsFuncTraceBase
  291. {
  292. public:
  293. CRsFuncTraceBool( const char * FuncName, const BOOL * pBool ) :
  294. CRsFuncTraceBase( FuncName ), m_pBool( pBool )
  295. {
  296. if( m_TraceEnabled )
  297. TraceOuter( m_TraceInFmt, m_FuncName );
  298. }
  299. ~CRsFuncTraceBool( void )
  300. {
  301. if( m_TraceEnabled )
  302. TraceOuter( m_TraceOutFmt, m_FuncName, RsBoolAsString( *m_pBool ) );
  303. }
  304. private:
  305. const BOOL * m_pBool;
  306. static const _TCHAR * m_TraceInFmt;
  307. static const _TCHAR * m_TraceOutFmt;
  308. };
  309. #define RSTRACE_INIT(Module) \
  310. LONG CRsFuncTrace::m_IndentLevel = 0; \
  311. const LONG CRsFuncTrace::m_IndentChars = 2; \
  312. BOOL CRsFuncTrace::m_TraceEnabled = CRsFuncTrace::CheckRegEnabled( _T(Module) ); \
  313. const _TCHAR * CRsFuncTrace::m_TraceInFmt = _T("Enter <%hs>"); \
  314. const _TCHAR * CRsFuncTrace::m_TraceOutFmt = _T("Exit <%hs>"); \
  315. const _TCHAR * CRsFuncTraceHr::m_TraceInFmt = _T("Enter <%hs>"); \
  316. const _TCHAR * CRsFuncTraceHr::m_TraceOutFmt = _T("Exit <%hs> <0x%p>"); \
  317. const _TCHAR * CRsFuncTraceDw::m_TraceInFmt = _T("Enter <%hs>"); \
  318. const _TCHAR * CRsFuncTraceDw::m_TraceOutFmt = _T("Exit <%hs> <0x%p><%lu>"); \
  319. const _TCHAR * CRsFuncTraceLong::m_TraceInFmt = _T("Enter <%hs>"); \
  320. const _TCHAR * CRsFuncTraceLong::m_TraceOutFmt = _T("Exit <%hs> <%ld>"); \
  321. const _TCHAR * CRsFuncTraceShort::m_TraceInFmt = _T("Enter <%hs>"); \
  322. const _TCHAR * CRsFuncTraceShort::m_TraceOutFmt = _T("Exit <%hs> <%hd>"); \
  323. const _TCHAR * CRsFuncTraceBool::m_TraceInFmt = _T("Enter <%hs>"); \
  324. const _TCHAR * CRsFuncTraceBool::m_TraceOutFmt = _T("Exit <%hs> <%hs>"); \
  325. #endif // _RSTRACE_H