Refactor the console stuff
parent
cb47f17b31
commit
628271959c
@ -1,34 +0,0 @@
|
||||
import sys
|
||||
import logging
|
||||
|
||||
import dbus
|
||||
|
||||
import sugar.env
|
||||
|
||||
class LogWriter:
|
||||
def __init__(self, application, use_console = True):
|
||||
self._application = application
|
||||
self._use_console = use_console
|
||||
|
||||
bus = dbus.SessionBus()
|
||||
proxy_obj = bus.get_object('com.redhat.Sugar.Shell', '/com/redhat/Sugar/Shell')
|
||||
self._logger = dbus.Interface(proxy_obj, 'com.redhat.Sugar.Shell')
|
||||
|
||||
def start(self):
|
||||
if self._use_console:
|
||||
sys.stdout = self
|
||||
sys.stderr = self
|
||||
|
||||
level = sugar.env.get_logging_level()
|
||||
if level == 'debug':
|
||||
logging.basicConfig(level=logging.DEBUG,
|
||||
format='%(levelname)s %(message)s')
|
||||
|
||||
def write(self, s):
|
||||
self._logger.log(self._application, s, ignore_reply=True)
|
||||
|
||||
def emit(self, record):
|
||||
pass
|
||||
|
||||
def flush(self):
|
||||
pass
|
@ -0,0 +1,34 @@
|
||||
import logging
|
||||
|
||||
import dbus
|
||||
import gobject
|
||||
|
||||
class Handler(logging.Handler):
|
||||
def __init__(self, shell, console_id):
|
||||
logging.Handler.__init__(self)
|
||||
|
||||
self._console_id = console_id
|
||||
self._shell = shell
|
||||
self._messages = []
|
||||
|
||||
def _log(self):
|
||||
for message in self._messages:
|
||||
# FIXME use a single dbus call
|
||||
self._shell.log(self._console_id, message)
|
||||
return False
|
||||
|
||||
def emit(self, record):
|
||||
self._messages.append(record.msg)
|
||||
if len(self._messages) == 1:
|
||||
gobject.idle_add(self._log)
|
||||
|
||||
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))
|
Loading…
Reference in New Issue