From 2c06ce0f9d63e7201879fa1c4f407b27438cf0d0 Mon Sep 17 00:00:00 2001 From: Soumya Basu Date: Sun, 12 Oct 2014 22:45:58 -0700 Subject: [PATCH] Make log messages serializable --- client/cli/ok.py | 5 ++++- client/utils/output.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/client/cli/ok.py b/client/cli/ok.py index 701eb1515..a43fa6857 100644 --- a/client/cli/ok.py +++ b/client/cli/ok.py @@ -170,11 +170,12 @@ def main(): msg_queue.put(messages) staging_queue = multiprocessing.Queue() + interceptor = output.LogInterceptor() server_thread = multiprocessing.Process( target=network.dump_to_server, args=(access_token, msg_queue, assignment['name'], args.server, args.insecure, staging_queue, - client.__version__, log)) + client.__version__, interceptor)) server_thread.start() except error.URLError as ex: log.warning('on_start messages not sent to server: %s', str(e)) @@ -200,6 +201,8 @@ def main(): else: server_thread.join() + interceptor.dump_to_logger(log) + dump_list = [] while not msg_queue.empty(): dump_list.append(msg_queue.get_nowait()) diff --git a/client/utils/output.py b/client/utils/output.py index bfd152fc7..5bebaf671 100644 --- a/client/utils/output.py +++ b/client/utils/output.py @@ -56,3 +56,25 @@ def flush(self): def __getattr__(self, attr): return getattr(self._current_stream, attr) +class LogInterceptor(object): + """A serializable interceptor object that relays output to a logger object""" + def __init__(self): + self._msgs = [] + + def info(self, *args): + self._msgs.append(('info', args)) + + def warning(self, *args): + self._msgs.append(('warning', args)) + + def error(self, *args): + self._msgs.append(('error', args)) + + def dump_to_logger(self, log): + for msg_type, msg in self._msgs: + if msg_type == 'info': + log.info(*msg) + elif msg_type == 'warning': + log.warning(*msg) + elif msg_type == 'error': + log.error(*msg)