diff --git a/shell/Shell.py b/shell/Shell.py index 0baea0e3..b142963d 100755 --- a/shell/Shell.py +++ b/shell/Shell.py @@ -18,6 +18,7 @@ from ActivityHost import ActivityHost from ChatController import ChatController from sugar.activity import ActivityFactory from sugar.activity import Activity +import sugar.logger class ShellDbusService(dbus.service.Object): def __init__(self, shell, bus_name): @@ -59,13 +60,12 @@ class Shell(gobject.GObject): self._console_windows = {} def start(self): - #log_writer = LogWriter("Shell", False) - #log_writer.start() - session_bus = dbus.SessionBus() bus_name = dbus.service.BusName('com.redhat.Sugar.Shell', bus=session_bus) ShellDbusService(self, bus_name) + sugar.logger.start('Shell', self) + self._owner = ShellOwner() self._owner.announce() @@ -132,6 +132,9 @@ class Shell(gobject.GObject): module = self._registry.get_activity(activity.get_default_type()) console = self.get_console(module.get_id()) activity.show_dialog(console) + else: + console = self.get_console('Shell') + console.show() def join_activity(self, service): info = self._registry.get_activity(service.get_type()) diff --git a/shell/session/Emulator.py b/shell/session/Emulator.py index d70059d6..4928ec4d 100644 --- a/shell/session/Emulator.py +++ b/shell/session/Emulator.py @@ -1,4 +1,3 @@ -import logging import os import socket import sys @@ -9,8 +8,8 @@ def get_display_number(): """Find a free display number trying to connect to 6000+ ports""" retries = 20 display_number = 1 - display_is_free = False - + display_is_free = False + while not display_is_free and retries > 0: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: @@ -57,10 +56,6 @@ class XnestProcess(Process): class Emulator: """The OLPC emulator""" - - def __init__(self): - pass - def start(self): try: process = XephyrProcess() @@ -70,6 +65,6 @@ class Emulator: process = XnestProcess() process.start() except: - logging.error('Cannot run the emulator. You need to install \ - Xephyr or Xnest.') + print('Cannot run the emulator. You need to install\ + Xephyr or Xnest.') sys.exit(0) diff --git a/shell/sugar b/shell/sugar index 47c1225f..42de27e4 100755 --- a/shell/sugar +++ b/shell/sugar @@ -74,7 +74,6 @@ from ActivityRegistry import ActivityRegistry registry = ActivityRegistry() registry.scan_directory(activities_dest) -#registry.scan_directory(os.path.join(env.get_user_dir(), 'activities')) from session.Emulator import Emulator @@ -82,8 +81,6 @@ from session.Emulator import Emulator emulator = Emulator() emulator.start() -print 'Redirecting output to the console, press F3 to open it.' - from session.Session import Session session = Session(registry) diff --git a/shell/sugar-activity-factory b/shell/sugar-activity-factory index 9e028f1c..7be73d15 100755 --- a/shell/sugar-activity-factory +++ b/shell/sugar-activity-factory @@ -12,6 +12,9 @@ gtk.gdk.threads_init() dbus.glib.threads_init() from sugar.activity import ActivityFactory +import sugar.logger + +sugar.logger.start(sys.argv[1]) logging.info('Starting activity factory %s' % sys.argv[1]) diff --git a/sugar/LogWriter.py b/sugar/LogWriter.py deleted file mode 100644 index ba8bdb02..00000000 --- a/sugar/LogWriter.py +++ /dev/null @@ -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 diff --git a/sugar/logger.py b/sugar/logger.py new file mode 100644 index 00000000..54cb15b5 --- /dev/null +++ b/sugar/logger.py @@ -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))