Remove the old dbus stuff, use one log file per process
This commit is contained in:
parent
5cee6aa01d
commit
4e88e927fb
@ -17,6 +17,7 @@
|
|||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
import gtk
|
import gtk
|
||||||
import gtkmozembed
|
import gtkmozembed
|
||||||
|
import logging
|
||||||
|
|
||||||
from sugar.activity.Activity import Activity
|
from sugar.activity.Activity import Activity
|
||||||
from sugar import env
|
from sugar import env
|
||||||
@ -34,6 +35,8 @@ class WebActivity(Activity):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
Activity.__init__(self)
|
Activity.__init__(self)
|
||||||
|
|
||||||
|
logging.debug('Starting the web activity')
|
||||||
|
|
||||||
self.set_title(_('Web Activity'))
|
self.set_title(_('Web Activity'))
|
||||||
|
|
||||||
vbox = gtk.VBox()
|
vbox = gtk.VBox()
|
||||||
|
@ -4,7 +4,7 @@ import logging
|
|||||||
from presence import PresenceService
|
from presence import PresenceService
|
||||||
import sugar.logger
|
import sugar.logger
|
||||||
|
|
||||||
sugar.logger.start('PresenceService')
|
sugar.logger.start('presenceservice')
|
||||||
|
|
||||||
logging.info('Starting presence service')
|
logging.info('Starting presence service')
|
||||||
|
|
||||||
|
@ -1,25 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
# Copyright (C) 2006, Red Hat, Inc.
|
|
||||||
#
|
|
||||||
# This program is free software; you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation; either version 2 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# This program is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with this program; if not, write to the Free Software
|
|
||||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
||||||
|
|
||||||
import dbus
|
|
||||||
import dbus.glib
|
|
||||||
|
|
||||||
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')
|
|
||||||
shell.show_console()
|
|
131
sugar/logger.py
131
sugar/logger.py
@ -15,137 +15,58 @@
|
|||||||
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||||
# Boston, MA 02111-1307, USA.
|
# Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
import sys, os
|
import sys
|
||||||
|
import os
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
from cStringIO import StringIO
|
from cStringIO import StringIO
|
||||||
|
|
||||||
import dbus
|
|
||||||
|
|
||||||
#internals of D-Bus Python are changing
|
|
||||||
#try not to use it in the future
|
|
||||||
if dbus.version < (0, 70, 0):
|
|
||||||
import dbus.dbus_bindings
|
|
||||||
|
|
||||||
import gobject
|
|
||||||
|
|
||||||
from sugar import env
|
from sugar import env
|
||||||
|
|
||||||
__queue = None
|
_log_writer = None
|
||||||
|
|
||||||
CONSOLE_BUS_NAME = 'org.laptop.Sugar.Console'
|
class LogWriter:
|
||||||
CONSOLE_OBJECT_PATH = '/org/laptop/Sugar/Console'
|
def __init__(self, module_id):
|
||||||
CONSOLE_IFACE = 'org.laptop.Sugar.Console'
|
self._module_id = module_id
|
||||||
|
|
||||||
class MessageQueue:
|
logs_dir = os.path.join(env.get_profile_path(), 'logs')
|
||||||
def __init__(self, console, console_id):
|
if not os.path.isdir(logs_dir):
|
||||||
self._idle_id = 0
|
os.makedirs(logs_dir)
|
||||||
self._console = console
|
|
||||||
self._console_id = console_id
|
|
||||||
self._levels = []
|
|
||||||
self._messages = []
|
|
||||||
self._bus = dbus.SessionBus()
|
|
||||||
|
|
||||||
if self._console == None:
|
|
||||||
con = self._bus._connection
|
|
||||||
name_has_owner = False
|
|
||||||
|
|
||||||
try:
|
|
||||||
name_has_owner = dbus.dbus_bindings.bus_name_has_owner(con, CONSOLE_BUS_NAME)
|
|
||||||
except:
|
|
||||||
name_has_owner = con.name_has_owner(CONSOLE_BUS_NAME)
|
|
||||||
|
|
||||||
if name_has_owner:
|
log_path = os.path.join(logs_dir, module_id + '.log')
|
||||||
self.setup_console()
|
self._log_file = open(log_path, 'w')
|
||||||
else:
|
|
||||||
self._bus.add_signal_receiver(
|
|
||||||
self.__name_owner_changed,
|
|
||||||
dbus_interface = "org.freedesktop.DBus",
|
|
||||||
signal_name = "NameOwnerChanged")
|
|
||||||
|
|
||||||
def setup_console(self):
|
def write_record(self, record):
|
||||||
proxy_obj = self._bus.get_object(CONSOLE_BUS_NAME,
|
self.write(record.levelno, record.msg)
|
||||||
CONSOLE_OBJECT_PATH)
|
|
||||||
self._console = dbus.Interface(proxy_obj, CONSOLE_IFACE)
|
|
||||||
self._queue_log()
|
|
||||||
|
|
||||||
def __name_owner_changed(self, service_name, old_name, new_name):
|
def write(self, level, msg):
|
||||||
if service_name == CONSOLE_BUS_NAME:
|
fmt = "(%s): Level %s - %s\n" % (os.getpid(), level, msg)
|
||||||
if new_name != None:
|
fmt = fmt.encode("utf8")
|
||||||
self.setup_console()
|
self._log_file.write(fmt)
|
||||||
else:
|
|
||||||
self._console = None
|
|
||||||
|
|
||||||
def _queue_log(self):
|
|
||||||
if self._idle_id == 0:
|
|
||||||
self._idle_id = gobject.idle_add(self._log)
|
|
||||||
|
|
||||||
def _log(self):
|
|
||||||
# Use stderr for now until dbus issues get sorted out
|
|
||||||
use_dbus = False
|
|
||||||
|
|
||||||
if use_dbus:
|
|
||||||
if self._console == None or len(self._messages) == 0:
|
|
||||||
self._idle_id = 0
|
|
||||||
return False
|
|
||||||
|
|
||||||
if isinstance(self._console, dbus.Interface):
|
|
||||||
self._console.log(self._console_id, self._levels,
|
|
||||||
self._messages, timeout = 1000)
|
|
||||||
else:
|
|
||||||
self._console.log(self._console_id, self._levels,
|
|
||||||
self._messages)
|
|
||||||
else:
|
|
||||||
for x in range(0, len(self._messages)):
|
|
||||||
level = self._levels[x]
|
|
||||||
msg = self._messages[x]
|
|
||||||
prog = os.path.basename(sys.argv[0])
|
|
||||||
fmt = "%s (%s): Level %s - %s\n" % (prog, os.getpid(), level, msg)
|
|
||||||
fmt = fmt.encode("utf8")
|
|
||||||
sys.stderr.write(fmt)
|
|
||||||
|
|
||||||
self._levels = []
|
|
||||||
self._messages = []
|
|
||||||
self._idle_id = 0
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
def append_record(self, record):
|
|
||||||
self.append(record.levelno, record.msg)
|
|
||||||
|
|
||||||
def append(self, level, message):
|
|
||||||
self._levels.append(level)
|
|
||||||
self._messages.append(message)
|
|
||||||
self._queue_log()
|
|
||||||
|
|
||||||
class Handler(logging.Handler):
|
class Handler(logging.Handler):
|
||||||
def __init__(self, queue):
|
def __init__(self, writer):
|
||||||
logging.Handler.__init__(self)
|
logging.Handler.__init__(self)
|
||||||
|
|
||||||
self._queue = queue
|
self._writer = writer
|
||||||
|
|
||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
self._queue.append_record(record)
|
self._writer.write_record(record)
|
||||||
|
|
||||||
def __exception_handler(typ, exc, tb):
|
def __exception_handler(typ, exc, tb):
|
||||||
trace = StringIO()
|
trace = StringIO()
|
||||||
traceback.print_exception(typ, exc, tb, None, trace)
|
traceback.print_exception(typ, exc, tb, None, trace)
|
||||||
print >> sys.stderr, trace.getvalue()
|
print >> sys.stderr, trace.getvalue()
|
||||||
|
|
||||||
__queue.append(logging.ERROR, trace.getvalue())
|
_log_writer.write(logging.ERROR, trace.getvalue())
|
||||||
|
|
||||||
def start(console_id, console = None):
|
def start(module_id):
|
||||||
queue = MessageQueue(console, console_id)
|
log_writer = LogWriter(module_id)
|
||||||
|
|
||||||
root_logger = logging.getLogger('')
|
root_logger = logging.getLogger('')
|
||||||
root_logger.setLevel(logging.DEBUG)
|
root_logger.setLevel(logging.DEBUG)
|
||||||
root_logger.addHandler(Handler(queue))
|
root_logger.addHandler(Handler(log_writer))
|
||||||
|
|
||||||
log_file = os.path.join(env.get_profile_path(), 'sugar.log')
|
global _log_writer
|
||||||
fileh = logging.FileHandler(log_file)
|
_log_writer = log_writer
|
||||||
fileh.setFormatter(logging.Formatter("""[%(asctime)s] %(message)s"""))
|
|
||||||
root_logger.addHandler(fileh)
|
|
||||||
|
|
||||||
global __queue
|
|
||||||
__queue = queue
|
|
||||||
sys.excepthook = __exception_handler
|
sys.excepthook = __exception_handler
|
||||||
|
Loading…
Reference in New Issue
Block a user