[fpc-pascal] redirecting stdout

Bernd prof7bit at googlemail.com
Sun Jul 18 19:21:46 CEST 2010


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;


and then I can use it like this:

procedure TForm1.FormCreate(Sender : TObject);
begin
  redirect;
  writeln('form created');
end;

procedure TForm1.Button1Click(Sender : TObject);
begin
  writeln('button clicked');
end;


This might seem trivial but my Pascal times have been 20 years ago
with TurboPascal and I have to re-learn many things that I have
forgotten.

http://imagebin.org/105806  with GDB enabled
http://imagebin.org/105807  without debugger, output sent to DebugView

Now I think I have spent ten times more time with trying to figure out
how to redirect writeln to OutputDebugString()  than I could ever save
by using it ;-)



More information about the fpc-pascal mailing list