2

I'm trying to create a simple dashboard for my app and want to display logs in curses window.

Also, using solution from this question cause same problem.

import curses
import logging
import coloredlogs
import os

os.environ['COLOREDLOGS_LOG_FORMAT'] ='%(asctime)s.%(msecs)03d %(name)s - %(message)s'
os.environ['COLOREDLOGS_DATE_FORMAT'] ='%H%M%S'

logger = logging.getLogger("Test")

coloredlogs.install(level='DEBUG')

class CursesHandler(logging.Handler):
    def __init__(self, win, level=logging.DEBUG):
        logging.Handler.__init__(self, level)
        self.win = win

    def emit(self, record):
        self.win.addstr(record.getMessage())
        self.win.refresh()

def main(scrn):

    handler = CursesHandler(scrn.subwin(5,80, 19,0))
    logger.addHandler(handler)

    win = scrn.subwin(10,80, 0,0)

    for i in range(0,10):
        curses.delay_output(250)
        logger.info("Now i = %s", i)
    win.getch()


if __name__ == "__main__":
    curses.wrapper(main)

This code results in this: enter image description here

Moreover, if I have another panels, logs just overwrites them.

stsdc
  • 406
  • 5
  • 14
  • Possible duplicate of [How to manage logging in curses](https://stackoverflow.com/questions/27774093/how-to-manage-logging-in-curses) – Thomas Dickey Sep 25 '17 at 23:15
  • Offhand, it seems your `__init__` call is the place to start. – Thomas Dickey Sep 25 '17 at 23:16
  • @ThomasDickey, I tried this already and result is same. Log shows up like in screenshot and overwrites curses layout. – stsdc Sep 26 '17 at 10:55
  • Can You explain what may cause the problem, because I have no experience in curses and logging handling? – stsdc Sep 26 '17 at 11:14
  • 1
    The apparent problem is that your handler is actually not capturing the logger output; that's going to the standard output which is staircasing because curses has set the terminal to raw mode (and turning `onlcr` off, for instance: "man stty" for **that**). – Thomas Dickey Sep 26 '17 at 20:01

0 Answers0