2006-10-15 01:08:44 +02:00
|
|
|
# Copyright (C) 2006, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library 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
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
import sys
|
|
|
|
import os
|
2006-08-11 13:05:33 +02:00
|
|
|
import logging
|
2006-08-11 15:21:11 +02:00
|
|
|
import traceback
|
|
|
|
from cStringIO import StringIO
|
2006-08-11 13:05:33 +02:00
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
from sugar import env
|
2006-10-03 20:27:51 +02:00
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
_log_writer = None
|
2006-10-03 20:27:51 +02:00
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
class LogWriter:
|
|
|
|
def __init__(self, module_id):
|
|
|
|
self._module_id = module_id
|
2006-08-11 13:05:33 +02:00
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
logs_dir = os.path.join(env.get_profile_path(), 'logs')
|
|
|
|
if not os.path.isdir(logs_dir):
|
|
|
|
os.makedirs(logs_dir)
|
|
|
|
|
|
|
|
log_path = os.path.join(logs_dir, module_id + '.log')
|
|
|
|
self._log_file = open(log_path, 'w')
|
2006-10-06 18:53:12 +02:00
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
def write_record(self, record):
|
|
|
|
self.write(record.levelno, record.msg)
|
|
|
|
|
|
|
|
def write(self, level, msg):
|
2006-10-16 17:04:26 +02:00
|
|
|
if level == logging.ERROR:
|
|
|
|
level_txt = 'ERROR'
|
|
|
|
elif level == logging.DEBUG:
|
|
|
|
level_txt = 'DEBUG'
|
|
|
|
elif level == logging.INFO:
|
|
|
|
level_txt = 'INFO'
|
|
|
|
|
|
|
|
fmt = "%s - %s\n" % (level_txt, msg)
|
2006-10-16 15:56:22 +02:00
|
|
|
fmt = fmt.encode("utf8")
|
|
|
|
self._log_file.write(fmt)
|
2006-10-16 16:56:48 +02:00
|
|
|
self._log_file.flush()
|
2006-08-14 12:18:58 +02:00
|
|
|
|
|
|
|
class Handler(logging.Handler):
|
2006-10-16 15:56:22 +02:00
|
|
|
def __init__(self, writer):
|
2006-08-14 12:18:58 +02:00
|
|
|
logging.Handler.__init__(self)
|
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
self._writer = writer
|
2006-08-14 12:18:58 +02:00
|
|
|
|
2006-08-11 13:05:33 +02:00
|
|
|
def emit(self, record):
|
2006-10-16 15:56:22 +02:00
|
|
|
self._writer.write_record(record)
|
2006-08-11 13:05:33 +02:00
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
def __exception_handler(typ, exc, tb):
|
|
|
|
trace = StringIO()
|
|
|
|
traceback.print_exception(typ, exc, tb, None, trace)
|
2006-08-15 13:15:46 +02:00
|
|
|
print >> sys.stderr, trace.getvalue()
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
_log_writer.write(logging.ERROR, trace.getvalue())
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
def start(module_id):
|
|
|
|
log_writer = LogWriter(module_id)
|
2006-08-14 12:18:58 +02:00
|
|
|
|
2006-08-11 13:05:33 +02:00
|
|
|
root_logger = logging.getLogger('')
|
|
|
|
root_logger.setLevel(logging.DEBUG)
|
2006-10-16 15:56:22 +02:00
|
|
|
root_logger.addHandler(Handler(log_writer))
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
global _log_writer
|
|
|
|
_log_writer = log_writer
|
2006-08-11 23:30:03 +02:00
|
|
|
sys.excepthook = __exception_handler
|