From 17c2fe68f2cf53c1ead0169aa07082c9b07f2a6e Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 24 Aug 2017 15:56:55 +0530 Subject: [PATCH] Add Router.enable_debug() rather than cutpasting snippets constantly. --- econtext/core.py | 26 +++++++++++++++++++++----- econtext/master.py | 19 ++++++++++++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/econtext/core.py b/econtext/core.py index cce2b2e4..8f99528a 100644 --- a/econtext/core.py +++ b/econtext/core.py @@ -101,6 +101,20 @@ def set_cloexec(fd): fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC) +def enable_debug_logging(): + root = logging.getLogger() + root.setLevel(logging.DEBUG) + IOLOG.setLevel(logging.DEBUG) + fp = open('/tmp/econtext.%s.log' % (os.getpid(),), 'w', 1) + set_cloexec(fp.fileno()) + handler = logging.StreamHandler(fp) + handler.formatter = logging.Formatter( + '%(asctime)s %(levelname).1s %(name)s: %(message)s', + '%H:%M:%S' + ) + root.handlers.insert(0, handler) + + class Message(object): dst_id = None src_id = None @@ -946,11 +960,12 @@ class ExternalContext(object): os.wait() # Reap first stage. os.close(100) - def _setup_logging(self, log_level): - logging.basicConfig(level=log_level) + def _setup_logging(self, debug, log_level): root = logging.getLogger() root.setLevel(log_level) root.handlers = [LogHandler(self.context)] + if debug: + enable_debug_logging() def _setup_importer(self): with os.fdopen(101, 'r', 1) as fp: @@ -1008,15 +1023,16 @@ class ExternalContext(object): e = CallError(str(e)) self.context.send(Message.pickled(e, handle=msg.reply_to)) - def main(self, parent_id, context_id, key, log_level): + def main(self, parent_id, context_id, key, debug, log_level): self._setup_master(parent_id, context_id, key) try: try: - self._setup_logging(log_level) + self._setup_logging(debug, log_level) self._setup_importer() self._setup_package(context_id) self._setup_stdio() - LOG.debug('Connected to %s; my ID is %r', self.context, context_id) + LOG.debug('Connected to %s; my ID is %r, PID is %r', + self.context, context_id, os.getpid()) self.router.register(self.context, self.stream) self._dispatch_calls() diff --git a/econtext/master.py b/econtext/master.py index d2d457f3..a4e8769c 100644 --- a/econtext/master.py +++ b/econtext/master.py @@ -326,7 +326,10 @@ class Stream(econtext.core.Stream): #: The path to the remote Python interpreter. python_path = 'python2.7' - def construct(self, remote_name=None, python_path=None, **kwargs): + #: True to cause context to write verbose /tmp/econtext..log. + debug = False + + def construct(self, remote_name=None, python_path=None, debug=False, **kwargs): """Get the named context running on the local machine, creating it if it does not exist.""" super(Stream, self).construct(**kwargs) @@ -338,6 +341,7 @@ class Stream(econtext.core.Stream): remote_name %= (getpass.getuser(), socket.gethostname(), os.getpid()) self.remote_name = remote_name self.name = 'local.default' + self.debug = debug def on_shutdown(self, broker): """Request the slave gracefully shut itself down.""" @@ -388,6 +392,7 @@ class Stream(econtext.core.Stream): 0, self.remote_id, # context_id self.key, + self.debug, LOG.level or logging.getLogger().level or logging.INFO, ),) @@ -506,7 +511,19 @@ class Router(econtext.core.Router): import econtext.ssh return self.connect(econtext.ssh.Stream, **kwargs) + debug = False + + def enable_debug(self): + """ + Cause this context and any descendant child contexts to write debug + logs to /tmp/econtext..log. + """ + econtext.core.enable_debug_logging() + self.debug = True + def connect(self, klass, name=None, **kwargs): + kwargs.setdefault('debug', self.debug) + via = kwargs.pop('via', None) if via is not None: return self.proxy_connect(via, klass, name=name, **kwargs)