[ros-diffs] [tretiakov] 22737: Reduce code dublication

tretiakov at svn.reactos.org tretiakov at svn.reactos.org
Sat Jul 1 08:52:41 CEST 2006


Author: tretiakov
Date: Sat Jul  1 10:52:41 2006
New Revision: 22737

URL: http://svn.reactos.org/svn/reactos?rev=22737&view=rev
Log:
Reduce code dublication

Modified:
    trunk/reactos/base/services/eventlog/eventlog.c
    trunk/reactos/base/services/eventlog/eventlog.h
    trunk/reactos/base/services/eventlog/file.c
    trunk/reactos/base/services/eventlog/logport.c

Modified: trunk/reactos/base/services/eventlog/eventlog.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/services/eventlog/eventlog.c?rev=22737&r1=22736&r2=22737&view=diff
==============================================================================
--- trunk/reactos/base/services/eventlog/eventlog.c (original)
+++ trunk/reactos/base/services/eventlog/eventlog.c Sat Jul  1 10:52:41 2006
@@ -178,7 +178,8 @@
 INT main()
 {
 	WCHAR LogPath[MAX_PATH];
-	PLOGFILE pLogf;	
+	PLOGFILE pLogf;
+	INT RetCode = 0;
 	LONG result;
 	HKEY elogKey;
 
@@ -189,15 +190,8 @@
 	if(MyHeap==NULL)
 	{
 		DPRINT1("FATAL ERROR, can't create heap.\n");
-		
-		DeleteCriticalSection(&LogListCs);	
-		// Close all log files.
-		for(pLogf = LogListHead; pLogf; pLogf = ((PLOGFILE)pLogf)->Next)
-		{
-			LogfClose(pLogf);
-		}
-
-		return 1;
+		RetCode = 1;
+		goto bye_bye;
 	}
 	
 	GetWindowsDirectory(LogPath, MAX_PATH);
@@ -217,17 +211,8 @@
 		if(result != ERROR_SUCCESS)
 		{
 			DPRINT1("Fatal error: can't open eventlog registry key.\n");
-
-			DeleteCriticalSection(&LogListCs);
-	
-			// Close all log files.
-			for(pLogf = LogListHead; pLogf; pLogf = ((PLOGFILE)pLogf)->Next)
-			{
-			    LogfClose(pLogf);
-			}
-	 
-			HeapDestroy(MyHeap);
-			return 1;
+			RetCode = 1;
+			goto bye_bye;
 		}
 		
 		LoadLogFiles(elogKey);
@@ -235,16 +220,16 @@
 
     StartServiceCtrlDispatcher(ServiceTable);
 
+bye_bye:
 	DeleteCriticalSection(&LogListCs);
 	
 	// Close all log files.
 	for(pLogf = LogListHead; pLogf; pLogf = ((PLOGFILE)pLogf)->Next)
-	{
 		LogfClose(pLogf);
-	}
 	 
-	HeapDestroy(MyHeap);
-    return 0;
+	if(MyHeap) HeapDestroy(MyHeap);
+
+    return RetCode;
 }
 
 VOID EventTimeToSystemTime(DWORD EventTime, 

Modified: trunk/reactos/base/services/eventlog/eventlog.h
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/services/eventlog/eventlog.h?rev=22737&r1=22736&r2=22737&view=diff
==============================================================================
--- trunk/reactos/base/services/eventlog/eventlog.h (original)
+++ trunk/reactos/base/services/eventlog/eventlog.h Sat Jul  1 10:52:41 2006
@@ -140,7 +140,7 @@
 							  ULONG ulNumber,
 							  ULONG ulOffset);
 
-DWORD LogfBuildNewRecord(PBYTE Buffer, 
+PBYTE LogfAllocAndBuildNewRecord(LPDWORD lpRecSize,
 						 DWORD dwRecordNumber,
 						 WORD wType,
 						 WORD wCategory,
@@ -153,6 +153,8 @@
 						 WCHAR *lpStrings,
 						 DWORD dwDataSize,
 						 LPVOID lpRawData);
+
+inline void LogfFreeRecord(LPVOID Rec);
 
 /* eventlog.c */
 VOID PRINT_HEADER(PFILE_HEADER header);

Modified: trunk/reactos/base/services/eventlog/file.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/services/eventlog/file.c?rev=22737&r1=22736&r2=22737&view=diff
==============================================================================
--- trunk/reactos/base/services/eventlog/file.c (original)
+++ trunk/reactos/base/services/eventlog/file.c Sat Jul  1 10:52:41 2006
@@ -618,7 +618,7 @@
 	return TRUE;
 }
 
