LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 05: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, 07:17 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
you need to NOT write to stdout... maybe you can chuck output to syslog, or open a log file directly?
 
Old 06-14-2012, 09: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 06:37 PM
[SOLVED] redirect system console to a virtual terminal or a file but after boot process agambier Linux - Kernel 1 03-08-2011 04:24 AM
How to redirect the stdin and stdout of child process which exec interactive prog? john.daker Programming 1 11-04-2008 09:27 AM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-04-2008 12:20 AM
redirect stdin/stdout of running process xtravar Linux - Software 3 03-06-2004 08:54 PM


All times are GMT -5. The time now is 10:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration