Can a process redirect stdout and still use ncurses in a terminal?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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?
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.