2007-04-10 04:47:37 +02:00
|
|
|
"""Logging module configuration for Sugar"""
|
2007-06-24 14:43:48 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2006-10-15 01:08:44 +02:00
|
|
|
#
|
|
|
|
# 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
|
2007-05-29 18:11:57 +02:00
|
|
|
import time
|
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-25 15:24:40 +02:00
|
|
|
STDOUT_LEVEL = 1000
|
|
|
|
STDERR_LEVEL = 2000
|
|
|
|
|
2007-05-08 17:24:48 +02:00
|
|
|
formatter = logging.Formatter('%(name)s: %(message)s')
|
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
class LogWriter:
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self, module_id):
|
|
|
|
self._module_id = module_id
|
|
|
|
|
|
|
|
logs_dir = _get_logs_dir()
|
|
|
|
log_path = os.path.join(logs_dir, module_id + '.log')
|
|
|
|
self._log_file = open(log_path, 'w')
|
|
|
|
|
|
|
|
def write_record(self, record):
|
2007-05-08 17:24:48 +02:00
|
|
|
self.write(record.levelno, formatter.format(record))
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def write(self, level, msg):
|
|
|
|
if level == logging.ERROR:
|
|
|
|
level_txt = 'ERROR'
|
|
|
|
elif level == logging.WARNING:
|
|
|
|
level_txt = 'WARNING'
|
|
|
|
elif level == logging.DEBUG:
|
|
|
|
level_txt = 'DEBUG'
|
|
|
|
elif level == logging.INFO:
|
|
|
|
level_txt = 'INFO'
|
|
|
|
elif level == STDERR_LEVEL:
|
|
|
|
level_txt = 'STDERR'
|
|
|
|
elif level == STDOUT_LEVEL:
|
|
|
|
level_txt = 'STDOUT'
|
|
|
|
|
2007-04-09 22:05:32 +02:00
|
|
|
if msg[len(msg) - 1] != '\n':
|
|
|
|
msg += "\n"
|
|
|
|
fmt = "%s - %s" % (level_txt, msg)
|
2006-12-04 20:12:24 +01:00
|
|
|
fmt = fmt.encode("utf8")
|
|
|
|
self._log_file.write(fmt)
|
|
|
|
self._log_file.flush()
|
2006-08-14 12:18:58 +02:00
|
|
|
|
|
|
|
class Handler(logging.Handler):
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self, writer):
|
|
|
|
logging.Handler.__init__(self)
|
2006-08-14 12:18:58 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
self._writer = writer
|
2006-08-14 12:18:58 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def emit(self, record):
|
|
|
|
self._writer.write_record(record)
|
2006-08-11 13:05:33 +02:00
|
|
|
|
2006-10-25 15:24:40 +02:00
|
|
|
class StdoutCatcher:
|
2006-12-04 20:12:24 +01:00
|
|
|
def write(self, txt):
|
|
|
|
_log_writer.write(STDOUT_LEVEL, txt)
|
|
|
|
sys.__stdout__.write(txt)
|
2006-10-25 15:24:40 +02:00
|
|
|
|
2007-07-06 18:24:38 +02:00
|
|
|
def flush(self):
|
|
|
|
sys.__stderr__.flush()
|
|
|
|
|
2006-10-25 15:24:40 +02:00
|
|
|
class StderrCatcher:
|
2006-12-04 20:12:24 +01:00
|
|
|
def write(self, txt):
|
|
|
|
_log_writer.write(STDERR_LEVEL, txt)
|
|
|
|
sys.__stderr__.write(txt)
|
2006-10-25 15:24:40 +02:00
|
|
|
|
2007-05-22 18:24:56 +02:00
|
|
|
def flush(self):
|
|
|
|
sys.__stderr__.flush()
|
|
|
|
|
2006-08-11 15:21:11 +02:00
|
|
|
def __exception_handler(typ, exc, tb):
|
2006-12-04 20:12:24 +01:00
|
|
|
trace = StringIO()
|
|
|
|
traceback.print_exception(typ, exc, tb, None, trace)
|
|
|
|
print >> sys.stderr, trace.getvalue()
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
_log_writer.write(logging.ERROR, trace.getvalue())
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-10-24 20:00:14 +02:00
|
|
|
def _get_logs_dir():
|
2006-12-04 20:12:24 +01:00
|
|
|
logs_dir = os.path.join(env.get_profile_path(), 'logs')
|
|
|
|
if not os.path.isdir(logs_dir):
|
|
|
|
os.makedirs(logs_dir)
|
|
|
|
return logs_dir
|
2006-10-24 20:00:14 +02:00
|
|
|
|
2006-10-16 15:56:22 +02:00
|
|
|
def start(module_id):
|
2006-12-04 20:12:24 +01:00
|
|
|
log_writer = LogWriter(module_id)
|
2006-08-14 12:18:58 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
root_logger = logging.getLogger('')
|
|
|
|
root_logger.setLevel(logging.DEBUG)
|
|
|
|
root_logger.addHandler(Handler(log_writer))
|
2006-08-11 15:21:11 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
sys.stdout = StdoutCatcher()
|
|
|
|
sys.stderr = StderrCatcher()
|
2006-10-25 15:24:40 +02:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
global _log_writer
|
|
|
|
_log_writer = log_writer
|
|
|
|
sys.excepthook = __exception_handler
|
2006-10-24 20:00:14 +02:00
|
|
|
|
|
|
|
def cleanup():
|
2007-05-29 18:11:57 +02:00
|
|
|
logs_dir = _get_logs_dir()
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
# File extension for backed up logfiles.
|
2007-05-29 21:24:01 +02:00
|
|
|
|
|
|
|
file_suffix = int(time.time())
|
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
# Absolute directory path where to store old logfiles.
|
|
|
|
# It will be created recursivly if it's not present.
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
backup_dirpath = os.path.join(logs_dir, 'old')
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
# How many versions shall be backed up of every logfile?
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
num_backup_versions = 4
|
|
|
|
|
|
|
|
# Make sure the backup location for old log files exists
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
if not os.path.exists(backup_dirpath):
|
|
|
|
os.makedirs(backup_dirpath)
|
|
|
|
|
|
|
|
# Iterate over every item in 'logs' directory
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
for filename in os.listdir(logs_dir):
|
|
|
|
|
|
|
|
old_filepath = os.path.join(logs_dir, filename)
|
|
|
|
|
|
|
|
if os.path.isfile(old_filepath):
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
# Backup every file
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
new_filename = filename + '.' + str(file_suffix)
|
|
|
|
new_filepath = os.path.join(backup_dirpath, new_filename)
|
|
|
|
os.rename(old_filepath, new_filepath)
|
|
|
|
|
2007-05-29 23:25:47 +02:00
|
|
|
backup_map = {}
|
|
|
|
|
|
|
|
# Temporarily map all backup logfiles
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
for filename in os.listdir(backup_dirpath):
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
# Remove the 'file_suffix' from the filename.
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
end = filename.rfind(".")
|
|
|
|
key = filename[0:end].lower()
|
|
|
|
key = key.replace(".", "_")
|
|
|
|
|
|
|
|
if key not in backup_map:
|
|
|
|
backup_map[key] = []
|
|
|
|
|
|
|
|
backup_list = backup_map[key]
|
|
|
|
|
|
|
|
backup_list.append( os.path.join(backup_dirpath, filename) )
|
|
|
|
|
|
|
|
# Only keep 'num_backup_versions' versions of every logfile.
|
|
|
|
# Remove the others.
|
2007-05-29 21:24:01 +02:00
|
|
|
|
2007-05-29 18:11:57 +02:00
|
|
|
for key in backup_map:
|
|
|
|
backup_list = backup_map[key]
|
|
|
|
backup_list.sort()
|
|
|
|
backup_list.reverse()
|
|
|
|
|
|
|
|
for i in range(num_backup_versions, len(backup_list)):
|
|
|
|
os.remove(backup_list[i])
|
2007-05-29 21:24:01 +02:00
|
|
|
|