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.

288 lines
8.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: log.hxx
  7. //
  8. // Contents: Simple thread safe logging routine.
  9. //
  10. // Classes: CLog
  11. //
  12. // History: 02-10-94 DavidMun Created
  13. //
  14. //----------------------------------------------------------------------------
  15. #ifndef _LOG_HXX
  16. #define _LOG_HXX
  17. //
  18. // Handy array size utility macro
  19. //
  20. #define NUM_ELEMS(x) (sizeof(x) / sizeof((x)[0]))
  21. //
  22. // Misc constants
  23. //
  24. // BANNER_WIDTH - the width of the lines written to the log. Except for
  25. // LOG_TEXT lines, all lines are wrapped at this column
  26. //
  27. // CCH_MAX_LOG_STRING - max size of logged string
  28. //
  29. // BANNER_WIDTH_EQUALS - BANNER_WIDTH '=' characters
  30. //
  31. // BANNER_WIDTH_DASH - BANNER_WIDTH '-' characters
  32. //
  33. // DEFAULT_LOGFILE - default value for ctor log filename
  34. //
  35. const ULONG BANNER_WIDTH = 79;
  36. const ULONG CCH_MAX_LOG_STRING = 1024;
  37. const CHAR BANNER_WIDTH_EQUALS[] = "===============================================================================";
  38. const CHAR BANNER_WIDTH_DASH[] = "-------------------------------------------------------------------------------";
  39. const CHAR DEFAULT_LOGFILE[] = "default.log";
  40. //
  41. // Logging destination bits used with Write() and SetDefaultDestinations()
  42. //
  43. // LOG_TOCONSOLE - log to the console (i.e., use printf()).
  44. //
  45. // LOG_TOFILE - log to a file
  46. //
  47. // LOG_TODEBUG - log to debugger
  48. //
  49. const ULONG LOG_TOCONSOLE = 0x10000000;
  50. const ULONG LOG_TOFILE = 0x20000000;
  51. const ULONG LOG_TODEBUG = 0x40000000;
  52. const ULONG LOG_DESTINATIONBITS = 0xF0000000;
  53. //
  54. // Logging info levels. If the bitwise and of the log infolevel and the
  55. // infolevel passed to Write is nonzero, the string passed to Write is
  56. // logged, otherwise it goes to the bit bucket.
  57. //
  58. // No more than one of these infolevel bits should be on.
  59. //
  60. //
  61. const ULONG LOG_PASS = 0x00000001;
  62. const ULONG LOG_FAIL = 0x00000002;
  63. const ULONG LOG_WARN = 0x00000004;
  64. const ULONG LOG_START = 0x00000008;
  65. const ULONG LOG_INFO = 0x00000010;
  66. const ULONG LOG_TEXT = 0x00000020;
  67. const ULONG LOG_END = 0x00000040;
  68. const ULONG LOG_SKIP = 0x00000080;
  69. const ULONG LOG_ABORT = 0x00000100;
  70. const ULONG LOG_ERROR = 0x00000200;
  71. const ULONG LOG_TRACE = 0x00000400;
  72. const ULONG LOG_PERF = 0x00000800;
  73. const ULONG LOG_DEBUG = 0x00001000;
  74. const ULONG LOG_RESERVEDBITS = 0xF0001FFF;
  75. const CHAR START_PREFIX[] = "[START %s]";
  76. const CHAR END_PREFIX[] = "[END %s]";
  77. const CHAR PASS_PREFIX[] = "[PASS ] ";
  78. const CHAR FAIL_PREFIX[] = "[FAIL ] ";
  79. const CHAR WARN_PREFIX[] = "[WARN ] ";
  80. const CHAR INFO_PREFIX[] = "[INFO ] ";
  81. const CHAR SKIP_PREFIX[] = "[SKIP ] ";
  82. const CHAR ABORT_PREFIX[] = "[ABORT] ";
  83. const CHAR ERROR_PREFIX[] = "[ERROR] ";
  84. const CHAR TRACE_PREFIX[] = "[TRACE] ";
  85. const CHAR PERF_PREFIX[] = "[PERF ] ";
  86. const CHAR DEBUG_PREFIX[] = "[DEBUG] ";
  87. const ULONG CCH_MAX_START_MESSAGE = BANNER_WIDTH - NUM_ELEMS(START_PREFIX);
  88. //+---------------------------------------------------------------------------
  89. //
  90. // Class: CLog
  91. //
  92. // Purpose: Provide simple thread safe logging support.
  93. //
  94. // Interface: CLog - ctor
  95. // ~CLog - dtor
  96. // SetDefaultDestinations - set default output locations
  97. // GetDefaultDestinations - get default output locations
  98. // SetFile - set log file name
  99. // SetInfoLevel - set infolevel mask bits
  100. // GetInfoLevel - get infolevel mask bits
  101. // Write - write a line in the log file
  102. //
  103. // History: 02-10-94 DavidMun Created
  104. //
  105. //----------------------------------------------------------------------------
  106. class CLog
  107. {
  108. public:
  109. CLog(
  110. const CHAR *szTestTitle,
  111. const CHAR *szLogFile = DEFAULT_LOGFILE,
  112. ULONG flDefaultDestinations = LOG_TOCONSOLE,
  113. ULONG flLogInfoLevel = (LOG_RESERVEDBITS & ~LOG_DESTINATIONBITS));
  114. ~CLog();
  115. inline ULONG SetDefaultDestinations(ULONG flDestinations);
  116. inline ULONG GetDefaultDestinations() const;
  117. VOID SetFile(const CHAR *szNewFilename);
  118. VOID SetFile(const WCHAR *wszNewFilename);
  119. inline const CHAR *GetFile() const;
  120. ULONG SetInfoLevel(ULONG flNewInfoLevel);
  121. inline ULONG GetInfoLevel() const;
  122. VOID Write(ULONG flLevelAndDest, const CHAR *pszFmt, ...);
  123. inline VOID SuppressHeaderFooter(BOOL fSuppress);
  124. private:
  125. VOID _LogPrefix(
  126. ULONG flLevel,
  127. const CHAR *szLastStart,
  128. CHAR *pszPrefix);
  129. VOID _LogHeader();
  130. VOID _LogFooter();
  131. CRITICAL_SECTION _critsec;
  132. BOOL _fLoggedHeader;
  133. BOOL _fSuppress;
  134. ULONG _cLogPass;
  135. ULONG _cLogFail;
  136. ULONG _cLogWarn;
  137. ULONG _cLogStart;
  138. ULONG _cLogInfo;
  139. ULONG _cLogSkip;
  140. ULONG _cLogAbort;
  141. ULONG _cLogError;
  142. ULONG _cLogOther;
  143. ULONG _flDefaultDestinations;
  144. ULONG _flInfoLevel;
  145. CHAR _szLogFile[MAX_PATH];
  146. CHAR _szTestTitle[BANNER_WIDTH / 2]; // enforce short test titles
  147. };
  148. //+---------------------------------------------------------------------------
  149. //
  150. // Member: CLog::GetFile
  151. //
  152. // Synopsis: Return name of log file.
  153. //
  154. // History: 04-01-95 DavidMun Created
  155. //
  156. //----------------------------------------------------------------------------
  157. inline const CHAR *CLog::GetFile() const
  158. {
  159. return _szLogFile;
  160. }
  161. //+---------------------------------------------------------------------------
  162. //
  163. // Member: CLog::SuppressHeaderFooter
  164. //
  165. // Synopsis: Call this with TRUE before logging anything to prevent
  166. // the header/footer from being logged.
  167. //
  168. // Arguments: [fSuppress] - if TRUE, no header or footer will be logged.
  169. //
  170. // History: 04-01-95 DavidMun Created
  171. //
  172. //----------------------------------------------------------------------------
  173. inline VOID CLog::SuppressHeaderFooter(BOOL fSuppress)
  174. {
  175. _fSuppress = fSuppress;
  176. }
  177. //+---------------------------------------------------------------------------
  178. //
  179. // Member: CLog::SetDefaultDestinations
  180. //
  181. // Synopsis: Set the default destinations for logging
  182. //
  183. // Arguments: [flDestinations] - LOG_TO* bits
  184. //
  185. // Returns: Previous default destination bits
  186. //
  187. // History: 02-11-94 DavidMun Created
  188. //
  189. //----------------------------------------------------------------------------
  190. inline ULONG CLog::SetDefaultDestinations(ULONG flDestinations)
  191. {
  192. ULONG flOldDefault = _flDefaultDestinations;
  193. _flDefaultDestinations = flDestinations & LOG_DESTINATIONBITS;
  194. return flOldDefault;
  195. }
  196. //+---------------------------------------------------------------------------
  197. //
  198. // Member: CLog::GetDefaultDestinations
  199. //
  200. // Synopsis: Return default destination bits
  201. //
  202. // History: 02-11-94 DavidMun Created
  203. //
  204. //----------------------------------------------------------------------------
  205. inline ULONG CLog::GetDefaultDestinations() const
  206. {
  207. return _flDefaultDestinations;
  208. }
  209. //+---------------------------------------------------------------------------
  210. //
  211. // Member: CLog::GetInfoLevel
  212. //
  213. // Synopsis: Return infolevel mask
  214. //
  215. // History: 02-11-94 DavidMun Created
  216. //
  217. //----------------------------------------------------------------------------
  218. inline ULONG CLog::GetInfoLevel() const
  219. {
  220. return _flInfoLevel;
  221. }
  222. //
  223. // Consts and declaration for LogIt() helper func
  224. //
  225. const ULONG MAX_LOGIT_MSG = 1024UL;
  226. const HRESULT EXPECT_SUCCEEDED = (HRESULT) -1;
  227. VOID LogIt(HRESULT hrFound, HRESULT hrExpected, CHAR *szFormat, ...);
  228. #endif