23
23
To use, simply 'import logging.handlers' and log away!
24
24
"""
25
25
26
- import io , logging , socket , os , pickle , struct , time , re
27
- from stat import ST_DEV , ST_INO , ST_MTIME
26
+ import copy
27
+ import io
28
+ import logging
29
+ import os
30
+ import pickle
28
31
import queue
32
+ import re
33
+ import socket
34
+ import struct
29
35
import threading
30
- import copy
36
+ import time
31
37
32
38
#
33
39
# Some constants...
@@ -269,7 +275,7 @@ def __init__(self, filename, when='h', interval=1, backupCount=0,
269
275
# path object (see Issue #27493), but self.baseFilename will be a string
270
276
filename = self .baseFilename
271
277
if os .path .exists (filename ):
272
- t = os .stat (filename )[ ST_MTIME ]
278
+ t = int ( os .stat (filename ). st_mtime )
273
279
else :
274
280
t = int (time .time ())
275
281
self .rolloverAt = self .computeRollover (t )
@@ -459,8 +465,7 @@ class WatchedFileHandler(logging.FileHandler):
459
465
This handler is not appropriate for use under Windows, because
460
466
under Windows open files cannot be moved or renamed - logging
461
467
opens the files with exclusive locks - and so there is no need
462
- for such a handler. Furthermore, ST_INO is not supported under
463
- Windows; stat always returns zero for this value.
468
+ for such a handler.
464
469
465
470
This handler is based on a suggestion and patch by Chad J.
466
471
Schroeder.
@@ -476,9 +481,11 @@ def __init__(self, filename, mode='a', encoding=None, delay=False,
476
481
self ._statstream ()
477
482
478
483
def _statstream (self ):
479
- if self .stream :
480
- sres = os .fstat (self .stream .fileno ())
481
- self .dev , self .ino = sres [ST_DEV ], sres [ST_INO ]
484
+ if self .stream is None :
485
+ return
486
+ sres = os .fstat (self .stream .fileno ())
8000
487
+ self .dev = sres .st_dev
488
+ self .ino = sres .st_ino
482
489
483
490
def reopenIfNeeded (self ):
484
491
"""
@@ -488,25 +495,33 @@ def reopenIfNeeded(self):
488
495
has, close the old stream and reopen the file to get the
489
496
current stream.
490
497
"""
498
+ if self .stream is None :
499
+ return
500
+
491
501
# Reduce the chance of race conditions by stat'ing by path only
492
502
# once and then fstat'ing our new fd if we opened a new log stream.
493
503
# See issue #14632: Thanks to John Mulligan for the problem report
494
504
# and patch.
495
505
try :
496
506
# stat the file by path, checking for existence
497
507
sres = os .stat (self .baseFilename )
508
+
509
+ # compare file system stat with that of our stream file handle
510
+ reopen = (sres .st_dev != self .dev or sres .st_ino != self .ino )
498
511
except FileNotFoundError :
499
- sres = None
500
- # compare file system stat with that of our stream file handle
501
- if not sres or sres [ST_DEV ] != self .dev or sres [ST_INO ] != self .ino :
502
- if self .stream is not None :
503
- # we have an open file handle, clean it up
504
- self .stream .flush ()
505
- self .stream .close ()
506
- self .stream = None # See Issue #21742: _open () might fail.
507
- # open a new file handle and get new stat info from that fd
508
- self .stream = self ._open ()
509
- self ._statstream ()
512
+ reopen = True
513
+
514
+ if not reopen :
515
+ return
516
+
517
+ # we have an open file handle, clean it up
518
+ self .stream .flush ()
519
+ self .stream .close ()
520
+ self .stream = None # See Issue #21742: _open () might fail.
521
+
522
+ # open a new file handle and get new stat info from that fd
523
+ self .stream = self ._open ()
524
+ self ._statstream ()
510
525
511
526
def emit (self , record ):
512
527
"""
0 commit comments