[fpc-devel] StdOut capture for FPC RTL

michael.vancanneyt at wisa.be michael.vancanneyt at wisa.be
Thu Nov 25 09:03:05 CET 2010

On Thu, 25 Nov 2010, Thaddy wrote:

> On 24-11-2010 21:32, Michael Van Canneyt wrote:
>> On Wed, 24 Nov 2010, Anton Kavalenka wrote: Get stdout handle (duplicate it 
>> under linux),
>>> create pipe,
>>> replace the stdout (keeping the old stdout) for current process with write 
>>> handle of pipe,
>>> There are following problems with FPC
>>> Under Windows: I have to call rewrite(output) for every thread which wants 
>>> to use  new (captured) stdout
>>> So the questions:
>>> How to force all the threads of process and all DLLs write into same 
>>> captured stdout?
>> There is no way that I know of to avoid the rewrite(), since the file 
>> descriptor records for Input, Output and StdErr are threadvars. And the 
>> threadvars must be initialized on thread start.
>>> How to revert stdout back (stop capturing)?
>> Close and reopen using the standard filedescriptors ?
>> Michael.
> You can write a "classic" pascal textfile device driver and assign  directly 
> to the global TEXT variables Input, Output and ErrOutput.
> Don't know is this is threadsafe, though.

No, it will not be thread-safe. You'll have to do it over and over again for
each thread.

> I have some code by Peter Below on archive for that, that also works on FPC 
> win. The implementation part of that unit can be adapted for nixen.
> the file is called streamio.pas and can be easily found on the codegear 
> website, but I will be happy to send it to you if you want by email.

The same unit and implementation exists in FPC since years. You can assign 
any classical pascal textfile to an arbitrary TStream descendent.


More information about the fpc-devel mailing list