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.

240 lines
3.6 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. logutil.c
  5. Abstract:
  6. Helper routines for logging data.
  7. Author:
  8. 03-Oct-1999 SamerA
  9. Revision History:
  10. --*/
  11. #include "w64logp.h"
  12. WOW64LOGAPI
  13. NTSTATUS
  14. Wow64LogMessageArgList(
  15. IN UINT_PTR Flags,
  16. IN PSZ Format,
  17. IN va_list ArgList)
  18. /*++
  19. Routine Description:
  20. Logs a message.
  21. Arguments:
  22. Flags - Determine the output log type
  23. Format - Formatting string
  24. ... - Variable argument
  25. Return Value:
  26. NTSTATUS
  27. --*/
  28. {
  29. int BytesWritten;
  30. CHAR Buffer[ MAX_LOG_BUFFER ];
  31. //
  32. // Check trace gate flag
  33. //
  34. if (!((Wow64LogFlags & ~(UINT_PTR)LF_CONSOLE) & Flags))
  35. {
  36. return STATUS_SUCCESS;
  37. }
  38. BytesWritten = _vsnprintf(Buffer,
  39. sizeof(Buffer) - 1,
  40. Format,
  41. ArgList);
  42. if (BytesWritten < 0)
  43. {
  44. return STATUS_BUFFER_TOO_SMALL;
  45. }
  46. //
  47. // Log the results
  48. //
  49. LogOut(Buffer, Wow64LogFlags);
  50. return STATUS_SUCCESS;
  51. }
  52. WOW64LOGAPI
  53. NTSTATUS
  54. Wow64LogMessage(
  55. IN UINT_PTR Flags,
  56. IN PSZ Format,
  57. IN ...)
  58. /*++
  59. Routine Description:
  60. Helper around logs a message that accepts a variable argument list
  61. Arguments:
  62. Flags - Determine the output log type
  63. Format - Formatting string
  64. ... - Variable argument
  65. Return Value:
  66. NTSTATUS
  67. --*/
  68. {
  69. NTSTATUS NtStatus;
  70. va_list ArgList;
  71. va_start(ArgList, Format);
  72. NtStatus = Wow64LogMessageArgList(Flags, Format, ArgList);
  73. va_end(ArgList);
  74. return NtStatus;
  75. }
  76. NTSTATUS
  77. LogFormat(
  78. IN OUT PLOGINFO LogInfo,
  79. IN PSZ Format,
  80. ...)
  81. /*++
  82. Routine Description:
  83. Formats a message
  84. Arguments:
  85. LogInfo - Logging Information (buffer + available bytes)
  86. pszFormat - Format string
  87. ... - Optional arguments
  88. Return Value:
  89. NTSTATUS - BufferSize is increment with the amount of bytes
  90. written if successful.
  91. --*/
  92. {
  93. va_list ArgList;
  94. NTSTATUS NtStatus = STATUS_SUCCESS;
  95. int BytesWritten;
  96. va_start(ArgList, Format);
  97. BytesWritten = _vsnprintf(LogInfo->OutputBuffer, LogInfo->BufferSize, Format, ArgList);
  98. va_end(ArgList);
  99. if (BytesWritten < 0)
  100. {
  101. NtStatus = STATUS_BUFFER_TOO_SMALL;
  102. }
  103. else
  104. {
  105. LogInfo->BufferSize -= BytesWritten;
  106. LogInfo->OutputBuffer += BytesWritten;
  107. }
  108. return NtStatus;
  109. }
  110. VOID
  111. LogOut(
  112. IN PSZ Text,
  113. UINT_PTR Flags
  114. )
  115. /*++
  116. Routine Description:
  117. Logs -outputs- a message
  118. Arguments:
  119. Text - Formatted string to log
  120. Flags - Control flags
  121. Return Value:
  122. None
  123. --*/
  124. {
  125. if ((Flags & LF_CONSOLE) != 0)
  126. {
  127. DbgPrint(Text);
  128. }
  129. //
  130. // Check if we need to send the output to a file
  131. //
  132. if (Wow64LogFileHandle != INVALID_HANDLE_VALUE)
  133. {
  134. LogWriteFile(Wow64LogFileHandle, Text);
  135. }
  136. }
  137. NTSTATUS
  138. LogWriteFile(
  139. IN HANDLE FileHandle,
  140. IN PSZ LogText)
  141. /*++
  142. Routine Description:
  143. Writes text to a file handle
  144. Arguments:
  145. FileHandle - Handle to a file object
  146. LogText - Text to log to file
  147. Return Value:
  148. NTSTATUS
  149. --*/
  150. {
  151. IO_STATUS_BLOCK IoStatus;
  152. NTSTATUS NtStatus;
  153. NtStatus = NtWriteFile(FileHandle,
  154. NULL, // event
  155. NULL, // apcroutine
  156. NULL, // apccontext
  157. &IoStatus,
  158. LogText,
  159. strlen(LogText),
  160. NULL, // ByteOffset
  161. NULL); // key
  162. return NtStatus;
  163. }