[Lazarus] Cloning standard output to text file

Carlos E. R. robin.listas at telefonica.net
Fri May 4 20:09:00 CEST 2018


On 2018-05-04 18:46, Reimar Grabowski via Lazarus wrote:
> On Fri, 4 May 2018 13:34:54 +0200
> "Carlos E. R. via Lazarus" <lazarus at lists.lazarus-ide.org> 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.



-- 
Cheers / Saludos,

		Carlos E. R.
		(from 42.3 x86_64 "Malachite" at Telcontar)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: OpenPGP digital signature
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20180504/088c00f0/attachment.sig>


More information about the Lazarus mailing list