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.

138 lines
4.7 KiB

  1. /*++
  2. Copyright(c) 1998,99 Microsoft Corporation
  3. Module Name:
  4. log.c
  5. Abstract:
  6. Windows Load Balancing Service (WLBS)
  7. Driver - event logging support
  8. Author:
  9. kyrilf
  10. --*/
  11. #include <ntddk.h>
  12. #include "log.h"
  13. #include "univ.h"
  14. /* PROCEDURES */
  15. BOOLEAN Log_event (
  16. NTSTATUS code,
  17. PWSTR msg1,
  18. PWSTR msg2,
  19. PWSTR msg3,
  20. PWSTR msg4,
  21. ULONG loc,
  22. ULONG d1,
  23. ULONG d2,
  24. ULONG d3,
  25. ULONG d4)
  26. {
  27. PIO_ERROR_LOG_PACKET ErrorLogEntry;
  28. UNICODE_STRING ErrorStr[4];
  29. PWCHAR InsertStr;
  30. ULONG EntrySize;
  31. ULONG BytesLeft;
  32. ULONG i;
  33. RtlInitUnicodeString(&ErrorStr[0], msg1);
  34. RtlInitUnicodeString(&ErrorStr[1], msg2);
  35. RtlInitUnicodeString(&ErrorStr[2], msg3);
  36. RtlInitUnicodeString(&ErrorStr[3], msg4);
  37. /* Remember the insertion string should be NUL terminated. So we allocate the
  38. extra space of WCHAR. The first parameter to IoAllocateErrorLogEntry can
  39. be either the driver object or the device object. If it is given a device
  40. object, the name of the device (used in IoCreateDevice) will show up in the
  41. place of %1 in the message. See the message file (.mc) for more details. */
  42. EntrySize = sizeof(IO_ERROR_LOG_PACKET) + LOG_NUMBER_DUMP_DATA_ENTRIES * sizeof (ULONG) +
  43. ErrorStr[0].Length + ErrorStr[1].Length + ErrorStr[2].Length + ErrorStr[3].Length + 4 * sizeof(WCHAR);
  44. /* Truncate the size if necessary. */
  45. if (EntrySize > ERROR_LOG_MAXIMUM_SIZE) {
  46. if (ERROR_LOG_MAXIMUM_SIZE > (sizeof(IO_ERROR_LOG_PACKET) + LOG_NUMBER_DUMP_DATA_ENTRIES * sizeof (ULONG))) {
  47. UNIV_PRINT (("Log_event: log entry size too large, truncating: size=%u, max=%u", EntrySize, ERROR_LOG_MAXIMUM_SIZE));
  48. EntrySize = ERROR_LOG_MAXIMUM_SIZE;
  49. } else {
  50. UNIV_PRINT (("Log_event: log entry size too large, exiting: size=%u, max=%u", EntrySize, ERROR_LOG_MAXIMUM_SIZE));
  51. return FALSE;
  52. }
  53. }
  54. ErrorLogEntry = IoAllocateErrorLogEntry(univ_driver_ptr, (UCHAR)(EntrySize));
  55. if (ErrorLogEntry == NULL) {
  56. #if DBG
  57. /* Convert Unicode string to AnsiCode; %ls can only be used in PASSIVE_LEVEL. */
  58. CHAR AnsiString[64];
  59. for (i = 0; (i < sizeof(AnsiString) - 1) && (i < ErrorStr[0].Length); i++)
  60. AnsiString[i] = (CHAR)msg1[i];
  61. AnsiString[i] = '\0';
  62. UNIV_PRINT (("Log_event: error allocating log entry %s", AnsiString));
  63. #endif
  64. return FALSE;
  65. }
  66. ErrorLogEntry -> ErrorCode = code;
  67. ErrorLogEntry -> SequenceNumber = 0;
  68. ErrorLogEntry -> MajorFunctionCode = 0;
  69. ErrorLogEntry -> RetryCount = 0;
  70. ErrorLogEntry -> UniqueErrorValue = 0;
  71. ErrorLogEntry -> FinalStatus = STATUS_SUCCESS;
  72. ErrorLogEntry -> DumpDataSize = (LOG_NUMBER_DUMP_DATA_ENTRIES + 1) * sizeof (ULONG);
  73. ErrorLogEntry -> StringOffset = sizeof (IO_ERROR_LOG_PACKET) + LOG_NUMBER_DUMP_DATA_ENTRIES * sizeof (ULONG);
  74. ErrorLogEntry -> NumberOfStrings = 0;
  75. /* load the NUMBER_DUMP_DATA_ENTRIES plus location id here */
  76. ErrorLogEntry -> DumpData [0] = loc;
  77. ErrorLogEntry -> DumpData [1] = d1;
  78. ErrorLogEntry -> DumpData [2] = d2;
  79. ErrorLogEntry -> DumpData [3] = d3;
  80. ErrorLogEntry -> DumpData [4] = d4;
  81. /* Calculate the number of bytes available in the string storage area. */
  82. BytesLeft = EntrySize - ErrorLogEntry->StringOffset;
  83. /* Set a pointer to the beginning of the string storage area. */
  84. InsertStr = (PWCHAR)((PCHAR)ErrorLogEntry + ErrorLogEntry->StringOffset);
  85. /* Loop through all strings and put in as many as we can. */
  86. for (i = 0; i < 4, BytesLeft > 0; i++) {
  87. /* Find out how much of this string we can fit into the buffer - save room for the NUL character. */
  88. ULONG Length = (ErrorStr[i].Length <= (BytesLeft - sizeof(WCHAR))) ? ErrorStr[i].Length : BytesLeft - sizeof(WCHAR);
  89. /* Copy the number of characters that will fit. */
  90. RtlMoveMemory(InsertStr, ErrorStr[i].Buffer, Length);
  91. /* Put the NUL character at the end. */
  92. *(PWCHAR)((PCHAR)InsertStr + Length) = L'\0';
  93. /* Increment the number of strings successfully fit in the buffer. */
  94. ErrorLogEntry->NumberOfStrings++;
  95. /* Move the string pointer past the string. */
  96. InsertStr = (PWCHAR)((PCHAR)InsertStr + Length + sizeof(WCHAR));
  97. /* Calculate the number of bytes left now. */
  98. BytesLeft -= (Length + sizeof(WCHAR));
  99. }
  100. IoWriteErrorLogEntry(ErrorLogEntry);
  101. return TRUE;
  102. }