Add Router.enable_debug() rather than cutpasting snippets constantly.

pull/35/head
David Wilson 7 years ago
parent a76d871a70
commit 17c2fe68f2

@ -101,6 +101,20 @@ def set_cloexec(fd):
fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC) 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): class Message(object):
dst_id = None dst_id = None
src_id = None src_id = None
@ -946,11 +960,12 @@ class ExternalContext(object):
os.wait() # Reap first stage. os.wait() # Reap first stage.
os.close(100) os.close(100)
def _setup_logging(self, log_level): def _setup_logging(self, debug, log_level):
logging.basicConfig(level=log_level)
root = logging.getLogger() root = logging.getLogger()
root.setLevel(log_level) root.setLevel(log_level)
root.handlers = [LogHandler(self.context)] root.handlers = [LogHandler(self.context)]
if debug:
enable_debug_logging()
def _setup_importer(self): def _setup_importer(self):
with os.fdopen(101, 'r', 1) as fp: with os.fdopen(101, 'r', 1) as fp:
@ -1008,15 +1023,16 @@ class ExternalContext(object):
e = CallError(str(e)) e = CallError(str(e))
self.context.send(Message.pickled(e, handle=msg.reply_to)) 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) self._setup_master(parent_id, context_id, key)
try: try:
try: try:
self._setup_logging(log_level) self._setup_logging(debug, log_level)
self._setup_importer() self._setup_importer()
self._setup_package(context_id) self._setup_package(context_id)
self._setup_stdio() 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.router.register(self.context, self.stream)
self._dispatch_calls() self._dispatch_calls()

@ -326,7 +326,10 @@ class Stream(econtext.core.Stream):
#: The path to the remote Python interpreter. #: The path to the remote Python interpreter.
python_path = 'python2.7' python_path = 'python2.7'
def construct(self, remote_name=None, python_path=None, **kwargs): #: True to cause context to write verbose /tmp/econtext.<pid>.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 """Get the named context running on the local machine, creating it if
it does not exist.""" it does not exist."""
super(Stream, self).construct(**kwargs) super(Stream, self).construct(**kwargs)
@ -338,6 +341,7 @@ class Stream(econtext.core.Stream):
remote_name %= (getpass.getuser(), socket.gethostname(), os.getpid()) remote_name %= (getpass.getuser(), socket.gethostname(), os.getpid())
self.remote_name = remote_name self.remote_name = remote_name
self.name = 'local.default' self.name = 'local.default'
self.debug = debug
def on_shutdown(self, broker): def on_shutdown(self, broker):
"""Request the slave gracefully shut itself down.""" """Request the slave gracefully shut itself down."""
@ -388,6 +392,7 @@ class Stream(econtext.core.Stream):
0, 0,
self.remote_id, # context_id self.remote_id, # context_id
self.key, self.key,
self.debug,
LOG.level or logging.getLogger().level or logging.INFO, LOG.level or logging.getLogger().level or logging.INFO,
),) ),)
@ -506,7 +511,19 @@ class Router(econtext.core.Router):
import econtext.ssh import econtext.ssh
return self.connect(econtext.ssh.Stream, **kwargs) 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.<pid>.log.
"""
econtext.core.enable_debug_logging()
self.debug = True
def connect(self, klass, name=None, **kwargs): def connect(self, klass, name=None, **kwargs):
kwargs.setdefault('debug', self.debug)
via = kwargs.pop('via', None) via = kwargs.pop('via', None)
if via is not None: if via is not None:
return self.proxy_connect(via, klass, name=name, **kwargs) return self.proxy_connect(via, klass, name=name, **kwargs)

Loading…
Cancel
Save