Solved

Log file

Posted on 2001-06-11
1
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…

730 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