diff --git a/IPython/zmq/iostream.py b/IPython/zmq/iostream.py index 437933e..260b8b5 100644 --- a/IPython/zmq/iostream.py +++ b/IPython/zmq/iostream.py @@ -1,3 +1,4 @@ +import logging import sys import time from cStringIO import StringIO @@ -15,7 +16,7 @@ class OutStream(object): # The time interval between automatic flushes, in seconds. flush_interval = 0.05 - + _logger = logging.getLogger() def __init__(self, session, pub_socket, name): self.session = session self.pub_socket = pub_socket @@ -39,7 +40,7 @@ class OutStream(object): content = {u'name':self.name, u'data':data} msg = self.session.msg(u'stream', content=content, parent=self.parent_header) - io.raw_print(msg) + self._logger.debug(msg) self.pub_socket.send_json(msg) self._buffer.close() diff --git a/IPython/zmq/ipkernel.py b/IPython/zmq/ipkernel.py index 772c798..487fc5c 100755 --- a/IPython/zmq/ipkernel.py +++ b/IPython/zmq/ipkernel.py @@ -21,7 +21,7 @@ import atexit import sys import time import traceback - +import logging # System library imports. import zmq @@ -79,7 +79,10 @@ class Kernel(Configurable): # This is a dict of port number that the kernel is listening on. It is set # by record_ports and used by connect_request. _recorded_ports = None - + + + _logger = logging.getLogger() + def __init__(self, **kwargs): super(Kernel, self).__init__(**kwargs) @@ -122,21 +125,20 @@ class Kernel(Configurable): # easier to trace visually the message chain when debugging. Each # handler prints its message at the end. # Eventually we'll move these from stdout to a logger. - io.raw_print('\n*** MESSAGE TYPE:', msg['msg_type'], '***') - io.raw_print(' Content: ', msg['content'], - '\n --->\n ', sep='', end='') + self._logger.debug('\n*** MESSAGE TYPE:'+str(msg['msg_type'])+'***') + self._logger.debug(' Content: '+str(msg['content'])+'\n --->\n ') # Find and call actual handler for message handler = self.handlers.get(msg['msg_type'], None) if handler is None: - io.raw_print_err("UNKNOWN MESSAGE TYPE:", msg) + self._logger.error("UNKNOWN MESSAGE TYPE:" +str(msg)) else: handler(ident, msg) # Check whether we should exit, in case the incoming message set the # exit flag on if self.shell.exit_now: - io.raw_print('\nExiting IPython kernel...') + self._logger.debug('\nExiting IPython kernel...') # We do a normal, clean exit, which allows any actions registered # via atexit (such as history saving) to take place. sys.exit(0) @@ -186,8 +188,8 @@ class Kernel(Configurable): code = content[u'code'] silent = content[u'silent'] except: - io.raw_print_err("Got bad msg: ") - io.raw_print_err(Message(parent)) + self._logger.error("Got bad msg: ") + self._logger.error(str(Message(parent))) return shell = self.shell # we'll need this a lot here @@ -265,7 +267,7 @@ class Kernel(Configurable): # Send the reply. reply_msg = self.session.msg(u'execute_reply', reply_content, parent) - io.raw_print(reply_msg) + self._logger.debug(str(reply_msg)) # Flush output before sending the reply. sys.stdout.flush() @@ -295,7 +297,7 @@ class Kernel(Configurable): 'status' : 'ok'} completion_msg = self.session.send(self.reply_socket, 'complete_reply', matches, parent, ident) - io.raw_print(completion_msg) + self._logger.debug(str(completion_msg)) def object_info_request(self, ident, parent): object_info = self.shell.object_inspect(parent['content']['oname']) @@ -303,7 +305,7 @@ class Kernel(Configurable): oinfo = json_clean(object_info) msg = self.session.send(self.reply_socket, 'object_info_reply', oinfo, parent, ident) - io.raw_print(msg) + self._logger.debug(msg) def history_request(self, ident, parent): output = parent['content']['output'] @@ -313,7 +315,7 @@ class Kernel(Configurable): content = {'history' : hist} msg = self.session.send(self.reply_socket, 'history_reply', content, parent, ident) - io.raw_print(msg) + self._logger.debug(str(msg)) def connect_request(self, ident, parent): if self._recorded_ports is not None: @@ -322,7 +324,7 @@ class Kernel(Configurable): content = {} msg = self.session.send(self.reply_socket, 'connect_reply', content, parent, ident) - io.raw_print(msg) + self._logger.debug(msg) def shutdown_request(self, ident, parent): self.shell.exit_now = True @@ -344,11 +346,11 @@ class Kernel(Configurable): assert self.reply_socket.rcvmore(), \ "Unexpected missing message part." msg = self.reply_socket.recv_json() - io.raw_print("Aborting:\n", Message(msg)) + self._logger.debug("Aborting:\n"+str(Message(msg))) msg_type = msg['msg_type'] reply_type = msg_type.split('_')[0] + '_reply' reply_msg = self.session.msg(reply_type, {'status' : 'aborted'}, msg) - io.raw_print(reply_msg) + self._logger.debug(reply_msg) self.reply_socket.send(ident,zmq.SNDMORE) self.reply_socket.send_json(reply_msg) # We need to wait a bit for requests to come in. This can probably @@ -370,8 +372,8 @@ class Kernel(Configurable): try: value = reply['content']['value'] except: - io.raw_print_err("Got bad raw_input reply: ") - io.raw_print_err(Message(parent)) + self._logger.error("Got bad raw_input reply: ") + self._logger.error(str(Message(parent))) value = '' return value @@ -425,7 +427,7 @@ class Kernel(Configurable): if self._shutdown_message is not None: self.reply_socket.send_json(self._shutdown_message) self.pub_socket.send_json(self._shutdown_message) - io.raw_print(self._shutdown_message) + self._logger.debug(str(self._shutdown_message)) # A very short sleep to give zmq time to flush its message buffers # before Python truly shuts down. time.sleep(0.01) diff --git a/IPython/zmq/kernelmanager.py b/IPython/zmq/kernelmanager.py index 089e519..98ea930 100644 --- a/IPython/zmq/kernelmanager.py +++ b/IPython/zmq/kernelmanager.py @@ -23,6 +23,7 @@ import signal import sys from threading import Thread import time +import logging # System library imports. import zmq