1 #include "Log.h" 2 #include <stdarg.h> 3 #include <stdio.h> 4 #pragma hdrstop 5 6 BOOLEAN LogMessage(PCHAR szFormat, ...) 7 { 8 ULONG Length; 9 char messagebuf[256]; 10 va_list va; 11 IO_STATUS_BLOCK IoStatus; 12 OBJECT_ATTRIBUTES objectAttributes; 13 NTSTATUS status; 14 HANDLE FileHandle; 15 UNICODE_STRING fileName; 16 17 18 //format the string 19 va_start(va,szFormat); 20 vsprintf(messagebuf,szFormat,va); 21 va_end(va); 22 23 //get a handle to the log file object 24 fileName.Buffer = NULL; 25 fileName.Length = 0; 26 fileName.MaximumLength = sizeof(DEFAULT_LOG_FILE_NAME) + sizeof(UNICODE_NULL); 27 fileName.Buffer = ExAllocatePool(PagedPool, 28 fileName.MaximumLength); 29 if (!fileName.Buffer) 30 { 31 return FALSE; 32 } 33 RtlZeroMemory(fileName.Buffer, fileName.MaximumLength); 34 status = RtlAppendUnicodeToString(&fileName, (PWSTR)DEFAULT_LOG_FILE_NAME); 35 36 //DbgPrint("\n Initializing Object attributes"); 37 38 InitializeObjectAttributes (&objectAttributes, 39 (PUNICODE_STRING)&fileName, 40 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, 41 NULL, 42 NULL ); 43 44 DbgPrint("\n BusLogic - Creating the file"); 45 46 status = ZwCreateFile(&FileHandle, 47 FILE_APPEND_DATA | SYNCHRONIZE, 48 &objectAttributes, 49 &IoStatus, 50 0, 51 FILE_ATTRIBUTE_NORMAL, 52 FILE_SHARE_WRITE, 53 FILE_OPEN_IF, 54 FILE_SYNCHRONOUS_IO_NONALERT, 55 NULL, 56 0 ); 57 58 if(NT_SUCCESS(status)) 59 { 60 CHAR buf[300]; 61 LARGE_INTEGER time; 62 KeQuerySystemTime(&time); 63 64 DbgPrint("\n BusLogic - Created the file"); 65 66 //put a time stamp on the output message 67 sprintf(buf,"%10u-%10u %s",time.HighPart,time.LowPart,messagebuf); 68 69 //format the string to make sure it appends a newline carriage-return to the 70 //end of the string. 71 Length=strlen(buf); 72 if(buf[Length-1]=='\n') 73 { 74 buf[Length-1]='\r'; 75 strcat(buf,"\n"); 76 Length++; 77 } 78 else 79 { 80 strcat(buf,"\r\n"); 81 Length+=2; 82 } 83 84 buf[Length+1] = '\0'; 85 DbgPrint("\n BusLogic - Writing to the file"); 86 DbgPrint("\n BusLogic - Buf = %s", buf); 87 88 status = ZwWriteFile(FileHandle, 89 NULL, 90 NULL, 91 NULL, 92 &IoStatus, 93 buf, 94 Length, 95 NULL, 96 NULL ); 97 98 ZwClose(FileHandle); 99 } 100 if (fileName.Buffer) 101 ExFreePool (fileName.Buffer); 102 103 return STATUS_SUCCESS; 104 } 105