exilon / QuickLogger

Powerful and flexible library for logging on files, console, memory, email, rest, eventlog, syslog, slack, telegram, redis, logstash, elasticsearch, influxdb, graylog, Sentry, Twilio, ide debug messages and throw events for Delphi/Firemonkey/freepascal/.NET (Windows/Linux/OSX/IOS/Android).
Apache License 2.0
381 stars 84 forks source link

Setting GlobalLogFileProvider.FileName after enabled causes AV error later #70

Closed nbuyer closed 2 years ago

nbuyer commented 2 years ago

Hi, I tried to use this logger the very first time and encountered some minor issues. It is very easy to avoid but I just want to report what happened. Thank you.

Delphi 11.1 FMX.MultiLogger project

procedure TfrmMain.FormCreate(Sender: TObject); begin GlobalLogFileProvider.LogLevel := LOG_ALL; GlobalLogFileProvider.Enabled := True; // 1. BTW, can not do this: GlobalLogFileProvider.FormatSettings.ShortDateFormat := 'yyyy-mm-dd hh:nn:ss'; // 2. Setting GlobalLogFileProvider.FileName after enabled causes AV error later GlobalLogFileProvider.FileName := '.\abc.log'; GlobalLogFileProvider.IncludedInfo := GlobalLogFileProvider.IncludedInfo + [iiThreadId]; ...

exilon commented 2 years ago

// 1. BTW, can not do this: GlobalLogFileProvider.FormatSettings.ShortDateFormat := 'yyyy-mm-dd hh:nn:ss'; Do you mean date format output in your file not corresponds to your selected format?

// 2. Setting GlobalLogFileProvider.FileName after enabled causes AV error later Filename should be setted before enable file provider, but i will revise to adapt to this change.

nbuyer commented 2 years ago

// 1. BTW, can not do this: GlobalLogFileProvider.FormatSettings.ShortDateFormat := 'yyyy-mm-dd hh:nn:ss'; Do you mean date format output in your file not corresponds to your selected format?

No, just a compiler error: [dcc32 Error] E2064 Left side cannot be assigned to

exilon commented 2 years ago

ok, FormatSettings is a record. In delphi property of record type is readonly. This is because you can read (return the entire record) or write (assign the entire record). If you want to change FormatSettings you need to assign a record to the property:

var
  fsettings : TFormatSettings;
begin
   fsettings.ShortDateFormat := 'yyyy-mm-dd hh:nn:ss';
   GlobalLogFileProvider.FormatSettings := fsettings;
end;
exilon commented 2 years ago

// 2. Setting GlobalLogFileProvider.FileName after enabled causes AV error later

I've tried and i can change filename after enabled and new log entries was written to new file. Can you provide more info?

nbuyer commented 2 years ago

Thanks! New a VCL form and put a button1 on it.

procedure TForm11.Button1Click(Sender: TObject);
begin
  GlobalLogFileProvider.LogLevel := LOG_ALL;
  GlobalLogFileProvider.IncludedInfo := GlobalLogFileProvider.IncludedInfo + [iiThreadId];
  GlobalLogFileProvider.FileName := '.\abc.log';
  GlobalLogFileProvider.Enabled := True;
  GlobalLogFileProvider.FileName := '.\def.log'; // Remove this line to stop AV
  Logger.Providers.Add(GlobalLogFileProvider);
end;

Click the button1 and will throw this exception:


Debugger Exception Notification

Project Project11.exe raised exception class $C0000005 with message 'access violation at 0x01066e1e: read of address 0x00700041'.

Break Continue Copy Help

Quick.Threads.pas line 940

function TThreadedQueueList<T>.PopItem(var AQueueSize: Integer; var AItem: T): TWaitResult;
begin
  AItem := Default(T);
  fQueueLock.Enter;   // <-- Exception break on this line
exilon commented 2 years ago

What delphi version are you using?

exilon commented 2 years ago

Please, change line 372 in Quick.Logger.Provider.Files:

if IsEnabled then Restart;

with:

if IsEnabled then Init

nbuyer commented 2 years ago

Please, change line 372 in Quick.Logger.Provider.Files:

if IsEnabled then Restart;

with:

if IsEnabled then Init

Problem solved! Thank you.