sinoTrinity
sinoTrinity

Reputation: 1195

output to a new terminal

Suppose I create a thread from my "main" thread, which aims to monitor and print some variables in "main". But "main" is also generating some output, so I want to print the outputs of these two threads separately. How can I redirect the output of the new thread to a new console other than the one "main" thread is using in my c program?

Upvotes: 3

Views: 1702

Answers (3)

nategoose
nategoose

Reputation: 12382

I would go with writing the data to a log file and then using tail -f log_file.txt from another window to watch it since that way you get to keep a copy of the data, but if you decide that you need to use separate terminals then you can just open the new terminal (tty) like a regular file. The main problem with this is that you probably really want it to open a new terminal window for you as well.

Opening a new terminal window to do something like this is tricky and is different between different terminal emulators. Here is another question about opening new terminals from a make file. You could try doing the same thing from your program or from a script that runs your program and run cat or tail in the new terminal to be your log window.

#!/bin/sh
truncate  --size=0 ./logfile.txt
xterm "tail -f ./logfile.txt" 2>&1 > /dev/null &
your_program --log-file=./logfile.txt

I don't currently know of a better way to accomplish this.

Another thing you might want to look into is syslog.

Upvotes: 2

RichieHindle
RichieHindle

Reputation: 281375

I would output the monitoring information to a file, then use tail -f filename to display it in my other terminal.

That has the advantage that you can run grep or what-have-you on the output as well.

Upvotes: 2

Šimon Tóth
Šimon Tóth

Reputation: 36433

What would you expect to happen in such case (externally)?

If you run a program you are attached to a console. You of course don't have to write output to the console (you can use files), but the console cannot be duplicated obviously.

Isn't using a different file descriptor good enough for you?

Upvotes: 0

Related Questions