[fpc-pascal] redirecting stdout
Michael Van Canneyt
michael at freepascal.org
Sun Jul 18 19:52:37 CEST 2010
On Sun, 18 Jul 2010, Bernd wrote:
> OK, I got it working now. Just in case somebody else is googling for
> something like this:
>
> first I define a class TDebugStream that will later replace the standard output:
>
> 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;
>
>
> then in my application i have two global variables:
>
> 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;
I think that simply
AssignStream(Output,S);
Rewrite(Output);
Probably would work as well.
Michael.
More information about the fpc-pascal
mailing list