Author: mjmartin
Date: Wed May 20 17:07:19 2009
New Revision: 41018
URL:
http://svn.reactos.org/svn/reactos?rev=41018&view=rev
Log:
- eventlog.h: Add CurrentRecord to EventSource struct as each EventLog handle must keep
track of current record.
- LogfInitializeNew/LogfInitializeExisting: Set LogFile MaxSize to just under 5k and add a
FIXME to read value from registry. TODO.
- LogfReadEvent: Change return value to DWORD so that errors can be passed back and last
error can be set from caller(advapi32).
Change RecordNumber parameter to pointer so that it can be returned and saved in rpc's
handle for the EventLog.
RecordNumber of 0 can only be used with flag EVENTLOG_SEQUENTIAL READ.
Set BytesRead and BytesNeeded to 0 as caller expects those values for any failure.
- LogfWriteData: Implement record overwriting when the LogFile has reached MaxSize.
- Implement LogfGetCurrentRecord.
- Code not active yet, will be with upcoming changes to base\services\eventlog\rpc.c.
Modified:
trunk/reactos/base/services/eventlog/eventlog.h
trunk/reactos/base/services/eventlog/file.c
Modified: trunk/reactos/base/services/eventlog/eventlog.h
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/services/eventlog/eve…
==============================================================================
--- trunk/reactos/base/services/eventlog/eventlog.h [iso-8859-1] (original)
+++ trunk/reactos/base/services/eventlog/eventlog.h [iso-8859-1] Wed May 20 17:07:19 2009
@@ -94,6 +94,7 @@
typedef struct
{
PLOGFILE LogFile;
+ ULONG CurrentRecord;
WCHAR *Name;
} EVENTSOURCE, *PEVENTSOURCE;
@@ -114,9 +115,9 @@
VOID LogfListRemoveItem(PLOGFILE Item);
-BOOL LogfReadEvent(PLOGFILE LogFile,
+DWORD LogfReadEvent(PLOGFILE LogFile,
DWORD Flags,
- DWORD RecordNumber,
+ DWORD * RecordNumber,
DWORD BufSize,
PBYTE Buffer,
DWORD * BytesRead,
@@ -138,6 +139,8 @@
BOOL LogfInitializeExisting(PLOGFILE LogFile);
DWORD LogfGetOldestRecord(PLOGFILE LogFile);
+
+DWORD LogfGetCurrentRecord(PLOGFILE LogFile);
ULONG LogfOffsetByNumber(PLOGFILE LogFile,
DWORD RecordNumber);
Modified: trunk/reactos/base/services/eventlog/file.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/base/services/eventlog/fil…
==============================================================================
--- trunk/reactos/base/services/eventlog/file.c [iso-8859-1] (original)
+++ trunk/reactos/base/services/eventlog/file.c [iso-8859-1] Wed May 20 17:07:19 2009
@@ -33,7 +33,9 @@
LogFile->Header.MajorVersion = MAJORVER;
LogFile->Header.MinorVersion = MINORVER;
LogFile->Header.CurrentRecordNumber = 1;
-
+ /* FIXME: Read MaxSize from registry for this LogFile.
+ But for now limit EventLog size to just under 5K. */
+ LogFile->Header.MaxSize = 5000;
LogFile->Header.Signature = LOGFILE_SIGNATURE;
if (!WriteFile(LogFile->hFile,
&LogFile->Header,
@@ -227,6 +229,10 @@
LogFile->Header.CurrentRecordNumber = dwRecordsNumber + 1;
LogFile->Header.OldestRecordNumber = dwRecordsNumber ? 1 : 0; // FIXME
+ /* FIXME: Read MaxSize from registry for this LogFile.
+ But for now limit EventLog size to just under 5K. */
+ LogFile->Header.MaxSize = 5000;
+
if (!SetFilePointer(LogFile->hFile, 0, NULL, FILE_CURRENT) ==
INVALID_SET_FILE_POINTER)
{
@@ -509,9 +515,9 @@
LeaveCriticalSection(&LogFileListCs);
}
-BOOL LogfReadEvent(PLOGFILE LogFile,
+DWORD LogfReadEvent(PLOGFILE LogFile,
DWORD Flags,
- DWORD RecordNumber,
+ DWORD * RecordNumber,
DWORD BufSize,
PBYTE Buffer,
DWORD * BytesRead,
@@ -521,46 +527,51 @@
DWORD dwBufferUsage = 0, dwRecNum;
if (Flags & EVENTLOG_FORWARDS_READ && Flags &
EVENTLOG_BACKWARDS_READ)
- return FALSE;
+ return ERROR_INVALID_PARAMETER;
if (!(Flags & EVENTLOG_FORWARDS_READ) && !(Flags &
EVENTLOG_BACKWARDS_READ))
- return FALSE;
+ return ERROR_INVALID_PARAMETER;
if (!Buffer || !BytesRead || !BytesNeeded)
- return FALSE;
-
- dwRecNum = RecordNumber;
+ return ERROR_INVALID_PARAMETER;
+
+ if ((*RecordNumber==0) && !(EVENTLOG_SEQUENTIAL_READ))
+ {
+ return ERROR_INVALID_PARAMETER;
+ }
+
+ dwRecNum = *RecordNumber;
EnterCriticalSection(&LogFile->cs);
+
+ *BytesRead = 0;
+ *BytesNeeded = 0;
+
dwOffset = LogfOffsetByNumber(LogFile, dwRecNum);
if (!dwOffset)
{
LeaveCriticalSection(&LogFile->cs);
- return FALSE;
+ return ERROR_HANDLE_EOF;
}
if (SetFilePointer(LogFile->hFile, dwOffset, NULL, FILE_BEGIN) ==
INVALID_SET_FILE_POINTER)
{
- DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
+ DPRINT1("SetFilePointer() failed!\n");
+ goto Done;
+ }
+
+ if (!ReadFile(LogFile->hFile, &dwRecSize, sizeof(DWORD), &dwRead, NULL))
+ {
+ DPRINT1("ReadFile() failed!\n");
+ goto Done;
+ }
+
+ if (dwRecSize > BufSize)
+ {
+ *BytesNeeded = dwRecSize;
LeaveCriticalSection(&LogFile->cs);
- return FALSE;
- }
-
- if (!ReadFile(LogFile->hFile, &dwRecSize, sizeof(DWORD), &dwRead, NULL))
- {
- DPRINT1("ReadFile() failed! %d\n", GetLastError());
- LeaveCriticalSection(&LogFile->cs);
- return FALSE;
- }
-
- if (dwRecSize > BufSize)
- {
- *BytesRead = 0;
- *BytesNeeded = dwRecSize;
- SetLastError(ERROR_INSUFFICIENT_BUFFER);
- LeaveCriticalSection(&LogFile->cs);
- return FALSE;
+ return ERROR_INSUFFICIENT_BUFFER;
}
if (SetFilePointer(LogFile->hFile,
@@ -568,21 +579,19 @@
NULL,
FILE_CURRENT) == INVALID_SET_FILE_POINTER)
{
- DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
- LeaveCriticalSection(&LogFile->cs);
- return FALSE;
+ DPRINT1("SetFilePointer() failed!\n");
+ goto Done;
}
if (!ReadFile(LogFile->hFile, Buffer, dwRecSize, &dwRead, NULL))
{
- DPRINT1("ReadFile() failed! %d\n", GetLastError());
- LeaveCriticalSection(&LogFile->cs);
- return FALSE;
+ DPRINT1("ReadFile() failed!\n");
+ goto Done;
}
dwBufferUsage += dwRead;
- while (dwBufferUsage < BufSize)
+ while (dwBufferUsage <= BufSize)
{
if (Flags & EVENTLOG_FORWARDS_READ)
dwRecNum++;
@@ -596,9 +605,8 @@
if (SetFilePointer(LogFile->hFile, dwOffset, NULL, FILE_BEGIN) ==
INVALID_SET_FILE_POINTER)
{
- DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
- LeaveCriticalSection(&LogFile->cs);
- return FALSE;
+ DPRINT1("SetFilePointer() failed!\n");
+ goto Done;
}
if (!ReadFile(LogFile->hFile,
@@ -607,9 +615,8 @@
&dwRead,
NULL))
{
- DPRINT1("ReadFile() failed! %d\n", GetLastError());
- LeaveCriticalSection(&LogFile->cs);
- return FALSE;
+ DPRINT1("ReadFile() failed!\n");
+ goto Done;
}
if (dwBufferUsage + dwRecSize > BufSize)
@@ -620,9 +627,8 @@
NULL,
FILE_CURRENT) == INVALID_SET_FILE_POINTER)
{
- DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
- LeaveCriticalSection(&LogFile->cs);
- return FALSE;
+ DPRINT1("SetFilePointer() failed!\n");
+ goto Done;
}
if (!ReadFile(LogFile->hFile,
@@ -631,25 +637,33 @@
&dwRead,
NULL))
{
- DPRINT1("ReadFile() failed! %d\n", GetLastError());
- LeaveCriticalSection(&LogFile->cs);
- return FALSE;
+ DPRINT1("ReadFile() failed!\n");
+ goto Done;
}
dwBufferUsage += dwRead;
}
*BytesRead = dwBufferUsage;
+ * RecordNumber = dwRecNum;
LeaveCriticalSection(&LogFile->cs);
- return TRUE;
+ return ERROR_SUCCESS;
+
+Done:
+ DPRINT1("LogfReadEvent failed with %x\n",GetLastError());
+ LeaveCriticalSection(&LogFile->cs);
+ return GetLastError();
}
BOOL LogfWriteData(PLOGFILE LogFile, DWORD BufSize, PBYTE Buffer)
{
DWORD dwWritten;
+ DWORD dwRead;
SYSTEMTIME st;
EVENTLOGEOF EofRec;
-
+ PEVENTLOGRECORD RecBuf;
+ LARGE_INTEGER logFileSize;
+ ULONG RecOffSet;
if (!Buffer)
return FALSE;
@@ -657,6 +671,72 @@
SystemTimeToEventTime(&st, &((PEVENTLOGRECORD) Buffer)->TimeWritten);
EnterCriticalSection(&LogFile->cs);
+
+ if (!GetFileSizeEx(LogFile->hFile, &logFileSize))
+ {
+ return FALSE;
+ }
+
+ /* If the size of the file is over MaxSize */
+ if ((logFileSize.QuadPart + BufSize)> LogFile->Header.MaxSize)
+ {
+ RecBuf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(EVENTLOGRECORD));
+ /* Determine how many records need to be overwritten */
+ while (TRUE)
+ {
+ DPRINT("EventLogFile has reached maximume size\n");
+
+ if (!RecBuf)
+ {
+ DPRINT1("Failed to allocate buffer for OldestRecord!\n");
+ HeapFree(GetProcessHeap(), 0, RecBuf);
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ /* Get the oldest record data */
+ RecOffSet = LogfOffsetByNumber(LogFile,
LogFile->Header.OldestRecordNumber);
+ if (SetFilePointer(LogFile->hFile,
+ RecOffSet,
+ NULL,
+ FILE_BEGIN) == INVALID_SET_FILE_POINTER)
+ {
+ DPRINT1("SetFilePointer() failed! %d\n", GetLastError());
+ HeapFree(GetProcessHeap(), 0, RecBuf);
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ if (!ReadFile(LogFile->hFile, RecBuf, sizeof(EVENTLOGRECORD), &dwRead,
NULL))
+ {
+ DPRINT1("ReadFile() failed!\n");
+ HeapFree(GetProcessHeap(), 0, RecBuf);
+ LeaveCriticalSection(&LogFile->cs);
+ return FALSE;
+ }
+
+ if (RecBuf->Reserved != LOGFILE_SIGNATURE)
+ {
+ DPRINT1("LogFile corrupt!\n");
+ return FALSE;
+ }
+
+ LogFile->Header.OldestRecordNumber++;
+
+ /* Check the size of the record as the record adding may be larger */
+ if (RecBuf->Length >= BufSize)
+ {
+ DPRINT1("Record will fit\n");
+ LogFile->Header.StartOffset = LogfOffsetByNumber(LogFile,
LogFile->Header.OldestRecordNumber);
+ break;
+ }
+ else
+ {
+ DPRINT1("Record wont fit\n");
+ }
+ }
+ HeapFree(GetProcessHeap(), 0, RecBuf);
+ }
if (SetFilePointer(LogFile->hFile,
LogFile->Header.EndOffset,
@@ -758,6 +838,11 @@
DWORD LogfGetOldestRecord(PLOGFILE LogFile)
{
return LogFile->Header.OldestRecordNumber;
+}
+
+DWORD LogfGetCurrentRecord(PLOGFILE LogFile)
+{
+ return LogFile->Header.CurrentRecordNumber;
}
BOOL LogfAddOffsetInformation(PLOGFILE LogFile, ULONG ulNumber, ULONG ulOffset)