#1 Re: mORMot 1 » How to close a TSynLog (File) » 2014-03-31 06:39:52

The example with the thread was just for trying. In my particular case I need more loggers within the same thread - but I must be able to close them. The thread was the easiest way to demonstrate this.

#2 Re: mORMot 1 » How to close a TSynLog (File) » 2014-03-28 22:03:00

Thank you for the change - this fixed the locked file problem.
But the instance of TSynLog is still alive and each run of my thread cost 70 kb of heap memory.
After same investigation I found a solution in calling SynLogFile.Remove(self) at the end of CloseLogFile

procedure TSynLog.CloseLogFile;
begin
  if fWriter=nil then
    exit;
  EnterCriticalSection(fThreadLock);
  try
    fWriter.Flush;
    FreeAndNil(fWriterStream);
    FreeAndNil(fWriter);
    SynLogFile.Remove(self);
  finally
    LeaveCriticalSection(fThreadLock);
  end;
end;

Maybe there is a better place for calling this function but this frees the TSynLock object and no memory is lost.

#3 mORMot 1 » How to close a TSynLog (File) » 2014-03-27 17:04:35

Telefonmann
Replies: 5

Hello,
I need same help with logging. I want to log same values inside a thread. Each thread should have it's own logfile. So I create a simple demo:

var
  TThreadLogger : TSynLogClass = TSynLog;

procedure TForm1.Button1Click(Sender: TObject);
var i : Integer;
begin
  with TThreadLogger.Family do begin
    Level := LOG_VERBOSE;
    PerThreadLog := ptOneFilePerThread;
    DestinationPath := 'Threads/';
  end;

  for I := 0 to 10 do
    TMyThread.Create;

end;

procedure TMyThread.Execute;
var
  log : ISynLog;
  i : Integer;
begin
  log := TThreadLogger.Enter(self);
  for i := 0 to 10 do begin
    Sleep(Random(1000));
    log.Log(sllInfo, 'Hello from Thread %',[i]);
  end;
end;

Every thread get his own logfile - but after the threads have finshed the logfile is still locked.
How can I close the logfile and free the instance of TSynLog without closing my program?

Oliver

Board footer

Powered by FluxBB