sugar-toolkit-gtk3/sugar/logger.py

103 lines
2.5 KiB
Python
Raw Normal View History

import sys
2006-08-11 13:05:33 +02:00
import logging
import traceback
from cStringIO import StringIO
2006-08-11 13:05:33 +02:00
import dbus
import dbus.dbus_bindings
2006-08-11 13:05:33 +02:00
import gobject
__queue = None
CONSOLE_BUS_NAME = 'org.laptop.Sugar.Console'
CONSOLE_OBJECT_PATH = '/org/laptop/Sugar/Console'
CONSOLE_IFACE = 'org.laptop.Sugar.Console'
2006-08-11 13:05:33 +02:00
class MessageQueue:
def __init__(self, console, console_id):
self._idle_id = 0
self._console = console
self._console_id = console_id
self._levels = []
self._messages = []
2006-08-16 00:41:51 +02:00
self._bus = dbus.SessionBus()
if self._console == None:
2006-08-16 00:41:51 +02:00
con = self._bus._connection
if dbus.dbus_bindings.bus_name_has_owner(con, CONSOLE_BUS_NAME):
self.setup_console()
else:
2006-08-16 00:41:51 +02:00
self._bus.add_signal_receiver(
self.__name_owner_changed,
dbus_interface = "org.freedesktop.DBus",
signal_name = "NameOwnerChanged")
def setup_console(self):
2006-08-16 00:41:51 +02:00
proxy_obj = self._bus.get_object(CONSOLE_BUS_NAME,
CONSOLE_OBJECT_PATH)
self._console = dbus.Interface(proxy_obj, CONSOLE_IFACE)
self._queue_log()
def __name_owner_changed(self, service_name, old_name, new_name):
if service_name == CONSOLE_BUS_NAME:
if new_name != None:
self.setup_console()
else:
self._console = None
def _queue_log(self):
if self._idle_id == 0:
self._idle_id = gobject.idle_add(self._log)
2006-08-11 13:05:33 +02:00
def _log(self):
if self._console == None or len(self._messages) == 0:
return False
if isinstance(self._console, dbus.Interface):
self._console.log(self._console_id, self._levels,
self._messages, timeout = 1000)
else:
self._console.log(self._console_id, self._levels,
self._messages)
self._levels = []
self._messages = []
self._idle_id = 0
2006-08-11 13:05:33 +02:00
return False
def append_record(self, record):
self.append(record.levelno, record.msg)
def append(self, level, message):
self._levels.append(level)
self._messages.append(message)
self._queue_log()
class Handler(logging.Handler):
def __init__(self, queue):
logging.Handler.__init__(self)
self._queue = queue
2006-08-11 13:05:33 +02:00
def emit(self, record):
self._queue.append_record(record)
2006-08-11 13:05:33 +02:00
def __exception_handler(typ, exc, tb):
trace = StringIO()
traceback.print_exception(typ, exc, tb, None, trace)
2006-08-15 13:15:46 +02:00
print >> sys.stderr, trace.getvalue()
__queue.append(logging.ERROR, trace.getvalue())
def start(console_id, console = None):
queue = MessageQueue(console, console_id)
2006-08-11 13:05:33 +02:00
root_logger = logging.getLogger('')
root_logger.setLevel(logging.DEBUG)
root_logger.addHandler(Handler(queue))
global __queue
__queue = queue
2006-08-11 23:30:03 +02:00
sys.excepthook = __exception_handler