Urwid: how to see errors?

1k Views Asked by At

I am building application with interactive console interface (line htop, atop utilities) using urwid library, so my trouble is: as interface takes all the space in console window - I could not see python's errors, I tried to do that:

import sys
f = open("test_err", "w")
original_stderr = sys.stderr
sys.stderr = f

print a #a is undefined

sys.stderr = original_stderr
f.close() 

It works when I dont use urwid, but not when I use it...

2

There are 2 best solutions below

0
On

you could try redirecting errors to a file. after each time you run the program, you will need to refresh the file. most editors let you easily do this by pushing f5

def main():
    #your code here
    print someError #raises an error

try: #run main function
    main()
except BaseException as err: #catch all errors
    with open('errors.txt','a') as errors: #open a file to write the errors to
        errors.write(err.message+'\n')#write the error

change the 'a' to 'w' in the open function if you only want to see one error in the file at a time (instead of having multiple error over a long period of time in one file).

if you want to see the error right when it happens, you can make the error catcher open a window that has the error on it.

def main():
    #your code here
    print someErr

try: #run main function
   main()
except BaseException as err: #catch all errors
    import Tkinter as tk #imports the ui module

    root = tk.Tk() #creates the root of the window

    #creates the text and attaches it to the root
    window = tk.Label(root, text=err.message)
    window.pack()

    #runs the window
    root.mainloop()

if you want to build your own window to catch errors, you can learn about Tkinter here. (it is built into python, you don't have to install anything)

0
On

Here's what I came up with. I'm taking advantage of unicode-rxvt (urxvt) feature to be passed in a file descriptor. Of course this means you need to be developing this in an X environment, and not a console.

from __future__ import print_function

import os
from datetime import datetime

_debugfile = None

def _close_debug(fo):
    fo.close()

def DEBUG(*obj):
    """Open a terminal emulator and write messages to it for debugging."""
    global _debugfile
    if _debugfile is None:
        import atexit
        masterfd, slavefd = os.openpty()
        pid = os.fork()
        if pid: # parent
            os.close(masterfd)
            _debugfile = os.fdopen(slavefd, "w+", 0)
            atexit.register(_close_debug, _debugfile)
        else: # child
            os.close(slavefd)
            os.execlp("urxvt", "urxvt", "-pty-fd", str(masterfd))
    print(datetime.now(), ":", ", ".join(map(repr, obj)), file=_debugfile)

This will open a new terminal window automatically when you call DEBUG for the first time and close it at exit. Then any messages passed to it are shown in this new window. This is your "debug window". So your main app works normally, without cluttering it up with messages, but you can still see debug output in this new terminal.