Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/logger.py
blob: fd5bf3121226f80d6ca5b99fe55a71fb73558c82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sys
import logging
import traceback
from cStringIO import StringIO

import dbus
import gobject

__console = None
__console_id = None

class Handler(logging.Handler):
	def __init__(self, console, console_id):
		logging.Handler.__init__(self)

		self._console_id = console_id
		self._console = console
		self._records = []

	def _log(self):
		for record in self._records:
			self._console.log(record.levelno, self._console_id, record.msg)
		self._records = []
		return False

	def emit(self, record):
		self._records.append(record)
		if len(self._records) == 1:
			gobject.idle_add(self._log)

def __exception_handler(typ, exc, tb):
	trace = StringIO()
	traceback.print_exception(typ, exc, tb, None, trace)

	__console.log(logging.ERROR, __console_id, trace.getvalue())

def start(console_id, console = None):
	root_logger = logging.getLogger('')
	root_logger.setLevel(logging.DEBUG)
	
	if console == None:
		bus = dbus.SessionBus()
		proxy_obj = bus.get_object('org.laptop.Sugar.Console',
								   '/org/laptop/Sugar/Console')
		console = dbus.Interface(proxy_obj, 'org.laptop.Sugar.Console')

	root_logger.addHandler(Handler(console, console_id))

	global __console
	global __console_id

	__console = console
	__console_id = console_id
	sys.excepthook = __exception_handler