Leaked source code of windows server 2003
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.

173 lines
5.2 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1992, Microsoft Corporation
  4. //
  5. // File: log.c
  6. //
  7. // Contents: Module to log messages from the driver to the NT event logging
  8. // system.
  9. //
  10. // Classes:
  11. //
  12. // Functions: LogWriteMessage()
  13. //
  14. // History: 3/30/93 Milans created
  15. // 04/18/93 SudK modified to use a MessageFile. and some
  16. // cleanup to the function below.
  17. //
  18. //-----------------------------------------------------------------------------
  19. #include "dfsprocs.h"
  20. #define Dbg DEBUG_TRACE_EVENTLOG
  21. VOID LogpPutString(
  22. IN PUNICODE_STRING pustrString,
  23. IN OUT PCHAR *ppStringBuffer,
  24. IN OUT UCHAR *pcbBuffer);
  25. #ifdef ALLOC_PRAGMA
  26. #pragma alloc_text( PAGE, LogWriteMessage )
  27. #pragma alloc_text( PAGE, LogpPutString )
  28. #endif // ALLOC_PRAGMA
  29. //+----------------------------------------------------------------------------
  30. //
  31. // Function: LogWriteMessage
  32. //
  33. // Synopsis: Logs a message to the NT event logging system.
  34. //
  35. // Arguments: [UniqueErrCode] -- The code that identifes the message.
  36. // [NtStatusCode] -- Status code from some error.
  37. // [nStrings] -- Number of strings being passed in.
  38. // [pustrArg] -- The Array of insertion strings.
  39. //
  40. // Returns: Nothing at all.
  41. //
  42. // History: 04/18/93 SudK Created.
  43. //
  44. //-----------------------------------------------------------------------------
  45. VOID LogWriteMessage(
  46. IN ULONG UniqueErrorCode,
  47. IN NTSTATUS NtStatusCode,
  48. IN ULONG nStrings,
  49. IN PUNICODE_STRING pustrArg OPTIONAL)
  50. {
  51. PIO_ERROR_LOG_PACKET pErrorLog;
  52. UCHAR cbSize;
  53. UCHAR *pStringBuffer;
  54. ULONG i;
  55. //
  56. // Compute the size of the Error Log Packet that we need to start with.
  57. //
  58. cbSize = sizeof(IO_ERROR_LOG_PACKET);
  59. for (i = 0; i < nStrings; i++) {
  60. cbSize = (UCHAR)( cbSize + pustrArg[i].Length + sizeof(WCHAR));
  61. }
  62. if (cbSize > ERROR_LOG_MAXIMUM_SIZE) {
  63. cbSize = ERROR_LOG_MAXIMUM_SIZE;
  64. }
  65. pErrorLog = (PIO_ERROR_LOG_PACKET) IoAllocateErrorLogEntry(
  66. DfsData.DriverObject,
  67. cbSize);
  68. if (!pErrorLog) {
  69. //
  70. // Well, I guess we won't be logging this one.
  71. //
  72. return;
  73. }
  74. //
  75. // Zero out all fields, then set the ones we want.
  76. //
  77. RtlZeroMemory((PVOID) pErrorLog, sizeof(IO_ERROR_LOG_PACKET));
  78. pErrorLog->FinalStatus = NtStatusCode;
  79. pErrorLog->ErrorCode = UniqueErrorCode;
  80. pErrorLog->NumberOfStrings = (USHORT) nStrings;
  81. pErrorLog->StringOffset = sizeof(IO_ERROR_LOG_PACKET);
  82. pStringBuffer = ((PCHAR) pErrorLog) + sizeof(IO_ERROR_LOG_PACKET);
  83. //
  84. // Copy the strings into the buffer, making sure we truncate if and when
  85. // we need to.
  86. //
  87. cbSize -= sizeof(IO_ERROR_LOG_PACKET);
  88. for (i = 0; i < nStrings; i++) {
  89. LogpPutString(&pustrArg[i], &pStringBuffer, &cbSize);
  90. }
  91. //
  92. // And finally, write out the log
  93. //
  94. IoWriteErrorLogEntry(pErrorLog);
  95. }
  96. //+----------------------------------------------------------------------------
  97. //
  98. // Function: LogpPutString
  99. //
  100. // Synopsis: Copies a string into the buffer part of an IO_ERROR_LOG_PACKET.
  101. // Takes care of truncating if the whole string won't fit.
  102. //
  103. // Arguments: [pustrString] -- Pointer to unicode string to copy.
  104. // [ppStringBuffer] -- On input, pointer to beginning of buffer
  105. // to copy to. On exit, will point one past the
  106. // end of the copied string.
  107. // [pcbBuffer] -- On input, max size of buffer. On output,
  108. // remaining size after string has been copied.
  109. //
  110. // Returns: Nothing
  111. //
  112. // History: 04/18/93 SudK Created.
  113. //
  114. //-----------------------------------------------------------------------------
  115. VOID LogpPutString(
  116. IN PUNICODE_STRING pustrString,
  117. IN OUT PCHAR *ppStringBuffer,
  118. IN OUT UCHAR *pcbBuffer)
  119. {
  120. ULONG len;
  121. PWCHAR pwch;
  122. if ((*pcbBuffer == 0) || (pustrString->Length == 0)) {
  123. return;
  124. }
  125. if ( *pcbBuffer >= (pustrString->Length + sizeof(WCHAR)) ) {
  126. RtlMoveMemory(*ppStringBuffer, pustrString->Buffer, pustrString->Length);
  127. (*pcbBuffer) -= pustrString->Length;
  128. (*ppStringBuffer) += pustrString->Length;
  129. } else {
  130. RtlMoveMemory(*ppStringBuffer, pustrString->Buffer, (*pcbBuffer)-sizeof(WCHAR));
  131. *pcbBuffer = sizeof(WCHAR);
  132. (*ppStringBuffer) += (*pcbBuffer - sizeof(WCHAR));
  133. }
  134. //
  135. // Null Terminate the String Now if necessary.
  136. //
  137. if (*((PWCHAR) *ppStringBuffer - 1) != L'\0') {
  138. *((PWCHAR) *ppStringBuffer) = L'\0';
  139. *ppStringBuffer += sizeof(WCHAR);
  140. (*pcbBuffer) -= sizeof(WCHAR);
  141. }
  142. }