open terminal for cout - linux

Started by
5 comments, last by user0 11 years, 4 months ago
I am trying to make an executable that will open a terminal for output along side a graphical window. I have already figured that by geting the terminal column count I can determin if a terminal is open or not but need to find a way to open a new terminal and pipe the cout to it.

Note:
The terminal will only open if options for debuging are selected while running so a bash script or reopening the program through a command wouldnt be ideal.

I am thinking of creating a new process of xterm or gnome-terminal and then piping the cout to it but I believe it would pipe commands instead of just output text so im not sure what to do.
Advertisement
What is the problem you are trying to solve? What kind of application is it? Is the terminal going to be used for input?

I believe you almost certainly should not be opening a terminal to do this. Write to standard out or standard error, or to a log file. The user can have a terminal to run your application or can start a terminal and use "tail -f" on the log file to view the output. This is going to be more flexible, as the user can use applications like "tee" to capture the standard streams, or can use "grep" on the output to highlight or suppress certain messages of interest.
It sounds like you're trying to get some kind of Microsoft Windows-style error console so you can analyse internal state of your graphical application for debugging purposes.

Rather than trying to figure out some complex way to bend your target platform into something it's not, you should embrace the native way of accomplishing your goals. In fact, the way the system was designed to be used and is widely used by many many other applications.

Send all you debug output to stderr. When invoking your program capture stderr and use the tee program to both save it to a file and display it in real time on your launching terminal. This is how it's done.

If you're launching your application from a GUI, you'll find all window managers also capture the stderr of launched programs and log it. Most X-based window managers, for example, will send stderr of GUI applications to the [font=courier new,courier,monospace]$HOME/.xsession-errors[/font] file.

You will gain productivity if you work with your tools rather than against them.

Stephen M. Webb
Professional Free Software Developer

@rip-off I dont need input, I can already do this on windows so portability isnt an issue and I want realtime output so a file isnt ideal. Im not sure why making the application open an output terminal is bad?

@bregma Thats it exactly I have it working in windows. I do send all my output to the terminal via cout anyways I just want it to open a terminal to be easier for end users so they would not have to open it in a terminal prior to launch.

The tool is already there and being used mainly gnome-terminal all I want to do is open it.
I have the application that launches via the menu or shortcut 90% of the time the users will not need the terminal output but some cases based on what they are doing it is nesaccary and I would just like to open the terminal and use the tools available rather than doing something like creating my own terminal or something like that.
If the data is being written to a file, you can use the "tail -f" command to see the data in "realtime". That is, aside from whatever buffering is going on. If your program regularly flushes output, then it will promptly appear in the terminal that is using tail.

Standard output and error are even simpler in this regard, the kind of advanced user will be able to use their terminal of choice to run your program.

I've never seen a program that tried to open a terminal on Linux. The reason is there are objectively simpler and better ways of doing this.
I guess the tail -f might work well enough just open a terminal reading a temp file that cout writes too. Im not a fan of that though being I would also like to know when it is closed and I want a genuine feel I dont want people to see a tail -f command.

If they already have a terminal open it simply wont open one I already do this by getting the column count of the terminal.

I dont know of any linux programs that open a terminal but I know there are plenty of windows ones.
What are the simpler methods, having a user open a terminal before the launch of a program? Isnt that a bit unfriendly to basic users?

I got xterm working the way I need it minus the close button

Im trying to figure out how to make it work with gnome-terminal ill worrie about kde some other day.
Here is the xterm code for some reason its not keeping the tabs though

//not the best way but it works for now
bool Console_Check(){
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
if( w.ws_row + w.ws_col > 5000 ){
return false;
}
if( w.ws_row < 0 || w.ws_col < 0 ){
return false;
}
stringstream hold;
hold << "already open " << w.ws_row << " " << w.ws_col;
System_Error( hold.str(), "Error", false);
return true;
}
void Console_Open(){
if( Console_Check() ){
System_Error( "already open", "Error", false);
return;
}
int pt = posix_openpt(O_RDWR);
if (pt == -1){
std::cerr << "Could not open pseudo terminal.\n";
System_Error( "Could not open pseudo terminal.", "Error", false);
return; //EXIT_FAILURE;
}
char* ptname = ptsname(pt);
if (!ptname){
std::cerr << "Could not get pseudo terminal device name.\n";
close(pt);
System_Error( "Could not get pseudo terminal device name.", "Error", false);
return; //EXIT_FAILURE;
}
if (unlockpt(pt) == -1){
std::cerr << "Could not get pseudo terminal device name.\n";
close(pt);
System_Error( "Could not get pseudo terminal device name.", "Error", false);
return; //EXIT_FAILURE;
}
std::ostringstream oss;
oss << "xterm -S" << (strrchr(ptname, '/')+1) << "/" << pt << " &";
//oss << "gnome-terminal -S" << (strrchr(ptname, '/')+1) << "/" << pt << " &";
system( oss.str().c_str() );
std::cout << "\n" << oss.str() << std::endl;
int xterm_fd = open(ptname,O_RDWR);
char c;
do read(xterm_fd, &c, 1); while (c!='\n');
if (dup2(pt, 1) <0){
std::cerr << "Could not redirect standard output.\n";
close(pt);
System_Error( "Could not redirect standard output.", "Error", false);
return;//EXIT_FAILURE;
}
if (dup2(pt, 2) <0){
std::cerr << "Could not redirect standard error output.\n";
close(pt);
System_Error( "Could not redirect standard error output.", "Error", false);
return; //EXIT_FAILURE;
}
}

This topic is closed to new replies.

Advertisement