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
|
2006-08-13 02:57:12 +02:00
|
|
|
import dbus.dbus_bindings
|
2006-08-11 13:05:33 +02:00
|
|
|
import gobject
|
|
|
|
|
2006-08-14 12:18:58 +02:00
|
|
|
__queue = None
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-08-14 12:18:58 +02:00
|
|
|
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
|
|
|
|
2006-08-14 12:18:58 +02:00
|
|
|
class MessageQueue:
|
|
|
|
def __init__(self, console, console_id):
|
2006-08-13 02:57:12 +02:00
|
|
|
self._idle_id = 0
|
2006-08-14 12:18:58 +02:00
|
|
|
self._console = console
|
|
|
|
self._console_id = console_id
|
|
|
|
self._levels = []
|
|
|
|
self._messages = []
|
2006-08-16 00:41:51 +02:00
|
|
|
self._bus = dbus.SessionBus()
|
|
|
|
|
2006-08-14 12:18:58 +02:00
|
|
|
if self._console == None:
|
2006-08-16 00:41:51 +02:00
|
|
|
con = self._bus._connection
|
2006-08-14 12:18:58 +02:00
|
|
|
if dbus.dbus_bindings.bus_name_has_owner(con, CONSOLE_BUS_NAME):
|
|
|
|
self.setup_console()
|
2006-08-16 00:30:50 +02:00
|
|
|
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")
|
2006-08-13 01:31:24 +02:00
|
|
|
|
2006-08-14 12:18:58 +02:00
|
|
|
def setup_console(self):
|
2006-08-16 00:41:51 +02:00
|
|
|
proxy_obj = self._bus.get_object(CONSOLE_BUS_NAME,
|
|
|
|
CONSOLE_OBJECT_PATH)
|
2006-08-14 12:18:58 +02:00
|
|
|
self._console = dbus.Interface(proxy_obj, CONSOLE_IFACE)
|
|
|
|
self._queue_log()
|
2006-08-13 01:31:24 +02:00
|
|
|
|
|
|
|
def __name_owner_changed(self, service_name, old_name, new_name):
|
2006-08-14 12:18:58 +02:00
|
|
|
if service_name == CONSOLE_BUS_NAME:
|
2006-08-13 02:57:12 +02:00
|
|
|
if new_name != None:
|
2006-08-14 12:18:58 +02:00
|
|
|
self.setup_console()
|
2006-08-13 02:57:12 +02:00
|
|
|
else:
|
2006-08-14 12:18:58 +02:00
|
|
|
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):
|
2006-08-14 12:18:58 +02:00
|
|
|
if self._console == None or len(self._messages) == 0:
|
|
|
|
return False
|
|
|
|
|
2006-08-16 00:30:50 +02:00
|
|
|
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)
|
2006-08-14 12:18:58 +02:00
|
|
|
|
|
|
|
self._levels = []
|
|
|
|
self._messages = []
|
2006-08-13 02:57:12 +02:00
|
|
|
self._idle_id = 0
|
2006-08-13 01:31:24 +02:00
|
|
|
|
2006-08-11 13:05:33 +02:00
|
|
|
return False
|
|
|
|
|
2006-08-14 12:18:58 +02:00
|
|
|
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):
|
2006-08-14 12:18:58 +02:00
|
|
|
self._queue.append_record(record)
|
2006-08-11 13:05:33 +02:00
|
|
|
|
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-15 13:15:46 +02:00
|
|
|
print >> sys.stderr, trace.getvalue()
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-08-14 12:18:58 +02:00
|
|
|
__queue.append(logging.ERROR, 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-14 12:18:58 +02:00
|
|
|
queue = MessageQueue(console, console_id)
|
|
|
|
|
2006-08-11 13:05:33 +02:00
|
|
|
root_logger = logging.getLogger('')
|
|
|
|
root_logger.setLevel(logging.DEBUG)
|
2006-08-14 12:18:58 +02:00
|
|
|
root_logger.addHandler(Handler(queue))
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-08-14 12:18:58 +02:00
|
|
|
global __queue
|
|
|
|
__queue = queue
|
2006-08-11 23:30:03 +02:00
|
|
|
sys.excepthook = __exception_handler
|