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