-DWORD LogfBuildNewRecord(PBYTE Buffer, 
+PBYTE LogfAllocAndBuildNewRecord(LPDWORD lpRecSize,
 						 DWORD dwRecordNumber,
 						 WORD wType,
 						 WORD wCategory,
@@ -637,6 +637,7 @@
 	SYSTEMTIME SysTime;
 	WCHAR *str;
 	UINT i, pos, nStrings;
+	PBYTE Buffer;
 
 	dwRecSize = sizeof(EVENTLOGRECORD) + (lstrlenW(ComputerName) + 
 	lstrlenW(SourceName) + 2)*sizeof(WCHAR);
@@ -654,12 +655,13 @@
 	if(dwRecSize % 4 != 0) dwRecSize += 4 - (dwRecSize % 4);
 	dwRecSize+=4;
 
+	Buffer = HeapAlloc(MyHeap, HEAP_ZERO_MEMORY, dwRecSize);
 	if(!Buffer)
 	{
-		return dwRecSize;
-	}
-
-	ZeroMemory(Buffer, dwRecSize);
+		DPRINT1("Can't allocate heap!\n");
+		return NULL;
+	}
+
 	pRec = (PEVENTLOGRECORD)Buffer;
 	pRec->Length = dwRecSize;
 	pRec->Reserved = LOGFILE_SIGNATURE;
@@ -711,6 +713,12 @@
 
 	if(pos % 4 != 0) pos += 4 - (pos % 4);
 	*((PDWORD)(Buffer+pos)) = dwRecSize;
-
-	return TRUE;
-}
+	
+	*lpRecSize = dwRecSize;
+	return Buffer;
+}
+
+inline void LogfFreeRecord(LPVOID Rec)
+{
+	HeapFree(MyHeap, 0, Rec);
+}

Modified: trunk/reactos/base/services/eventlog/logport.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/services/eventlog/logport.c?rev=22737&r1=22736&r2=22737&view=diff
==============================================================================
--- trunk/reactos/base/services/eventlog/logport.c (original)
+++ trunk/reactos/base/services/eventlog/logport.c Sat Jul  1 10:52:41 2006
@@ -120,7 +120,8 @@
     IO_ERROR_LPC Request;
     PIO_ERROR_LOG_MESSAGE Message;
 	PEVENTLOGRECORD pRec;
-    ULONG ulRecNum, ulRecSize ;
+    ULONG ulRecNum;
+    DWORD dwRecSize;
     NTSTATUS Status;
 	PLOGFILE SystemLog = NULL;
     
@@ -160,7 +161,7 @@
             Message = (PIO_ERROR_LOG_MESSAGE)&Request.Message;
 			ulRecNum = SystemLog ? SystemLog->Header.NextRecord : 0;
 
-			ulRecSize = LogfBuildNewRecord(NULL,
+			pRec = (PEVENTLOGRECORD)LogfAllocAndBuildNewRecord(&dwRecSize,
 				ulRecNum,
 				Message->Type,
 				Message->EntryData.EventCategory,
@@ -175,30 +176,13 @@
 				(LPVOID)(((PBYTE)Message)
 					+sizeof(IO_ERROR_LOG_PACKET)-sizeof(ULONG)));
 
-			DPRINT("ulRecSize = %d\n", ulRecSize);
-
-			pRec = HeapAlloc(MyHeap, 0, ulRecSize);
-
 			if(pRec == NULL)
 			{
-				DPRINT("Can't allocate heap!\n");
+				DPRINT("LogfAllocAndBuildNewRecord failed!\n");
 				return STATUS_NO_MEMORY;
 			}
-
-			LogfBuildNewRecord((PBYTE)pRec,
-				ulRecNum,
-				Message->Type,
-				Message->EntryData.EventCategory,
-				Message->EntryData.ErrorCode,
-				(WCHAR*)(((PBYTE)Message)+Message->DriverNameOffset),
-				L"MyComputer", /* FIXME */
-				0,
-				NULL,
-				Message->EntryData.NumberOfStrings,
-				(WCHAR*)(((PBYTE)Message)+Message->EntryData.StringOffset),
-				Message->EntryData.DumpDataSize,
-				(LPVOID)(((PBYTE)Message)
-					+sizeof(IO_ERROR_LOG_PACKET)-sizeof(ULONG)));
+			
+			DPRINT("dwRecSize = %d\n", dwRecSize);
 			
 			DPRINT("\n --- EVENTLOG RECORD ---\n");
 			PRINT_RECORD(pRec);
@@ -206,12 +190,12 @@
 			
 			if(!onLiveCD && SystemLog)
 			{
-				if(!LogfWriteData(SystemLog, ulRecSize, (PBYTE)pRec))
+				if(!LogfWriteData(SystemLog, dwRecSize, (PBYTE)pRec))
 					DPRINT("LogfWriteData failed!\n");
 				else DPRINT("Data written to Log!\n");
 			}
 
-			HeapFree(MyHeap, 0, pRec);
+			LogfFreeRecord(pRec);
         }
     }
     return Status;




More information about the Ros-diffs mailing list