logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.

This commit is contained in:
Vinay Sajip 2012-02-23 19:37:18 +00:00
parent 62cc44db0c
commit d23845e270
2 changed files with 29 additions and 22 deletions

View file

@ -1,4 +1,4 @@
# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved. # Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
# #
# Permission to use, copy, modify, and distribute this software and its # Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, # documentation for any purpose and without fee is hereby granted,
@ -828,8 +828,9 @@ class StreamHandler(Handler):
""" """
Flushes the stream. Flushes the stream.
""" """
if self.stream and hasattr(self.stream, "flush"): with self.lock:
self.stream.flush() if self.stream and hasattr(self.stream, "flush"):
self.stream.flush()
def emit(self, record): def emit(self, record):
""" """
@ -900,12 +901,13 @@ class FileHandler(StreamHandler):
""" """
Closes the stream. Closes the stream.
""" """
if self.stream: with self.lock:
self.flush() if self.stream:
if hasattr(self.stream, "close"): self.flush()
self.stream.close() if hasattr(self.stream, "close"):
StreamHandler.close(self) self.stream.close()
self.stream = None StreamHandler.close(self)
self.stream = None
def _open(self): def _open(self):
""" """

View file

@ -1,4 +1,4 @@
# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved. # Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
# #
# Permission to use, copy, modify, and distribute this software and its # Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, # documentation for any purpose and without fee is hereby granted,
@ -563,9 +563,10 @@ class SocketHandler(logging.Handler):
""" """
Closes the socket. Closes the socket.
""" """
if self.sock: with self.lock:
self.sock.close() if self.sock:
self.sock = None self.sock.close()
self.sock = None
logging.Handler.close(self) logging.Handler.close(self)
class DatagramHandler(SocketHandler): class DatagramHandler(SocketHandler):
@ -767,8 +768,9 @@ class SysLogHandler(logging.Handler):
""" """
Closes the socket. Closes the socket.
""" """
if self.unixsocket: with self.lock:
self.socket.close() if self.unixsocket:
self.socket.close()
logging.Handler.close(self) logging.Handler.close(self)
def mapPriority(self, levelName): def mapPriority(self, levelName):
@ -1096,7 +1098,8 @@ class BufferingHandler(logging.Handler):
This version just zaps the buffer to empty. This version just zaps the buffer to empty.
""" """
self.buffer = [] with self.lock:
self.buffer = []
def close(self): def close(self):
""" """
@ -1144,15 +1147,17 @@ class MemoryHandler(BufferingHandler):
records to the target, if there is one. Override if you want records to the target, if there is one. Override if you want
different behaviour. different behaviour.
""" """
if self.target: with self.lock:
for record in self.buffer: if self.target:
self.target.handle(record) for record in self.buffer:
self.buffer = [] self.target.handle(record)
self.buffer = []
def close(self): def close(self):
""" """
Flush, set the target to None and lose the buffer. Flush, set the target to None and lose the buffer.
""" """
self.flush() self.flush()
self.target = None with self.lock:
BufferingHandler.close(self) self.target = None
BufferingHandler.close(self)