?
Solved

Log file

Posted on 2001-06-11
1
Medium Priority
?
172 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 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 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