Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Log file

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

636 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