2003-11-26 19:21:12 +01:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2003-11-26 19:21:12 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
__revision__ = "$Id$"
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.fix as fix
|
2003-11-26 19:21:12 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2004-01-19 21:22:06 +01:00
|
|
|
import time
|
2003-12-03 23:32:33 +01:00
|
|
|
import types
|
2003-11-26 19:21:12 +01:00
|
|
|
import atexit
|
|
|
|
import logging
|
2004-05-07 14:36:57 +02:00
|
|
|
import operator
|
2004-08-05 07:40:32 +02:00
|
|
|
import textwrap
|
2004-05-07 14:36:57 +02:00
|
|
|
import traceback
|
2003-11-26 19:21:12 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.ansi as ansi
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
import supybot.registry as registry
|
2004-01-18 08:58:26 +01:00
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.ircutils as ircutils
|
2004-07-20 00:33:58 +02:00
|
|
|
|
2003-11-26 19:21:12 +01:00
|
|
|
deadlyExceptions = [KeyboardInterrupt, SystemExit]
|
|
|
|
|
2004-08-26 07:23:24 +02:00
|
|
|
###
|
|
|
|
# This is for testing, of course. Mostly is just disables the firewall code
|
|
|
|
# so exceptions can propagate.
|
|
|
|
###
|
|
|
|
testing = False
|
|
|
|
|
2003-11-26 19:21:12 +01:00
|
|
|
class Formatter(logging.Formatter):
|
|
|
|
def formatTime(self, record, datefmt=None):
|
2004-04-14 04:37:17 +02:00
|
|
|
return timestamp(record.created)
|
2003-11-26 19:21:12 +01:00
|
|
|
|
|
|
|
def formatException(self, (E, e, tb)):
|
|
|
|
for exn in deadlyExceptions:
|
|
|
|
if issubclass(e.__class__, exn):
|
|
|
|
raise
|
2004-02-03 19:21:19 +01:00
|
|
|
return logging.Formatter.formatException(self, (E, e, tb))
|
2003-11-26 19:21:12 +01:00
|
|
|
|
2003-12-01 16:38:42 +01:00
|
|
|
|
2004-05-07 14:36:57 +02:00
|
|
|
class Logger(logging.Logger):
|
|
|
|
def exception(self, *args):
|
|
|
|
(E, e, tb) = sys.exc_info()
|
|
|
|
tbinfo = traceback.extract_tb(tb)
|
2004-05-07 16:41:25 +02:00
|
|
|
path = '[%s]' % '|'.join(map(operator.itemgetter(2), tbinfo))
|
2004-05-07 14:36:57 +02:00
|
|
|
eStrId = '%s:%s' % (E, path)
|
2004-05-07 16:47:21 +02:00
|
|
|
eId = hex(hash(eStrId) & 0xFFFFF)
|
2004-05-07 14:36:57 +02:00
|
|
|
logging.Logger.exception(self, *args)
|
2004-08-30 22:06:05 +02:00
|
|
|
if hasattr(e, '__revision__') and e.__revision__:
|
|
|
|
self.error('Exception __revision__: %r', e.__revision__)
|
2004-05-07 14:36:57 +02:00
|
|
|
self.error('Exception id: %s', eId)
|
2004-05-12 00:37:32 +02:00
|
|
|
# The traceback should be sufficient if we want it.
|
|
|
|
# self.error('Exception string: %s', eStrId)
|
2004-05-07 14:36:57 +02:00
|
|
|
|
2004-03-27 21:18:47 +01:00
|
|
|
|
2004-05-12 00:29:29 +02:00
|
|
|
class StdoutStreamHandler(logging.StreamHandler):
|
2004-04-06 11:32:09 +02:00
|
|
|
def disable(self):
|
|
|
|
self.setLevel(sys.maxint) # Just in case.
|
|
|
|
_logger.removeHandler(self)
|
|
|
|
logging._acquireLock()
|
|
|
|
try:
|
|
|
|
del logging._handlers[self]
|
|
|
|
finally:
|
|
|
|
logging._releaseLock()
|
|
|
|
|
2004-08-05 07:40:32 +02:00
|
|
|
def format(self, record):
|
|
|
|
s = logging.StreamHandler.format(self, record)
|
|
|
|
if record.levelname != 'ERROR' and conf.supybot.log.stdout.wrap():
|
|
|
|
# We check for ERROR there because otherwise, tracebacks (which are
|
|
|
|
# already wrapped by Python itself) wrap oddly.
|
|
|
|
prefixLen = len(record.name) + 2 # ": "
|
|
|
|
s = textwrap.fill(s, width=78, subsequent_indent=' '*prefixLen)
|
2004-08-07 02:57:13 +02:00
|
|
|
s.rstrip('\r\n')
|
2004-08-05 07:40:32 +02:00
|
|
|
return s
|
|
|
|
|
2004-02-02 01:59:02 +01:00
|
|
|
def emit(self, record):
|
2004-04-18 07:56:31 +02:00
|
|
|
if conf.supybot.log.stdout() and not conf.daemonized:
|
2004-04-06 11:32:09 +02:00
|
|
|
try:
|
2004-05-12 00:29:29 +02:00
|
|
|
logging.StreamHandler.emit(self, record)
|
2004-04-06 11:32:09 +02:00
|
|
|
except ValueError, e: # Raised if sys.stdout is closed.
|
|
|
|
self.disable()
|
|
|
|
error('Error logging to stdout. Removing stdout handler.')
|
|
|
|
exception('Uncaught exception in StdoutStreamHandler:')
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2003-12-03 23:32:33 +01:00
|
|
|
|
|
|
|
class BetterFileHandler(logging.FileHandler):
|
|
|
|
def emit(self, record):
|
|
|
|
msg = self.format(record)
|
|
|
|
if not hasattr(types, "UnicodeType"): #if no unicode support...
|
2004-09-09 00:48:53 +02:00
|
|
|
self.stream.write(msg)
|
|
|
|
self.stream.write(os.linesep)
|
2003-12-03 23:32:33 +01:00
|
|
|
else:
|
|
|
|
try:
|
2004-09-09 00:48:53 +02:00
|
|
|
self.stream.write(msg)
|
|
|
|
self.stream.write(os.linesep)
|
2003-12-03 23:32:33 +01:00
|
|
|
except UnicodeError:
|
2004-09-09 00:48:53 +02:00
|
|
|
self.stream.write(msg.encode("utf8"))
|
|
|
|
self.stream.write(os.linesep)
|
2003-12-03 23:32:33 +01:00
|
|
|
self.flush()
|
2004-03-25 13:17:48 +01:00
|
|
|
|
2003-12-03 23:32:33 +01:00
|
|
|
|
2003-12-01 16:38:42 +01:00
|
|
|
class ColorizedFormatter(Formatter):
|
|
|
|
def formatException(self, (E, e, tb)):
|
2004-01-18 08:58:26 +01:00
|
|
|
if conf.supybot.log.stdout.colorized():
|
2004-05-07 14:36:57 +02:00
|
|
|
return ''.join([ansi.RED,
|
2003-12-01 16:38:42 +01:00
|
|
|
Formatter.formatException(self, (E, e, tb)),
|
|
|
|
ansi.RESET])
|
|
|
|
else:
|
|
|
|
return Formatter.formatException(self, (E, e, tb))
|
|
|
|
|
|
|
|
def format(self, record, *args, **kwargs):
|
2004-01-18 08:58:26 +01:00
|
|
|
if conf.supybot.log.stdout.colorized():
|
2003-12-01 16:38:42 +01:00
|
|
|
color = ''
|
|
|
|
if record.levelno == logging.CRITICAL:
|
|
|
|
color = ansi.WHITE + ansi.BOLD
|
|
|
|
elif record.levelno == logging.ERROR:
|
|
|
|
color = ansi.RED
|
|
|
|
elif record.levelno == logging.WARNING:
|
|
|
|
color = ansi.YELLOW
|
2004-08-08 19:59:49 +02:00
|
|
|
if color:
|
|
|
|
return ''.join([color,
|
|
|
|
Formatter.format(self, record, *args, **kwargs),
|
|
|
|
ansi.RESET])
|
|
|
|
else:
|
|
|
|
return Formatter.format(self, record, *args, **kwargs)
|
2003-12-01 16:38:42 +01:00
|
|
|
else:
|
|
|
|
return Formatter.format(self, record, *args, **kwargs)
|
|
|
|
|
|
|
|
# These are public.
|
2003-11-26 19:21:12 +01:00
|
|
|
formatter = Formatter('%(levelname)s %(asctime)s %(message)s')
|
|
|
|
pluginFormatter = Formatter('%(levelname)s %(asctime)s %(name)s %(message)s')
|
|
|
|
|
|
|
|
# These are not.
|
2004-05-07 14:36:57 +02:00
|
|
|
logging.setLoggerClass(Logger)
|
2003-11-26 19:21:12 +01:00
|
|
|
_logger = logging.getLogger('supybot')
|
2004-07-20 00:33:58 +02:00
|
|
|
|
|
|
|
# These just make things easier.
|
2003-11-26 19:21:12 +01:00
|
|
|
debug = _logger.debug
|
|
|
|
info = _logger.info
|
|
|
|
warning = _logger.warning
|
|
|
|
error = _logger.error
|
|
|
|
critical = _logger.critical
|
|
|
|
exception = _logger.exception
|
|
|
|
|
2004-08-10 09:39:23 +02:00
|
|
|
def stat(*args):
|
|
|
|
level = conf.supybot.log.statistics()
|
|
|
|
_logger.log(level, *args)
|
|
|
|
|
2004-01-13 19:03:01 +01:00
|
|
|
setLevel = _logger.setLevel
|
|
|
|
|
2003-11-26 19:21:12 +01:00
|
|
|
atexit.register(logging.shutdown)
|
|
|
|
|
2004-07-20 00:33:58 +02:00
|
|
|
# ircutils will work without this, but it's useful.
|
|
|
|
ircutils.debug = debug
|
|
|
|
|
2003-11-26 19:21:12 +01:00
|
|
|
def getPluginLogger(name):
|
2004-04-14 18:06:22 +02:00
|
|
|
if not conf.supybot.log.individualPluginLogfiles():
|
|
|
|
return _logger
|
2003-11-26 19:21:12 +01:00
|
|
|
log = logging.getLogger('supybot.plugins.%s' % name)
|
|
|
|
if not log.handlers:
|
|
|
|
filename = os.path.join(pluginLogDir, '%s.log' % name)
|
2003-12-03 23:32:33 +01:00
|
|
|
handler = BetterFileHandler(filename)
|
2004-01-20 11:46:18 +01:00
|
|
|
handler.setLevel(-1)
|
2003-11-26 19:21:12 +01:00
|
|
|
handler.setFormatter(pluginFormatter)
|
|
|
|
log.addHandler(handler)
|
2004-03-25 13:17:48 +01:00
|
|
|
if name in sys.modules:
|
|
|
|
# Let's log the version, this might be useful.
|
|
|
|
module = sys.modules[name]
|
|
|
|
try:
|
|
|
|
if hasattr(module, '__revision__'):
|
|
|
|
version = module.__revision__.split()[2]
|
|
|
|
log.info('Starting log for %s (revision %s)', name, version)
|
|
|
|
else:
|
|
|
|
debug('Module %s has no __revision__ string.', name)
|
|
|
|
log.info('Starting log for %s.', name)
|
|
|
|
except IndexError:
|
|
|
|
log.debug('Improper __revision__ string in %s.', name)
|
|
|
|
log.info('Starting log for %s.', name)
|
2003-11-26 19:21:12 +01:00
|
|
|
return log
|
|
|
|
|
2004-01-19 21:22:06 +01:00
|
|
|
def timestamp(when=None):
|
|
|
|
if when is None:
|
|
|
|
when = time.time()
|
|
|
|
format = conf.supybot.log.timestampFormat()
|
2004-04-14 04:37:17 +02:00
|
|
|
t = time.localtime(when)
|
|
|
|
if format:
|
|
|
|
return time.strftime(format, t)
|
|
|
|
else:
|
|
|
|
return str(int(time.mktime(t)))
|
2004-01-19 21:22:06 +01:00
|
|
|
|
2004-02-06 10:20:47 +01:00
|
|
|
def firewall(f, errorHandler=None):
|
|
|
|
def logException(self, s=None):
|
|
|
|
if s is None:
|
|
|
|
s = 'Uncaught exception'
|
|
|
|
if hasattr(self, 'log'):
|
|
|
|
self.log.exception('%s:', s)
|
|
|
|
else:
|
|
|
|
exception('%s in %s.%s:', s, self.__class__.__name__, f.func_name)
|
|
|
|
def m(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
return f(self, *args, **kwargs)
|
|
|
|
except Exception, e:
|
2004-08-26 07:23:24 +02:00
|
|
|
if testing:
|
|
|
|
raise
|
2004-02-06 10:20:47 +01:00
|
|
|
logException(self)
|
|
|
|
if errorHandler is not None:
|
|
|
|
try:
|
|
|
|
errorHandler(self, *args, **kwargs)
|
|
|
|
except Exception, e:
|
|
|
|
logException(self, 'Uncaught exception in errorHandler')
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-02-06 10:20:47 +01:00
|
|
|
m = utils.changeFunctionName(m, f.func_name, f.__doc__)
|
|
|
|
return m
|
|
|
|
|
|
|
|
class MetaFirewall(type):
|
|
|
|
def __new__(cls, name, bases, dict):
|
|
|
|
firewalled = {}
|
|
|
|
for base in bases:
|
|
|
|
if hasattr(base, '__firewalled__'):
|
|
|
|
firewalled.update(base.__firewalled__)
|
|
|
|
if '__firewalled__' in dict:
|
|
|
|
firewalled.update(dict['__firewalled__'])
|
|
|
|
for attr in firewalled:
|
|
|
|
if attr in dict:
|
|
|
|
try:
|
|
|
|
errorHandler = firewalled[attr]
|
|
|
|
except:
|
|
|
|
errorHandler = None
|
|
|
|
dict[attr] = firewall(dict[attr], errorHandler)
|
2004-09-16 12:54:59 +02:00
|
|
|
return super(MetaFirewall, cls).__new__(cls, name, bases, dict)
|
|
|
|
#return type.__new__(cls, name, bases, dict)
|
2004-02-06 10:20:47 +01:00
|
|
|
|
2003-11-26 19:21:12 +01:00
|
|
|
|
2004-04-18 08:24:37 +02:00
|
|
|
class ValidLogLevel(registry.String):
|
2004-08-25 06:06:07 +02:00
|
|
|
"""Invalid log level."""
|
2004-04-18 08:24:37 +02:00
|
|
|
minimumLevel = -1
|
2004-01-20 13:10:18 +01:00
|
|
|
def set(self, s):
|
|
|
|
s = s.upper()
|
|
|
|
try:
|
2004-04-18 08:24:37 +02:00
|
|
|
level = getattr(logging, s)
|
2004-01-20 13:10:18 +01:00
|
|
|
except AttributeError:
|
2004-08-25 06:06:07 +02:00
|
|
|
try:
|
|
|
|
level = int(s)
|
|
|
|
except ValueError:
|
|
|
|
self.error()
|
|
|
|
if level < self.minimumLevel:
|
2004-04-13 07:19:56 +02:00
|
|
|
self.error()
|
2004-08-25 06:06:07 +02:00
|
|
|
self.setValue(level)
|
2004-04-13 07:19:56 +02:00
|
|
|
|
2004-01-20 13:10:18 +01:00
|
|
|
def __str__(self):
|
2004-08-25 06:06:07 +02:00
|
|
|
level = logging.getLevelName(self.value)
|
|
|
|
if level.startswith('Level'):
|
|
|
|
level = level.split()[-1]
|
2004-08-25 06:08:42 +02:00
|
|
|
return level
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-04-18 08:24:37 +02:00
|
|
|
class LogLevel(ValidLogLevel):
|
|
|
|
"""Invalid log level. Value must be either DEBUG, INFO, WARNING, ERROR,
|
|
|
|
or CRITICAL."""
|
|
|
|
def setValue(self, v):
|
|
|
|
ValidLogLevel.setValue(self, v)
|
|
|
|
_logger.setLevel(self.value) # _logger defined later.
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-07-23 08:54:29 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot.directories, 'log',
|
2004-09-06 10:19:42 +02:00
|
|
|
conf.Directory('logs', """Determines what directory the bot will store its
|
2004-07-23 08:54:29 +02:00
|
|
|
logfiles in."""))
|
|
|
|
|
|
|
|
conf.registerGroup(conf.supybot, 'log')
|
|
|
|
conf.registerGlobalValue(conf.supybot.log, 'level',
|
|
|
|
LogLevel(logging.INFO, """Determines what the minimum priority level logged
|
|
|
|
will be. Valid values are DEBUG, INFO, WARNING, ERROR, and CRITICAL, in
|
|
|
|
order of increasing priority."""))
|
2004-08-10 09:39:23 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot.log, 'statistics',
|
|
|
|
ValidLogLevel(logging.DEBUG, """Determines what level statistics reporting
|
|
|
|
is to be logged at. Mostly, this just includes, for instance, the time it
|
|
|
|
took to parse a message, process a command, etc. You probably don't care
|
|
|
|
about this."""))
|
2004-07-23 08:54:29 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot.log, 'stdout',
|
|
|
|
registry.Boolean(True, """Determines whether the bot will log to
|
|
|
|
stdout."""))
|
|
|
|
conf.registerGlobalValue(conf.supybot.log, 'individualPluginLogfiles',
|
2004-08-24 00:48:17 +02:00
|
|
|
registry.Boolean(False, """Determines whether the bot will separate plugin
|
2004-07-23 08:54:29 +02:00
|
|
|
logs into their own individual logfiles."""))
|
2004-01-20 13:10:18 +01:00
|
|
|
|
|
|
|
class BooleanRequiredFalseOnWindows(registry.Boolean):
|
|
|
|
def set(self, s):
|
|
|
|
registry.Boolean.set(self, s)
|
|
|
|
if self.value and os.name == 'nt':
|
|
|
|
raise InvalidRegistryValue, 'Value cannot be true on Windows.'
|
2004-07-21 21:36:35 +02:00
|
|
|
|
2004-07-23 08:54:29 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot.log.stdout, 'colorized',
|
|
|
|
BooleanRequiredFalseOnWindows(False, """Determines whether the bot's logs
|
|
|
|
to stdout (if enabled) will be colorized with ANSI color."""))
|
2004-02-03 19:21:19 +01:00
|
|
|
|
2004-08-05 07:40:32 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot.log.stdout, 'wrap',
|
|
|
|
registry.Boolean(True, """Determines whether the bot will wrap its logs
|
|
|
|
when they're output to stdout."""))
|
|
|
|
|
2004-07-23 08:54:29 +02:00
|
|
|
conf.registerGlobalValue(conf.supybot.log, 'timestampFormat',
|
|
|
|
registry.String('[%d-%b-%Y %H:%M:%S]', """Determines the format string for
|
|
|
|
timestamps in logfiles. Refer to the Python documentation for the time
|
|
|
|
module to see what formats are accepted. If you set this variable to the
|
|
|
|
empty string, times will be logged in a simple seconds-since-epoch
|
|
|
|
format."""))
|
2004-01-20 13:10:18 +01:00
|
|
|
|
2004-05-07 14:36:57 +02:00
|
|
|
_logDir = conf.supybot.directories.log()
|
|
|
|
if not os.path.exists(_logDir):
|
|
|
|
os.mkdir(_logDir, 0755)
|
|
|
|
|
|
|
|
pluginLogDir = os.path.join(_logDir, 'plugins')
|
2004-01-20 13:10:18 +01:00
|
|
|
|
|
|
|
if not os.path.exists(pluginLogDir):
|
|
|
|
os.mkdir(pluginLogDir, 0755)
|
|
|
|
|
2004-05-07 14:36:57 +02:00
|
|
|
_handler = BetterFileHandler(os.path.join(_logDir, 'misc.log'))
|
2004-01-20 13:10:18 +01:00
|
|
|
_handler.setFormatter(formatter)
|
2004-04-13 02:59:19 +02:00
|
|
|
_handler.setLevel(-1)
|
2004-01-20 13:10:18 +01:00
|
|
|
_logger.addHandler(_handler)
|
2004-04-13 02:59:19 +02:00
|
|
|
_logger.setLevel(conf.supybot.log.level())
|
2004-01-20 13:10:18 +01:00
|
|
|
|
2004-04-18 07:56:31 +02:00
|
|
|
if not conf.daemonized:
|
|
|
|
_stdoutHandler = StdoutStreamHandler(sys.stdout)
|
|
|
|
_formatString = '%(name)s: %(levelname)s %(message)s'
|
|
|
|
_stdoutFormatter = ColorizedFormatter(_formatString)
|
|
|
|
_stdoutHandler.setFormatter(_stdoutFormatter)
|
|
|
|
_stdoutHandler.setLevel(-1)
|
|
|
|
_logger.addHandler(_stdoutHandler)
|
2004-01-20 13:10:18 +01:00
|
|
|
|
2004-04-12 01:12:36 +02:00
|
|
|
|
2003-11-26 19:21:12 +01:00
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
|
|
|
|