##// END OF EJS Templates
Remove all direct shell access from logger....
Fernando Perez -
Show More
@@ -1,6 +1,4 b''
1 # -*- coding: utf-8 -*-
2 """
3 Logger class for IPython's logging facilities.
1 """Logger class for IPython's logging facilities.
4 2 """
5 3
6 4 #*****************************************************************************
@@ -26,13 +24,12 b' import time'
26 24 class Logger(object):
27 25 """A Logfile class with different policies for file creation"""
28 26
29 def __init__(self,shell,logfname='Logger.log',loghead='',logmode='over'):
30
31 self._i00,self._i,self._ii,self._iii = '','','',''
27 def __init__(self, home_dir, logfname='Logger.log', loghead='',
28 logmode='over'):
32 29
33 30 # this is the full ipython instance, we need some attributes from it
34 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 34 self.logfname = logfname
38 35 self.loghead = loghead
@@ -102,7 +99,7 b' class Logger(object):'
102 99 self.logfile = open(self.logfname,'w')
103 100
104 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 103 self.logfile = open(self.logfname, 'a')
107 104
108 105 elif logmode == 'over':
@@ -166,66 +163,18 b' which already exists. But you must first start the logging process with'
166 163 print 'Timestamping :',self.timestamp
167 164 print 'State :',state
168 165
169 def log(self,line_ori,line_mod,continuation=None):
170 """Write the line to a log and create input cache variables _i*.
166 def log(self, line_mod, line_ori):
167 """Write the sources to a log.
171 168
172 169 Inputs:
173 170
174 - line_ori: unmodified input line from the user. This is not
175 necessarily valid Python.
176
177 171 - line_mod: possibly modified input, such as the transformations made
178 172 by input prefilters or input handlers of various kinds. This should
179 173 always be valid Python.
180 174
181 - continuation: if True, indicates this is part of multi-line input."""
182
183 # update the auto _i tables
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)
175 - line_ori: unmodified input line from the user. This is not
176 necessarily valid Python.
177 """
229 178
230 179 # Write the log line, but decide which one according to the
231 180 # log_raw_input flag, set when the log is started.
@@ -244,10 +193,10 b' which already exists. But you must first start the logging process with'
244 193 if self.timestamp:
245 194 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
246 195 time.localtime()))
247 write('%s\n' % data)
196 write(data)
248 197 elif kind=='output' and self.log_output:
249 198 odata = '\n'.join(['#[Out]# %s' % s
250 for s in data.split('\n')])
199 for s in data.splitlines()])
251 200 write('%s\n' % odata)
252 201 self.logfile.flush()
253 202
General Comments 0
You need to be logged in to leave comments. Login now