Skip to content

Commit 8ade670

Browse files
Slight code reorganization on recent lock file changes.
1 parent 1c4dd87 commit 8ade670

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/concurrent_log_handler/__init__.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
# Random numbers for rotation temp file names, using secrets module if available (Python 3.6).
7676
# Otherwise use `random.SystemRandom` if available, then fall back on `random.Random`.
7777
try:
78-
# noinspection PyPackageRequirements
78+
# noinspection PyPackageRequirements,PyCompatibility
7979
from secrets import randbits
8080
except ImportError:
8181
import random
@@ -185,12 +185,15 @@ def __init__(self, filename, mode='a', maxBytes=0, backupCount=0,
185185
self._set_uid = pwd.getpwnam(self.owner[0]).pw_uid
186186
self._set_gid = grp.getgrnam(self.owner[1]).gr_gid
187187

188+
self.lockFilename = self.getLockFilename()
188189

189-
def _open_lockfile(self):
190-
if self.stream_lock and not self.stream_lock.closed:
191-
self._console_log("Lockfile already open in this process")
192-
return
193-
# Use 'file.lock' and not 'file.log.lock' (Only handles the normal "*.log" case.)
190+
def getLockFilename(self):
191+
"""
192+
Decide the lock filename. If the logfile is file.log, then we use `.__file.lock` and
193+
not `file.log.lock`. This only removes the extension if it's `*.log`.
194+
195+
:return: the path to the lock file.
196+
"""
194197
if self.baseFilename.endswith(".log"):
195198
lock_file = self.baseFilename[:-4]
196199
else:
@@ -199,7 +202,13 @@ def _open_lockfile(self):
199202
lock_path, lock_name = os.path.split(lock_file)
200203
# hide the file on Unix and generally from file completion
201204
lock_name = ".__" + lock_name
202-
lock_file = os.path.join(lock_path, lock_name)
205+
return os.path.join(lock_path, lock_name)
206+
207+
def _open_lockfile(self):
208+
if self.stream_lock and not self.stream_lock.closed:
209+
self._console_log("Lockfile already open in this process")
210+
return
211+
lock_file = self.lockFilename
203212
self._console_log(
204213
"concurrent-log-handler %s opening %s" % (hash(self), lock_file), stack=False)
205214
self.stream_lock = open(lock_file, "wb", buffering=0)
@@ -266,8 +275,6 @@ def emit(self, record):
266275
try:
267276
msg = self.format(record)
268277
try:
269-
self._open_lockfile()
270-
271278
self._do_lock()
272279

273280
try:
@@ -281,12 +288,6 @@ def emit(self, record):
281288

282289
finally:
283290
self._do_unlock()
284-
if self.stream_lock:
285-
unlock(self.stream_lock)
286-
self.stream_lock.close()
287-
self.stream_lock = None
288-
289-
290291
except Exception:
291292
self.handleError(record)
292293

@@ -307,6 +308,7 @@ def do_write(self, msg):
307308
return
308309

309310
def _do_lock(self):
311+
self._open_lockfile()
310312
if self.stream_lock:
311313
lock(self.stream_lock, LOCK_EX)
312314
else:
@@ -315,6 +317,8 @@ def _do_lock(self):
315317
def _do_unlock(self):
316318
if self.stream_lock:
317319
unlock(self.stream_lock)
320+
self.stream_lock.close()
321+
self.stream_lock = None
318322
else:
319323
self._console_log("No self.stream_lock to unlock", stack=True)
320324

0 commit comments

Comments
 (0)