Source code of Windows XP (NT5)
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.

200 lines
4.4 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. //using namespace std;
  15. #define MAXSTR 4096
  16. #define MAXNAME 512
  17. #define ARGUMENT_PRESENT(ArgPtr) ( (CHAR*)(ArgPtr) != (CHAR*)(NULL) )
  18. // Macros that allow the file name and line number to be passed in as a string.
  19. #ifndef FILE_AND_LINE
  20. #define LineNumAsString(x) #x
  21. #define LineNum(x) LineNumAsString(x)
  22. #define FILE_AND_LINE __FILE__"_"LineNum(__LINE__)
  23. #endif
  24. // Use these macros in your components
  25. #define PPTracePrint if (PPTraceStatus::TraceOnFlag) TracePrint
  26. #define PPTraceFunc CTraceFunc
  27. #define PPTraceFuncV CTraceFuncVoid
  28. // Use these macros to supply level and szFileAndName argument
  29. // Additional levels (upto 255) can be defined
  30. #define PPTRACE_ERR 0, FILE_AND_LINE
  31. #define PPTRACE_RAW 1, FILE_AND_LINE
  32. #define PPTRACE_FUNC 2, FILE_AND_LINE
  33. #define PPTRACE_VERB 3, FILE_AND_LINE
  34. // Use PPInitTrace/PPEndTrace at the entry/exit points of a component
  35. ULONG PPInitTrace(LPGUID pControlGuid);
  36. ULONG PPEndTrace();
  37. namespace PPTraceStatus {
  38. extern bool TraceOnFlag;
  39. extern UCHAR EnableLevel;
  40. extern ULONG EnableFlags;
  41. }
  42. //
  43. // Don't use the following functions and class names directly
  44. // Use them via above macros
  45. //
  46. VOID TracePrint(UCHAR Level, LPCSTR szFileAndLine, LPCSTR ParameterList OPTIONAL, ...);
  47. ULONG TraceString(UCHAR Level, IN LPCSTR szBuf);
  48. ULONG TraceString(UCHAR Level, IN LPCWSTR wszBuf);
  49. ULONG64 GetTraceHandle();
  50. void SetTraceHandle(ULONG64 TraceHandle);
  51. // Template class to trace functions with a reference type T argument
  52. template <class T> class CTraceFunc
  53. {
  54. public:
  55. CTraceFunc(UCHAR Level, LPCSTR szFileAndLine, T & ret, LPCSTR szFuncName, LPCSTR ParameterList = NULL, ...) : m_Level(Level), m_ret(ret)
  56. {
  57. // no data generated for the following two cases
  58. if (!PPTraceStatus::TraceOnFlag || m_Level > PPTraceStatus::EnableLevel)
  59. return;
  60. strncpy(m_szFuncName, szFuncName, MAXNAME-1);
  61. CHAR buf[MAXSTR];
  62. int len = _snprintf(buf, MAXSTR-1, "+%s(", m_szFuncName);
  63. int count = 0;
  64. if (ARGUMENT_PRESENT(ParameterList)) {
  65. va_list parms;
  66. va_start(parms, ParameterList);
  67. count = _vsnprintf(buf+len, MAXSTR-len-1, (CHAR*)ParameterList, parms);
  68. len = (count > 0) ? len + count : MAXSTR - 1;
  69. va_end(parms);
  70. }
  71. if (len < (MAXSTR - 1))
  72. {
  73. CHAR* pStr = strrchr(szFileAndLine, '\\');
  74. if (pStr)
  75. {
  76. pStr++; //remove '\'
  77. _snprintf(buf+len, MAXSTR-len-1, ")@%s", pStr);
  78. }
  79. }
  80. TraceString(m_Level, buf);
  81. };
  82. virtual ~CTraceFunc()
  83. {
  84. // no data generated for the following two cases
  85. if (!PPTraceStatus::TraceOnFlag || m_Level > PPTraceStatus::EnableLevel)
  86. return;
  87. std::ostringstream ost;
  88. ost << "-" << m_szFuncName << "=" << m_ret;
  89. TraceString(m_Level, ost.str().c_str());
  90. };
  91. private:
  92. UCHAR m_Level;
  93. T m_ret;
  94. CHAR m_szFuncName[MAXNAME];
  95. };
  96. // class to trace void type function
  97. class CTraceFuncVoid
  98. {
  99. public:
  100. CTraceFuncVoid(UCHAR Level, LPCSTR szFileAndLine, LPCSTR szFuncName, LPCSTR ParameterList = NULL, ...);
  101. virtual ~CTraceFuncVoid();
  102. private:
  103. UCHAR m_Level;
  104. CHAR m_szFuncName[MAXNAME];
  105. };
  106. //
  107. // old tracing stuff to be removed?
  108. //
  109. // the default (all) flag with each trace level
  110. //#define TRACE_FLOW_ALL 0xFFFFFFFF
  111. //#define TRACE_WARN_ALL 0xFFFFFFFF
  112. //#define TRACE_ERR_ALL 0xFFFFFFFF
  113. #define TRACE_FLOW_ALL 0
  114. #define TRACE_WARN_ALL 0
  115. #define TRACE_ERR_ALL 0
  116. // category flag (define your own!)
  117. #define TRACE_TAG_REG 0x00000001
  118. #define TRACE_TAG_foo1 0x00000002
  119. #define TRACE_TAG_foo2 0x00000004
  120. // level
  121. #define TRACE_INFO 0x10000000
  122. #define TRACE_WARN 0x20000000
  123. #define TRACE_ERR 0x40000000
  124. typedef enum {
  125. None,
  126. Bool,
  127. Int,
  128. Dword,
  129. HResult,
  130. String,
  131. WString,
  132. Pointer
  133. } TRACE_FUNCTION_RETURN_TYPE;
  134. VOID
  135. PPInitTrace(LPWSTR wszAppName);
  136. VOID
  137. PPFuncEnter(
  138. DWORD Category,
  139. LPCSTR Function,
  140. LPCSTR ParameterList OPTIONAL,
  141. ...
  142. );
  143. VOID
  144. PPFuncLeave(
  145. IN DWORD Category,
  146. IN TRACE_FUNCTION_RETURN_TYPE ReturnType,
  147. IN DWORD_PTR Variable,
  148. IN LPCSTR Function,
  149. IN LPCSTR ParameterList OPTIONAL,
  150. ...
  151. );
  152. VOID
  153. PPTrace(
  154. DWORD Category,
  155. DWORD Level,
  156. LPCSTR ParameterList OPTIONAL,
  157. ...
  158. );