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.

417 lines
10 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 1998-2000 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: IULogger.h: interface for the CIULogger class.
  6. //
  7. // Description:
  8. //
  9. // CIULogger is a class that output the program logs to
  10. // a text file, in order to help debugging the program.
  11. //
  12. // Programs wish to have this logging function should NOT use
  13. // class directly. They should only use the macro defined
  14. // at the end of this file.
  15. //
  16. //=======================================================================
  17. #ifndef _IULOGGER_H_INCLUDED_
  18. #include <wtypes.h>
  19. #include <FreeLog.h>
  20. extern const LPCTSTR pszHeapAllocFailed;
  21. #if defined(DBG) // full logging for checked builds
  22. //
  23. // Common format strings
  24. //
  25. class CIULogger
  26. {
  27. public:
  28. CIULogger(char* szBlockName);
  29. ~CIULogger();
  30. //
  31. // log with no flag, so can not be removed by excluding directives
  32. //
  33. void Log(LPCTSTR szLogFormat, ...);
  34. //
  35. // log error, can not be removed by excluding directives
  36. // Key word "Error: " is inserted before the log msg
  37. //
  38. void LogError(LPCTSTR szLogFormat, ...);
  39. //
  40. // similar to LogError, but try to log the system msg based
  41. // on the error code. If the sysmsg not avail, log
  42. // "Unknown error with error code 0x%08x"
  43. //
  44. void LogErrorMsg(DWORD dwErrCode);
  45. //
  46. // similar to LogErrorMsg but prepends with "Info" rather than "Error"
  47. //
  48. void LogInfoMsg(DWORD dwErrCode);
  49. //
  50. // log with type INTERNET, this function will do nothing
  51. // if the Internet exclusion directive is detected from reg
  52. //
  53. void LogInternet(LPCTSTR szLogFormat, ...);
  54. //
  55. // log with type XML, this function will do nothing
  56. // if the XML exclusion directive is detected from reg
  57. //
  58. void LogXML(LPCTSTR szLogFormat, ...);
  59. //
  60. // log BSTR containing valid XML. This gets around length limitations
  61. // of LogOutput and attempts to break lines following ">". This
  62. // output is sent for both fre and chk builds unless excluded from reg.
  63. //
  64. void LogXmlBSTR(BSTR bstrXML);
  65. //
  66. // log with type SOFTWARE, this function will do nothing
  67. // if the SOFTWARE exclusion directive is detected from reg
  68. //
  69. void LogSoftware(LPCTSTR szLogFormat, ...);
  70. //
  71. // log with type DRIVER, this function will do nothing
  72. // if the DRIVER exclusion directive is detected from reg
  73. //
  74. void LogDriver(LPCTSTR szLogFormat, ...);
  75. //
  76. // log with type CHECKTRUST, this function will do nothing
  77. // if the CHECKTRUST exclusion directive is detected from reg
  78. //
  79. void LogTrust(LPCTSTR szLogFormat, ...);
  80. //
  81. // log with type DOWNLOAD, this function will do nothing
  82. // if the DOWNLOAD exclusion directive is detected from reg
  83. //
  84. void LogDownload(LPCTSTR szLogFormat, ...);
  85. int m_LineNum;
  86. private:
  87. //
  88. // Helper for LogErrorMsg and LogInfoMsg (which supply message to prepend)
  89. //
  90. void _LogFormattedMsg(DWORD dwErrCode, LPCTSTR pszErrorInfo);
  91. //
  92. // Overwrite <CR> and <LF> with space
  93. //
  94. void _NukeCrLf(LPTSTR pszBuffer);
  95. //
  96. // actual base logging function, returns
  97. // if it actually logged, or just returned
  98. // because directives say don't make this kind of log
  99. //
  100. void _Log(DWORD LogType, LPCTSTR pszLogFormat, va_list va);
  101. //
  102. // function to write the log to log file
  103. //
  104. void _LogOut(LPTSTR pszLog);
  105. //
  106. // functions go guard writing to file
  107. //
  108. BOOL AcquireMutex();
  109. void ReleaseMutex();
  110. //
  111. // structure used to remember indent steps per thread
  112. //
  113. struct _THREAD_INDENT
  114. {
  115. DWORD dwThreadId;
  116. int iIndent;
  117. };
  118. //
  119. // static integer to remember the log indent steps
  120. //
  121. static _THREAD_INDENT* m_psIndent;
  122. //
  123. // size of array pointed by m_psIndent
  124. //
  125. static int m_Size;
  126. //
  127. // static handle for log file
  128. //
  129. static HANDLE m_shFile;
  130. //
  131. // bitmap for logging type
  132. //
  133. static DWORD m_sdwLogMask;
  134. //
  135. // indent per step
  136. //
  137. // 1~8 - number of spaces
  138. // other - one tab
  139. //
  140. static int m_siIndentStep;
  141. //
  142. // function to retrieve the indent of current thread
  143. //
  144. inline int GetIndent(void);
  145. //
  146. // function to change indention of current thread
  147. //
  148. void SetIndent(int IndentDelta);
  149. //
  150. // index of indent array
  151. //
  152. int m_Index;
  153. //
  154. // controlling vars
  155. //
  156. static bool m_fLogUsable;
  157. static bool m_fLogFile;
  158. static bool m_fLogDebugMsg;
  159. static HANDLE m_hMutex;
  160. static int m_cFailedWaits;
  161. //
  162. // current block name
  163. //
  164. char m_szBlockName[MAX_PATH];
  165. //
  166. // if this log object is for whole process. If yes,
  167. // no indention will be handled.
  168. //
  169. bool m_fProcessLog;
  170. //
  171. // var to remember time elapsed
  172. //
  173. DWORD m_dwTickBegin;
  174. //
  175. // disable the default constructor
  176. //
  177. CIULogger() {};
  178. //
  179. // timestamp helper
  180. //
  181. void GetLogHeader(LPTSTR pszBuffer, DWORD cchBufferLen);
  182. //
  183. // read registry value helper
  184. //
  185. void ReadRegistrySettings(void);
  186. //
  187. // remember thread id of itself
  188. //
  189. DWORD m_dwThreadId;
  190. //
  191. // flush every time?
  192. // added by charlma 11/27/01
  193. // if this flag is set, then flush everytime. otherwise, don't flush in order
  194. // to improve logging performance.
  195. //
  196. static BOOL m_fFlushEveryTime;
  197. };
  198. //=======================================================================
  199. //
  200. // Define the macros that should be used in the programs to utilize
  201. // the CIULogger class.
  202. //
  203. // Note: each of the following macro will practically doing nothing
  204. // if the registry of supporting this logging feature does not exist
  205. // or values not appropriately set.
  206. //
  207. //=======================================================================
  208. //
  209. // LOG_Process, is the one you can use in global namespace.
  210. // The purpose of this macro is to pump up ref count of log file use so
  211. // during the whole process this log file will keep open, therefore
  212. // the actual logging to file feature will have minimum performance
  213. // impact on your code.
  214. //
  215. // This macro is mainly designed for the scenario that you can't use LOG_Block
  216. // inside main, such as DLL_ATTACH of DllMain() - without this, each
  217. // function call to a DLL will cause the log file open/close.
  218. //
  219. #define LOG_Process CIULogger LogBlock(NULL);
  220. //
  221. // LOG_Block, is the one you should use at the beginning of each
  222. // function or block. It declares an instance of CIULogger, log the
  223. // the entering status, and when control goes out of the scope, the
  224. // exit/end statement is automatically logged
  225. //
  226. #define LOG_Block(name) CIULogger LogBlock(name);
  227. //
  228. // the following macro will always send log to log file
  229. //
  230. #define LOG_Out LogBlock.Log
  231. //
  232. // the following macro will always send log to log file, even for Free builds
  233. // This should be used very sparingly to avoid bloating the DLLs
  234. //
  235. #define LOG_OutFree LogBlock.Log
  236. //
  237. // the following macro will always send log to log file
  238. // this should be used to log ANY error case
  239. //
  240. #define LOG_Error LogBlock.m_LineNum = __LINE__; LogBlock.LogError
  241. //
  242. // the follwoing macro will always send log to log file
  243. // prepended with "Error Line..."
  244. // the log is constructed based on passed in error code
  245. // if system msg is not available for this error code,
  246. // a generic error log "Unknown Error 0x%08x" is written.
  247. //
  248. #define LOG_ErrorMsg LogBlock.m_LineNum = __LINE__; LogBlock.LogErrorMsg
  249. //
  250. // the follwoing macro will always send log to log file
  251. // prepended with "Info Line..."
  252. // the log is constructed based on passed in error code
  253. // if system msg is not available for this error code,
  254. // a generic error log "Unknown Info 0x%08x" is written.
  255. //
  256. #define LOG_InfoMsg LogBlock.m_LineNum = __LINE__; LogBlock.LogInfoMsg
  257. //
  258. // this is used to log anything related to Internet, such as
  259. // WinInet info.
  260. //
  261. #define LOG_Internet LogBlock.LogInternet
  262. //
  263. // this should be used to log anything directly related to XML
  264. // operation details
  265. //
  266. #define LOG_XML LogBlock.LogXML
  267. //
  268. // this should be used to log BSTRs containing valid XML
  269. //
  270. #define LOG_XmlBSTR LogBlock.LogXmlBSTR
  271. //
  272. // this should be used to log anything related to device drivers
  273. //
  274. #define LOG_Driver LogBlock.LogDriver
  275. //
  276. // this should be used to log anything related to software, e.g.,
  277. // detection/installation
  278. //
  279. #define LOG_Software LogBlock.LogSoftware
  280. //
  281. // this should be used to log anything related to check trust
  282. //
  283. #define LOG_Trust LogBlock.LogTrust
  284. //
  285. // this should be used to log anything related to download processing
  286. //
  287. #define LOG_Download LogBlock.LogDownload
  288. //
  289. //
  290. //
  291. #else
  292. //
  293. // Remove all debug style logging for release builds
  294. // using the compiler __noop intrinsic
  295. //
  296. #define LOG_Process __noop
  297. #define LOG_Block __noop
  298. #define LOG_Error __noop
  299. #define LOG_ErrorMsg __noop
  300. #define LOG_InfoMsg __noop
  301. #define LOG_OutFree __noop
  302. #define LOG_XmlBSTR __noop
  303. #define LOG_Out __noop
  304. #define LOG_Internet __noop
  305. #define LOG_XML __noop
  306. #define LOG_Driver __noop
  307. #define LOG_Software __noop
  308. #define LOG_Trust __noop
  309. #define LOG_Download __noop
  310. #endif // defined(DBG)
  311. //
  312. // explanation of registry settings for log feature
  313. //
  314. // The registry settings control how the logging feature works.
  315. //
  316. // All log related settings are under key
  317. // \\HKLM\Software\Microsoft\Windows\CurrentVersion\WindowsUpdate\IUControlLogging
  318. //
  319. // All values are DWORD except "Logging File"
  320. //
  321. // Value "Logging File" - specify the absolute file path, e.g., c:\iuctl.log
  322. // the actual log file name in this case is "c:\iuctl_xxxx.log",
  323. // where xxxx is a decimal number represents process id.
  324. //
  325. // Value "Logging DebugMsg" - indicate if log should be put onto debug window.
  326. // true if vlaue is 1, false for other values. this output and log file
  327. // output control by these 2 values independently.
  328. //
  329. // Value "LogIndentStep" - indicates how many space chars to use for each
  330. // indent. If 0 or negative value, then tab char is used.
  331. //
  332. // Value "LogExcludeBlock" - do not output block enter/exit if it's 1
  333. //
  334. // Value "LogExcludeXML" - do not output logs you logged with LOG_XML if it's 1
  335. //
  336. // Value "LogExcludeXmlBSTR" - do not output logs you logged with LOG_XmlBSTR if 1
  337. //
  338. // Value "LogExcludeInternet" - do not output logs you logged with LOG_Internet if 1
  339. //
  340. // Value "LogExcludeDriver" - do not output logs you logged with LOG_Driver if 1
  341. //
  342. // Value "LogExcludeSoftware - do not output logs you logged with LOG_Software if 1
  343. //
  344. // Value "LogExcludeTrust" - do not output logs you logged with LOG_Trust if 1
  345. //
  346. #define _IULOGGER_H_INCLUDED_
  347. #endif // #ifndef _IULOGGER_H_INCLUDED_