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-12 16:19:47 +02:00
|
|
|
__console = None
|
2006-08-11 15:21:11 +02:00
|
|
|
__console_id = None
|
|
|
|
|
2006-08-11 13:05:33 +02:00
|
|
|
class Handler(logging.Handler):
|
2006-08-12 16:19:47 +02:00
|
|
|
def __init__(self, console, console_id):
|
2006-08-11 13:05:33 +02:00
|
|
|
logging.Handler.__init__(self)
|
|
|
|
|
|
|
|
self._console_id = console_id
|
2006-08-12 16:19:47 +02:00
|
|
|
self._console = console
|
2006-08-11 17:05:06 +02:00
|
|
|
self._records = []
|
2006-08-11 13:05:33 +02:00
|
|
|
|
|
|
|
def _log(self):
|
2006-08-11 17:05:06 +02:00
|
|
|
for record in self._records:
|
2006-08-12 16:19:47 +02:00
|
|
|
self._console.log(record.levelno, self._console_id, record.msg)
|
2006-08-11 17:05:06 +02:00
|
|
|
self._records = []
|
2006-08-11 13:05:33 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def emit(self, record):
|
2006-08-11 17:05:06 +02:00
|
|
|
self._records.append(record)
|
|
|
|
if len(self._records) == 1:
|
2006-08-11 13:05:33 +02:00
|
|
|
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)
|
|
|
|
|
2006-08-12 16:19:47 +02:00
|
|
|
__console.log(logging.ERROR, __console_id, trace.getvalue())
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-08-12 16:19:47 +02:00
|
|
|
def start(console_id, console = None):
|
2006-08-11 13:05:33 +02:00
|
|
|
root_logger = logging.getLogger('')
|
|
|
|
root_logger.setLevel(logging.DEBUG)
|
|
|
|
|
2006-08-12 16:19:47 +02:00
|
|
|
if console == None:
|
2006-08-11 13:05:33 +02:00
|
|
|
bus = dbus.SessionBus()
|
2006-08-12 16:19:47 +02:00
|
|
|
proxy_obj = bus.get_object('org.laptop.Sugar.Console',
|
|
|
|
'/org/laptop/Sugar/Console')
|
|
|
|
console = dbus.Interface(proxy_obj, 'org.laptop.Sugar.Console')
|
2006-08-11 13:05:33 +02:00
|
|
|
|
2006-08-12 16:19:47 +02:00
|
|
|
root_logger.addHandler(Handler(console, console_id))
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-08-12 16:19:47 +02:00
|
|
|
global __console
|
2006-08-11 15:21:11 +02:00
|
|
|
global __console_id
|
|
|
|
|
2006-08-12 16:19:47 +02:00
|
|
|
__console = console
|
2006-08-11 15:21:11 +02:00
|
|
|
__console_id = console_id
|
2006-08-11 23:30:03 +02:00
|
|
|
sys.excepthook = __exception_handler
|