From 00ee46007294fe39a5d2114849cc7ea682fd3db1 Mon Sep 17 00:00:00 2001 From: Michael Justin Date: Sat, 20 Jun 2026 14:46:36 +0200 Subject: [PATCH 1/2] Refactor log file creation in SetLogFile procedure Refactored log file creation to use TFileStream directly, improving file handling. --- src/main/Log4D.pas | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/Log4D.pas b/src/main/Log4D.pas index 478e71b..5ff658a 100644 --- a/src/main/Log4D.pas +++ b/src/main/Log4D.pas @@ -3042,7 +3042,6 @@ constructor TLogFileAppender.Create(const Name, FileName: string; procedure TLogFileAppender.SetLogFile(const Name: string); var strPath: string; - f : TextFile; begin CloseLogFile; FFileName := Name; @@ -3057,24 +3056,19 @@ procedure TLogFileAppender.SetLogFile(const Name: string); begin // Check if directory exists strPath := ExtractFileDir(FFileName); - if (strPath <> '') and not DirectoryExists(strPath) then + if (strPath <> '') and not DirectoryExists(strPath) then ForceDirectories(strPath); //FIX 04.10.2006 MHoenemann: // SysUtils.FileCreate() ignores any sharing option (like our fmShareDenyWrite), - // Creating new file - AssignFile(f, FFileName); - try - ReWrite(f); - finally - CloseFile(f); - end; - // now use this file - FStream := TFileStream.Create(FFileName, fmOpenReadWrite or fmShareDenyNone); + // Creating new file directly via TFileStream with fmCreate, which truncates/creates + // the file and gives us the stream with the desired share mode in one step. + FStream := TFileStream.Create(FFileName, fmCreate or fmShareDenyNone); end; WriteHeader; end; + { close file stream } procedure TLogFileAppender.CloseLogFile; begin From b1a1f71844016e46bd337176ec3176bb814a2c61 Mon Sep 17 00:00:00 2001 From: Michael Justin Date: Sat, 20 Jun 2026 14:49:58 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Michael Justin --- src/main/Log4D.pas | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/Log4D.pas b/src/main/Log4D.pas index 5ff658a..a69abae 100644 --- a/src/main/Log4D.pas +++ b/src/main/Log4D.pas @@ -3068,7 +3068,6 @@ procedure TLogFileAppender.SetLogFile(const Name: string); WriteHeader; end; - { close file stream } procedure TLogFileAppender.CloseLogFile; begin