LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-14-2012, 04:16 AM   #1
JamesBaker
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Rep: Reputation: Disabled
Can a process redirect stdout and still use ncurses in a terminal?


Hi all.

Hopefully I can explain this OK, but if not I am happy to try to expand my problem.

I am using ncurses to display my program output in a terminal. I would also like to redirect stdout and stderr to my own logging functions so that any third-party software errors etc. are picked up and recorded/displayed as part of my logs.

However, ncurses appears to use stdout to display its windows in the terminal so when I redirect stdout, the ncurses output gets redirected to my logging output too. It then also obviously stops displaying the ncurses window in the terminal.

So, can (and if so how?), the stdout be redirected and yet ncurses still be used to display in the terminal?
 
Old 06-14-2012, 06:17 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you need to NOT write to stdout... maybe you can chuck output to syslog, or open a log file directly?
 
Old 06-14-2012, 08:37 AM   #3
JamesBaker
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Er, I knew my description would be not as good as it could be.

My aim was to catch all the printf and similar output from third-party code I have no control over, from stdout and redirect it to my own logger. This logger can then record and update the ncurses display with its own formatted output. Except the ncurses displays using stdout, so any re-direction caused ncurses to be logged. And no re-direction caused printf's to corrupt the display.

Thanks for answering though, because on the plus side, as always happens when you post and ask for help, writing the post seems to clarify things in your mind and after days of work and searching a solution presents itself...

For anyone else that needs this:

1. Instead of using initscr(), use newterm() with the default terminal file desciptor to start ncurses:

FILE *fd = fopen("/dev/tty", "r+");
SCREEN *scr = newterm(NULL, fd, fd);

This opens a new ncurses terminal bypassing stdin and stdout (I think). stdout can then by redirected wherever:

int newfd;
dup2(newfd, 1);

I then use setvbuf(stdout, NULL, _IONBF, 0) to make stdout unbuffered. This doesn't seem to automatically pass printf's with no "\n" through, so in an ncurses update function, I call fflush(stdout) every pass to punt through the messages. And hey presto I seem to be getting there!

Don't forget delscreen(scr) when you're done with ncurses.

Cheers
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
redirect AND print stdout samel_tvom Programming 12 10-07-2012 05:37 PM
[SOLVED] redirect system console to a virtual terminal or a file but after boot process agambier Linux - Kernel 1 03-08-2011 03:24 AM
How to redirect the stdin and stdout of child process which exec interactive prog? john.daker Programming 1 11-04-2008 08:27 AM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 07:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration