Reimar Grabowski wrote 8 lines
On Fri, 4 May 2018 13:34:54 +0200
"Carlos E. R. via Lazarus" wrote:
> Yes, of course, that's what I'm doing now, but I wanted to do it
> internally, control the file name, perhaps rotate it, etc :-)
Perhaps you should use a logger.
There are surely some FPC ones out there that can do all this for you.
A fast google search brings up
http://wiki.freepascal.org/Log4Delphi which is based on Log4J (which does all that).
Well, I send to syslog the important things :-)
In this phase I use many writeln as debug help, and using a pipe and a tee does the job fine.
The idea here:
<http://lists.freepascal.org/fpc-pascal/2010-July/026163.html> redirects all stdout to a file, but I'm thinking that it is basically the same as replacing all writeln found with writeln(F, ...), achieving the same thing perhaps simpler. Well, except that commenting out a the call to "redirect" disables it.
The basic trick it does is:
var
f : TextFile;
s: TDebugStream;
and a procedure to redirect the output:
procedure redirect;
begin
s := TDebugStream.Create();
AssignStream(f, s);
Rewrite(f);
output := f;
end;
wich replaces stdout (output) with f. The role of tTDebugStream I don't get. It is created thus:
Type
TDebugStream = class(TStream)
function Write(const Buffer; Count : Longint) : Longint; override;
end;
implementation
function TDebugStream.Write(const Buffer; Count : Longint) : Longint;
var
msg : ansistring;
begin
result := count;
SetLength(msg, count);
move(buffer, PChar(msg)[0], count);
OutputDebugString(PChar(TrimRight(msg)));
end;
I also miss where the file name is assigned.
But anyway, what I wanted is to write both to the screen and to a file, so this is not sufficient.
Further in the thread they say that this would do - this is the classic method I had forgotten:
var
oldoutput, f: TextFile;
begin
AssignFile(f, 'somefile');
Rewrite(f);
oldoutput := Output;
Output := f;
Writeln('Hello World'); // this is send to Output
Output := oldoutput;
CloseFile(f);
end.
At the start of the thread there is a suggestion to "implementing your own textfile driver", but the link is dead.