##// END OF EJS Templates
Merge branch 'kernel-logging' of https://github.com/omazapa/ipython into omazapa-kernel-logging...
Fernando Perez -
r3322:9d65bd5f merge
parent child Browse files
Show More
@@ -1,3 +1,4 b''
1 import logging
1 import sys
2 import sys
2 import time
3 import time
3 from cStringIO import StringIO
4 from cStringIO import StringIO
@@ -7,6 +8,13 b' from session import extract_header, Message'
7 from IPython.utils import io
8 from IPython.utils import io
8
9
9 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11 # Globals
12 #-----------------------------------------------------------------------------
13
14 # Module-level logger
15 logger = logging.getLogger(__name__)
16
17 #-----------------------------------------------------------------------------
10 # Stream classes
18 # Stream classes
11 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
12
20
@@ -15,7 +23,7 b' class OutStream(object):'
15
23
16 # The time interval between automatic flushes, in seconds.
24 # The time interval between automatic flushes, in seconds.
17 flush_interval = 0.05
25 flush_interval = 0.05
18
26
19 def __init__(self, session, pub_socket, name):
27 def __init__(self, session, pub_socket, name):
20 self.session = session
28 self.session = session
21 self.pub_socket = pub_socket
29 self.pub_socket = pub_socket
@@ -37,10 +45,10 b' class OutStream(object):'
37 data = self._buffer.getvalue()
45 data = self._buffer.getvalue()
38 if data:
46 if data:
39 content = {u'name':self.name, u'data':data}
47 content = {u'name':self.name, u'data':data}
40 msg = self.session.send(self.pub_socket, u'stream', content=content,
48 msg = self.session.send(self.pub_socket, u'stream',
41 parent=self.parent_header)
49 content=content,
42 io.raw_print(msg)
50 parent=self.parent_header)
43
51 logger.debug(msg)
44 self._buffer.close()
52 self._buffer.close()
45 self._new_buffer()
53 self._new_buffer()
46
54
@@ -21,7 +21,7 b' import atexit'
21 import sys
21 import sys
22 import time
22 import time
23 import traceback
23 import traceback
24
24 import logging
25 # System library imports.
25 # System library imports.
26 import zmq
26 import zmq
27
27
@@ -38,6 +38,13 b' from session import Session, Message'
38 from zmqshell import ZMQInteractiveShell
38 from zmqshell import ZMQInteractiveShell
39
39
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41 # Globals
42 #-----------------------------------------------------------------------------
43
44 # Module-level logger
45 logger = logging.getLogger(__name__)
46
47 #-----------------------------------------------------------------------------
41 # Main kernel class
48 # Main kernel class
42 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
43
50
@@ -79,7 +86,8 b' class Kernel(Configurable):'
79 # This is a dict of port number that the kernel is listening on. It is set
86 # This is a dict of port number that the kernel is listening on. It is set
80 # by record_ports and used by connect_request.
87 # by record_ports and used by connect_request.
81 _recorded_ports = None
88 _recorded_ports = None
82
89
90
83 def __init__(self, **kwargs):
91 def __init__(self, **kwargs):
84 super(Kernel, self).__init__(**kwargs)
92 super(Kernel, self).__init__(**kwargs)
85
93
@@ -121,21 +129,20 b' class Kernel(Configurable):'
121 # easier to trace visually the message chain when debugging. Each
129 # easier to trace visually the message chain when debugging. Each
122 # handler prints its message at the end.
130 # handler prints its message at the end.
123 # Eventually we'll move these from stdout to a logger.
131 # Eventually we'll move these from stdout to a logger.
124 io.raw_print('\n*** MESSAGE TYPE:', msg['msg_type'], '***')
132 logger.debug('\n*** MESSAGE TYPE:'+str(msg['msg_type'])+'***')
125 io.raw_print(' Content: ', msg['content'],
133 logger.debug(' Content: '+str(msg['content'])+'\n --->\n ')
126 '\n --->\n ', sep='', end='')
127
134
128 # Find and call actual handler for message
135 # Find and call actual handler for message
129 handler = self.handlers.get(msg['msg_type'], None)
136 handler = self.handlers.get(msg['msg_type'], None)
130 if handler is None:
137 if handler is None:
131 io.raw_print_err("UNKNOWN MESSAGE TYPE:", msg)
138 logger.error("UNKNOWN MESSAGE TYPE:" +str(msg))
132 else:
139 else:
133 handler(ident, msg)
140 handler(ident, msg)
134
141
135 # Check whether we should exit, in case the incoming message set the
142 # Check whether we should exit, in case the incoming message set the
136 # exit flag on
143 # exit flag on
137 if self.shell.exit_now:
144 if self.shell.exit_now:
138 io.raw_print('\nExiting IPython kernel...')
145 logger.debug('\nExiting IPython kernel...')
139 # We do a normal, clean exit, which allows any actions registered
146 # We do a normal, clean exit, which allows any actions registered
140 # via atexit (such as history saving) to take place.
147 # via atexit (such as history saving) to take place.
141 sys.exit(0)
148 sys.exit(0)
@@ -183,8 +190,8 b' class Kernel(Configurable):'
183 code = content[u'code']
190 code = content[u'code']
184 silent = content[u'silent']
191 silent = content[u'silent']
185 except:
192 except:
186 io.raw_print_err("Got bad msg: ")
193 logger.error("Got bad msg: ")
187 io.raw_print_err(Message(parent))
194 logger.error(str(Message(parent)))
188 return
195 return
189
196
190 shell = self.shell # we'll need this a lot here
197 shell = self.shell # we'll need this a lot here
@@ -262,8 +269,9 b' class Kernel(Configurable):'
262 shell.payload_manager.clear_payload()
269 shell.payload_manager.clear_payload()
263
270
264 # Send the reply.
271 # Send the reply.
265 reply_msg = self.session.send(self.reply_socket, u'execute_reply', reply_content, parent, ident=ident)
272 reply_msg = self.session.send(self.reply_socket, u'execute_reply',
266 io.raw_print(reply_msg)
273 reply_content, parent, ident=ident)
274 logger.debug(str(reply_msg))
267
275
268 # Flush output before sending the reply.
276 # Flush output before sending the reply.
269 sys.stdout.flush()
277 sys.stdout.flush()
@@ -290,7 +298,7 b' class Kernel(Configurable):'
290 'status' : 'ok'}
298 'status' : 'ok'}
291 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
299 completion_msg = self.session.send(self.reply_socket, 'complete_reply',
292 matches, parent, ident)
300 matches, parent, ident)
293 io.raw_print(completion_msg)
301 logger.debug(str(completion_msg))
294
302
295 def object_info_request(self, ident, parent):
303 def object_info_request(self, ident, parent):
296 object_info = self.shell.object_inspect(parent['content']['oname'])
304 object_info = self.shell.object_inspect(parent['content']['oname'])
@@ -298,7 +306,7 b' class Kernel(Configurable):'
298 oinfo = json_clean(object_info)
306 oinfo = json_clean(object_info)
299 msg = self.session.send(self.reply_socket, 'object_info_reply',
307 msg = self.session.send(self.reply_socket, 'object_info_reply',
300 oinfo, parent, ident)
308 oinfo, parent, ident)
301 io.raw_print(msg)
309 logger.debug(msg)
302
310
303 def history_request(self, ident, parent):
311 def history_request(self, ident, parent):
304 output = parent['content']['output']
312 output = parent['content']['output']
@@ -308,7 +316,7 b' class Kernel(Configurable):'
308 content = {'history' : hist}
316 content = {'history' : hist}
309 msg = self.session.send(self.reply_socket, 'history_reply',
317 msg = self.session.send(self.reply_socket, 'history_reply',
310 content, parent, ident)
318 content, parent, ident)
311 io.raw_print(msg)
319 logger.debug(str(msg))
312
320
313 def connect_request(self, ident, parent):
321 def connect_request(self, ident, parent):
314 if self._recorded_ports is not None:
322 if self._recorded_ports is not None:
@@ -317,7 +325,7 b' class Kernel(Configurable):'
317 content = {}
325 content = {}
318 msg = self.session.send(self.reply_socket, 'connect_reply',
326 msg = self.session.send(self.reply_socket, 'connect_reply',
319 content, parent, ident)
327 content, parent, ident)
320 io.raw_print(msg)
328 logger.debug(msg)
321
329
322 def shutdown_request(self, ident, parent):
330 def shutdown_request(self, ident, parent):
323 self.shell.exit_now = True
331 self.shell.exit_now = True
@@ -336,12 +344,13 b' class Kernel(Configurable):'
336 else:
344 else:
337 assert ident is not None, \
345 assert ident is not None, \
338 "Unexpected missing message part."
346 "Unexpected missing message part."
339 io.raw_print("Aborting:\n", Message(msg))
347
348 logger.debug("Aborting:\n"+str(Message(msg)))
340 msg_type = msg['msg_type']
349 msg_type = msg['msg_type']
341 reply_type = msg_type.split('_')[0] + '_reply'
350 reply_type = msg_type.split('_')[0] + '_reply'
342 reply_msg = self.session.send(self.reply_socket, reply_type,
351 reply_msg = self.session.send(self.reply_socket, reply_type,
343 {'status' : 'aborted'}, msg, ident=ident)
352 {'status' : 'aborted'}, msg, ident=ident)
344 io.raw_print(reply_msg)
353 logger.debug(reply_msg)
345 # We need to wait a bit for requests to come in. This can probably
354 # We need to wait a bit for requests to come in. This can probably
346 # be set shorter for true asynchronous clients.
355 # be set shorter for true asynchronous clients.
347 time.sleep(0.1)
356 time.sleep(0.1)
@@ -360,8 +369,8 b' class Kernel(Configurable):'
360 try:
369 try:
361 value = reply['content']['value']
370 value = reply['content']['value']
362 except:
371 except:
363 io.raw_print_err("Got bad raw_input reply: ")
372 logger.error("Got bad raw_input reply: ")
364 io.raw_print_err(Message(parent))
373 logger.error(str(Message(parent)))
365 value = ''
374 value = ''
366 return value
375 return value
367
376
@@ -415,7 +424,7 b' class Kernel(Configurable):'
415 if self._shutdown_message is not None:
424 if self._shutdown_message is not None:
416 self.session.send(self.reply_socket, self._shutdown_message)
425 self.session.send(self.reply_socket, self._shutdown_message)
417 self.session.send(self.pub_socket, self._shutdown_message)
426 self.session.send(self.pub_socket, self._shutdown_message)
418 io.raw_print(self._shutdown_message)
427 logger.debug(str(self._shutdown_message))
419 # A very short sleep to give zmq time to flush its message buffers
428 # A very short sleep to give zmq time to flush its message buffers
420 # before Python truly shuts down.
429 # before Python truly shuts down.
421 time.sleep(0.01)
430 time.sleep(0.01)
@@ -23,6 +23,7 b' import signal'
23 import sys
23 import sys
24 from threading import Thread
24 from threading import Thread
25 import time
25 import time
26 import logging
26
27
27 # System library imports.
28 # System library imports.
28 import zmq
29 import zmq
General Comments 0
You need to be logged in to leave comments. Login now