Reputation: 199
I make a very simple log with tstringlist. I write it to file:
pLog.SaveToFile(FileName);
Somewhere there is a bug, and my computer is shut-down. After, I cannot find my log file. Probably the file is saved in asych mode. There is a way for waiting for the write before to go on with the execution?
Thanks, Alberto
Upvotes: 1
Views: 2222
Reputation: 6477
If the savetofile call is at the program end, and the program terminates abnormally, then this call probably won't execute. I use a logging mechanism which for every log call opens the log file, writes the log text and time, flushes and then closes the log file. This way, the log text is guaranteed to be written to a file, even if the program terminates abnormally.
Here's the code:
procedure TMainForm.Log (const s: string);
var
f: textfile;
begin
assignfile (f, logfilename);
{$I-} // yes, I know: modern programming style requires a try/except block
append (f);
if ioresult <> 0 then rewrite (f);
{$I+}
writeln (f, datetostr (now), ' ', timetostr (now), ' ', s);
flush (f);
closefile (f);
end;
Upvotes: 9
Reputation: 612794
TStringList.SaveToFile
operates synchronously.
The most obvious causes of the problem are:
TStringList.SaveToFile
.Upvotes: 3