3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-01 09:19:23 +01:00

log: attempt to remedy #164 (more testing needed)

This commit is contained in:
James Lu 2016-02-20 19:28:45 -08:00
parent 40d76c8bb6
commit eec8e0dca4

30
log.py
View File

@ -62,6 +62,10 @@ class PyLinkChannelLogger(logging.Handler):
self.irc = irc self.irc = irc
self.channel = channel self.channel = channel
# Set whether we've been called already. This is used to prevent recursive
# loops when logging.
self.called = False
# Use a slightly simpler message formatter - logging to IRC doesn't need # Use a slightly simpler message formatter - logging to IRC doesn't need
# logging the time. # logging the time.
formatter = logging.Formatter('[%(levelname)s] %(message)s') formatter = logging.Formatter('[%(levelname)s] %(message)s')
@ -81,8 +85,26 @@ class PyLinkChannelLogger(logging.Handler):
""" """
Logs a record to the configured channels for the network given. Logs a record to the configured channels for the network given.
""" """
# Only start logging if we're finished bursting # Only start logging if we're finished bursting, and our main client is in
if hasattr(self.irc, 'pseudoclient') and self.irc.connected.is_set(): # a stable condition.
msg = self.format(record) # 1) irc.pseudoclient must be initialized already
self.irc.msg(self.channel, msg) # 2) IRC object must be finished bursting
# 3) Target channel must exist
# 4) Main PyLink client must be in this target channel
# 5) This function hasn't been called already (prevents recursive loops).
if hasattr(self.irc, 'pseudoclient') and self.irc.connected.is_set() \
and self.channel in self.irc.channels and self.irc.pseudoclient.uid in \
self.irc.channels[self.channel].users and not self.called:
self.called = True
msg = self.format(record)
# Send the message. If this fails, abort. No more messages will be
# sent from this logger until the next sending succeeds.
try:
self.irc.msg(self.channel, msg)
except:
return
else:
self.called = False