From 547ebe941b10d06a23c0d79ff254ae1ed6ef8c3e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 2 Jun 2015 14:11:16 +0530 Subject: [PATCH] Fix locking thread safe log objects' exception() Also fix the default stream ignoring the stream parameter to its constructor. Change RLock to Lock for better performance, since locking recursion is not needed --- src/calibre/utils/logging.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/calibre/utils/logging.py b/src/calibre/utils/logging.py index c557ee6790..2296d47cf8 100644 --- a/src/calibre/utils/logging.py +++ b/src/calibre/utils/logging.py @@ -12,18 +12,17 @@ ERROR = 3 import sys, traceback, cStringIO from functools import partial -from threading import RLock +from threading import Lock -from calibre import isbytestring, force_unicode, as_unicode +from calibre import isbytestring, force_unicode, as_unicode, prints class Stream(object): def __init__(self, stream=None): - from calibre import prints - self._prints = partial(prints, safe_encode=True) if stream is None: stream = cStringIO.StringIO() self.stream = stream + self._prints = partial(prints, safe_encode=True, file=stream) def flush(self): self.stream.flush() @@ -53,7 +52,6 @@ class FileStream(Stream): Stream.__init__(self, stream) def prints(self, level, *args, **kwargs): - kwargs['file'] = self.stream self._prints(*args, **kwargs) class HTMLStream(Stream): @@ -171,18 +169,24 @@ class ThreadSafeLog(Log): def __init__(self, level=Log.INFO): Log.__init__(self, level=level) - self._lock = RLock() + self._lock = Lock() def prints(self, *args, **kwargs): with self._lock: Log.prints(self, *args, **kwargs) + def exception(self, *args, **kwargs): + limit = kwargs.pop('limit', None) + with self._lock: + Log.prints(self, ERROR, *args, **kwargs) + Log.prints(self, DEBUG, traceback.format_exc(limit)) + class ThreadSafeWrapper(Log): def __init__(self, other_log): Log.__init__(self, level=other_log.filter_level) self.outputs = list(other_log.outputs) - self._lock = RLock() + self._lock = Lock() def prints(self, *args, **kwargs): with self._lock: