cpython: b2adcd90e656 (original) (raw)
Mercurial > cpython
changeset 75211:b2adcd90e656 3.2
logging: Added locking in flush() and close() handler methods. Thanks to Fayaz Yusuf Khan for the suggestion.
Vinay Sajip <vinay_sajip@yahoo.co.uk> | |
---|---|
date | Thu, 23 Feb 2012 19:45:52 +0000 |
parents | 85d08a1ba74e |
children | cb9a2dff6240 c06bcfbbf123 344b4737d2fe |
files | Lib/logging/__init__.py Lib/logging/handlers.py |
diffstat | 2 files changed, 35 insertions(+), 29 deletions(-)[+] [-] Lib/logging/__init__.py 24 Lib/logging/handlers.py 40 |
line wrap: on
line diff
--- a/Lib/logging/init.py +++ b/Lib/logging/init.py @@ -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
documentation for any purpose and without fee is hereby granted,
@@ -16,9 +16,9 @@
"""
Logging package for Python. Based on PEP 282 and comments thereto in
-comp.lang.python, and influenced by Apache's log4j system.
+comp.lang.python.
-Copyright (C) 2001-2011 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
To use, simply 'import logging' and log away
"""
@@ -917,8 +917,9 @@ class StreamHandler(Handler):
"""
Flushes the stream.
"""
if self.stream and hasattr(self.stream, "flush"):[](#l1.25)
self.stream.flush()[](#l1.26)
with self.lock:[](#l1.27)
if self.stream and hasattr(self.stream, "flush"):[](#l1.28)
self.stream.flush()[](#l1.29)
def emit(self, record): """ @@ -969,12 +970,13 @@ class FileHandler(StreamHandler): """ Closes the stream. """
if self.stream:[](#l1.37)
self.flush()[](#l1.38)
if hasattr(self.stream, "close"):[](#l1.39)
self.stream.close()[](#l1.40)
StreamHandler.close(self)[](#l1.41)
self.stream = None[](#l1.42)
with self.lock:[](#l1.43)
if self.stream:[](#l1.44)
self.flush()[](#l1.45)
if hasattr(self.stream, "close"):[](#l1.46)
self.stream.close()[](#l1.47)
StreamHandler.close(self)[](#l1.48)
self.stream = None[](#l1.49)
--- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -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
documentation for any purpose and without fee is hereby granted,
@@ -16,10 +16,9 @@
"""
Additional handlers for the logging package for Python. The core package is
-based on PEP 282 and comments thereto in comp.lang.python, and influenced by
-Apache's log4j system.
+based on PEP 282 and comments thereto in comp.lang.python.
-Copyright (C) 2001-2010 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
To use, simply 'import logging.handlers' and log away
"""
@@ -554,10 +553,11 @@ class SocketHandler(logging.Handler):
"""
Closes the socket.
"""
if self.sock:[](#l2.26)
self.sock.close()[](#l2.27)
self.sock = None[](#l2.28)
logging.Handler.close(self)[](#l2.29)
with self.lock:[](#l2.30)
if self.sock:[](#l2.31)
self.sock.close()[](#l2.32)
self.sock = None[](#l2.33)
logging.Handler.close(self)[](#l2.34)
class DatagramHandler(SocketHandler): """ @@ -752,9 +752,10 @@ class SysLogHandler(logging.Handler): """ Closes the socket. """
if self.unixsocket:[](#l2.42)
self.socket.close()[](#l2.43)
logging.Handler.close(self)[](#l2.44)
with self.lock:[](#l2.45)
if self.unixsocket:[](#l2.46)
self.socket.close()[](#l2.47)
logging.Handler.close(self)[](#l2.48)
def mapPriority(self, levelName): """ @@ -1095,7 +1096,8 @@ class BufferingHandler(logging.Handler): This version just zaps the buffer to empty. """
self.buffer = [][](#l2.56)
with self.lock:[](#l2.57)
self.buffer = [][](#l2.58)
def close(self): """ @@ -1145,18 +1147,20 @@ class MemoryHandler(BufferingHandler): The record buffer is also cleared by this operation. """
if self.target:[](#l2.66)
for record in self.buffer:[](#l2.67)
self.target.handle(record)[](#l2.68)
self.buffer = [][](#l2.69)
with self.lock:[](#l2.70)
if self.target:[](#l2.71)
for record in self.buffer:[](#l2.72)
self.target.handle(record)[](#l2.73)
self.buffer = [][](#l2.74)
def close(self): """ Flush, set the target to None and lose the buffer. """ self.flush()
self.target = None[](#l2.81)
BufferingHandler.close(self)[](#l2.82)
with self.lock:[](#l2.83)
self.target = None[](#l2.84)
BufferingHandler.close(self)[](#l2.85)