Reputation: 485
I've written a python program that acts as a server (tcp, kindof) and logs a ton of vital info on stdout (and file). The program runs on an ARM board with a small VGA dispay attached to it.
Now I want to display some information on the physical display of the board, while maintaining the logging on the tty where the program was launched.
Let us say I connect to the ARM board via SSH, and run the program on /dev/tty3.
I've managed to use ncurses to display things on /dev/tty1 (which is the physical display). Problem is, all the logging goes also to the pyhsical display.
To achieve this I've basically redirected stodut to /dev/tty1 with some piece of code found on SO:
NCURSES_TTY = '/dev/tty1'
with open(NCURSES_TTY, 'rb') as inf, open(NCURSES_TTY, 'wb') as outf:
os.dup2(inf.fileno(), 0)
os.dup2(outf.fileno(), 1)
os.dup2(outf.fileno(), 2)
os.environ['TERM'] = 'linux'
logging.debug("Starting server..")
But this, of course, redirects all output. Any idea of how to split ncurses and plain python logging to separate TTYs?
Upvotes: 3
Views: 1048
Reputation: 216
The answer from @zontar seems a bit overcomplicated. Just log to a file
import logging
logging.basicConfig(filename='log.log', level=logging.DEBUG)
and in another tty:
tail -F log.log
Upvotes: 1
Reputation: 485
Ok, solved.. it was pretty simple..
1) detect current tty
import os
import sys
tty=os.ttyname(sys.stdout.fileno())
2) set up python loggin to log to detected tty
import logging
logger = logging.getLogger(__package__)
logger.setLevel(log_level)
# console_handler = logging.StreamHandler()
console_handler = logging.FileHandler(tty)
console_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
3) trick stdout and err to go to desired tty
NCURSES_TTY = '/dev/tty1'
with open(NCURSES_TTY, 'rb') as inf, open(NCURSES_TTY, 'wb') as outf:
os.dup2(inf.fileno(), 0)
os.dup2(outf.fileno(), 1)
os.dup2(outf.fileno(), 2)
os.environ['TERM'] = 'linux'
4) import ncurses and do logging
import curses
stdscr = curses.initscr()
curses.noecho()
logging.debug("Tryout")
stdscr.addstr(0, 0, "Current mode: Paola mode",
curses.A_REVERSE)
stdscr.refresh()
That's it. The power of Unix (tested on OSX)..
Upvotes: 3