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.

199 lines
5.6 KiB

  1. // trace.h : tracing helper macros
  2. // Copyright (c) Microsoft Corporation 1995-1997.
  3. #pragma once
  4. #pragma warning(disable : 4296)
  5. #ifndef TRACE_H
  6. #define TRACE_H
  7. // if you want to use the vidctl helper *impl templates without using the
  8. // vidctl trace/debug infrastructure then #define NO_VIDCTL_TRACE_SUPPORT
  9. // separately if you don't want to use the vidctl ASSERT then
  10. // #define NO_VIDCTL_ASSERT_SUPPORT
  11. #ifdef NO_VIDCTL_TRACE_SUPPORT
  12. #ifndef TRACELM
  13. #define TRACELM(level, msg)
  14. #endif
  15. #ifndef TRACELS
  16. #define TRACELS(level, stmt)
  17. #endif
  18. #ifndef TRACELSM
  19. #define TRACELSM(level, stmt, msg)
  20. #endif
  21. #ifndef BEGIN_TRACEL
  22. #define BEGIN_TRACEL(level)
  23. #endif
  24. #ifndef TRACES
  25. #define TRACES(stmt)
  26. #endif
  27. #ifndef END_TRACEL
  28. #define END_TRACEL
  29. #endif
  30. #ifndef END_TRACELM
  31. #define END_TRACELM(msg)
  32. #endif
  33. #ifndef TRACEINDENT
  34. #define TRACEINDENT()
  35. #endif
  36. #ifndef TRACEOUTDENT
  37. #define TRACEOUTDENT()
  38. #endif
  39. #else // NO_VIDCTL_TRACE_SUPPORT
  40. #ifdef NO_VIDCTL_ASSERT_SUPPORT
  41. #ifndef ASSERT
  42. #define ASSERT(x)
  43. #endif
  44. #else // NO_VIDCTL_ASSERT_SUPPORT
  45. #if !defined(DEBUG) && (defined(_DBG) || defined(DBG) || defined(_DEBUG))
  46. #define DEBUG 1
  47. #endif
  48. #if !defined(_DEBUG) && (defined(_DBG) || defined(DBG) || defined(DEBUG))
  49. #define _DEBUG 1
  50. #endif
  51. #ifdef DEBUG
  52. enum {
  53. TRACE_ALWAYS = 0,
  54. TRACE_ERROR,
  55. TRACE_DEBUG,
  56. TRACE_DETAIL,
  57. TRACE_PAINT,
  58. TRACE_TEMP,
  59. };
  60. #pragma warning (push)
  61. #pragma warning (disable : 4018) // sign/unsign mismatch in ostream line 312
  62. #include <fstream>
  63. #include <tstring.h>
  64. #include <odsstream.h>
  65. #pragma warning (pop)
  66. #define STRSAFE_NO_CB_FUNCTIONS
  67. #include <strsafe.h>
  68. #define SIZEOF_CH(X) (sizeof(X)/sizeof(X[0]))
  69. inline Tstring hexdump(unsigned long ul) {
  70. TCHAR c[32];
  71. _ltot(ul, c, 16);
  72. return Tstring(c);
  73. }
  74. #ifdef DUMP_TIME_STAMPS
  75. extern _int64 g_ulFreq, g_ulTimeStart;
  76. extern DumpPerfTime();
  77. #else
  78. #define DumpPerfTime()
  79. #endif
  80. #define DUMP_TID
  81. #ifdef DUMP_TID
  82. inline Tstring DumpTid() {
  83. TCHAR c[32];
  84. (void) StringCchPrintf(c, SIZEOF_CH(c), _T("T0x%8.8lX: "), ::GetCurrentThreadId());
  85. return Tstring(c);
  86. }
  87. #else // DUMP_TID
  88. #define DumpTid()
  89. #endif // DUMP_TID
  90. extern DWORD dwTraceLevel;
  91. extern DWORD dwTraceIndent;
  92. extern tostream* tdbgout;
  93. #define dbgDump (*tdbgout)
  94. void DebugInit(LPCTSTR pszLogFile = NULL);
  95. void DebugTerm(void);
  96. inline tostream& DumpHdr(tostream& tout) {
  97. DumpPerfTime();
  98. tout << DumpTid();
  99. for (DWORD i = 0; i < dwTraceIndent; ++i) {
  100. tout << " ";
  101. }
  102. return tout;
  103. }
  104. template<class _E, class _Tr = std::char_traits<_E> >
  105. class basic_oftstream : public basic_otstream<_E, _Tr> {
  106. public:
  107. typedef basic_oftstream<_E, _Tr> _Myt;
  108. typedef std::basic_filebuf<_E, _Tr> _Myfb;
  109. basic_oftstream()
  110. : basic_otstream<_E, _Tr>(&_Fb) {}
  111. explicit basic_oftstream(const char *_S,
  112. ios_base::openmode _M = out | trunc)
  113. : basic_otstream<_E, _Tr>(&_Fb)
  114. {if (_Fb.open(_S, _M | out) == 0)
  115. setstate(failbit); }
  116. virtual ~basic_oftstream()
  117. {}
  118. _Myfb *rdbuf() const
  119. {return ((_Myfb *)&_Fb); }
  120. bool is_open() const
  121. {return (_Fb.is_open()); }
  122. void open(const char *_S, ios_base::openmode _M = out | trunc)
  123. {if (_Fb.open(_S, _M | out) == 0)
  124. setstate(failbit); }
  125. void open(const char *_S, ios_base::open_mode _M)
  126. {open(_S, (openmode)_M); }
  127. void close()
  128. {if (_Fb.close() == 0)
  129. setstate(failbit); }
  130. private:
  131. _Myfb _Fb;
  132. };
  133. #define TRACELM(level, msg) if (dwTraceLevel >= level) { DumpHdr(dbgDump) << msg << _T("\r\n"); dbgDump.flush(); };
  134. #define TRACELS(level, stmt) if (dwTraceLevel >= level) { stmt; }
  135. #define TRACELSM(level, stmt, msg) if (dwTraceLevel >= level) {DumpHdr(dbgDump); stmt; dbgDump << msg << _T("\r\n"); dbgDump.flush(); }
  136. #define BEGIN_TRACEL(level) if (dwTraceLevel >= level) {
  137. #define TRACES(stmt) stmt;
  138. #define END_TRACEL }
  139. #define END_TRACELM(msg) {DumpHdr(dbgDump) << msg << _T("\r\n"); dbgDump.flush(); } }
  140. #ifdef _M_IX86
  141. #define ASSERT(f) \
  142. do { \
  143. if (!(f)) { \
  144. dbgDump << "ASSERT Failed (" #f ") at: " << __FILE__ << " " << __LINE__; dbgDump.flush(); \
  145. _ASSERT(f);\
  146. } \
  147. } while (0)
  148. #else
  149. #define ASSERT(f) \
  150. do { \
  151. if (!(f)) { \
  152. dbgDump << "ASSERT Failed (" #f ") at: " << __FILE__ << " " << __LINE__; dbgDump.flush(); \
  153. _ASSERT(f);\
  154. } \
  155. } while (0)
  156. #endif // _M_IX86
  157. inline void TRACEINDENT() { ++dwTraceIndent; }
  158. inline void TRACEOUTDENT() { ASSERT(dwTraceIndent != 0); --dwTraceIndent; }
  159. #else //debug
  160. #define TRACELM(level, msg)
  161. #define TRACELS(level, stmt)
  162. #define TRACELSM(level, stmt, msg)
  163. #define BEGIN_TRACEL(level)
  164. #define TRACES(stmt)
  165. #define END_TRACEL
  166. #define END_TRACELM(msg)
  167. #define ASSERT(x)
  168. #define TRACEINDENT()
  169. #define TRACEOUTDENT()
  170. #pragma warning (push)
  171. #pragma warning (disable : 4018) // sign/unsign mismatch in ostream line 312
  172. #include <fstream>
  173. #pragma warning (pop)
  174. #define STRSAFE_NO_CB_FUNCTIONS
  175. #include <strsafe.h>
  176. #define SIZEOF_CH(X) (sizeof(X)/sizeof(X[0]))
  177. #endif // DEBUG
  178. #endif // NO_VIDCTL_ASSERT_SUPPORT
  179. #endif // NO_VIDCTL_TRACE_SUPPORT
  180. #endif // TRACE_H