##// END OF EJS Templates
Remove all direct shell access from logger....
Fernando Perez -
Show More
@@ -1,6 +1,4 b''
1 # -*- coding: utf-8 -*-
1 """Logger class for IPython's logging facilities.
2 """
3 Logger class for IPython's logging facilities.
4 """
2 """
5
3
6 #*****************************************************************************
4 #*****************************************************************************
@@ -26,13 +24,12 b' import time'
26 class Logger(object):
24 class Logger(object):
27 """A Logfile class with different policies for file creation"""
25 """A Logfile class with different policies for file creation"""
28
26
29 def __init__(self,shell,logfname='Logger.log',loghead='',logmode='over'):
27 def __init__(self, home_dir, logfname='Logger.log', loghead='',
30
28 logmode='over'):
31 self._i00,self._i,self._ii,self._iii = '','','',''
32
29
33 # this is the full ipython instance, we need some attributes from it
30 # this is the full ipython instance, we need some attributes from it
34 # which won't exist until later. What a mess, clean up later...
31 # which won't exist until later. What a mess, clean up later...
35 self.shell = shell
32 self.home_dir = home_dir
36
33
37 self.logfname = logfname
34 self.logfname = logfname
38 self.loghead = loghead
35 self.loghead = loghead
@@ -102,7 +99,7 b' class Logger(object):'
102 self.logfile = open(self.logfname,'w')
99 self.logfile = open(self.logfname,'w')
103
100
104 elif logmode == 'global':
101 elif logmode == 'global':
105 self.logfname = os.path.join(self.shell.home_dir,self.logfname)
102 self.logfname = os.path.join(self.home_dir,self.logfname)
106 self.logfile = open(self.logfname, 'a')
103 self.logfile = open(self.logfname, 'a')
107
104
108 elif logmode == 'over':
105 elif logmode == 'over':
@@ -166,66 +163,18 b' which already exists. But you must first start the logging process with'
166 print 'Timestamping :',self.timestamp
163 print 'Timestamping :',self.timestamp
167 print 'State :',state
164 print 'State :',state
168
165
169 def log(self,line_ori,line_mod,continuation=None):
166 def log(self, line_mod, line_ori):
170 """Write the line to a log and create input cache variables _i*.
167 """Write the sources to a log.
171
168
172 Inputs:
169 Inputs:
173
170
174 - line_ori: unmodified input line from the user. This is not
175 necessarily valid Python.
176
177 - line_mod: possibly modified input, such as the transformations made
171 - line_mod: possibly modified input, such as the transformations made
178 by input prefilters or input handlers of various kinds. This should
172 by input prefilters or input handlers of various kinds. This should
179 always be valid Python.
173 always be valid Python.
180
174
181 - continuation: if True, indicates this is part of multi-line input."""
175 - line_ori: unmodified input line from the user. This is not
182
176 necessarily valid Python.
183 # update the auto _i tables
177 """
184 #print '***logging line',line_mod # dbg
185 #print '***cache_count', self.shell.displayhook.prompt_count # dbg
186 try:
187 input_hist = self.shell.user_ns['_ih']
188 except:
189 #print 'userns:',self.shell.user_ns.keys() # dbg
190 return
191
192 out_cache = self.shell.displayhook
193
194 # add blank lines if the input cache fell out of sync.
195 if out_cache.do_full_cache and \
196 out_cache.prompt_count +1 > len(input_hist):
197 pass
198 #input_hist.extend(['\n'] * (out_cache.prompt_count - len(input_hist)))
199
200 if not continuation and line_mod:
201 self._iii = self._ii
202 self._ii = self._i
203 self._i = self._i00
204 # put back the final \n of every input line
205 self._i00 = line_mod+'\n'
206 #print 'Logging input:<%s>' % line_mod # dbg
207 #input_hist.append(self._i00)
208 #print '---[%s]' % (len(input_hist)-1,) # dbg
209
210 # hackish access to top-level namespace to create _i1,_i2... dynamically
211 to_main = {'_i':self._i,'_ii':self._ii,'_iii':self._iii}
212 if self.shell.displayhook.do_full_cache:
213 in_num = self.shell.displayhook.prompt_count
214
215 # but if the opposite is true (a macro can produce multiple inputs
216 # with no output display called), then bring the output counter in
217 # sync:
218 ## last_num = len(input_hist)-1
219 ## if in_num != last_num:
220 ## pass # dbg
221 ## #in_num = self.shell.execution_count = last_num
222
223 new_i = '_i%s' % in_num
224 if continuation:
225 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line_mod)
226 #input_hist[in_num] = self._i00
227 to_main[new_i] = self._i00
228 self.shell.user_ns.update(to_main)
229
178
230 # Write the log line, but decide which one according to the
179 # Write the log line, but decide which one according to the
231 # log_raw_input flag, set when the log is started.
180 # log_raw_input flag, set when the log is started.
@@ -234,7 +183,7 b' which already exists. But you must first start the logging process with'
234 else:
183 else:
235 self.log_write(line_mod)
184 self.log_write(line_mod)
236
185
237 def log_write(self,data,kind='input'):
186 def log_write(self, data, kind='input'):
238 """Write data to the log file, if active"""
187 """Write data to the log file, if active"""
239
188
240 #print 'data: %r' % data # dbg
189 #print 'data: %r' % data # dbg
@@ -244,10 +193,10 b' which already exists. But you must first start the logging process with'
244 if self.timestamp:
193 if self.timestamp:
245 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
194 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
246 time.localtime()))
195 time.localtime()))
247 write('%s\n' % data)
196 write(data)
248 elif kind=='output' and self.log_output:
197 elif kind=='output' and self.log_output:
249 odata = '\n'.join(['#[Out]# %s' % s
198 odata = '\n'.join(['#[Out]# %s' % s
250 for s in data.split('\n')])
199 for s in data.splitlines()])
251 write('%s\n' % odata)
200 write('%s\n' % odata)
252 self.logfile.flush()
201 self.logfile.flush()
253
202
General Comments 0
You need to be logged in to leave comments. Login now