diff --git a/shell/ConsoleWindow.py b/shell/ConsoleWindow.py index e9a25636..3cbd7926 100644 --- a/shell/ConsoleWindow.py +++ b/shell/ConsoleWindow.py @@ -1,19 +1,41 @@ +import logging + import gtk class Console(gtk.ScrolledWindow): def __init__(self): gtk.ScrolledWindow.__init__(self) + + self._show_debug = False + self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) - + self._textview = gtk.TextView() self._textview.set_wrap_mode(gtk.WRAP_WORD) self.add(self._textview) self._textview.show() - def log(self, message): buf = self._textview.get_buffer() - buf.insert(buf.get_end_iter(), message) + self._debug_tag = buf.create_tag("debug") + self._debug_tag.set_property("invisible", True) + + def get_show_debug(self): + return self._show_debug + + def set_show_debug(self, show_debug): + self._show_debug = show_debug + self._debug_tag.set_property("invisible", not show_debug) + + def log(self, level, message): + msg = message + '\n' + buf = self._textview.get_buffer() + it = buf.get_end_iter() + + if level == logging.DEBUG: + buf.insert_with_tags(it, msg, self._debug_tag) + else: + buf.insert(it, msg) class ConsoleWindow(gtk.Window): def __init__(self): @@ -24,10 +46,27 @@ class ConsoleWindow(gtk.Window): self.set_title("Console") self.connect("delete_event", lambda w, e: w.hide_on_delete()) + vbox = gtk.VBox() + + toolbar = gtk.Toolbar() + + self._debug_toggle = gtk.ToggleToolButton() + self._debug_toggle.connect('toggled', self.__debug_toggled_cb) + self._debug_toggle.set_label('Debug') + toolbar.insert(self._debug_toggle, -1) + self._debug_toggle.show() + + vbox.pack_start(toolbar, False) + toolbar.show() + self._nb = gtk.Notebook() - self.add(self._nb) + self._nb.connect('switch-page', self.__page_changed_cb) + vbox.pack_start(self._nb) self._nb.show() + self.add(vbox) + vbox.show() + self._consoles = {} def _add_console(self, page_id): @@ -46,10 +85,18 @@ class ConsoleWindow(gtk.Window): console = self._consoles[page_id] return console + def __debug_toggled_cb(self, button): + console = self._nb.get_nth_page(self._nb.get_current_page()) + console.set_show_debug(button.get_active()) + + def __page_changed_cb(self, notebook, page, page_num): + console = self._nb.get_nth_page(page_num) + self._debug_toggle.set_active(console.get_show_debug()) + def set_page(self, page_id): page = self._nb.page_num(self._consoles[page_id]) self._nb.set_current_page(page) - def log(self, page_id, message): + def log(self, level, page_id, message): console = self._get_console(page_id) - console.log(message) + console.log(level, message) diff --git a/shell/Shell.py b/shell/Shell.py index 9c87a25a..bf0d89be 100755 --- a/shell/Shell.py +++ b/shell/Shell.py @@ -40,8 +40,8 @@ class ShellDbusService(dbus.service.Object): gobject.idle_add(self.__show_console_idle) @dbus.service.method('com.redhat.Sugar.Shell') - def log(self, module_id, message): - self._shell.log(module_id, message) + def log(self, level, module_id, message): + self._shell.log(level, module_id, message) class Shell(gobject.GObject): __gsignals__ = { @@ -151,8 +151,8 @@ class Shell(gobject.GObject): logging.error('No such activity in the directory') return None - def log(self, module_id, message): - self._console.log(module_id, message) + def log(self, level, module_id, message): + self._console.log(level, module_id, message) def get_registry(self): return self._registry diff --git a/sugar/activity/Activity.py b/sugar/activity/Activity.py index 59a9c37c..13077ea7 100644 --- a/sugar/activity/Activity.py +++ b/sugar/activity/Activity.py @@ -1,4 +1,5 @@ import os +import logging import dbus import dbus.service @@ -134,6 +135,8 @@ class Activity(gtk.Window): def share(self): """Share the activity on the network.""" + logging.debug('Share activity %s on the network.' % self.get_id()) + properties = { 'title' : self.get_title() } self._service = self._pservice.share_activity(self, self._default_type, properties) diff --git a/sugar/logger.py b/sugar/logger.py index df5aebda..03964dde 100644 --- a/sugar/logger.py +++ b/sugar/logger.py @@ -15,24 +15,24 @@ class Handler(logging.Handler): self._console_id = console_id self._shell = shell - self._messages = [] + self._records = [] def _log(self): - for message in self._messages: - self._shell.log(self._console_id, message) - self._messages = [] + for record in self._records: + self._shell.log(record.levelno, self._console_id, record.msg) + self._records = [] return False def emit(self, record): - self._messages.append(record.msg) - if len(self._messages) == 1: + 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(__console_id, trace.getvalue()) + __sugar_shell.log(logging.ERROR, __console_id, trace.getvalue()) def start(console_id, shell = None): root_logger = logging.getLogger('') @@ -50,4 +50,4 @@ def start(console_id, shell = None): __sugar_shell = shell __console_id = console_id - sys.excepthook = __exception_handler + #sys.excepthook = __exception_handler