[DUG] Best way to make this thread safe
Trevor Jones
trevorj at ihug.co.nz
Thu May 17 23:15:49 NZST 2007
In what way is this thread safe?
-----Original Message-----
From: delphi-bounces at delphi.org.nz [mailto:delphi-bounces at delphi.org.nz] On
Behalf Of Maurice Butler
Sent: Thursday, 17 May 2007 10:48 p.m.
To: 'NZ Borland Developers Group - Delphi List'
Subject: RE: [DUG] Best way to make this thread safe
May be of use - the trick is at the bottom - Initialization & Finalization
Maurice
unit ErrorLog;
interface
function Logfile(comment:string):boolean;
implementation
uses sysutils,
ascii,
syncobjs {required for critical section},
Forms {required for access to application title};
var
ExclusiveUse : tCriticalSection;
TempLog :TextFile;
sFileName : string;
Procedure OpenLog;
Begin
sFileName := Application.ExeName + '.' +
formatdatetime('yyyymmdd',now)+ '.log';
AssignFile(TempLog, sFileName);
//open file if it exists else create
if FileExists(sFileName) then //adding to existing file
Append(TempLog)
else //no file exist so create
begin
rewrite(Templog);
Writeln(TempLog, 'Created for ',Application.ExeName, ' on ',
datetostr(date),' at ',timetostr(time));
writeln(TempLog,
'===========================================================================
===');
end;
End;
Function Logfile(comment:string):boolean;
begin
ExclusiveUse.Enter;
try
result := FALSE;
OpenLog;
try
//if string contains second line move the start clear of the date
if Pos(Ascii_LF, comment) <> 0 then
insert(StringOfChar(' ', 20), Comment, Pos(Ascii_LF, comment)+1);
writeln(TempLog, datetostr(date),' ',timetostr(time),', ', comment);
result := TRUE;
finally //so no loose ends are left
closeFile(TempLog);
end;
finally
ExclusiveUse.Leave;
end;
end;
initialization
begin
ExclusiveUse := TCriticalSection.Create;
end;
Finalization
begin
ExclusiveUse.Free;
end;
end.
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi at delphi.org.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
unsubscribe
More information about the Delphi
mailing list