未验证 提交 1c8e9f93 编写于 作者: C Cleber Rosa

Merge remote-tracking branch 'clebergnu/fddrainer_flush_fileno_v2'

Signed-off-by: NCleber Rosa <crosa@redhat.com>
......@@ -31,7 +31,7 @@ import sys
import threading
import time
from io import BytesIO
from io import BytesIO, UnsupportedOperation
from six import string_types
from . import gdb
......@@ -419,7 +419,12 @@ class FDDrainer(object):
# the same interface, so let's try to use it if available
stream = getattr(handler, 'stream', None)
if (stream is not None) and (not stream.closed):
os.fsync(stream.fileno())
if hasattr(stream, 'fileno'):
try:
fileno = stream.fileno()
os.fsync(fileno)
except UnsupportedOperation:
pass
if hasattr(handler, 'close'):
handler.close()
......
import io
import logging
import os
import unittest
......@@ -305,6 +306,41 @@ class FDDrainerTests(unittest.TestCase):
b"should go to the log\n")
self.assertTrue(handler.caught_record)
def test_flush_on_closed_handler(self):
handler = logging.StreamHandler(io.StringIO())
log = logging.getLogger("test_flush_on_closed_handler")
log.addHandler(handler)
read_fd, write_fd = os.pipe()
result = process.CmdResult()
fd_drainer = process.FDDrainer(read_fd, result, name="test",
stream_logger=log)
fd_drainer.start()
os.close(write_fd)
self.assertIsNotNone(fd_drainer._stream_logger)
one_stream_closed = False
for handler in fd_drainer._stream_logger.handlers:
stream = getattr(handler, 'stream', None)
if stream is not None:
if hasattr(stream, 'close'):
# force closing the handler's stream to check if
# flush will adapt to it
stream.close()
one_stream_closed = True
self.assertTrue(one_stream_closed)
fd_drainer.flush()
def test_flush_on_handler_with_no_fileno(self):
handler = logging.StreamHandler(io.StringIO())
log = logging.getLogger("test_flush_on_handler_with_no_fileno")
log.addHandler(handler)
read_fd, write_fd = os.pipe()
result = process.CmdResult()
fd_drainer = process.FDDrainer(read_fd, result, name="test",
stream_logger=log)
fd_drainer.start()
os.close(write_fd)
fd_drainer.flush()
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册