Changed the name from LogToChannel to LogToIrc and made it capable of message nicks and not just channels.

This commit is contained in:
Jeremy Fincher 2004-04-14 09:31:15 +00:00
parent a4bf228a45
commit b15b85ea7c
2 changed files with 40 additions and 34 deletions

View File

@ -1,3 +1,6 @@
* Added the LogToIrc plugin, for sending logs to an IRC
channel or nick. Useful for traceback notification and whatnot.
* Changed supybot.log.timestampFormat to specially handle the * Changed supybot.log.timestampFormat to specially handle the
empty string -- if it's set to the empty string, it will log empty string -- if it's set to the empty string, it will log
times in seconds-since-epoch format. times in seconds-since-epoch format.

View File

@ -31,7 +31,7 @@
### ###
""" """
Allows for sending the bot's logging output to a channel. Allows for sending the bot's logging output to a channel or nick.
""" """
__revision__ = "$Id$" __revision__ = "$Id$"
@ -53,13 +53,13 @@ import callbacks
class IrcHandler(logging.Handler): class IrcHandler(logging.Handler):
def emit(self, record): def emit(self, record):
channel = conf.supybot.plugins.LogToChannel.channel() target = conf.supybot.plugins.LogToIrc.target()
try: try:
s = utils.normalizeWhitespace(self.format(record)) s = utils.normalizeWhitespace(self.format(record))
except: except:
self.handleError(record) self.handleError(record)
msg = ircmsgs.privmsg(channel, s) msg = ircmsgs.privmsg(target, s)
if channel: if target:
for irc in world.ircs: for irc in world.ircs:
try: try:
if not irc.driver.connected: if not irc.driver.connected:
@ -67,7 +67,8 @@ class IrcHandler(logging.Handler):
except AttributeError, e: except AttributeError, e:
print '*** AttributeError, shouldn\'t happen: %s' % e print '*** AttributeError, shouldn\'t happen: %s' % e
continue continue
if channel in irc.state.channels: if target in irc.state.channels or \
target in irc.state.nicksToHostmasks:
irc.queueMsg(msg) irc.queueMsg(msg)
@ -84,7 +85,7 @@ class IrcFormatter(log.Formatter):
class ColorizedIrcFormatter(IrcFormatter): class ColorizedIrcFormatter(IrcFormatter):
def formatException(self, (E, e, tb)): def formatException(self, (E, e, tb)):
if conf.supybot.plugins.LogToChannel.colorized(): if conf.supybot.plugins.LogToIrc.colorized():
return ircutils.bold(ircutils.mircColor( return ircutils.bold(ircutils.mircColor(
IrcFormatter.formatException(self, (E, e, tb)), IrcFormatter.formatException(self, (E, e, tb)),
fg='red')) fg='red'))
@ -93,7 +94,7 @@ class ColorizedIrcFormatter(IrcFormatter):
def format(self, record, *args, **kwargs): def format(self, record, *args, **kwargs):
s = IrcFormatter.format(self, record, *args, **kwargs) s = IrcFormatter.format(self, record, *args, **kwargs)
if conf.supybot.plugins.LogToChannel.colorized(): if conf.supybot.plugins.LogToIrc.colorized():
if record.levelno == logging.CRITICAL: if record.levelno == logging.CRITICAL:
s = ircutils.bold(ircutils.mircColor(s, fg='red')) s = ircutils.bold(ircutils.mircColor(s, fg='red'))
elif record.levelno == logging.ERROR: elif record.levelno == logging.ERROR:
@ -108,9 +109,8 @@ _formatString = '%(name)s: %(levelname)s %(message)s'
_ircFormatter = ColorizedIrcFormatter(_formatString) _ircFormatter = ColorizedIrcFormatter(_formatString)
_ircHandler.setFormatter(_ircFormatter) _ircHandler.setFormatter(_ircFormatter)
class ChannelLogLevel(log.LogLevel): class IrcLogLevel(log.LogLevel):
"""Invalid log level. Value must be either INFO, WARNING, ERROR, """Value must be one of INFO, WARNING, ERROR, or CRITICAL."""
or CRITICAL."""
def setValue(self, v): def setValue(self, v):
if v <= logging.DEBUG: if v <= logging.DEBUG:
self.error() self.error()
@ -118,51 +118,53 @@ class ChannelLogLevel(log.LogLevel):
log.LogLevel.setValue(self, v) log.LogLevel.setValue(self, v)
_ircHandler.setLevel(v) _ircHandler.setLevel(v)
class ValidChannelOrNot(conf.ValidChannel): class ValidChannelOrNickOrNot(registry.String):
"""Value must be a valid channel, a valid nick, or an empty string."""
def setValue(self, v): def setValue(self, v):
if v: if v:
conf.ValidChannel.setValue(self, v) if not (ircutils.isNick(v) or ircutils.isChannel(v)):
else: self.error()
registry.Value.setValue(self, '') registry.String.setValue(self, v)
conf.registerPlugin('LogToChannel') conf.registerPlugin('LogToIrc')
conf.registerGlobalValue(conf.supybot.plugins.LogToChannel, 'level', conf.registerGlobalValue(conf.supybot.plugins.LogToIrc, 'level',
ChannelLogLevel(logging.WARNING, """Determines what the minimum priority IrcLogLevel(logging.WARNING, """Determines what the minimum priority
level logged will be to IRC. See supybot.log.level for possible level logged will be to IRC. See supybot.log.level for possible
values. DEBUG is disabled due to the large quantity of output.""")) values. DEBUG is disabled due to the large quantity of output."""))
conf.registerGlobalValue(conf.supybot.plugins.LogToChannel, 'channel', conf.registerGlobalValue(conf.supybot.plugins.LogToIrc, 'target',
ValidChannelOrNot('', """Determines which channel the bot should log to or ValidChannelOrNickOrNot('', """Determines which channel/nick the bot should
empty if none at all.""")) log to. If no channel/nick is set, this plugin will be effectively
conf.registerGlobalValue(conf.supybot.plugins.LogToChannel, 'colorized', off."""))
conf.registerGlobalValue(conf.supybot.plugins.LogToIrc, 'colorized',
registry.Boolean(False, """Determines whether the bot's logs registry.Boolean(False, """Determines whether the bot's logs
to IRC will be colorized with mIRC colors.""")) to IRC will be colorized with mIRC colors."""))
def configure(advanced): def configure(advanced):
from questions import something, anything, yn, output from questions import something, anything, yn, output
channel = '' target = ''
while not channel: while not target:
try: try:
channel = anything('Which channel would you like to send log ' target = anything('Which channel would you like to send log '
'messages too?') 'messages too?')
conf.supybot.plugins.LogToChannel.channel.set(channel) conf.supybot.plugins.LogToIrc.target.set(target)
except registry.InvalidRegistryValue, e: except registry.InvalidRegistryValue, e:
output(str(e)) output(str(e))
channel = '' target = ''
colorized = yn('Would you like these messages to be colored?') colorized = yn('Would you like these messages to be colored?')
conf.supybot.plugins.LogToChannel.colorized.setValue(colorized) conf.supybot.plugins.LogToIrc.colorized.setValue(colorized)
if advanced: if advanced:
level = '' level = ''
while not level: while not level:
try: try:
level = something('What would you like the minimum priority ' level = something('What would you like the minimum priority '
'level to be which will be logged to IRC?') 'level to be which will be logged to IRC?')
conf.supybot.plugins.LogToChannel.level.set(level) conf.supybot.plugins.LogToIrc.level.set(level)
except registry.InvalidRegistryValue, e: except registry.InvalidRegistryValue, e:
output(str(e)) output(str(e))
level = '' level = ''
class LogToChannel(callbacks.Privmsg): class LogToIrc(callbacks.Privmsg):
threaded = True threaded = True
def __init__(self): def __init__(self):
callbacks.Privmsg.__init__(self) callbacks.Privmsg.__init__(self)
@ -172,11 +174,12 @@ class LogToChannel(callbacks.Privmsg):
log._logger.removeHandler(_ircHandler) log._logger.removeHandler(_ircHandler)
def do376(self, irc, msg): def do376(self, irc, msg):
channel = self.registryValue('channel') target = self.registryValue('target')
if channel: if target and ircutils.isChannel(target):
irc.queueMsg(ircmsgs.join(channel)) irc.queueMsg(ircmsgs.join(target))
do377 = do422 = do376
Class = LogToChannel Class = LogToIrc
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: