2323To use, simply 'import logging.handlers' and log away!
2424"""
2525
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
2831import queue
32+ import re
33+ import socket
34+ import struct
2935import threading
30- import copy
36+ import time
3137
3238#
3339# Some constants...
@@ -269,7 +275,7 @@ def __init__(self, filename, when='h', interval=1, backupCount=0,
269275 # path object (see Issue #27493), but self.baseFilename will be a string
270276 filename = self .baseFilename
271277 if os .path .exists (filename ):
272- t = os .stat (filename )[ ST_MTIME ]
278+ t = int ( os .stat (filename ). st_mtime )
273279 else :
274280 t = int (time .time ())
275281 self .rolloverAt = self .computeRollover (t )
@@ -459,8 +465,7 @@ class WatchedFileHandler(logging.FileHandler):
459465 This handler is not appropriate for use under Windows, because
460466 under Windows open files cannot be moved or renamed - logging
461467 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.
464469
465470 This handler is based on a suggestion and patch by Chad J.
466471 Schroeder.
@@ -476,9 +481,11 @@ def __init__(self, filename, mode='a', encoding=None, delay=False,
476481 self ._statstream ()
477482
478483 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 ())
487+ self .dev = sres .st_dev
488+ self .ino = sres .st_ino
482489
483490 def reopenIfNeeded (self ):
484491 """
@@ -488,25 +495,33 @@ def reopenIfNeeded(self):
488495 has, close the old stream and reopen the file to get the
489496 current stream.
490497 """
498+ if self .stream is None :
499+ return
500+
491501 # Reduce the chance of race conditions by stat'ing by path only
492502 # once and then fstat'ing our new fd if we opened a new log stream.
493503 # See issue #14632: Thanks to John Mulligan for the problem report
494504 # and patch.
495505 try :
496506 # stat the file by path, checking for existence
497507 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 )
498511 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 ()
510525
511526 def emit (self , record ):
512527 """
0 commit comments