Pergunta

I want to redirect all console output to my own GUI console, including all calls to C write functions.

Things I've tried:

  • Creating a new stream class, but stdio.stdout is a file and you can't assign a stream to it
  • Creating a new file class altogether, but you can't extend a struct

Do I need to get deeper into the C bindings for that? Any help much appreciated.

Foi útil?

Solução 2

I chose another path and wrote my own writeln function. Since apparently none of the libraries I use does terminal output directly it seems less of a hassle than pipes or files.

Outras dicas

The typical solution to this problem is explained in many places. Here is an old article that explains how to do it on Windows: http://edn.embarcadero.com/article/10387

It works the same way on any UNIX as well.

In short:

  1. create two pipes, one for child process stdin, and second for child process stdout.
  2. create process, and attach these two pipes to its stdin/out
  3. now from your GUI you just read from the pipe that is attacked to child process stdout, and you write to the pipe that is attacked to child process stdin.

Second approach, which is easier, is to simply use the popen() function whenever you run some child process. This standard function does more/less all the above.

An easy, but really hacky way to do it, is to use C's freopen() to redirect stdout to a temporary file, then read the file in for your console.

You might be able to redirect it on the operating system level into a pipe, then have your console listen on the other end of the pipe. That'd be cleaner, but a lot more of a pain.

Either way though, I don't think the D library will help much - you'll want to use some kind of C library solution.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top