Solved

Log file

Posted on 2001-06-11
1
165 Views
Last Modified: 2010-05-18
I have application base on paradox with multi threads.
I wish to make log file (Ascii).

what is the best way to open / write an ascii file ?

I prefer that the methods will threads safe ...

10
Asi

0
Comment
Question by:asi
1 Comment
 
LVL 1

Accepted Solution

by:
edsteele earned 50 total points
ID: 6177446
Unless you are using an object with built in locking, you will probably need to use a TCriticalSection object.


...

var
  MyLogLock: TCriticalSection

...

  MyLogLock := TCriticalSection.Create;

...

MyLogLock.Acquire; { lock out other threads }

try
  { Do your log functions here! }
finally
  MyLogLock.Release;
end;

My suggestion for opening and writing to the ASCII file would be to use a TStringStream.  Allow the threads to write to an intermediate location (a TStringList, maybe) and have the main application thread be in charge of periodic log updates.  CAUTION: Remember to acquire a lock from MyLogLock before doing this so the threads don't try to update the TStringList while the main thread is reading from it.

The code for using TStringStream in this case is very easy:

procedure UpdateLogFile;
var
  MyStream: TStringStream;
  i: Integer;
begin
  MyStream := TStringStream.Create('C:\Logs\MyApp.log');
  try
    MyStream.Seek(0, soFromEnd);

    MyLogLock.Acquire; { lock out other threads }

    try
      for i := 0 to LogList.Count - 1 do
        MyStream.WriteString(LogList[i]);
      LogList.Clear;
    finally
      MyLogLock.Release;
    end;
  finally
    MyStream.Free;
  end;
end;

Good Luck!
Eric
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now