Skip to content

Commit

Permalink
Merge pull request #257 from Cal-CS-61A-Staff/log-serialize
Browse files Browse the repository at this point in the history
Make log messages serializable
  • Loading branch information
albert12132 committed Oct 13, 2014
2 parents 21cfd32 + 2c06ce0 commit 15e6be0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion client/cli/ok.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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())
Expand Down
22 changes: 22 additions & 0 deletions client/utils/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 15e6be0

Please sign in to comment.