##// END OF EJS Templates
Add -r option to %logstart, to log 'raw' input instead of the processed one....
fperez -
Show More
@@ -2,7 +2,7 b''
2 2 """
3 3 Logger class for IPython's logging facilities.
4 4
5 $Id: Logger.py 1077 2006-01-24 18:15:27Z vivainio $
5 $Id: Logger.py 1335 2006-05-30 06:02:44Z fperez $
6 6 """
7 7
8 8 #*****************************************************************************
@@ -46,6 +46,9 b' class Logger(object):'
46 46 self.logmode = logmode
47 47 self.logfile = None
48 48
49 # Whether to log raw or processed input
50 self.log_raw_input = False
51
49 52 # whether to also log output
50 53 self.log_output = False
51 54
@@ -67,7 +70,7 b' class Logger(object):'
67 70 logmode = property(_get_mode,_set_mode)
68 71
69 72 def logstart(self,logfname=None,loghead=None,logmode=None,
70 log_output=False,timestamp=False):
73 log_output=False,timestamp=False,log_raw_input=False):
71 74 """Generate a new log-file with a default header.
72 75
73 76 Raises RuntimeError if the log has already been started"""
@@ -78,12 +81,15 b' class Logger(object):'
78 81
79 82 self.log_active = True
80 83
81 # The three parameters can override constructor defaults
82 if logfname: self.logfname = logfname
83 if loghead: self.loghead = loghead
84 if logmode: self.logmode = logmode
84 # The parameters can override constructor defaults
85 if logfname is not None: self.logfname = logfname
86 if loghead is not None: self.loghead = loghead
87 if logmode is not None: self.logmode = logmode
88
89 # Parameters not part of the constructor
85 90 self.timestamp = timestamp
86 91 self.log_output = log_output
92 self.log_raw_input = log_raw_input
87 93
88 94 # init depending on the log mode requested
89 95 isfile = os.path.isfile
@@ -166,11 +172,22 b' which already exists. But you must first start the logging process with'
166 172 print 'Timestamping :',self.timestamp
167 173 print 'State :',state
168 174
169 def log(self, line,continuation=None):
170 """Write the line to a log and create input cache variables _i*."""
175 def log(self,line_ori,line_mod,continuation=None):
176 """Write the line to a log and create input cache variables _i*.
177
178 Inputs:
179
180 - line_ori: unmodified input line from the user. This is not
181 necessarily valid Python.
182
183 - line_mod: possibly modified input, such as the transformations made
184 by input prefilters or input handlers of various kinds. This should
185 always be valid Python.
186
187 - continuation: if True, indicates this is part of multi-line input."""
171 188
172 189 # update the auto _i tables
173 #print '***logging line',line # dbg
190 #print '***logging line',line_mod # dbg
174 191 #print '***cache_count', self.shell.outputcache.prompt_count # dbg
175 192 try:
176 193 input_hist = self.shell.user_ns['_ih']
@@ -178,13 +195,13 b' which already exists. But you must first start the logging process with'
178 195 print 'userns:',self.shell.user_ns.keys()
179 196 return
180 197
181 if not continuation and line:
198 if not continuation and line_mod:
182 199 self._iii = self._ii
183 200 self._ii = self._i
184 201 self._i = self._i00
185 202 # put back the final \n of every input line
186 self._i00 = line+'\n'
187 #print 'Logging input:<%s>' % line # dbg
203 self._i00 = line_mod+'\n'
204 #print 'Logging input:<%s>' % line_mod # dbg
188 205 input_hist.append(self._i00)
189 206 #print '---[%s]' % (len(input_hist)-1,) # dbg
190 207
@@ -205,11 +222,17 b' which already exists. But you must first start the logging process with'
205 222 in_num = self.shell.outputcache.prompt_count = last_num
206 223 new_i = '_i%s' % in_num
207 224 if continuation:
208 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line)
225 self._i00 = '%s%s\n' % (self.shell.user_ns[new_i],line_mod)
209 226 input_hist[in_num] = self._i00
210 227 to_main[new_i] = self._i00
211 228 self.shell.user_ns.update(to_main)
212 self.log_write(line)
229
230 # Write the log line, but decide which one according to the
231 # log_raw_input flag, set when the log is started.
232 if self.log_raw_input:
233 self.log_write(line_ori)
234 else:
235 self.log_write(line_mod)
213 236
214 237 def log_write(self,data,kind='input'):
215 238 """Write data to the log file, if active"""
@@ -1,7 +1,7 b''
1 1 # -*- coding: utf-8 -*-
2 2 """Magic functions for InteractiveShell.
3 3
4 $Id: Magic.py 1322 2006-05-24 07:51:39Z fperez $"""
4 $Id: Magic.py 1335 2006-05-30 06:02:44Z fperez $"""
5 5
6 6 #*****************************************************************************
7 7 # Copyright (C) 2001 Janko Hauser <jhauser@zscout.de> and
@@ -991,7 +991,7 b' Currently the magic system has the following functions:\\n"""'
991 991 def magic_logstart(self,parameter_s=''):
992 992 """Start logging anywhere in a session.
993 993
994 %logstart [-o|-t] [log_name [log_mode]]
994 %logstart [-o|-r|-t] [log_name [log_mode]]
995 995
996 996 If no name is given, it defaults to a file named 'ipython_log.py' in your
997 997 current directory, in 'rotate' mode (see below).
@@ -1020,11 +1020,18 b' Currently the magic system has the following functions:\\n"""'
1020 1020
1021 1021 awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py
1022 1022
1023 -r: log 'raw' input. Normally, IPython's logs contain the processed
1024 input, so that user lines are logged in their final form, converted
1025 into valid Python. For example, %Exit is logged as
1026 '_ip.magic("Exit"). If the -r flag is given, all input is logged
1027 exactly as typed, with no transformations applied.
1028
1023 1029 -t: put timestamps before each input line logged (these are put in
1024 1030 comments)."""
1025 1031
1026 opts,par = self.parse_options(parameter_s,'ot')
1032 opts,par = self.parse_options(parameter_s,'ort')
1027 1033 log_output = 'o' in opts
1034 log_raw_input = 'r' in opts
1028 1035 timestamp = 't' in opts
1029 1036
1030 1037 rc = self.shell.rc
@@ -1051,7 +1058,7 b' Currently the magic system has the following functions:\\n"""'
1051 1058 loghead = self.shell.loghead_tpl % (rc.opts,rc.args)
1052 1059 try:
1053 1060 started = logger.logstart(logfname,loghead,logmode,
1054 log_output,timestamp)
1061 log_output,timestamp,log_raw_input)
1055 1062 except:
1056 1063 rc.opts.logfile = old_logfile
1057 1064 warn("Couldn't start log: %s" % sys.exc_info()[1])
@@ -1063,16 +1070,21 b' Currently the magic system has the following functions:\\n"""'
1063 1070 # disable timestamping for the previous history, since we've
1064 1071 # lost those already (no time machine here).
1065 1072 logger.timestamp = False
1073
1074 if log_raw_input:
1075 input_hist = self.shell.input_hist_raw
1076 else:
1077 input_hist = self.shell.input_hist
1078
1066 1079 if log_output:
1067 1080 log_write = logger.log_write
1068 input_hist = self.shell.input_hist
1069 1081 output_hist = self.shell.output_hist
1070 1082 for n in range(1,len(input_hist)-1):
1071 1083 log_write(input_hist[n].rstrip())
1072 1084 if n in output_hist:
1073 1085 log_write(repr(output_hist[n]),'output')
1074 1086 else:
1075 logger.log_write(self.shell.input_hist[1:])
1087 logger.log_write(input_hist[1:])
1076 1088 if timestamp:
1077 1089 # re-enable timestamping
1078 1090 logger.timestamp = True
@@ -6,7 +6,7 b' Requires Python 2.3 or newer.'
6 6
7 7 This file contains all the classes and helper functions specific to IPython.
8 8
9 $Id: iplib.py 1334 2006-05-30 03:36:04Z fperez $
9 $Id: iplib.py 1335 2006-05-30 06:02:44Z fperez $
10 10 """
11 11
12 12 #*****************************************************************************
@@ -2031,7 +2031,7 b' want to merge them back into the new files.""" % locals()'
2031 2031 # aliases won't work in indented sections.
2032 2032 transformed = self.transform_alias(iFun, theRest)
2033 2033 line_out = '%s_ip.system(%s)' % (pre, make_quoted_expr( transformed ))
2034 self.log(line_out,continue_prompt)
2034 self.log(line,line_out,continue_prompt)
2035 2035 return line_out
2036 2036
2037 2037 def handle_shell_escape(self, line, continue_prompt=None,
@@ -2053,7 +2053,7 b' want to merge them back into the new files.""" % locals()'
2053 2053 cmd=line.lstrip().lstrip('!')
2054 2054 line_out = '%s_ip.system(%s)' % (pre,make_quoted_expr(cmd))
2055 2055 # update cache/log and return
2056 self.log(line_out,continue_prompt)
2056 self.log(line,line_out,continue_prompt)
2057 2057 return line_out
2058 2058
2059 2059 def handle_magic(self, line, continue_prompt=None,
@@ -2062,7 +2062,7 b' want to merge them back into the new files.""" % locals()'
2062 2062
2063 2063
2064 2064 cmd = '%s_ip.magic(%s)' % (pre,make_quoted_expr(iFun + " " + theRest))
2065 self.log(cmd,continue_prompt)
2065 self.log(line,cmd,continue_prompt)
2066 2066 #print 'in handle_magic, cmd=<%s>' % cmd # dbg
2067 2067 return cmd
2068 2068
@@ -2074,7 +2074,7 b' want to merge them back into the new files.""" % locals()'
2074 2074
2075 2075 # This should only be active for single-line input!
2076 2076 if continue_prompt:
2077 self.log(line,continue_prompt)
2077 self.log(line,line,continue_prompt)
2078 2078 return line
2079 2079
2080 2080 auto_rewrite = True
@@ -2115,7 +2115,7 b' want to merge them back into the new files.""" % locals()'
2115 2115 print >>Term.cout, self.outputcache.prompt1.auto_rewrite() + newcmd
2116 2116 # log what is now valid Python, not the actual user input (without the
2117 2117 # final newline)
2118 self.log(newcmd,continue_prompt)
2118 self.log(line,newcmd,continue_prompt)
2119 2119 return newcmd
2120 2120
2121 2121 def handle_help(self, line, continue_prompt=None,
@@ -2136,7 +2136,7 b' want to merge them back into the new files.""" % locals()'
2136 2136 line = line[1:]
2137 2137 elif line[-1]==self.ESC_HELP:
2138 2138 line = line[:-1]
2139 self.log('#?'+line)
2139 self.log(line,'#?'+line,continue_prompt)
2140 2140 if line:
2141 2141 self.magic_pinfo(line)
2142 2142 else:
@@ -1,3 +1,9 b''
1 2006-05-30 Fernando Perez <Fernando.Perez@colorado.edu>
2
3 * IPython/Logger.py (Logger.logstart): add option to log raw input
4 instead of the processed one. A -r flag was added to the
5 %logstart magic used for controlling logging.
6
1 7 2006-05-29 Fernando Perez <Fernando.Perez@colorado.edu>
2 8
3 9 * IPython/iplib.py (InteractiveShell.__init__): add check for the
General Comments 0
You need to be logged in to leave comments. Login now