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.

216 lines
5.3 KiB

  1. /**************************************************************
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. pptrace.h
  5. Abstract:
  6. Event tracing header file
  7. Author:
  8. Naiyi Jiang
  9. Revision History:
  10. ***************************************************************/
  11. #pragma once
  12. #pragma warning(disable:4786)
  13. #include <sstream> // use ostringstream
  14. #define MAXSTR 4096
  15. #define MAXNAME 512
  16. #ifndef ARGUMENT_PRESENT
  17. #define ARGUMENT_PRESENT(ArgPtr) ( (CHAR*)(ArgPtr) != (CHAR*)(NULL) )
  18. #endif
  19. #define TRACE_STRINGA(p) ((LPCSTR)(p) != NULL) ? (LPCSTR)(p) : ""
  20. // Macros that allow the file name and line number to be passed in as a string.
  21. #ifndef FILE_AND_LINE
  22. #define LineNumAsString(x) #x
  23. #define LineNum(x) LineNumAsString(x)
  24. #define FILE_AND_LINE __FILE__"_"LineNum(__LINE__)
  25. #endif
  26. // Use these macros in your components
  27. #define PPTracePrint if (PPTraceStatus::TraceOnFlag) TracePrint
  28. #define PPTracePrintBlob if (PPTraceStatus::TraceOnFlag) TracePrintBlob
  29. #define PPTracePrintString if (PPTraceStatus::TraceOnFlag) TracePrintString
  30. #define PPTraceFunc CTraceFunc
  31. #define PPTraceFuncV CTraceFuncVoid
  32. // Use these macros to supply level and szFileAndName argument
  33. // Additional levels (upto 255) can be defined
  34. #define PPTRACE_ERR 0, FILE_AND_LINE
  35. #define PPTRACE_RAW 1, FILE_AND_LINE
  36. #define PPTRACE_FUNC 2, FILE_AND_LINE
  37. #define PPTRACE_VERB 3, FILE_AND_LINE
  38. // Use PPInitTrace/PPEndTrace at the entry/exit points of a component
  39. ULONG PPInitTrace(LPGUID pControlGuid);
  40. ULONG PPEndTrace();
  41. // stop all traces
  42. ULONG PPStopTrace();
  43. namespace PPTraceStatus {
  44. extern bool TraceOnFlag;
  45. extern UCHAR EnableLevel;
  46. extern ULONG EnableFlags;
  47. }
  48. //
  49. // Don't use the following functions and class names directly
  50. // Use them via above macros
  51. //
  52. VOID TracePrint(UCHAR Level, LPCSTR szFileAndLine, LPCSTR ParameterList OPTIONAL, ...);
  53. VOID TracePrintBlob(UCHAR Level, LPCSTR szFileAndLine, LPCSTR szDesc, LPBYTE pBlob, DWORD cSize, BOOL bUnderscore = FALSE);
  54. VOID TracePrintString(
  55. UCHAR Level, //@parm log if current logging level is at least this
  56. LPCSTR szFileAndLine, //@parm ignored
  57. LPCSTR szContext, //@parm which function is this called from
  58. LPCSTR szBuf //@parm the string itself
  59. );
  60. ULONG TraceString(UCHAR Level, IN LPCSTR szBuf);
  61. ULONG TraceString(UCHAR Level, IN LPCWSTR wszBuf);
  62. ULONG64 GetTraceHandle();
  63. void SetTraceHandle(ULONG64 TraceHandle);
  64. ///////////////////////////////////////////////////////////////////////////
  65. // CTraceFunc
  66. // Generate trace events for functions with type T return value
  67. ///////////////////////////////////////////////////////////////////////////
  68. template <class T> class CTraceFunc
  69. {
  70. public:
  71. CTraceFunc(UCHAR Level, LPCSTR szFileAndLine, T & ret, LPCSTR szFuncName, LPCSTR ParameterList = NULL, ...) : m_Level(Level), m_ret(ret)
  72. {
  73. // no data generated for the following two cases
  74. if (!PPTraceStatus::TraceOnFlag || m_Level > PPTraceStatus::EnableLevel)
  75. return;
  76. strncpy(m_szFuncName, szFuncName, MAXNAME-1);
  77. CHAR buf[MAXSTR];
  78. int len = _snprintf(buf, MAXSTR-1, "+%s(", m_szFuncName);
  79. int count = 0;
  80. if (ARGUMENT_PRESENT(ParameterList)) {
  81. va_list parms;
  82. va_start(parms, ParameterList);
  83. count = _vsnprintf(buf+len, MAXSTR-len-1, (CHAR*)ParameterList, parms);
  84. len = (count > 0) ? len + count : MAXSTR - 1;
  85. va_end(parms);
  86. }
  87. if (len < (MAXSTR - 1))
  88. {
  89. CHAR* pStr = strrchr(szFileAndLine, '\\');
  90. if (pStr)
  91. {
  92. pStr++; //remove '\'
  93. _snprintf(buf+len, MAXSTR-len-1, ")@%s", pStr);
  94. }
  95. }
  96. TraceString(m_Level, buf);
  97. };
  98. virtual ~CTraceFunc()
  99. {
  100. // no data generated for the following two cases
  101. if (!PPTraceStatus::TraceOnFlag || m_Level > PPTraceStatus::EnableLevel)
  102. return;
  103. std::ostringstream ost;
  104. ost.flags(std::ios::hex);
  105. ost << "-" << m_szFuncName << "=0x" << m_ret;
  106. TraceString(m_Level, ost.str().c_str());
  107. };
  108. private:
  109. UCHAR m_Level;
  110. T &m_ret;
  111. CHAR m_szFuncName[MAXNAME];
  112. };
  113. // class to trace void type function
  114. class CTraceFuncVoid
  115. {
  116. public:
  117. CTraceFuncVoid(UCHAR Level, LPCSTR szFileAndLine, LPCSTR szFuncName, LPCSTR ParameterList = NULL, ...);
  118. virtual ~CTraceFuncVoid();
  119. private:
  120. UCHAR m_Level;
  121. CHAR m_szFuncName[MAXNAME];
  122. };
  123. //
  124. // old tracing stuff - only XMLUtilities project is still using them
  125. //
  126. #define TRACE_FLOW_ALL 0
  127. #define TRACE_WARN_ALL 0
  128. #define TRACE_ERR_ALL 0
  129. // category flag (define your own!)
  130. #define TRACE_TAG_REG 0x00000001
  131. #define TRACE_TAG_foo1 0x00000002
  132. #define TRACE_TAG_foo2 0x00000004
  133. // level
  134. #define TRACE_INFO 0x10000000
  135. #define TRACE_WARN 0x20000000
  136. #define TRACE_ERR 0x40000000
  137. typedef enum {
  138. None,
  139. Bool,
  140. Int,
  141. Dword,
  142. HResult,
  143. String,
  144. WString,
  145. Pointer
  146. } TRACE_FUNCTION_RETURN_TYPE;
  147. VOID
  148. PPInitTrace(LPWSTR wszAppName);
  149. VOID
  150. PPFuncEnter(
  151. DWORD Category,
  152. LPCSTR Function,
  153. LPCSTR ParameterList OPTIONAL,
  154. ...
  155. );
  156. VOID
  157. PPFuncLeave(
  158. IN DWORD Category,
  159. IN TRACE_FUNCTION_RETURN_TYPE ReturnType,
  160. IN DWORD_PTR Variable,
  161. IN LPCSTR Function,
  162. IN LPCSTR ParameterList OPTIONAL,
  163. ...
  164. );
  165. VOID
  166. PPTrace(
  167. DWORD Category,
  168. DWORD Level,
  169. LPCSTR ParameterList OPTIONAL,
  170. ...
  171. );