Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/logger.py
blob: d6e5daaa761e46e32c81ffcf030bdaa7ff50bea7 (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
import sys
import logging
import traceback
from cStringIO import StringIO

import dbus
import gobject

__sugar_shell = None
__console_id = None

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

		self._console_id = console_id
		self._shell = shell
		self._records = []

	def _log(self):
		for record in self._records:
			self._shell.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)

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

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))

	global __sugar_shell
	global __console_id

	__sugar_shell = shell
	__console_id = console_id
	sys.excepthook = __exception_handler