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.

3249 lines
68 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. log.c
  5. Abstract:
  6. Tools for logging problems for the user.
  7. Author:
  8. Jim Schmidt (jimschm) 23-Jan-1997
  9. Revisions:
  10. ovidiut 08-Oct-1999 Updated for new coding conventions and Win64 compliance
  11. ovidiut 23-Oct-1998 Implemented a new log mechanism and added new logging capabilities
  12. marcw 2-Sep-1999 Moved over from Win9xUpg project.
  13. ovidiut 15-Mar-2000 Eliminate dependencies on HashTable/PoolMemory
  14. --*/
  15. #include "pch.h"
  16. //
  17. // Includes
  18. //
  19. // None
  20. //
  21. // Strings
  22. //
  23. #define S_COLUMNDOUBLELINEA ":\r\n\r\n"
  24. #define S_COLUMNDOUBLELINEW L":\r\n\r\n"
  25. #define S_NEWLINEA "\r\n"
  26. #define S_NEWLINEW L"\r\n"
  27. #define DEBUG_SECTION "Debug"
  28. #define ENTRY_ALL "All"
  29. #define ENTRY_DEFAULTOVERRIDE "DefaultOverride"
  30. //
  31. // Constants
  32. //
  33. #define OUTPUT_BUFSIZE_LARGE 8192
  34. #define OUTPUT_BUFSIZE_SMALL 1024
  35. #define MAX_MSGTITLE_LEN 13
  36. #define MSGBODY_INDENT 14
  37. #define SCREEN_WIDTH 80
  38. #define MAX_TYPE 32
  39. #define TYPE_ARRAY_SIZE 10
  40. //
  41. // Macros
  42. //
  43. #define OUT_UNDEFINED(OutDest) (OutDest == OD_UNDEFINED)
  44. #define OUT_DEBUGLOG(OutDest) ((OutDest & OD_DEBUGLOG) != 0)
  45. #define OUT_SUPPRESSED(OutDest) ((OutDest & OD_SUPPRESS) != 0)
  46. #define OUT_NO_OUTPUT(OutDest) (OUT_UNDEFINED(OutDest) || OUT_SUPPRESSED(OutDest))
  47. #define OUT_ERROR(OutDest) ((OutDest & OD_ERROR) != 0)
  48. #define OUT_LOGFILE(OutDest) ((OutDest & OD_LOGFILE) != 0)
  49. #define OUT_DEBUGGER(OutDest) ((OutDest & OD_DEBUGGER) != 0)
  50. #define OUT_CONSOLE(OutDest) ((OutDest & OD_CONSOLE) != 0)
  51. #define OUT_POPUP(OutDest) ((OutDest & (OD_POPUP|OD_FORCE_POPUP|OD_UNATTEND_POPUP)) != 0)
  52. #define OUT_POPUP_CANCEL(OutDest) ((OutDest & (OD_POPUP_CANCEL|OD_FORCE_POPUP)) == OD_POPUP_CANCEL)
  53. #define OUT_FORCED_POPUP(OutDest) ((OutDest & (OD_FORCE_POPUP|OD_UNATTEND_POPUP)) != 0)
  54. #define MUST_BE_LOCALIZED(OutDest) ((OutDest & OD_MUST_BE_LOCALIZED) == OD_MUST_BE_LOCALIZED)
  55. #define OUT_ASSERT(OutDest) ((OutDest & OD_ASSERT) != 0)
  56. #ifdef DEBUG
  57. #define DEFAULT_ERROR_FLAGS (OD_DEBUGLOG | OD_LOGFILE | OD_POPUP | OD_ERROR | OD_UNATTEND_POPUP | OD_ASSERT)
  58. #define USER_POPUP_FLAGS (OD_FORCE_POPUP)
  59. #else
  60. #define DEFAULT_ERROR_FLAGS (OD_LOGFILE | OD_POPUP | OD_ERROR | OD_MUST_BE_LOCALIZED)
  61. #define USER_POPUP_FLAGS (OD_FORCE_POPUP | OD_MUST_BE_LOCALIZED)
  62. #endif
  63. #define END_OF_BUFFER(buf) ((buf) + (DWSIZEOF(buf) / DWSIZEOF(buf[0])) - 1)
  64. // This constant sets the default output
  65. #ifndef DEBUG
  66. #define NORMAL_DEFAULT OD_LOGFILE
  67. #else
  68. #define NORMAL_DEFAULT OD_DEBUGLOG
  69. #endif
  70. #ifdef DEBUG
  71. #define PRIVATE_ASSERT(expr) pPrivateAssert(expr,#expr,__LINE__);
  72. #else
  73. #define PRIVATE_ASSERT(expr)
  74. #endif // DEBUG
  75. #define NEWLINE_CHAR_COUNTA (DWSIZEOF (S_NEWLINEA) / DWSIZEOF (CHAR) - 1)
  76. #define NEWLINE_CHAR_COUNTW (DWSIZEOF (S_NEWLINEW) / DWSIZEOF (WCHAR) - 1)
  77. //
  78. // Types
  79. //
  80. typedef DWORD OUTPUTDEST;
  81. typedef struct {
  82. PCSTR Value; // string value entered by the user (LOG,POPUP,SUPPRESS etc.)
  83. OUTPUTDEST OutDest; // any combination of OutDest flags
  84. } STRING2BINARY, *PSTRING2BINARY;
  85. typedef struct {
  86. PCSTR Type;
  87. DWORD Flags;
  88. } DEFAULT_DESTINATION, *PDEFAULT_DESTINATION;
  89. typedef struct {
  90. CHAR Type[MAX_TYPE];
  91. DWORD OutputDest;
  92. } MAPTYPE2OUTDEST, *PMAPTYPE2OUTDEST;
  93. //
  94. // Globals
  95. //
  96. const STRING2BINARY g_String2Binary[] = {
  97. "SUPPRESS", OD_SUPPRESS,
  98. "LOG", OD_LOGFILE,
  99. "POPUP", OD_POPUP,
  100. "DEBUGGER", OD_DEBUGGER,
  101. "CONSOLE", OD_CONSOLE,
  102. "ERROR", OD_ERROR,
  103. "NOCANCEL", OD_FORCE_POPUP,
  104. "ASSERT", OD_ASSERT
  105. };
  106. const PCSTR g_IgnoreKeys[] = {
  107. "Debug",
  108. "KeepTempFiles"
  109. };
  110. BOOL g_LogInit;
  111. HMODULE g_LibHandle;
  112. CHAR g_MainLogFile [MAX_PATH] = "";
  113. HANDLE g_LogMutex;
  114. INT g_LoggingNow;
  115. // a window handle for popup parent
  116. HWND g_LogPopupParentWnd = NULL;
  117. // thread id that set this window handle
  118. DWORD g_InitThreadId = 0;
  119. DWORD g_LogError;
  120. //
  121. // type table elements
  122. //
  123. PMAPTYPE2OUTDEST g_FirstTypePtr = NULL;
  124. DWORD g_TypeTableCount = 0;
  125. DWORD g_TypeTableFreeCount = 0;
  126. OUTPUTDEST g_OutDestAll = OD_UNDEFINED;
  127. OUTPUTDEST g_OutDestDefault = NORMAL_DEFAULT;
  128. BOOL g_HasTitle = FALSE;
  129. CHAR g_LastType [MAX_TYPE];
  130. BOOL g_SuppressAllPopups = FALSE;
  131. BOOL g_ResetLog = FALSE;
  132. PLOGCALLBACKA g_LogCallbackA;
  133. PLOGCALLBACKW g_LogCallbackW;
  134. #ifdef DEBUG
  135. CHAR g_DebugInfPathBufA[] = "C:\\debug.inf";
  136. CHAR g_DebugLogFile[MAX_PATH];
  137. // If g_DoLog is TRUE, then, debug logging is enabled in the
  138. // checked build even if there is no debug.inf.
  139. BOOL g_DoLog = FALSE;
  140. DWORD g_FirstTickCount = 0;
  141. DWORD g_LastTickCount = 0;
  142. #endif
  143. //
  144. // Macro expansion list
  145. //
  146. #ifndef DEBUG
  147. #define TYPE_DEFAULTS \
  148. DEFMAC(LOG_FATAL_ERROR, DEFAULT_ERROR_FLAGS|USER_POPUP_FLAGS) \
  149. DEFMAC(LOG_MODULE_ERROR, DEFAULT_ERROR_FLAGS|USER_POPUP_FLAGS) \
  150. DEFMAC(LOG_ERROR, DEFAULT_ERROR_FLAGS) \
  151. DEFMAC(LOG_INFORMATION, OD_LOGFILE) \
  152. DEFMAC(LOG_STATUS, OD_SUPPRESS) \
  153. #else
  154. #define TYPE_DEFAULTS \
  155. DEFMAC(LOG_FATAL_ERROR, DEFAULT_ERROR_FLAGS|USER_POPUP_FLAGS) \
  156. DEFMAC(LOG_MODULE_ERROR, DEFAULT_ERROR_FLAGS|USER_POPUP_FLAGS) \
  157. DEFMAC(LOG_ERROR, DEFAULT_ERROR_FLAGS) \
  158. DEFMAC(DBG_WHOOPS, DEFAULT_ERROR_FLAGS) \
  159. DEFMAC(DBG_WARNING, OD_LOGFILE|OD_DEBUGGER) \
  160. DEFMAC(DBG_ASSERT,DEFAULT_ERROR_FLAGS|OD_UNATTEND_POPUP) \
  161. DEFMAC(LOG_INFORMATION, OD_LOGFILE) \
  162. DEFMAC(LOG_STATUS, OD_SUPPRESS) \
  163. #endif
  164. //
  165. // Private function prototypes
  166. //
  167. VOID
  168. InitializeLog (
  169. VOID
  170. );
  171. //
  172. // Macro expansion definition
  173. //
  174. /*++
  175. Macro Expansion List Description:
  176. TYPE_DEFAULTS specify the default destination for the frequently used types,
  177. such as LOG_ERROR, LOG_FATAL_ERROR, and so on.
  178. Line Syntax:
  179. DEFMAC(TypeString, Flags)
  180. Arguments:
  181. TypeString - Specifies the LOG_ constant as defined in log.h
  182. Flags - One or more of:
  183. DEFAULT_ERROR_FLAGS - Specifies debug log, setup log, debugger,
  184. popup, and the value of GetLastError.
  185. OD_DEBUGLOG - Specifies the debug log
  186. OD_ERROR - Specifies type is an error (gets value of
  187. GetLastError)
  188. OD_SUPPRESS - Suppresses all output for the type
  189. OD_LOGFILE - Specifies the setup log
  190. OD_DEBUGGER - Specifies the debugger (i.e., VC or remote debugger)
  191. OD_CONSOLE - Specifies the console (via printf)
  192. OD_POPUP - Specifies a message box
  193. OD_FORCE_POPUP - Specifies a message box, even if debug message
  194. was turned off via a click on Cancel
  195. OD_MUST_BE_LOCALIZED - Indicates the type must originate from a
  196. localized message; used for LOG() calls that
  197. generate popups. (So English messages
  198. don't sneak into the project.)
  199. OD_UNATTEND_POPUP - Causes popup even in unattend mode
  200. OD_ASSERT - Give DebugBreak option in popup
  201. Variables Generated From List:
  202. g_DefaultDest
  203. --*/
  204. #define DEFMAC(typestr, flags) {typestr, (flags)},
  205. DEFAULT_DESTINATION g_DefaultDest[] = {
  206. TYPE_DEFAULTS /* , */
  207. {NULL, 0}
  208. };
  209. #undef DEFMAC
  210. //
  211. // Code
  212. //
  213. #ifdef DEBUG
  214. VOID
  215. pPrivateAssert (
  216. IN BOOL Expr,
  217. IN PCSTR StringExpr,
  218. IN UINT Line
  219. )
  220. {
  221. CHAR buffer[256];
  222. if (Expr) {
  223. return;
  224. }
  225. wsprintfA (buffer, "LOG FAILURE: %s (log.c line %u)", StringExpr, Line);
  226. MessageBoxA (NULL, buffer, NULL, MB_OK);
  227. }
  228. #endif
  229. BOOL
  230. pIgnoreKey (
  231. IN PCSTR Key
  232. )
  233. /*++
  234. Routine Description:
  235. pIgnoreKey decides if a key from [debug] section of DEBUG.INF
  236. should be ignored for our purposes (we are only looking for
  237. <All>, <DefaultOverride> and log/debug types).
  238. Specifically, we ignore all keywords in <g_IgnoreKeys> table.
  239. Arguments:
  240. Key - Specifies the key from [debug] section of DEBUG.INF
  241. Return Value:
  242. TRUE if the key should be ignored, or FALSE if it will be taken into consideration.
  243. --*/
  244. {
  245. UINT i;
  246. for (i = 0; i < DWSIZEOF (g_IgnoreKeys) / DWSIZEOF (PCSTR); i++) {
  247. if (StringIMatchA (Key, g_IgnoreKeys[i])) {
  248. return TRUE;
  249. }
  250. }
  251. return FALSE;
  252. }
  253. OUTPUTDEST
  254. pConvertToOutputType (
  255. IN PCSTR Value
  256. )
  257. /*++
  258. Routine Description:
  259. pConvertToOutputType converts a text value entered by the user in
  260. DEBUG.INF file, associated with a type (e.g. "LOG", "POPUP" etc.).
  261. Arguments:
  262. Value - Specifies the text value
  263. Return Value:
  264. The OUTPUT_DESTINATION value associated with the given value or
  265. OD_UNDEFINED if the value is not valid.
  266. --*/
  267. {
  268. UINT i;
  269. for (i = 0; i < DWSIZEOF (g_String2Binary) / DWSIZEOF (STRING2BINARY); i++) {
  270. if (StringIMatchA (Value, g_String2Binary[i].Value)) {
  271. return g_String2Binary[i].OutDest;
  272. }
  273. }
  274. return OD_UNDEFINED;
  275. }
  276. OUTPUTDEST
  277. pGetTypeOutputDestFromTable (
  278. IN PCSTR Type
  279. )
  280. /*++
  281. Routine Description:
  282. pGetTypeOutputDestFromTable returns the output destination associated
  283. with the specified type in the global table
  284. Arguments:
  285. Type - Specifies the type
  286. Return Value:
  287. Any combination of enum OUTPUT_DESTINATION values associated with
  288. the given type.
  289. --*/
  290. {
  291. PMAPTYPE2OUTDEST typePtr;
  292. PMAPTYPE2OUTDEST last;
  293. OUTPUTDEST outDest = OD_UNDEFINED;
  294. if (g_FirstTypePtr) {
  295. typePtr = g_FirstTypePtr;
  296. last = g_FirstTypePtr + g_TypeTableCount;
  297. while (typePtr < last) {
  298. if (StringIMatchA (typePtr->Type, Type)) {
  299. outDest = typePtr->OutputDest;
  300. #ifdef DEBUG
  301. if (g_DoLog) {
  302. outDest |= OD_DEBUGLOG;
  303. }
  304. #endif
  305. break;
  306. }
  307. typePtr++;
  308. }
  309. }
  310. return outDest;
  311. }
  312. OUTPUTDEST
  313. pGetTypeOutputDest (
  314. IN PCSTR Type
  315. )
  316. /*++
  317. Routine Description:
  318. pGetTypeOutputDest returns the default output
  319. destination for the specified type.
  320. Arguments:
  321. Type - Specifies the type
  322. Return Value:
  323. Any combination of enum OUTPUT_DESTINATION values associated with
  324. the given type.
  325. --*/
  326. {
  327. OUTPUTDEST outDest;
  328. //
  329. // first check for ALL
  330. //
  331. if (!OUT_UNDEFINED (g_OutDestAll)) {
  332. outDest = g_OutDestAll;
  333. } else {
  334. //
  335. // otherwise try to get it from the table
  336. //
  337. outDest = pGetTypeOutputDestFromTable (Type);
  338. if (OUT_UNDEFINED (outDest)) {
  339. //
  340. // just return the default
  341. //
  342. outDest = g_OutDestDefault;
  343. }
  344. }
  345. #ifdef DEBUG
  346. if (g_DoLog) {
  347. outDest |= OD_DEBUGLOG;
  348. }
  349. #endif
  350. return outDest;
  351. }
  352. BOOL
  353. pIsPopupEnabled (
  354. IN PCSTR Type
  355. )
  356. /*++
  357. Routine Description:
  358. pIsPopupEnabled decides if the type should produce a popup output. The user may
  359. disable popup display for a type.
  360. Arguments:
  361. Type - Specifies the type
  362. Return Value:
  363. TRUE if the type should display a popup message.
  364. --*/
  365. {
  366. OUTPUTDEST outDest;
  367. //
  368. // first check if any specific output is available for this type,
  369. // and if so, check if the OUT_POPUP_CANCEL flag is not set
  370. //
  371. if (g_SuppressAllPopups) {
  372. return FALSE;
  373. }
  374. outDest = pGetTypeOutputDestFromTable (Type);
  375. if (OUT_POPUP_CANCEL (outDest)) {
  376. return FALSE;
  377. }
  378. // just return the popup type of ALL of DefaultOverride
  379. return OUT_POPUP (pGetTypeOutputDest (Type));
  380. }
  381. LOGSEVERITY
  382. pGetSeverityFromType (
  383. IN PCSTR Type
  384. )
  385. /*++
  386. Routine Description:
  387. pGetSeverityFromType converts a type to a default severity
  388. that will be used by the debug log system.
  389. Arguments:
  390. Type - Specifies the type
  391. Return Value:
  392. The default log severity associated with the given type; if the specified
  393. type is not found, it returns LOGSEV_INFORMATION.
  394. --*/
  395. {
  396. if (OUT_ERROR (pGetTypeOutputDest (Type))) {
  397. return LOGSEV_ERROR;
  398. }
  399. return LOGSEV_INFORMATION;
  400. }
  401. BOOL
  402. LogSetErrorDest (
  403. IN PCSTR Type,
  404. IN OUTPUT_DESTINATION OutDest
  405. )
  406. /*++
  407. Routine Description:
  408. LogSetErrorDest adds a <Type, OutDest> association
  409. to the table g_FirstTypePtr. If an association of Type already exists,
  410. it is modified to reflect the new association.
  411. Arguments:
  412. Type - Specifies the log/debug type string
  413. OutDest - Specifies what new destination(s) are associated with the type
  414. Return Value:
  415. TRUE if the association was successful and the Type is now in the table
  416. --*/
  417. {
  418. PMAPTYPE2OUTDEST typePtr;
  419. UINT u;
  420. //
  421. // Try to locate the existing type
  422. //
  423. for (u = 0 ; u < g_TypeTableCount ; u++) {
  424. typePtr = g_FirstTypePtr + u;
  425. if (StringIMatchA (typePtr->Type, Type)) {
  426. typePtr->OutputDest = OutDest;
  427. return TRUE;
  428. }
  429. }
  430. //
  431. // look if any free slots are available first
  432. //
  433. if (!g_TypeTableFreeCount) {
  434. PRIVATE_ASSERT (g_hHeap != NULL);
  435. if (!g_FirstTypePtr) {
  436. typePtr = HeapAlloc (
  437. g_hHeap,
  438. 0,
  439. DWSIZEOF (MAPTYPE2OUTDEST) * TYPE_ARRAY_SIZE
  440. );
  441. } else {
  442. typePtr = HeapReAlloc (
  443. g_hHeap,
  444. 0,
  445. g_FirstTypePtr,
  446. DWSIZEOF (MAPTYPE2OUTDEST) * (TYPE_ARRAY_SIZE + g_TypeTableCount)
  447. );
  448. }
  449. if (!typePtr) {
  450. return FALSE;
  451. }
  452. g_FirstTypePtr = typePtr;
  453. g_TypeTableFreeCount = TYPE_ARRAY_SIZE;
  454. }
  455. typePtr = g_FirstTypePtr + g_TypeTableCount;
  456. StringCopyByteCountA (typePtr->Type, Type, DWSIZEOF (typePtr->Type));
  457. typePtr->OutputDest = OutDest;
  458. g_TypeTableCount++;
  459. g_TypeTableFreeCount--;
  460. return TRUE;
  461. }
  462. OUTPUTDEST
  463. pGetAttributes (
  464. IN OUT PINFCONTEXT InfContext
  465. )
  466. /*++
  467. Routine Description:
  468. pGetAttributes converts the text values associated with the key on
  469. the line specified by the given context. If multiple values are
  470. specified, the corresponding OUTPUT_DESTINATION values are ORed together
  471. in the return value.
  472. Arguments:
  473. InfContext - Specifies the DEBUG.INF context of the key whose values
  474. are being converted and receives the updated context
  475. after this processing is done
  476. Return Value:
  477. Any combination of enum OUTPUT_DESTINATION values associated with
  478. the given key.
  479. --*/
  480. {
  481. OUTPUTDEST outDest = OD_UNDEFINED;
  482. CHAR value[OUTPUT_BUFSIZE_SMALL];
  483. UINT field;
  484. for (field = SetupGetFieldCount (InfContext); field > 0; field--) {
  485. if (SetupGetStringFieldA (
  486. InfContext,
  487. field,
  488. value,
  489. OUTPUT_BUFSIZE_SMALL,
  490. NULL
  491. )) {
  492. outDest |= pConvertToOutputType(value);
  493. }
  494. }
  495. return outDest;
  496. }
  497. BOOL
  498. pGetUserPreferences (
  499. IN HINF Inf
  500. )
  501. /*++
  502. Routine Description:
  503. pGetUserPreferences converts user's options specified in the given Inf file
  504. (usually DEBUG.INF) and stores them in g_FirstTypePtr table. If <All> and
  505. <DefaultOverride> entries are found, their values are stored in OutputTypeAll
  506. and OutputTypeDefault, respectivelly, if not NULL.
  507. Arguments:
  508. Inf - Specifies the open inf file hanlde to process
  509. OutputTypeAll - Receives the Output Dest for the special <All> entry
  510. OutputTypeDefault - Receives the Output Dest for the special <DefaultOverride> entry
  511. Return Value:
  512. TRUE if the processing of the INF file was OK.
  513. --*/
  514. {
  515. INFCONTEXT infContext;
  516. OUTPUTDEST outDest;
  517. CHAR key[OUTPUT_BUFSIZE_SMALL];
  518. if (SetupFindFirstLineA (Inf, DEBUG_SECTION, NULL, &infContext)) {
  519. do {
  520. // check to see if this key is not interesting
  521. if (!SetupGetStringFieldA (
  522. &infContext,
  523. 0,
  524. key,
  525. OUTPUT_BUFSIZE_SMALL,
  526. NULL
  527. )) {
  528. continue;
  529. }
  530. if (pIgnoreKey (key)) {
  531. continue;
  532. }
  533. // check for special cases
  534. if (StringIMatchA (key, ENTRY_ALL)) {
  535. g_OutDestAll = pGetAttributes (&infContext);
  536. // no reason to continue since ALL types will take this setting...
  537. break;
  538. } else {
  539. if (StringIMatchA (key, ENTRY_DEFAULTOVERRIDE)) {
  540. g_OutDestDefault = pGetAttributes(&infContext);
  541. } else {
  542. outDest = pGetAttributes(&infContext);
  543. // lines like <Type>= or like <Type>=<not a keyword(s)> are ignored
  544. if (!OUT_UNDEFINED (outDest)) {
  545. if (!LogSetErrorDest (key, outDest)) {
  546. return FALSE;
  547. }
  548. }
  549. }
  550. }
  551. } while (SetupFindNextLine (&infContext, &infContext));
  552. }
  553. return TRUE;
  554. }
  555. /*++
  556. Routine Description:
  557. pPadTitleA and pPadTitleW append to Title a specified number of spaces.
  558. Arguments:
  559. Title - Specifies the title (it will appear on the left column).
  560. The buffer must be large enough to hold the additional spaces
  561. Indent - Specifies the indent of the message body. If necessary,
  562. spaces will be appended to the Title to get to Indent column.
  563. Return Value:
  564. none
  565. --*/
  566. VOID
  567. pPadTitleA (
  568. IN OUT PSTR Title,
  569. IN UINT Indent
  570. )
  571. {
  572. UINT i;
  573. PSTR p;
  574. if (Title == NULL) {
  575. return;
  576. }
  577. for (i = ByteCountA (Title), p = GetEndOfStringA (Title); i < Indent; i++) {
  578. *p++ = ' '; //lint !e613 !e794
  579. }
  580. *p = 0; //lint !e613 !e794
  581. }
  582. VOID
  583. pPadTitleW (
  584. IN OUT PWSTR Title,
  585. IN UINT Indent
  586. )
  587. {
  588. UINT i;
  589. PWSTR p;
  590. if (Title == NULL) {
  591. return;
  592. }
  593. for (i = CharCountW (Title), p = GetEndOfStringW (Title); i < Indent; i++) {
  594. *p++ = L' '; //lint !e613
  595. }
  596. *p = 0; //lint !e613
  597. }
  598. /*++
  599. Routine Description:
  600. pFindNextLineA and pFindNextLineW return the position where
  601. the next line begins
  602. Arguments:
  603. Line - Specifies the current line
  604. Indent - Specifies the indent of the message body. The next line
  605. will start preferably after a newline or a white space,
  606. but no further than the last column, which is
  607. SCREEN_WIDTH - Indent.
  608. Return Value:
  609. The position of the first character on the next line.
  610. --*/
  611. PCSTR
  612. pFindNextLineA (
  613. IN PCSTR Line,
  614. IN UINT Indent,
  615. OUT PBOOL TrimLeadingSpace
  616. )
  617. {
  618. UINT column = 0;
  619. UINT columnMax = SCREEN_WIDTH - 1 - Indent;
  620. PCSTR lastSpace = NULL;
  621. PCSTR prevLine = Line;
  622. UINT ch;
  623. *TrimLeadingSpace = FALSE;
  624. while ( (ch = _mbsnextc (Line)) != 0 && column < columnMax) {
  625. if (ch == '\n') {
  626. lastSpace = Line;
  627. break;
  628. }
  629. if (ch > 255) {
  630. lastSpace = Line;
  631. column++;
  632. } else {
  633. if (_ismbcspace (ch)) {
  634. lastSpace = Line;
  635. }
  636. }
  637. column++;
  638. prevLine = Line;
  639. Line = _mbsinc (Line);
  640. }
  641. if (ch == 0) {
  642. return Line;
  643. }
  644. if (lastSpace == NULL) {
  645. // we must cut this even if no white space or 2-byte char was found
  646. lastSpace = prevLine;
  647. }
  648. if (ch != '\n') {
  649. *TrimLeadingSpace = TRUE;
  650. }
  651. return _mbsinc (lastSpace);
  652. }
  653. PCWSTR
  654. pFindNextLineW (
  655. IN PCWSTR Line,
  656. IN UINT Indent,
  657. OUT PBOOL TrimLeadingSpace
  658. )
  659. {
  660. UINT column = 0;
  661. UINT columnMax = SCREEN_WIDTH - 1 - Indent;
  662. PCWSTR lastSpace = NULL;
  663. PCWSTR prevLine = Line;
  664. WCHAR ch;
  665. *TrimLeadingSpace = FALSE;
  666. while ( (ch = *Line) != 0 && column < columnMax) {
  667. if (ch == L'\n') {
  668. lastSpace = Line;
  669. break;
  670. }
  671. if (ch > 255) {
  672. lastSpace = Line;
  673. } else {
  674. if (iswspace (ch)) {
  675. lastSpace = Line;
  676. }
  677. }
  678. column++;
  679. prevLine = Line;
  680. Line++;
  681. }
  682. if (ch == 0) {
  683. return Line;
  684. }
  685. if (lastSpace == NULL) {
  686. // we must cut this even if no white space was found
  687. lastSpace = prevLine;
  688. }
  689. if (ch != L'\n') {
  690. *TrimLeadingSpace = TRUE;
  691. }
  692. return lastSpace + 1;
  693. }
  694. /*++
  695. Routine Description:
  696. pHangingIndentA and pHangingIndentW break in lines and indent
  697. the text in buffer, which is no larger than Size.
  698. Arguments:
  699. buffer - Specifies the buffer containing text to format. The resulting
  700. text will be put in the same buffer
  701. Size - Specifies the size of this buffer, in bytes
  702. Indent - Specifies the indent to be used by all new generated lines.
  703. Return Value:
  704. none
  705. --*/
  706. VOID
  707. pHangingIndentA (
  708. IN OUT PSTR buffer,
  709. IN DWORD Size,
  710. IN UINT Indent
  711. )
  712. {
  713. CHAR indentBuffer[OUTPUT_BUFSIZE_LARGE];
  714. PCSTR nextLine;
  715. PCSTR s;
  716. PSTR d;
  717. UINT i;
  718. BOOL trimLeadingSpace;
  719. PCSTR endOfBuf;
  720. BOOL appendNewLine = FALSE;
  721. nextLine = buffer;
  722. s = buffer;
  723. d = indentBuffer;
  724. endOfBuf = END_OF_BUFFER(indentBuffer) - 3;
  725. while (*s && d < endOfBuf) {
  726. //
  727. // Find end of next line
  728. //
  729. nextLine = (PSTR)pFindNextLineA (s, Indent, &trimLeadingSpace);
  730. //
  731. // Copy one line from source to dest
  732. //
  733. while (s < nextLine && d < endOfBuf) {
  734. switch (*s) {
  735. case '\r':
  736. s++;
  737. if (*s == '\r') {
  738. continue;
  739. } else if (*s != '\n') {
  740. s--;
  741. }
  742. // fall through
  743. case '\n':
  744. *d++ = '\r';
  745. *d++ = '\n';
  746. s++;
  747. break;
  748. default:
  749. if (IsLeadByte (s)) {
  750. *d++ = *s++;
  751. }
  752. *d++ = *s++;
  753. break;
  754. }
  755. }
  756. //
  757. // Trim leading space if necessary
  758. //
  759. if (trimLeadingSpace) {
  760. while (*s == ' ') {
  761. s++;
  762. }
  763. }
  764. if (*s) {
  765. //
  766. // If another line, prepare an indent and insert a new line
  767. // after this multiline message
  768. //
  769. appendNewLine = TRUE;
  770. if (d < endOfBuf && trimLeadingSpace) {
  771. *d++ = L'\r';
  772. *d++ = L'\n';
  773. }
  774. for (i = 0 ; i < Indent && d < endOfBuf ; i++) {
  775. *d++ = ' ';
  776. }
  777. }
  778. }
  779. if (appendNewLine && d < endOfBuf) {
  780. *d++ = L'\r';
  781. *d++ = L'\n';
  782. }
  783. // make sure the string is zero-terminated
  784. PRIVATE_ASSERT (d <= END_OF_BUFFER(indentBuffer));
  785. *d = 0;
  786. // copy the result to output buffer
  787. StringCopyByteCountA (buffer, indentBuffer, Size);
  788. }
  789. VOID
  790. pHangingIndentW (
  791. IN OUT PWSTR buffer,
  792. IN DWORD Size,
  793. IN UINT Indent
  794. )
  795. {
  796. WCHAR indentBuffer[OUTPUT_BUFSIZE_LARGE];
  797. PCWSTR nextLine;
  798. PCWSTR s;
  799. PWSTR d;
  800. UINT i;
  801. BOOL trimLeadingSpace;
  802. PCWSTR endOfBuf;
  803. BOOL appendNewLine = FALSE;
  804. nextLine = buffer;
  805. s = buffer;
  806. d = indentBuffer;
  807. endOfBuf = END_OF_BUFFER(indentBuffer) - 1;
  808. while (*s && d < endOfBuf) {
  809. //
  810. // Find end of next line
  811. //
  812. nextLine = (PWSTR)pFindNextLineW (s, Indent, &trimLeadingSpace);
  813. //
  814. // Copy one line from source to dest
  815. //
  816. while (s < nextLine && d < endOfBuf) {
  817. switch (*s) {
  818. case L'\r':
  819. s++;
  820. if (*s == L'\r') {
  821. continue;
  822. } else if (*s != L'\n') {
  823. s--;
  824. }
  825. // fall through
  826. case L'\n':
  827. *d++ = L'\r';
  828. *d++ = L'\n';
  829. s++;
  830. break;
  831. default:
  832. *d++ = *s++;
  833. break;
  834. }
  835. }
  836. //
  837. // Trim leading space if necessary
  838. //
  839. if (trimLeadingSpace) {
  840. while (*s == L' ') {
  841. s++;
  842. }
  843. }
  844. if (*s) {
  845. //
  846. // If another line, prepare an indent and insert a new line
  847. // after this multiline message
  848. //
  849. appendNewLine = TRUE;
  850. if (d < endOfBuf && trimLeadingSpace) {
  851. *d++ = L'\r';
  852. *d++ = L'\n';
  853. }
  854. for (i = 0 ; i < Indent && d < endOfBuf ; i++) {
  855. *d++ = L' ';
  856. }
  857. }
  858. }
  859. if (appendNewLine && d < endOfBuf) {
  860. *d++ = L'\r';
  861. *d++ = L'\n';
  862. }
  863. // make sure the string is zero-terminated
  864. PRIVATE_ASSERT (d <= END_OF_BUFFER(indentBuffer));
  865. *d = 0;
  866. // copy the result to output buffer
  867. StringCopyCharCountW (buffer, indentBuffer, Size);
  868. }
  869. /*++
  870. Routine Description:
  871. pAppendLastErrorA and pAppendLastErrorW append the specified error code
  872. to the Message and writes the output to the MsgWithErr buffer.
  873. Arguments:
  874. MsgWithErr - Receives the formatted message. This buffer
  875. is supplied by caller
  876. BufferSize - Specifies the size of the buffer, in bytes
  877. Message - Specifies the body of the message
  878. LastError - Specifies the error code that will be appended
  879. Return Value:
  880. none
  881. --*/
  882. VOID
  883. pAppendLastErrorA (
  884. OUT PSTR MsgWithErr,
  885. IN DWORD BufferSize,
  886. IN PCSTR Message,
  887. IN DWORD LastError
  888. )
  889. {
  890. PSTR append;
  891. DWORD errMsgLen;
  892. StringCopyByteCountA (MsgWithErr, Message, BufferSize);
  893. append = GetEndOfStringA (MsgWithErr);
  894. errMsgLen = (DWORD)(MsgWithErr + BufferSize - append); //lint !e613
  895. if (errMsgLen > 0) {
  896. if (LastError < 10) {
  897. _snprintf (append, errMsgLen, " [ERROR=%lu]", LastError);
  898. } else {
  899. _snprintf (append, errMsgLen, " [ERROR=%lu (%lXh)]", LastError, LastError);
  900. }
  901. }
  902. }
  903. VOID
  904. pAppendLastErrorW (
  905. OUT PWSTR MsgWithErr,
  906. IN DWORD BufferSize,
  907. IN PCWSTR Message,
  908. IN DWORD LastError
  909. )
  910. {
  911. PWSTR append;
  912. DWORD errMsgLen;
  913. StringCopyCharCountW (MsgWithErr, Message, BufferSize / DWSIZEOF(WCHAR));
  914. append = GetEndOfStringW (MsgWithErr);
  915. errMsgLen = (DWORD)(MsgWithErr + (BufferSize / DWSIZEOF(WCHAR)) - append);
  916. if (errMsgLen > 0) {
  917. if (LastError < 10) {
  918. _snwprintf (append, errMsgLen, L" [ERROR=%lu]", LastError);
  919. } else {
  920. _snwprintf (append, errMsgLen, L" [ERROR=%lu (%lXh)]", LastError, LastError);
  921. }
  922. }
  923. }
  924. /*++
  925. Routine Description:
  926. pIndentMessageA and pIndentMessageW format the specified message
  927. with the type in the left column and body of the message in the right.
  928. Arguments:
  929. formattedMsg - Receives the formatted message. This buffer
  930. is supplied by caller
  931. BufferSize - Specifies the size of the buffer
  932. Type - Specifies the type of the message
  933. Body - Specifies the body of the message
  934. Indent - Specifies the column to indent to
  935. LastError - Specifies the last error code if different than ERROR_SUCCESS;
  936. in this case it will be appended to the message
  937. Return Value:
  938. none
  939. --*/
  940. VOID
  941. pIndentMessageA (
  942. OUT PSTR formattedMsg,
  943. IN DWORD BufferSize,
  944. IN PCSTR Type,
  945. IN PCSTR Body,
  946. IN UINT Indent,
  947. IN DWORD LastError
  948. )
  949. {
  950. CHAR bodyWithErr[OUTPUT_BUFSIZE_LARGE];
  951. PCSTR myMsgBody;
  952. PSTR currentPos;
  953. DWORD remaining;
  954. myMsgBody = Body;
  955. remaining = BufferSize - Indent;
  956. if (LastError != ERROR_SUCCESS) {
  957. myMsgBody = bodyWithErr;
  958. pAppendLastErrorA (bodyWithErr, DWSIZEOF (bodyWithErr), Body, LastError);
  959. }
  960. StringCopyByteCountA (formattedMsg, Type, MAX_MSGTITLE_LEN);
  961. pPadTitleA (formattedMsg, Indent);
  962. currentPos = formattedMsg + Indent;
  963. StringCopyByteCountA (currentPos, myMsgBody, remaining);
  964. pHangingIndentA (currentPos, remaining, Indent);
  965. // append a new line if space left
  966. currentPos = GetEndOfStringA (currentPos);
  967. if (currentPos + NEWLINE_CHAR_COUNTA + 1 < formattedMsg + BufferSize) { //lint !e613
  968. *currentPos++ = '\r'; //lint !e613
  969. *currentPos++ = '\n'; //lint !e613
  970. *currentPos = 0; //lint !e613
  971. }
  972. }
  973. VOID
  974. pIndentMessageW (
  975. OUT PWSTR formattedMsg,
  976. IN DWORD BufferSize,
  977. IN PCSTR Type,
  978. IN PCWSTR Body,
  979. IN UINT Indent,
  980. IN DWORD LastError
  981. )
  982. {
  983. WCHAR typeW[OUTPUT_BUFSIZE_SMALL];
  984. WCHAR bodyWithErr[OUTPUT_BUFSIZE_LARGE];
  985. PCWSTR myMsgBody;
  986. PWSTR currentPos;
  987. DWORD remaining;
  988. myMsgBody = Body;
  989. remaining = BufferSize - Indent;
  990. if (LastError != ERROR_SUCCESS) {
  991. myMsgBody = bodyWithErr;
  992. pAppendLastErrorW (bodyWithErr, DWSIZEOF (bodyWithErr), Body, LastError);
  993. }
  994. KnownSizeAtoW (typeW, Type);
  995. StringCopyCharCountW (formattedMsg, typeW, MAX_MSGTITLE_LEN);
  996. pPadTitleW (formattedMsg, Indent);
  997. currentPos = formattedMsg + Indent;
  998. StringCopyCharCountW (currentPos, myMsgBody, remaining);
  999. pHangingIndentW (currentPos, remaining, Indent);
  1000. // append a new line if space left
  1001. currentPos = GetEndOfStringW (currentPos);
  1002. if (currentPos + NEWLINE_CHAR_COUNTW + 1 < formattedMsg + BufferSize) {
  1003. *currentPos++ = L'\r';
  1004. *currentPos++ = L'\n';
  1005. *currentPos = 0;
  1006. }
  1007. }
  1008. PCSTR
  1009. pGetSeverityStr (
  1010. IN LOGSEVERITY Severity,
  1011. IN BOOL Begin
  1012. )
  1013. {
  1014. switch (Severity) {
  1015. case LogSevFatalError:
  1016. return Begin?"":"\r\n***";
  1017. case LogSevError:
  1018. return Begin?"":"\r\n***";
  1019. case LogSevWarning:
  1020. return "";
  1021. }
  1022. return "";
  1023. }
  1024. /*++
  1025. Routine Description:
  1026. pWriteToMainLogA and pWriteToMainLogW log the specified message to the main
  1027. end-user log.
  1028. Arguments:
  1029. Severity - Specifies the severity of the message, as defined by the Setup API
  1030. formattedMsg - Specifies the message
  1031. Return Value:
  1032. none
  1033. --*/
  1034. VOID
  1035. pWriteToMainLogA (
  1036. IN PCSTR Type,
  1037. IN LOGSEVERITY Severity,
  1038. IN PCSTR FormattedMsg
  1039. )
  1040. {
  1041. HANDLE logHandle = NULL;
  1042. logHandle = CreateFileA (
  1043. g_MainLogFile,
  1044. GENERIC_WRITE,
  1045. FILE_SHARE_READ,
  1046. NULL,
  1047. OPEN_ALWAYS,
  1048. FILE_ATTRIBUTE_NORMAL,
  1049. NULL
  1050. );
  1051. if (logHandle != INVALID_HANDLE_VALUE) {
  1052. SetFilePointer (logHandle, 0, NULL, FILE_END);
  1053. WriteFileStringA (logHandle, pGetSeverityStr (Severity, TRUE));
  1054. WriteFileStringA (logHandle, FormattedMsg);
  1055. WriteFileStringA (logHandle, pGetSeverityStr (Severity, FALSE));
  1056. CloseHandle (logHandle);
  1057. }
  1058. }
  1059. VOID
  1060. pWriteToMainLogW (
  1061. IN PCSTR Type,
  1062. IN LOGSEVERITY Severity,
  1063. IN PCWSTR FormattedMsg
  1064. )
  1065. {
  1066. HANDLE logHandle = NULL;
  1067. logHandle = CreateFileA (
  1068. g_MainLogFile,
  1069. GENERIC_WRITE,
  1070. FILE_SHARE_READ,
  1071. NULL,
  1072. OPEN_ALWAYS,
  1073. FILE_ATTRIBUTE_NORMAL,
  1074. NULL
  1075. );
  1076. if (logHandle != INVALID_HANDLE_VALUE) {
  1077. SetFilePointer (logHandle, 0, NULL, FILE_END);
  1078. WriteFileStringA (logHandle, pGetSeverityStr (Severity, TRUE));
  1079. WriteFileStringW (logHandle, FormattedMsg);
  1080. WriteFileStringA (logHandle, pGetSeverityStr (Severity, FALSE));
  1081. CloseHandle (logHandle);
  1082. }
  1083. }
  1084. /*++
  1085. Routine Description:
  1086. pDisplayPopupA and pDisplayPopupW displays the specified message to
  1087. a popup window, if <g_LogPopupParentWnd> is not NULL (attended mode).
  1088. Arguments:
  1089. Type - Specifies the type of the message, displayed as the popup's title
  1090. Msg - Specifies the message
  1091. LastError - Specifies the last error; it will be printed if != ERROR_SUCCESS
  1092. Forced - Specifies TRUE to force the popup, even in unattended mode
  1093. Return Value:
  1094. none
  1095. --*/
  1096. VOID
  1097. pDisplayPopupA (
  1098. IN PCSTR Type,
  1099. IN PCSTR Msg,
  1100. IN DWORD LastError,
  1101. IN BOOL Forced
  1102. )
  1103. {
  1104. #ifdef DEBUG
  1105. CHAR formattedMsg[OUTPUT_BUFSIZE_LARGE];
  1106. CHAR buffer[OUTPUT_BUFSIZE_SMALL];
  1107. PSTR currentPos = buffer;
  1108. #endif
  1109. UINT mbStyle;
  1110. LONG rc;
  1111. OUTPUTDEST outDest;
  1112. HWND parentWnd;
  1113. PCSTR displayMessage = Msg;
  1114. LOGSEVERITY severity = pGetSeverityFromType (Type);
  1115. outDest = pGetTypeOutputDest (Type);
  1116. if (g_LogPopupParentWnd || Forced) {
  1117. #ifdef DEBUG
  1118. if (LastError != ERROR_SUCCESS) {
  1119. if (LastError < 10) {
  1120. currentPos += wsprintfA (buffer, " [ERROR=%u]", LastError);
  1121. } else {
  1122. currentPos += wsprintfA (buffer, " [ERROR=%u (%Xh)]", LastError, LastError);
  1123. }
  1124. }
  1125. if (OUT_ASSERT (outDest)) {
  1126. currentPos += wsprintfA (
  1127. currentPos,
  1128. "\n\nBreak now? (Hit Yes to break, No to continue, or Cancel to disable '%s' message boxes)",
  1129. Type
  1130. );
  1131. } else {
  1132. currentPos += wsprintfA (
  1133. currentPos,
  1134. "\n\n(Hit Cancel to disable '%s' message boxes)",
  1135. Type
  1136. );
  1137. }
  1138. if (currentPos > buffer) {
  1139. //
  1140. // the displayed message should be modified to include additional info
  1141. //
  1142. displayMessage = formattedMsg;
  1143. StringCopyByteCountA (
  1144. formattedMsg,
  1145. Msg,
  1146. ARRAYSIZE(formattedMsg) - (HALF_PTR) (currentPos - buffer)
  1147. );
  1148. StringCatA (formattedMsg, buffer);
  1149. }
  1150. #endif
  1151. switch (severity) {
  1152. case LOGSEV_FATAL_ERROR:
  1153. mbStyle = MB_ICONSTOP;
  1154. break;
  1155. case LOGSEV_ERROR:
  1156. mbStyle = MB_ICONERROR;
  1157. break;
  1158. case LOGSEV_WARNING:
  1159. mbStyle = MB_ICONEXCLAMATION;
  1160. break;
  1161. default:
  1162. mbStyle = MB_ICONINFORMATION;
  1163. }
  1164. mbStyle |= MB_SETFOREGROUND;
  1165. #ifdef DEBUG
  1166. if (OUT_ASSERT (outDest)) {
  1167. mbStyle |= MB_YESNOCANCEL|MB_DEFBUTTON2;
  1168. } else {
  1169. mbStyle |= MB_OKCANCEL;
  1170. }
  1171. #else
  1172. mbStyle |= MB_OK;
  1173. #endif
  1174. //
  1175. // check current thread id; if different than thread that initialized
  1176. // parent window handle, set parent to NULL
  1177. //
  1178. if (GetCurrentThreadId () == g_InitThreadId) {
  1179. parentWnd = g_LogPopupParentWnd;
  1180. } else {
  1181. parentWnd = NULL;
  1182. }
  1183. rc = MessageBoxA (parentWnd, displayMessage, Type, mbStyle);
  1184. #ifdef DEBUG
  1185. if (rc == IDCANCEL) {
  1186. //
  1187. // cancel this type of messages
  1188. //
  1189. LogSetErrorDest (Type, outDest | OD_POPUP_CANCEL);
  1190. } else if (rc == IDYES) {
  1191. //
  1192. // If Yes was clicked, call DebugBreak to get assert behavoir
  1193. //
  1194. DebugBreak();
  1195. }
  1196. #endif
  1197. }
  1198. }
  1199. VOID
  1200. pDisplayPopupW (
  1201. IN PCSTR Type,
  1202. IN PWSTR Msg,
  1203. IN DWORD LastError,
  1204. IN BOOL Forced
  1205. )
  1206. {
  1207. PCSTR msgA;
  1208. //
  1209. // call the ANSI version because wsprintfW is not properly implemented on Win9x
  1210. //
  1211. msgA = ConvertWtoA (Msg);
  1212. pDisplayPopupA (Type, msgA, LastError, Forced);
  1213. FreeConvertedStr (msgA);
  1214. }
  1215. /*++
  1216. Routine Description:
  1217. pRawWriteLogOutputA and pRawWriteLogOutputW output specified message
  1218. to all character devices implied by the type. The message is not
  1219. formatted in any way
  1220. Arguments:
  1221. Type - Specifies the type of the message, displayed as the popup's title
  1222. Msg - Specifies the message
  1223. Return Value:
  1224. none
  1225. --*/
  1226. VOID
  1227. pRawWriteLogOutputA (
  1228. IN PCSTR Type,
  1229. IN PCSTR Message,
  1230. IN PCSTR formattedMsg,
  1231. IN BOOL NoMainLog
  1232. )
  1233. {
  1234. OUTPUTDEST outDest;
  1235. LOGARGA callbackArgA;
  1236. LOGARGW callbackArgW;
  1237. static BOOL inCallback = FALSE;
  1238. #ifdef DEBUG
  1239. HANDLE handle;
  1240. #endif
  1241. outDest = pGetTypeOutputDest (Type);
  1242. if (OUT_NO_OUTPUT (outDest)) {
  1243. return;
  1244. }
  1245. if (!inCallback && (g_LogCallbackA || g_LogCallbackW)) {
  1246. inCallback = TRUE;
  1247. if (g_LogCallbackA) {
  1248. ZeroMemory (&callbackArgA, sizeof (callbackArgA));
  1249. callbackArgA.Type = Type;
  1250. callbackArgA.ModuleInstance = g_LibHandle;
  1251. callbackArgA.Message = Message;
  1252. callbackArgA.FormattedMessage = formattedMsg;
  1253. callbackArgA.Debug = NoMainLog;
  1254. g_LogCallbackA (&callbackArgA);
  1255. } else {
  1256. ZeroMemory (&callbackArgW, sizeof (callbackArgW));
  1257. callbackArgW.Type = Type;
  1258. callbackArgW.ModuleInstance = g_LibHandle;
  1259. callbackArgW.Message = ConvertAtoW (Message);
  1260. callbackArgW.FormattedMessage = ConvertAtoW (formattedMsg);
  1261. callbackArgW.Debug = NoMainLog;
  1262. g_LogCallbackW (&callbackArgW);
  1263. if (callbackArgW.Message) {
  1264. FreeConvertedStr (callbackArgW.Message);
  1265. }
  1266. if (callbackArgW.FormattedMessage) {
  1267. FreeConvertedStr (callbackArgW.FormattedMessage);
  1268. }
  1269. }
  1270. inCallback = FALSE;
  1271. return;
  1272. }
  1273. if (!NoMainLog && OUT_LOGFILE (outDest)) {
  1274. pWriteToMainLogA (Type, LOGSEV_INFORMATION, formattedMsg);
  1275. }
  1276. //
  1277. // log to each specified device
  1278. //
  1279. if (OUT_DEBUGGER(outDest)) {
  1280. OutputDebugStringA (formattedMsg);
  1281. }
  1282. if (OUT_CONSOLE(outDest)) {
  1283. fprintf (stderr, "%s", formattedMsg);
  1284. }
  1285. #ifdef DEBUG
  1286. if (OUT_DEBUGLOG (outDest)) {
  1287. handle = CreateFileA (
  1288. g_DebugLogFile,
  1289. GENERIC_WRITE,
  1290. FILE_SHARE_READ,
  1291. NULL,
  1292. OPEN_ALWAYS,
  1293. FILE_ATTRIBUTE_NORMAL,
  1294. NULL
  1295. );
  1296. if (handle != INVALID_HANDLE_VALUE) {
  1297. SetFilePointer (handle, 0, NULL, FILE_END);
  1298. WriteFileStringA (handle, formattedMsg);
  1299. CloseHandle (handle);
  1300. }
  1301. }
  1302. #endif
  1303. }
  1304. VOID
  1305. pRawWriteLogOutputW (
  1306. IN PCSTR Type,
  1307. IN PCWSTR Message,
  1308. IN PCWSTR formattedMsg,
  1309. IN BOOL NoMainLog
  1310. )
  1311. {
  1312. OUTPUTDEST outDest;
  1313. LOGARGA callbackArgA;
  1314. LOGARGW callbackArgW;
  1315. static BOOL inCallback = FALSE;
  1316. #ifdef DEBUG
  1317. HANDLE handle;
  1318. #endif
  1319. outDest = pGetTypeOutputDest (Type);
  1320. if (OUT_NO_OUTPUT (outDest)) {
  1321. return;
  1322. }
  1323. if (!inCallback && (g_LogCallbackA || g_LogCallbackW)) {
  1324. inCallback = TRUE;
  1325. if (g_LogCallbackW) {
  1326. ZeroMemory (&callbackArgW, sizeof (callbackArgW));
  1327. callbackArgW.Type = Type;
  1328. callbackArgW.ModuleInstance = g_LibHandle;
  1329. callbackArgW.Message = Message;
  1330. callbackArgW.FormattedMessage = formattedMsg;
  1331. callbackArgW.Debug = NoMainLog;
  1332. g_LogCallbackW (&callbackArgW);
  1333. } else {
  1334. ZeroMemory (&callbackArgA, sizeof (callbackArgA));
  1335. callbackArgA.Type = Type;
  1336. callbackArgA.ModuleInstance = g_LibHandle;
  1337. callbackArgA.Message = ConvertWtoA (Message);
  1338. callbackArgA.FormattedMessage = ConvertWtoA (formattedMsg);
  1339. callbackArgA.Debug = NoMainLog;
  1340. g_LogCallbackA (&callbackArgA);
  1341. if (callbackArgA.Message) {
  1342. FreeConvertedStr (callbackArgA.Message);
  1343. }
  1344. if (callbackArgA.FormattedMessage) {
  1345. FreeConvertedStr (callbackArgA.FormattedMessage);
  1346. }
  1347. }
  1348. inCallback = FALSE;
  1349. return;
  1350. }
  1351. if (!NoMainLog && OUT_LOGFILE (outDest)) {
  1352. pWriteToMainLogW (Type, LOGSEV_INFORMATION, formattedMsg);
  1353. }
  1354. //
  1355. // log to each specified device
  1356. //
  1357. if (OUT_DEBUGGER(outDest)) {
  1358. OutputDebugStringW (formattedMsg);
  1359. }
  1360. if (OUT_CONSOLE(outDest)) {
  1361. fwprintf (stderr, L"%s", formattedMsg);
  1362. }
  1363. #ifdef DEBUG
  1364. if (OUT_DEBUGLOG (outDest)) {
  1365. handle = CreateFileA (
  1366. g_DebugLogFile,
  1367. GENERIC_WRITE,
  1368. FILE_SHARE_READ,
  1369. NULL,
  1370. OPEN_ALWAYS,
  1371. FILE_ATTRIBUTE_NORMAL,
  1372. NULL
  1373. );
  1374. if (handle != INVALID_HANDLE_VALUE) {
  1375. SetFilePointer (handle, 0, NULL, FILE_END);
  1376. WriteFileStringW (handle, formattedMsg);
  1377. CloseHandle (handle);
  1378. }
  1379. }
  1380. #endif
  1381. }
  1382. /*++
  1383. Routine Description:
  1384. pFormatAndWriteMsgA and pFormatAndWriteMsgW format the message
  1385. specified by the Format argument and outputs it to all destinations
  1386. specified in OutDest. If no destination for the message,
  1387. no action is performed.
  1388. Arguments:
  1389. Type - Specifies the type (category) of the message
  1390. Format - Specifies either the message in ASCII format or
  1391. a message ID (if SHIFTRIGHT16(Format) == 0). The message
  1392. will be formatted using args.
  1393. args - Specifies a list of arguments to be used when formatting
  1394. the message. If a message ID is used for Format, args
  1395. is supposed to be an array of pointers to strings
  1396. Return Value:
  1397. none
  1398. --*/
  1399. VOID
  1400. pFormatAndWriteMsgA (
  1401. IN BOOL NoMainLog,
  1402. IN PCSTR Type,
  1403. IN PCSTR Format,
  1404. IN va_list args
  1405. )
  1406. {
  1407. CHAR output[OUTPUT_BUFSIZE_LARGE];
  1408. CHAR formattedMsg[OUTPUT_BUFSIZE_LARGE];
  1409. OUTPUTDEST outDest;
  1410. DWORD lastError;
  1411. PRIVATE_ASSERT (g_LoggingNow > 0);
  1412. // clear LOGTITLE flag on each regular LOG
  1413. g_HasTitle = FALSE;
  1414. outDest = pGetTypeOutputDest (Type);
  1415. if (OUT_NO_OUTPUT(outDest)) {
  1416. return;
  1417. }
  1418. if (OUT_ERROR (outDest)) {
  1419. lastError = GetLastError();
  1420. } else {
  1421. lastError = ERROR_SUCCESS;
  1422. }
  1423. // format output string
  1424. if (SHIFTRIGHT16((UBINT)Format) == 0) {
  1425. //
  1426. // this is actually a Resource String ID
  1427. //
  1428. if (!FormatMessageA (
  1429. FORMAT_MESSAGE_FROM_HMODULE,
  1430. (LPVOID) g_LibHandle,
  1431. (DWORD)(UBINT) Format,
  1432. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  1433. (LPVOID) output,
  1434. OUTPUT_BUFSIZE_LARGE,
  1435. &args
  1436. )) {
  1437. // the string is missing from Resources
  1438. DEBUGMSG ((DBG_WHOOPS, "Log() called with invalid MsgID, Instance=0x%08X", g_LibHandle));
  1439. return;
  1440. }
  1441. } else {
  1442. //
  1443. // format given string using printf style
  1444. //
  1445. _vsnprintf(output, OUTPUT_BUFSIZE_LARGE, Format, args);
  1446. }
  1447. pIndentMessageA (
  1448. formattedMsg,
  1449. OUTPUT_BUFSIZE_LARGE,
  1450. Type,
  1451. output,
  1452. MSGBODY_INDENT,
  1453. lastError
  1454. );
  1455. pRawWriteLogOutputA (Type, output, formattedMsg, NoMainLog);
  1456. if (pIsPopupEnabled (Type)) {
  1457. #ifdef DEBUG
  1458. if (MUST_BE_LOCALIZED (outDest)) {
  1459. PRIVATE_ASSERT (
  1460. !MUST_BE_LOCALIZED (outDest) ||
  1461. (SHIFTRIGHT16((UBINT)Format) == 0)
  1462. );
  1463. }
  1464. pDisplayPopupA (Type, output, lastError, OUT_FORCED_POPUP(outDest));
  1465. #else
  1466. if (SHIFTRIGHT16 ((UBINT)Format) == 0) {
  1467. pDisplayPopupA (Type, output, lastError, OUT_FORCED_POPUP(outDest));
  1468. }
  1469. #endif
  1470. }
  1471. }
  1472. VOID
  1473. pFormatAndWriteMsgW (
  1474. IN BOOL NoMainLog,
  1475. IN PCSTR Type,
  1476. IN PCSTR Format,
  1477. IN va_list args
  1478. )
  1479. {
  1480. WCHAR formatW[OUTPUT_BUFSIZE_LARGE];
  1481. WCHAR output[OUTPUT_BUFSIZE_LARGE];
  1482. WCHAR formattedMsg[OUTPUT_BUFSIZE_LARGE];
  1483. OUTPUTDEST outDest;
  1484. DWORD lastError;
  1485. PRIVATE_ASSERT (g_LoggingNow > 0);
  1486. // clear LOGTITLE flag on each regular LOG
  1487. g_HasTitle = FALSE;
  1488. outDest = pGetTypeOutputDest (Type);
  1489. if (OUT_NO_OUTPUT(outDest)) {
  1490. return;
  1491. }
  1492. if (OUT_ERROR (outDest)) {
  1493. lastError = GetLastError();
  1494. } else {
  1495. lastError = ERROR_SUCCESS;
  1496. }
  1497. // format output string
  1498. if (SHIFTRIGHT16((UBINT)Format) == 0) {
  1499. //
  1500. // this is actually a Resource String ID
  1501. //
  1502. if (!FormatMessageW (
  1503. FORMAT_MESSAGE_FROM_HMODULE,
  1504. (LPVOID) g_LibHandle,
  1505. (DWORD)(UBINT) Format,
  1506. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  1507. (LPVOID) output,
  1508. OUTPUT_BUFSIZE_LARGE,
  1509. &args
  1510. )) {
  1511. // the string is missing from Resources
  1512. DEBUGMSG ((DBG_WHOOPS, "Log() called with invalid MsgID, Instance=0x%08X", g_LibHandle));
  1513. return;
  1514. }
  1515. } else {
  1516. KnownSizeAtoW (formatW, Format);
  1517. //
  1518. // format given string using printf style
  1519. //
  1520. _vsnwprintf(output, OUTPUT_BUFSIZE_LARGE, formatW, args);
  1521. }
  1522. pIndentMessageW (
  1523. formattedMsg,
  1524. OUTPUT_BUFSIZE_LARGE,
  1525. Type,
  1526. output,
  1527. MSGBODY_INDENT,
  1528. lastError
  1529. );
  1530. pRawWriteLogOutputW (Type, output, formattedMsg, NoMainLog);
  1531. if (pIsPopupEnabled (Type)) {
  1532. #ifdef DEBUG
  1533. if (MUST_BE_LOCALIZED (outDest)) {
  1534. PRIVATE_ASSERT (SHIFTRIGHT16((UBINT)Format) == 0);
  1535. }
  1536. pDisplayPopupW (Type, output, lastError, OUT_FORCED_POPUP(outDest));
  1537. #else
  1538. if (SHIFTRIGHT16 ((UBINT)Format) == 0) {
  1539. pDisplayPopupW (Type, output, lastError, OUT_FORCED_POPUP(outDest));
  1540. }
  1541. #endif
  1542. }
  1543. }
  1544. BOOL
  1545. pInitLog (
  1546. IN BOOL FirstTimeInit,
  1547. IN HWND LogPopupParentWnd, OPTIONAL
  1548. OUT HWND *OrgPopupParentWnd, OPTIONAL
  1549. IN PCSTR LogFile, OPTIONAL
  1550. IN PLOGCALLBACKA LogCallbackA, OPTIONAL
  1551. IN PLOGCALLBACKW LogCallbackW OPTIONAL
  1552. )
  1553. /*++
  1554. Routine Description:
  1555. pInitLog actually initializes the log system.
  1556. Arguments:
  1557. LogPopupParentWnd - Specifies the parent window to be used by the
  1558. popups, or NULL if popups are to be suppressed.
  1559. This value is not optional on the first call
  1560. to this function.
  1561. OrgPopupParentWnd - Receives the original parent window.
  1562. LogFile - Specifies the name of the log file. If not specified,
  1563. logging goes to a default file (%windir%\cobra.log).
  1564. LogCallback - Specifies a function to call instead of the internal
  1565. logging functions.
  1566. Return Value:
  1567. TRUE if log system successfully initialized
  1568. --*/
  1569. {
  1570. HINF hInf = INVALID_HANDLE_VALUE;
  1571. BOOL result = FALSE;
  1572. PDEFAULT_DESTINATION dest;
  1573. #ifdef DEBUG
  1574. PSTR p;
  1575. #endif
  1576. __try {
  1577. g_LogInit = FALSE;
  1578. if (FirstTimeInit) {
  1579. PRIVATE_ASSERT (!g_FirstTypePtr);
  1580. dest = g_DefaultDest;
  1581. while (dest->Type) {
  1582. LogSetErrorDest (dest->Type, dest->Flags);
  1583. dest++;
  1584. }
  1585. GetWindowsDirectoryA (g_MainLogFile, ARRAYSIZE(g_MainLogFile));
  1586. StringCatA (g_MainLogFile, "\\cobra");
  1587. #ifdef DEBUG
  1588. StringCopyA (g_DebugLogFile, g_MainLogFile);
  1589. StringCatA (g_DebugLogFile, ".dbg");
  1590. g_DebugInfPathBufA[0] = g_DebugLogFile[0];
  1591. #endif
  1592. StringCatA (g_MainLogFile, ".log");
  1593. }
  1594. if (LogFile) {
  1595. StackStringCopyA (g_MainLogFile, LogFile);
  1596. #ifdef DEBUG
  1597. StringCopyA (g_DebugLogFile, g_MainLogFile);
  1598. p = _mbsrchr (g_DebugLogFile, '.');
  1599. if (p) {
  1600. if (_mbschr (p, TEXT('\\'))) {
  1601. p = NULL;
  1602. }
  1603. }
  1604. if (p) {
  1605. StringCopyA (p, ".dbg");
  1606. } else {
  1607. StringCatA (g_DebugLogFile, ".dbg");
  1608. }
  1609. #endif
  1610. }
  1611. if (g_ResetLog) {
  1612. SetFileAttributesA (g_MainLogFile, FILE_ATTRIBUTE_NORMAL);
  1613. DeleteFileA (g_MainLogFile);
  1614. }
  1615. #ifdef DEBUG
  1616. if (g_ResetLog) {
  1617. SetFileAttributesA (g_DebugLogFile, FILE_ATTRIBUTE_NORMAL);
  1618. DeleteFileA (g_DebugLogFile);
  1619. }
  1620. #endif
  1621. if (LogCallbackA) {
  1622. g_LogCallbackA = LogCallbackA;
  1623. }
  1624. if (LogCallbackW) {
  1625. g_LogCallbackW = LogCallbackW;
  1626. }
  1627. #ifdef DEBUG
  1628. if (FirstTimeInit) {
  1629. //
  1630. // get user's preferences
  1631. //
  1632. hInf = SetupOpenInfFileA (g_DebugInfPathBufA, NULL, INF_STYLE_WIN4 | INF_STYLE_OLDNT, NULL);
  1633. if (INVALID_HANDLE_VALUE != hInf && pGetUserPreferences(hInf)) {
  1634. g_DoLog = TRUE;
  1635. }
  1636. }
  1637. if (g_ResetLog) {
  1638. SetFileAttributesA (g_DebugLogFile, FILE_ATTRIBUTE_NORMAL);
  1639. DeleteFileA (g_DebugLogFile);
  1640. }
  1641. #endif
  1642. if (OrgPopupParentWnd) {
  1643. *OrgPopupParentWnd = g_LogPopupParentWnd;
  1644. }
  1645. if (LogPopupParentWnd) {
  1646. g_LogPopupParentWnd = LogPopupParentWnd;
  1647. g_InitThreadId = GetCurrentThreadId ();
  1648. }
  1649. result = TRUE;
  1650. }
  1651. __finally {
  1652. if (hInf != INVALID_HANDLE_VALUE) {
  1653. SetupCloseInfFile (hInf);
  1654. }
  1655. if (!result) { //lint !e774
  1656. if (g_FirstTypePtr) {
  1657. HeapFree (g_hHeap, 0, g_FirstTypePtr);
  1658. g_FirstTypePtr = NULL;
  1659. g_TypeTableCount = 0;
  1660. g_TypeTableFreeCount = 0;
  1661. }
  1662. g_OutDestAll = OD_UNDEFINED;
  1663. g_OutDestDefault = OD_UNDEFINED;
  1664. #ifdef DEBUG
  1665. g_DoLog = FALSE;
  1666. #endif
  1667. }
  1668. g_LogInit = TRUE;
  1669. g_ResetLog = FALSE;
  1670. }
  1671. return result;
  1672. }
  1673. VOID
  1674. LogSetVerboseLevel (
  1675. IN OUTPUT_DESTINATION Level
  1676. )
  1677. {
  1678. OUTPUT_DESTINATION Debugger = 0;
  1679. if (Level > 3) {
  1680. Debugger = OD_DEBUGGER|OD_ASSERT;
  1681. }
  1682. LogSetErrorDest (LOG_FATAL_ERROR, Level > 0 ? OD_POPUP_CANCEL|OD_LOGFILE|OD_ERROR|OD_CONSOLE|Debugger : OD_SUPPRESS);
  1683. LogSetErrorDest (LOG_ERROR, Level > 0 ? OD_POPUP_CANCEL|OD_LOGFILE|OD_ERROR|OD_CONSOLE|Debugger : OD_SUPPRESS);
  1684. LogSetErrorDest (LOG_MODULE_ERROR, Level > 0 ? OD_POPUP_CANCEL|OD_LOGFILE|OD_ERROR|OD_CONSOLE|Debugger : OD_SUPPRESS);
  1685. LogSetErrorDest (LOG_WARNING, Level > 1 ? OD_LOGFILE|Debugger : OD_SUPPRESS);
  1686. LogSetErrorDest (LOG_INFORMATION, Level > 2 ? OD_LOGFILE|Debugger : OD_SUPPRESS);
  1687. LogSetErrorDest ("Assert", OD_POPUP|OD_ERROR|Debugger);
  1688. LogSetErrorDest ("Verbose", Level > 2 ? OD_LOGFILE|Debugger : OD_SUPPRESS);
  1689. }
  1690. VOID
  1691. LogSetVerboseBitmap (
  1692. IN LOG_LEVEL Bitmap,
  1693. IN LOG_LEVEL BitsToAdjustMask,
  1694. IN BOOL EnableDebugger
  1695. )
  1696. {
  1697. OUTPUT_DESTINATION Debugger = 0;
  1698. if (EnableDebugger) {
  1699. Debugger = OD_DEBUGGER|OD_ASSERT;
  1700. }
  1701. if (BitsToAdjustMask & LL_FATAL_ERROR) {
  1702. LogSetErrorDest (LOG_FATAL_ERROR, (Bitmap & LL_FATAL_ERROR) ? OD_LOGFILE|OD_ERROR|OD_CONSOLE|Debugger : OD_SUPPRESS);
  1703. }
  1704. if (BitsToAdjustMask & LL_MODULE_ERROR) {
  1705. LogSetErrorDest (LOG_MODULE_ERROR, (Bitmap & LL_MODULE_ERROR) ? OD_LOGFILE|OD_ERROR|OD_CONSOLE|Debugger : OD_SUPPRESS);
  1706. }
  1707. if (BitsToAdjustMask & LL_ERROR) {
  1708. LogSetErrorDest (LOG_ERROR, (Bitmap & LL_ERROR) ? OD_LOGFILE|OD_ERROR|OD_CONSOLE|Debugger : OD_SUPPRESS);
  1709. }
  1710. if (BitsToAdjustMask & LL_WARNING) {
  1711. LogSetErrorDest (LOG_WARNING, (Bitmap & LL_WARNING) ? OD_LOGFILE|Debugger : OD_SUPPRESS);
  1712. }
  1713. if (BitsToAdjustMask & LL_INFORMATION) {
  1714. LogSetErrorDest (LOG_INFORMATION, (Bitmap & LL_INFORMATION) ? OD_LOGFILE|Debugger : OD_SUPPRESS);
  1715. }
  1716. if (BitsToAdjustMask & LL_STATUS) {
  1717. LogSetErrorDest (LOG_STATUS, (Bitmap & LL_STATUS) ? OD_LOGFILE|Debugger : OD_SUPPRESS);
  1718. }
  1719. if (BitsToAdjustMask & LL_UPDATE) {
  1720. LogSetErrorDest (LOG_UPDATE, (Bitmap & LL_UPDATE) ? OD_CONSOLE : OD_SUPPRESS);
  1721. }
  1722. }
  1723. /*++
  1724. Routine Description:
  1725. pInitialize initializes the log system calling the worker pInitLog. This function
  1726. should be only called once
  1727. Arguments:
  1728. None
  1729. Return Value:
  1730. TRUE if log system successfully initialized
  1731. --*/
  1732. BOOL
  1733. pInitialize (
  1734. VOID
  1735. )
  1736. {
  1737. return pInitLog (TRUE, NULL, NULL, NULL, NULL, NULL);
  1738. }
  1739. /*++
  1740. Routine Description:
  1741. LogReInit re-initializes the log system calling the worker pInitLog.
  1742. This function may be called any number of times, but only after pInitialize()
  1743. Arguments:
  1744. NewParent - Specifies the new parent handle.
  1745. OrgParent - Receives the old parent handle.
  1746. LogFile - Specifies a new log file name
  1747. LogCallback - Specifies a callback function that handles the log message (so
  1748. one module can pass log messages to another)
  1749. ResourceImage - Specifies the module path to use in FormatMessage when the
  1750. message is resource-based
  1751. Return Value:
  1752. TRUE if log system was successfully re-initialized
  1753. --*/
  1754. BOOL
  1755. LogReInitA (
  1756. IN HWND NewParent, OPTIONAL
  1757. OUT HWND *OrgParent, OPTIONAL
  1758. IN PCSTR LogFile, OPTIONAL
  1759. IN PLOGCALLBACKA LogCallback OPTIONAL
  1760. )
  1761. {
  1762. return pInitLog (FALSE, NewParent, OrgParent, LogFile, LogCallback, NULL);
  1763. }
  1764. BOOL
  1765. LogReInitW (
  1766. IN HWND NewParent, OPTIONAL
  1767. OUT HWND *OrgParent, OPTIONAL
  1768. IN PCWSTR LogFile, OPTIONAL
  1769. IN PLOGCALLBACKW LogCallback OPTIONAL
  1770. )
  1771. {
  1772. CHAR ansiLogFile[MAX_MBCHAR_PATH];
  1773. if (LogFile) {
  1774. KnownSizeWtoA (ansiLogFile, LogFile);
  1775. LogFile = (PWSTR) ansiLogFile;
  1776. }
  1777. return pInitLog (FALSE, NewParent, OrgParent, (PCSTR) LogFile, NULL, LogCallback);
  1778. }
  1779. VOID
  1780. LogBegin (
  1781. IN HMODULE ModuleInstance
  1782. )
  1783. {
  1784. DWORD threadError;
  1785. DWORD rc;
  1786. threadError = GetLastError ();
  1787. if (!g_LogMutex) {
  1788. InitializeLog();
  1789. }
  1790. rc = WaitForSingleObject (g_LogMutex, INFINITE);
  1791. PRIVATE_ASSERT (rc == WAIT_OBJECT_0 || rc == WAIT_ABANDONED);
  1792. if (rc == WAIT_ABANDONED) {
  1793. g_LoggingNow = 0;
  1794. }
  1795. if (!g_LoggingNow) {
  1796. g_LibHandle = ModuleInstance;
  1797. SetLastError (threadError);
  1798. g_LogError = threadError;
  1799. }
  1800. g_LoggingNow++;
  1801. }
  1802. VOID
  1803. LogEnd (
  1804. VOID
  1805. )
  1806. {
  1807. g_LoggingNow--;
  1808. if (!g_LoggingNow) {
  1809. g_LibHandle = g_hInst;
  1810. SetLastError (g_LogError);
  1811. }
  1812. ReleaseMutex (g_LogMutex);
  1813. }
  1814. VOID
  1815. pDisableLog (
  1816. VOID
  1817. )
  1818. {
  1819. g_LogInit = FALSE;
  1820. }
  1821. VOID
  1822. pExitLog (
  1823. VOID
  1824. )
  1825. /*++
  1826. Routine Description:
  1827. pExitLog cleans up any resources used by the log system
  1828. Arguments:
  1829. none
  1830. Return Value:
  1831. none
  1832. --*/
  1833. {
  1834. g_LogInit = FALSE;
  1835. WaitForSingleObject (g_LogMutex, 60000);
  1836. CloseHandle (g_LogMutex);
  1837. g_LogMutex = NULL;
  1838. if (g_FirstTypePtr) {
  1839. HeapFree (g_hHeap, 0, g_FirstTypePtr);
  1840. g_FirstTypePtr = NULL;
  1841. g_TypeTableCount = 0;
  1842. g_TypeTableFreeCount = 0;
  1843. }
  1844. g_OutDestAll = OD_UNDEFINED;
  1845. g_OutDestDefault = OD_UNDEFINED;
  1846. }
  1847. /*++
  1848. Routine Description:
  1849. LogA and LogW preserve the last error code; they call the helpers
  1850. pFormatAndWriteMsgA and pFormatAndWriteMsgW respectivelly.
  1851. Arguments:
  1852. Type - Specifies the type (category) of the message
  1853. Format - Specifies either the message in ASCII format or
  1854. a message ID (if SHIFTRIGHT16(Format) == 0). The message
  1855. will be formatted using args.
  1856. ... - Specifies a list of arguments to be used when formatting
  1857. the message. If a message ID is used for Format, args
  1858. is supposed to be an array of pointers to strings
  1859. Return Value:
  1860. none
  1861. --*/
  1862. VOID
  1863. _cdecl
  1864. LogA (
  1865. IN PCSTR Type,
  1866. IN PCSTR Format,
  1867. ...
  1868. )
  1869. {
  1870. va_list args;
  1871. if (!g_LogInit) {
  1872. return;
  1873. }
  1874. va_start (args, Format);
  1875. pFormatAndWriteMsgA (
  1876. FALSE,
  1877. Type,
  1878. Format,
  1879. args
  1880. );
  1881. va_end (args);
  1882. }
  1883. VOID
  1884. _cdecl
  1885. LogW (
  1886. IN PCSTR Type,
  1887. IN PCSTR Format,
  1888. ...
  1889. )
  1890. {
  1891. va_list args;
  1892. if (!g_LogInit) {
  1893. return;
  1894. }
  1895. va_start (args, Format);
  1896. pFormatAndWriteMsgW (
  1897. FALSE,
  1898. Type,
  1899. Format,
  1900. args
  1901. );
  1902. va_end (args);
  1903. }
  1904. VOID
  1905. _cdecl
  1906. LogIfA (
  1907. IN BOOL Condition,
  1908. IN PCSTR Type,
  1909. IN PCSTR Format,
  1910. ...
  1911. )
  1912. {
  1913. va_list args;
  1914. if (!g_LogInit) {
  1915. return;
  1916. }
  1917. if (!Condition) {
  1918. return;
  1919. }
  1920. va_start (args, Format);
  1921. pFormatAndWriteMsgA (
  1922. FALSE,
  1923. Type,
  1924. Format,
  1925. args
  1926. );
  1927. va_end (args);
  1928. }
  1929. VOID
  1930. _cdecl
  1931. LogIfW (
  1932. IN BOOL Condition,
  1933. IN PCSTR Type,
  1934. IN PCSTR Format,
  1935. ...
  1936. )
  1937. {
  1938. va_list args;
  1939. if (!g_LogInit) {
  1940. return;
  1941. }
  1942. if (!Condition) {
  1943. return;
  1944. }
  1945. va_start (args, Format);
  1946. pFormatAndWriteMsgW (
  1947. FALSE,
  1948. Type,
  1949. Format,
  1950. args
  1951. );
  1952. va_end (args);
  1953. }
  1954. #ifdef DEBUG
  1955. VOID
  1956. _cdecl
  1957. DbgLogA (
  1958. IN PCSTR Type,
  1959. IN PCSTR Format,
  1960. ...
  1961. )
  1962. {
  1963. va_list args;
  1964. if (!g_LogInit) {
  1965. return;
  1966. }
  1967. va_start (args, Format);
  1968. pFormatAndWriteMsgA (
  1969. TRUE,
  1970. Type,
  1971. Format,
  1972. args
  1973. );
  1974. va_end (args);
  1975. }
  1976. VOID
  1977. _cdecl
  1978. DbgLogW (
  1979. IN PCSTR Type,
  1980. IN PCSTR Format,
  1981. ...
  1982. )
  1983. {
  1984. va_list args;
  1985. if (!g_LogInit) {
  1986. return;
  1987. }
  1988. va_start (args, Format);
  1989. pFormatAndWriteMsgW (
  1990. TRUE,
  1991. Type,
  1992. Format,
  1993. args
  1994. );
  1995. va_end (args);
  1996. }
  1997. VOID
  1998. _cdecl
  1999. DbgLogIfA (
  2000. IN BOOL Condition,
  2001. IN PCSTR Type,
  2002. IN PCSTR Format,
  2003. ...
  2004. )
  2005. {
  2006. va_list args;
  2007. if (!g_LogInit) {
  2008. return;
  2009. }
  2010. if (!Condition) {
  2011. return;
  2012. }
  2013. va_start (args, Format);
  2014. pFormatAndWriteMsgA (
  2015. TRUE,
  2016. Type,
  2017. Format,
  2018. args
  2019. );
  2020. va_end (args);
  2021. }
  2022. VOID
  2023. _cdecl
  2024. DbgLogIfW (
  2025. IN BOOL Condition,
  2026. IN PCSTR Type,
  2027. IN PCSTR Format,
  2028. ...
  2029. )
  2030. {
  2031. va_list args;
  2032. if (!g_LogInit) {
  2033. return;
  2034. }
  2035. if (!Condition) {
  2036. return;
  2037. }
  2038. va_start (args, Format);
  2039. pFormatAndWriteMsgW (
  2040. TRUE,
  2041. Type,
  2042. Format,
  2043. args
  2044. );
  2045. va_end (args);
  2046. }
  2047. #endif
  2048. VOID
  2049. LogTitleA (
  2050. IN PCSTR Type,
  2051. IN PCSTR Title OPTIONAL
  2052. )
  2053. {
  2054. CHAR formattedMsg[OUTPUT_BUFSIZE_LARGE];
  2055. if (!g_LogInit) {
  2056. return;
  2057. }
  2058. StringCopyByteCountA (g_LastType, Type, DWSIZEOF (g_LastType));
  2059. if (!Title) {
  2060. Title = Type;
  2061. }
  2062. StringCopyByteCountA (formattedMsg, Title, DWSIZEOF (formattedMsg) - DWSIZEOF (S_COLUMNDOUBLELINEA));
  2063. StringCatA (formattedMsg, S_COLUMNDOUBLELINEA);
  2064. pRawWriteLogOutputA (Type, NULL, formattedMsg, FALSE);
  2065. //
  2066. // set LOGTITLE flag
  2067. //
  2068. g_HasTitle = TRUE;
  2069. }
  2070. VOID
  2071. LogTitleW (
  2072. IN PCSTR Type,
  2073. IN PCWSTR Title OPTIONAL
  2074. )
  2075. {
  2076. WCHAR formattedMsg[OUTPUT_BUFSIZE_LARGE];
  2077. WCHAR typeW[OUTPUT_BUFSIZE_SMALL];
  2078. if (!g_LogInit) {
  2079. return;
  2080. }
  2081. StringCopyCharCountA (g_LastType, Type, DWSIZEOF (g_LastType));
  2082. if (!Title) {
  2083. KnownSizeAtoW (typeW, Type);
  2084. Title = typeW;
  2085. }
  2086. StringCopyCharCountW (formattedMsg, Title, DWSIZEOF (formattedMsg) - DWSIZEOF (S_COLUMNDOUBLELINEW));
  2087. StringCatW (formattedMsg, S_COLUMNDOUBLELINEW);
  2088. pRawWriteLogOutputW (Type, NULL, formattedMsg, FALSE);
  2089. //
  2090. // set LOGTITLE flag
  2091. //
  2092. g_HasTitle = TRUE;
  2093. }
  2094. VOID
  2095. LogLineA (
  2096. IN PCSTR Line
  2097. )
  2098. {
  2099. CHAR output[OUTPUT_BUFSIZE_LARGE];
  2100. BOOL hasNewLine = FALSE;
  2101. PCSTR p;
  2102. if (!g_LogInit) {
  2103. return;
  2104. }
  2105. if (!Line) {
  2106. return;
  2107. }
  2108. if (!g_HasTitle) {
  2109. DEBUGMSG ((DBG_WHOOPS, "LOGTITLE missing before LOGLINE"));
  2110. return;
  2111. }
  2112. StringCopyByteCountA (output, Line, DWSIZEOF (output) - 4);
  2113. //
  2114. // find out if the line terminates with newline
  2115. //
  2116. for (p = _mbsstr (output, S_NEWLINEA); p; p = _mbsstr (p + NEWLINE_CHAR_COUNTA, S_NEWLINEA)) {
  2117. if (p[NEWLINE_CHAR_COUNTA] == 0) {
  2118. //
  2119. // the line ends with a newline
  2120. //
  2121. hasNewLine = TRUE;
  2122. break;
  2123. }
  2124. }
  2125. if (!hasNewLine) {
  2126. StringCatA (output, S_NEWLINEA);
  2127. }
  2128. pRawWriteLogOutputA (g_LastType, NULL, output, FALSE);
  2129. }
  2130. VOID
  2131. LogLineW (
  2132. IN PCWSTR Line
  2133. )
  2134. {
  2135. WCHAR output[OUTPUT_BUFSIZE_LARGE];
  2136. BOOL hasNewLine = FALSE;
  2137. PCWSTR p;
  2138. if (!g_LogInit) {
  2139. return;
  2140. }
  2141. if (!Line) {
  2142. return;
  2143. }
  2144. if (!g_HasTitle) {
  2145. DEBUGMSG ((DBG_WHOOPS, "LOGTITLE missing before LOGLINE"));
  2146. return;
  2147. }
  2148. StringCopyCharCountW (output, Line, DWSIZEOF (output) / DWSIZEOF (WCHAR) - 4);
  2149. //
  2150. // find out if the line terminates with newline
  2151. //
  2152. for (p = wcsstr (output, S_NEWLINEW); p; p = wcsstr (p + NEWLINE_CHAR_COUNTW, S_NEWLINEW)) {
  2153. if (p[NEWLINE_CHAR_COUNTW] == 0) {
  2154. //
  2155. // the line ends with a newline
  2156. //
  2157. hasNewLine = TRUE;
  2158. break;
  2159. }
  2160. }
  2161. if (!hasNewLine) {
  2162. StringCatW (output, S_NEWLINEW);
  2163. }
  2164. pRawWriteLogOutputW (g_LastType, NULL, output, FALSE);
  2165. }
  2166. VOID
  2167. LogDirectA (
  2168. IN PCSTR Type,
  2169. IN PCSTR Text
  2170. )
  2171. {
  2172. if (!g_LogInit) {
  2173. return;
  2174. }
  2175. g_HasTitle = FALSE;
  2176. pRawWriteLogOutputA (Type, NULL, Text, FALSE);
  2177. }
  2178. VOID
  2179. LogDirectW (
  2180. IN PCSTR Type,
  2181. IN PCWSTR Text
  2182. )
  2183. {
  2184. if (!g_LogInit) {
  2185. return;
  2186. }
  2187. g_HasTitle = FALSE;
  2188. pRawWriteLogOutputW (Type, NULL, Text, FALSE);
  2189. }
  2190. #ifdef DEBUG
  2191. VOID
  2192. DbgDirectA (
  2193. IN PCSTR Type,
  2194. IN PCSTR Text
  2195. )
  2196. {
  2197. if (!g_LogInit) {
  2198. return;
  2199. }
  2200. g_HasTitle = FALSE;
  2201. pRawWriteLogOutputA (Type, NULL, Text, TRUE);
  2202. }
  2203. VOID
  2204. DbgDirectW (
  2205. IN PCSTR Type,
  2206. IN PCWSTR Text
  2207. )
  2208. {
  2209. if (!g_LogInit) {
  2210. return;
  2211. }
  2212. g_HasTitle = FALSE;
  2213. pRawWriteLogOutputW (Type, NULL, Text, TRUE);
  2214. }
  2215. #endif
  2216. VOID
  2217. SuppressAllLogPopups (
  2218. IN BOOL SuppressOn
  2219. )
  2220. {
  2221. g_SuppressAllPopups = SuppressOn;
  2222. }
  2223. #ifdef DEBUG
  2224. /*++
  2225. Routine Description:
  2226. DebugLogTimeA and DebugLogTimeW preserve the last error code;
  2227. they append the current date and time to the formatted message,
  2228. then call LogA and LogW to actually process the message.
  2229. Arguments:
  2230. Format - Specifies either the message in ASCII format or
  2231. a message ID (if SHIFTRIGHT16(Format) == 0). The message
  2232. will be formatted using args.
  2233. ... - Specifies a list of arguments to be used when formatting
  2234. the message. If a message ID is used for Format, args
  2235. is supposed to be an array of pointers to strings
  2236. Return Value:
  2237. none
  2238. --*/
  2239. VOID
  2240. _cdecl
  2241. DebugLogTimeA (
  2242. IN PCSTR Format,
  2243. ...
  2244. )
  2245. {
  2246. CHAR msg[OUTPUT_BUFSIZE_LARGE];
  2247. CHAR date[OUTPUT_BUFSIZE_SMALL];
  2248. CHAR ttime[OUTPUT_BUFSIZE_SMALL];
  2249. PSTR appendPos, end;
  2250. DWORD currentTickCount;
  2251. va_list args;
  2252. if (!g_LogInit) {
  2253. return;
  2254. }
  2255. if (!g_DoLog) {
  2256. return;
  2257. }
  2258. //
  2259. // first, get the current date and time into the string.
  2260. //
  2261. if (!GetDateFormatA (
  2262. LOCALE_SYSTEM_DEFAULT,
  2263. LOCALE_NOUSEROVERRIDE,
  2264. NULL,
  2265. NULL,
  2266. date,
  2267. OUTPUT_BUFSIZE_SMALL)) {
  2268. StringCopyA (date,"** Error retrieving date. **");
  2269. }
  2270. if (!GetTimeFormatA (
  2271. LOCALE_SYSTEM_DEFAULT,
  2272. LOCALE_NOUSEROVERRIDE,
  2273. NULL,
  2274. NULL,
  2275. ttime,
  2276. OUTPUT_BUFSIZE_SMALL)) {
  2277. StringCopyA (ttime,"** Error retrieving time. **");
  2278. }
  2279. //
  2280. // Now, get the current tick count.
  2281. //
  2282. currentTickCount = GetTickCount();
  2283. //
  2284. // If this is the first call save the tick count.
  2285. //
  2286. if (!g_FirstTickCount) {
  2287. g_FirstTickCount = currentTickCount;
  2288. g_LastTickCount = currentTickCount;
  2289. }
  2290. //
  2291. // Now, build the passed in string.
  2292. //
  2293. va_start (args, Format);
  2294. appendPos = msg + _vsnprintf (msg, OUTPUT_BUFSIZE_LARGE, Format, args);
  2295. va_end (args);
  2296. //
  2297. // Append the time statistics to the end of the string.
  2298. //
  2299. end = msg + OUTPUT_BUFSIZE_LARGE;
  2300. _snprintf(
  2301. appendPos,
  2302. ((UBINT)end - (UBINT)appendPos) / (DWSIZEOF (CHAR)),
  2303. "\nCurrent Date and Time: %s %s\n"
  2304. "Milliseconds since last DEBUGLOGTIME call : %u\n"
  2305. "Milliseconds since first DEBUGLOGTIME call: %u\n",
  2306. date,
  2307. ttime,
  2308. currentTickCount - g_LastTickCount,
  2309. currentTickCount - g_FirstTickCount
  2310. );
  2311. g_LastTickCount = currentTickCount;
  2312. //
  2313. // Now, pass the results onto debugoutput.
  2314. //
  2315. LogA (DBG_TIME, "%s", msg);
  2316. }
  2317. VOID
  2318. _cdecl
  2319. DebugLogTimeW (
  2320. IN PCSTR Format,
  2321. ...
  2322. )
  2323. {
  2324. WCHAR msgW[OUTPUT_BUFSIZE_LARGE];
  2325. WCHAR dateW[OUTPUT_BUFSIZE_SMALL];
  2326. WCHAR timeW[OUTPUT_BUFSIZE_SMALL];
  2327. PCWSTR formatW;
  2328. PWSTR appendPosW, endW;
  2329. DWORD currentTickCount;
  2330. va_list args;
  2331. if (!g_LogInit) {
  2332. return;
  2333. }
  2334. if (!g_DoLog) {
  2335. return;
  2336. }
  2337. //
  2338. // first, get the current date and time into the string.
  2339. //
  2340. if (!GetDateFormatW (
  2341. LOCALE_SYSTEM_DEFAULT,
  2342. LOCALE_NOUSEROVERRIDE,
  2343. NULL,
  2344. NULL,
  2345. dateW,
  2346. OUTPUT_BUFSIZE_SMALL)) {
  2347. StringCopyW (dateW, L"** Error retrieving date. **");
  2348. }
  2349. if (!GetTimeFormatW (
  2350. LOCALE_SYSTEM_DEFAULT,
  2351. LOCALE_NOUSEROVERRIDE,
  2352. NULL,
  2353. NULL,
  2354. timeW,
  2355. OUTPUT_BUFSIZE_SMALL)) {
  2356. StringCopyW (timeW, L"** Error retrieving time. **");
  2357. }
  2358. //
  2359. // Now, get the current tick count.
  2360. //
  2361. currentTickCount = GetTickCount();
  2362. //
  2363. // If this is the first call save the tick count.
  2364. //
  2365. if (!g_FirstTickCount) {
  2366. g_FirstTickCount = currentTickCount;
  2367. g_LastTickCount = currentTickCount;
  2368. }
  2369. //
  2370. // Now, build the passed in string.
  2371. //
  2372. va_start (args, Format);
  2373. formatW = ConvertAtoW (Format);
  2374. appendPosW = msgW + _vsnwprintf (msgW, OUTPUT_BUFSIZE_LARGE, formatW, args);
  2375. FreeConvertedStr (formatW);
  2376. va_end (args);
  2377. //
  2378. // Append the time statistics to the end of the string.
  2379. //
  2380. endW = msgW + OUTPUT_BUFSIZE_LARGE;
  2381. _snwprintf(
  2382. appendPosW,
  2383. ((UBINT)endW - (UBINT)appendPosW) / (DWSIZEOF (WCHAR)),
  2384. L"\nCurrent Date and Time: %s %s\n"
  2385. L"Milliseconds since last DEBUGLOGTIME call : %u\n"
  2386. L"Milliseconds since first DEBUGLOGTIME call: %u\n",
  2387. dateW,
  2388. timeW,
  2389. currentTickCount - g_LastTickCount,
  2390. currentTickCount - g_FirstTickCount
  2391. );
  2392. g_LastTickCount = currentTickCount;
  2393. //
  2394. // Now, pass the results onto debugoutput.
  2395. //
  2396. LogW (DBG_TIME, "%s", msgW);
  2397. }
  2398. #endif // DEBUG
  2399. VOID
  2400. InitializeLog (
  2401. VOID
  2402. )
  2403. {
  2404. g_LogMutex = CreateMutex (NULL, FALSE, TEXT("cobra_log_mutex"));
  2405. UtInitialize (NULL);
  2406. pInitialize ();
  2407. }
  2408. EXPORT
  2409. BOOL
  2410. WINAPI
  2411. DllMain (
  2412. IN HINSTANCE hInstance,
  2413. IN DWORD dwReason,
  2414. IN LPVOID lpReserved
  2415. )
  2416. {
  2417. if (dwReason == DLL_PROCESS_ATTACH) {
  2418. g_hInst = hInstance;
  2419. g_LibHandle = hInstance;
  2420. InitializeLog ();
  2421. PRIVATE_ASSERT (g_LogMutex != NULL);
  2422. }
  2423. return TRUE;
  2424. }
  2425. VOID
  2426. LogDeleteOnNextInit(
  2427. VOID
  2428. )
  2429. {
  2430. g_ResetLog = TRUE;
  2431. }
  2432. #ifdef DEBUG
  2433. VOID
  2434. LogCopyDebugInfPathA(
  2435. OUT PSTR MaxPathBuffer
  2436. )
  2437. {
  2438. StringCopyByteCountA (MaxPathBuffer, g_DebugInfPathBufA, MAX_PATH);
  2439. }
  2440. VOID
  2441. LogCopyDebugInfPathW(
  2442. OUT PWSTR MaxPathBuffer
  2443. )
  2444. {
  2445. KnownSizeAtoW (MaxPathBuffer, g_DebugInfPathBufA);
  2446. }
  2447. #endif