2006-08-11 15:21:11 +02:00
|
|
|
import sys
|
2006-08-11 13:05:33 +02:00
|
|
|
import logging
|
2006-08-11 15:21:11 +02:00
|
|
|
import traceback
|
|
|
|
from cStringIO import StringIO
|
2006-08-11 13:05:33 +02:00
|
|
|
|
|
|
|
import dbus
|
|
|
|
import gobject
|
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
__sugar_shell = None
|
|
|
|
__console_id = None
|
|
|
|
|
2006-08-11 13:05:33 +02:00
|
|
|
class Handler(logging.Handler):
|
|
|
|
def __init__(self, shell, console_id):
|
|
|
|
logging.Handler.__init__(self)
|
|
|
|
|
|
|
|
self._console_id = console_id
|
|
|
|
self._shell = shell
|
|
|
|
self._messages = []
|
|
|
|
|
|
|
|
def _log(self):
|
|
|
|
for message in self._messages:
|
|
|
|
self._shell.log(self._console_id, message)
|
2006-08-11 15:21:11 +02:00
|
|
|
self._messages = []
|
2006-08-11 13:05:33 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
self._messages.append(record.msg)
|
|
|
|
if len(self._messages) == 1:
|
|
|
|
gobject.idle_add(self._log)
|
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
def __exception_handler(typ, exc, tb):
|
|
|
|
trace = StringIO()
|
|
|
|
traceback.print_exception(typ, exc, tb, None, trace)
|
|
|
|
|
|
|
|
__sugar_shell.log(__console_id, trace.getvalue())
|
|
|
|
|
2006-08-11 13:05:33 +02:00
|
|
|
def start(console_id, shell = None):
|
|
|
|
root_logger = logging.getLogger('')
|
|
|
|
root_logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
if shell == None:
|
|
|
|
bus = dbus.SessionBus()
|
|
|
|
proxy_obj = bus.get_object('com.redhat.Sugar.Shell', '/com/redhat/Sugar/Shell')
|
|
|
|
shell = dbus.Interface(proxy_obj, 'com.redhat.Sugar.Shell')
|
|
|
|
|
|
|
|
root_logger.addHandler(Handler(shell, console_id))
|
2006-08-11 15:21:11 +02:00
|
|
|
|
|
|
|
global __sugar_shell
|
|
|
|
global __console_id
|
|
|
|
|
|
|
|
__sugar_shell = shell
|
|
|
|
__console_id = console_id
|
|
|
|
sys.excepthook = __exception_handler
|