From a06c92d285d4c53a064d09f3e1b4b62d5cd3dbd8 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sat, 17 Mar 2018 01:10:48 +0545 Subject: [PATCH] core: enable_debug_logging() should reopen file post-fork. --- mitogen/core.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/mitogen/core.py b/mitogen/core.py index 1d7f55b5..151d19d8 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -212,15 +212,36 @@ def io_op(func, *args): return None, True +class PidfulStreamHandler(logging.StreamHandler): + open_pid = None + template = '/tmp/mitogen.%s.%s.log' + + def _reopen(self): + self.acquire() + try: + ts = time.strftime('%Y%m%d_%H%M%S') + path = self.template % (os.getpid(), ts) + self.stream = open(path, 'w', 1) + set_cloexec(self.stream.fileno()) + self.stream.write('Parent PID: %s\n' % (os.getppid(),)) + self.stream.write('Created by:\n\n%s\n' % ( + ''.join(traceback.format_stack()), + )) + self.open_pid = os.getpid() + finally: + self.release() + + def emit(self, record): + if self.open_pid != os.getpid(): + self._reopen() + return super(PidfulStreamHandler, self).emit(record) + + def enable_debug_logging(): root = logging.getLogger() root.setLevel(logging.DEBUG) IOLOG.setLevel(logging.DEBUG) - fp = open('/tmp/mitogen.%s.log' % (os.getpid(),), 'w', 1) - fp.write('Parent PID: %s\n' % (os.getppid(),)) - fp.write('Created by:\n\n%s\n\n' % (''.join(traceback.format_stack()),)) - set_cloexec(fp.fileno()) - handler = logging.StreamHandler(fp) + handler = PidfulStreamHandler() handler.formatter = logging.Formatter( '%(asctime)s %(levelname).1s %(name)s: %(message)s', '%H:%M:%S'