logutil.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:pykit 作者: baishancloud 项目源码 文件源码
def emit(self, record):
        """
        Emit a record.

        First check if the underlying file has changed, and if it
        has, close the old stream and reopen the file to get the
        current stream.
        """
        # Reduce the chance of race conditions by stat'ing by path only
        # once and then fstat'ing our new fd if we opened a new log stream.
        # See issue #14632: Thanks to John Mulligan for the problem report
        # and patch.
        try:
            # stat the file by path, checking for existence
            sres = os.stat(self.baseFilename)
        except OSError as e:
            if e.errno == errno.ENOENT:
                sres = None
            else:
                raise
        # compare file system stat with that of our stream file handle
        if not sres or sres[ST_DEV] != self.dev or sres[ST_INO] != self.ino:

            # Fixed by xp 2017 Apr 03:
            #     os.fstat still gets OSError(errno=2), although it operates
            #     directly on fd instead of path.  The same for stream.flush().
            #     Thus we keep on trying this close/open/stat loop until no
            #     OSError raises.

            for ii in range(16):
                try:
                    if self.stream is not None:
                        # we have an open file handle, clean it up
                        self.stream.flush()
                        self.stream.close()
                        # See Issue #21742: _open () might fail.
                        self.stream = None
                        # open a new file handle and get new stat info from
                        # that fd
                        self.stream = self._open()
                        self._statstream()
                        break
                except OSError as e:
                    if e.errno == errno.ENOENT:
                        continue
                    else:
                        raise
        logging.FileHandler.emit(self, record)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号