Forum > General

Any way to intercept stdout/stderr?

(1/2) > >>

rishav:
Hi

I want to intercept all the run time errors Pascal code may have, so that I can reformat some of them.
I don't want to wrap all Pascal code in try catches, instead I was wondering if I can provide a custom stdout/stderr interface to it, so that i can update the strigs on the fly.

Would anyone know how to go about this?

dbannon:
rishav, I suspect most (all?) of the Pascal errors you are thinking about will terminate your application unless you handle them with a try statement.

But, maybe you are seeing some OS errors ?  The fact you know about stdout and stderr indicates you may be a linux user. GTK2, in particular generates lots of, mostly, insignificant errors. Usually distribution/desktop dependent. Damm nuisance but not much can be done about them.

Davo

howardpc:
One possibility may be to do a search for "textfile device driver" on this or a Delphi forum, which miight allow you to redirect output text data for further processing or analysis.

BobDog:

Windows.
This should redirect sdtdout/stderr to the console.(function console)
Everything else in the code is for a demo.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- uses sysutils; function  system(s:pchar):integer ; cdecl external 'msvcrt.dll' name 'system'; function filelength(filename:ansistring):longword;Var F : File Of byte;var L:longword;beginAssign (F,filename);  Reset (F);  L:=FileSize(F);  Close (F);  exit(L);end;   function loadfile(filename:ansistring):ansistring;   Var Fin : File;   Var x:longint;   var content:ansistring='';   begin   x:=filelength(filename);   setlength(content,x);   Assign (Fin,filename);   Reset (Fin,x);   BlockRead (Fin,content[1],1);   close(fin);   exit(content);end;  procedure savefile(s:ansistring ;filename:ansistring);    var    fout:file;    begin    Assign(fout,filename);    Rewrite(fout,length(s));    blockwrite(fout,s[1],1);    close(fout);  end;  function console(cmd:ansiString):ansistring;  var _file:string; e:integer; txt:ansistring=''; begin _file:=getcurrentdir+'/'+'testcode';    e:=system(pchar(cmd+' '+' >>'+_file+' 2>&1'));     if (e=-1) then exit('error');    txt:=loadfile(_file);    if fileexists(_File) then deletefile( _File);    exit( txt);End;   varcode:ansistring;pas:ansistring;   beginpas:=getcurrentdir+'\heloo.pas'; code:='var p:pinteger;'+chr(10);code:=code +'begin'+chr(10);code:=code+'writeln(p^);'+chr(10);code:=code+'readln;'+chr(10);code:=code+'end.';  savefile(code,pas);writeln(loadfile(pas));writeln(fileexists(pas));writeln;writeln; writeln(console('fpc -vwnhi '+ pas));writeln(console(getcurrentdir+'\heloo.exe'));//deletefile(pas);writeln('Press return to end . . .');readln;end.  please delete the files created by this manually.

rishav:
Thanks everyone. I was trying to see if I can do better error reporting/formatting than what is currently possible in Free Pascal. I was hoping that it would be easier to intercept error messages than putting everything in a large try catch. :(

Navigation

[0] Message Index

[#] Next page

Go to full version