Show More
@@ -1,183 +1,183 b'' | |||||
1 | """Implementation of magic functions for IPython's own logging. |
|
1 | """Implementation of magic functions for IPython's own logging. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (c) 2012 The IPython Development Team. |
|
4 | # Copyright (c) 2012 The IPython Development Team. | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the Modified BSD License. |
|
6 | # Distributed under the terms of the Modified BSD License. | |
7 | # |
|
7 | # | |
8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | # Stdlib |
|
15 | # Stdlib | |
16 | import os |
|
16 | import os | |
17 | import sys |
|
17 | import sys | |
18 |
|
18 | |||
19 | # Our own packages |
|
19 | # Our own packages | |
20 | from IPython.core.magic import Magics, magics_class, line_magic |
|
20 | from IPython.core.magic import Magics, magics_class, line_magic | |
21 | from warnings import warn |
|
21 | from warnings import warn | |
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Magic implementation classes |
|
24 | # Magic implementation classes | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 |
|
26 | |||
27 | @magics_class |
|
27 | @magics_class | |
28 | class LoggingMagics(Magics): |
|
28 | class LoggingMagics(Magics): | |
29 | """Magics related to all logging machinery.""" |
|
29 | """Magics related to all logging machinery.""" | |
30 |
|
30 | |||
31 | @line_magic |
|
31 | @line_magic | |
32 | def logstart(self, parameter_s=''): |
|
32 | def logstart(self, parameter_s=''): | |
33 | """Start logging anywhere in a session. |
|
33 | """Start logging anywhere in a session. | |
34 |
|
34 | |||
35 | %logstart [-o|-r|-t] [log_name [log_mode]] |
|
35 | %logstart [-o|-r|-t] [log_name [log_mode]] | |
36 |
|
36 | |||
37 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
37 | If no name is given, it defaults to a file named 'ipython_log.py' in your | |
38 | current directory, in 'rotate' mode (see below). |
|
38 | current directory, in 'rotate' mode (see below). | |
39 |
|
39 | |||
40 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your |
|
40 | '%logstart name' saves to file 'name' in 'backup' mode. It saves your | |
41 | history up to that point and then continues logging. |
|
41 | history up to that point and then continues logging. | |
42 |
|
42 | |||
43 | %logstart takes a second optional parameter: logging mode. This can be one |
|
43 | %logstart takes a second optional parameter: logging mode. This can be one | |
44 | of (note that the modes are given unquoted): |
|
44 | of (note that the modes are given unquoted): | |
45 |
|
45 | |||
46 | append |
|
46 | append | |
47 | Keep logging at the end of any existing file. |
|
47 | Keep logging at the end of any existing file. | |
48 |
|
48 | |||
49 | backup |
|
49 | backup | |
50 | Rename any existing file to name~ and start name. |
|
50 | Rename any existing file to name~ and start name. | |
51 |
|
51 | |||
52 | global |
|
52 | global | |
53 | Append to a single logfile in your home directory. |
|
53 | Append to a single logfile in your home directory. | |
54 |
|
54 | |||
55 | over |
|
55 | over | |
56 | Overwrite any existing log. |
|
56 | Overwrite any existing log. | |
57 |
|
57 | |||
58 | rotate |
|
58 | rotate | |
59 | Create rotating logs: name.1~, name.2~, etc. |
|
59 | Create rotating logs: name.1~, name.2~, etc. | |
60 |
|
60 | |||
61 | Options: |
|
61 | Options: | |
62 |
|
62 | |||
63 | -o |
|
63 | -o | |
64 | log also IPython's output. In this mode, all commands which |
|
64 | log also IPython's output. In this mode, all commands which | |
65 | generate an Out[NN] prompt are recorded to the logfile, right after |
|
65 | generate an Out[NN] prompt are recorded to the logfile, right after | |
66 | their corresponding input line. The output lines are always |
|
66 | their corresponding input line. The output lines are always | |
67 | prepended with a '#[Out]# ' marker, so that the log remains valid |
|
67 | prepended with a '#[Out]# ' marker, so that the log remains valid | |
68 | Python code. |
|
68 | Python code. | |
69 |
|
69 | |||
70 | Since this marker is always the same, filtering only the output from |
|
70 | Since this marker is always the same, filtering only the output from | |
71 | a log is very easy, using for example a simple awk call:: |
|
71 | a log is very easy, using for example a simple awk call:: | |
72 |
|
72 | |||
73 | awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py |
|
73 | awk -F'#\\[Out\\]# ' '{if($2) {print $2}}' ipython_log.py | |
74 |
|
74 | |||
75 | -r |
|
75 | -r | |
76 | log 'raw' input. Normally, IPython's logs contain the processed |
|
76 | log 'raw' input. Normally, IPython's logs contain the processed | |
77 | input, so that user lines are logged in their final form, converted |
|
77 | input, so that user lines are logged in their final form, converted | |
78 | into valid Python. For example, %Exit is logged as |
|
78 | into valid Python. For example, %Exit is logged as | |
79 | _ip.magic("Exit"). If the -r flag is given, all input is logged |
|
79 | _ip.magic("Exit"). If the -r flag is given, all input is logged | |
80 | exactly as typed, with no transformations applied. |
|
80 | exactly as typed, with no transformations applied. | |
81 |
|
81 | |||
82 | -t |
|
82 | -t | |
83 | put timestamps before each input line logged (these are put in |
|
83 | put timestamps before each input line logged (these are put in | |
84 | comments). |
|
84 | comments). | |
85 | """ |
|
85 | """ | |
86 |
|
86 | |||
87 | opts,par = self.parse_options(parameter_s,'ort') |
|
87 | opts,par = self.parse_options(parameter_s,'ort') | |
88 | log_output = 'o' in opts |
|
88 | log_output = 'o' in opts | |
89 | log_raw_input = 'r' in opts |
|
89 | log_raw_input = 'r' in opts | |
90 | timestamp = 't' in opts |
|
90 | timestamp = 't' in opts | |
91 |
|
91 | |||
92 | logger = self.shell.logger |
|
92 | logger = self.shell.logger | |
93 |
|
93 | |||
94 | # if no args are given, the defaults set in the logger constructor by |
|
94 | # if no args are given, the defaults set in the logger constructor by | |
95 | # ipython remain valid |
|
95 | # ipython remain valid | |
96 | if par: |
|
96 | if par: | |
97 | try: |
|
97 | try: | |
98 | logfname,logmode = par.split() |
|
98 | logfname,logmode = par.split() | |
99 | except: |
|
99 | except: | |
100 | logfname = par |
|
100 | logfname = par | |
101 | logmode = 'backup' |
|
101 | logmode = 'backup' | |
102 | else: |
|
102 | else: | |
103 | logfname = logger.logfname |
|
103 | logfname = logger.logfname | |
104 | logmode = logger.logmode |
|
104 | logmode = logger.logmode | |
105 | # put logfname into rc struct as if it had been called on the command |
|
105 | # put logfname into rc struct as if it had been called on the command | |
106 | # line, so it ends up saved in the log header Save it in case we need |
|
106 | # line, so it ends up saved in the log header Save it in case we need | |
107 | # to restore it... |
|
107 | # to restore it... | |
108 | old_logfile = self.shell.logfile |
|
108 | old_logfile = self.shell.logfile | |
109 | if logfname: |
|
109 | if logfname: | |
110 | logfname = os.path.expanduser(logfname) |
|
110 | logfname = os.path.expanduser(logfname) | |
111 | self.shell.logfile = logfname |
|
111 | self.shell.logfile = logfname | |
112 |
|
112 | |||
113 | loghead = u'# IPython log file\n\n' |
|
113 | loghead = u'# IPython log file\n\n' | |
114 | try: |
|
114 | try: | |
115 | logger.logstart(logfname, loghead, logmode, log_output, timestamp, |
|
115 | logger.logstart(logfname, loghead, logmode, log_output, timestamp, | |
116 | log_raw_input) |
|
116 | log_raw_input) | |
117 | except: |
|
117 | except: | |
118 | self.shell.logfile = old_logfile |
|
118 | self.shell.logfile = old_logfile | |
119 | warn("Couldn't start log: %s" % sys.exc_info()[1]) |
|
119 | warn("Couldn't start log: %s" % sys.exc_info()[1]) | |
120 | else: |
|
120 | else: | |
121 | # log input history up to this point, optionally interleaving |
|
121 | # log input history up to this point, optionally interleaving | |
122 | # output if requested |
|
122 | # output if requested | |
123 |
|
123 | |||
124 | if timestamp: |
|
124 | if timestamp: | |
125 | # disable timestamping for the previous history, since we've |
|
125 | # disable timestamping for the previous history, since we've | |
126 | # lost those already (no time machine here). |
|
126 | # lost those already (no time machine here). | |
127 | logger.timestamp = False |
|
127 | logger.timestamp = False | |
128 |
|
128 | |||
129 | if log_raw_input: |
|
129 | if log_raw_input: | |
130 | input_hist = self.shell.history_manager.input_hist_raw |
|
130 | input_hist = self.shell.history_manager.input_hist_raw | |
131 | else: |
|
131 | else: | |
132 | input_hist = self.shell.history_manager.input_hist_parsed |
|
132 | input_hist = self.shell.history_manager.input_hist_parsed | |
133 |
|
133 | |||
134 | if log_output: |
|
134 | if log_output: | |
135 | log_write = logger.log_write |
|
135 | log_write = logger.log_write | |
136 | output_hist = self.shell.history_manager.output_hist |
|
136 | output_hist = self.shell.history_manager.output_hist | |
137 | for n in range(1,len(input_hist)-1): |
|
137 | for n in range(1,len(input_hist)-1): | |
138 | log_write(input_hist[n].rstrip() + u'\n') |
|
138 | log_write(input_hist[n].rstrip() + u'\n') | |
139 | if n in output_hist: |
|
139 | if n in output_hist: | |
140 |
log_write |
|
140 | log_write(repr(output_hist[n]),'output') | |
141 | else: |
|
141 | else: | |
142 | logger.log_write(u'\n'.join(input_hist[1:])) |
|
142 | logger.log_write(u'\n'.join(input_hist[1:])) | |
143 | logger.log_write(u'\n') |
|
143 | logger.log_write(u'\n') | |
144 | if timestamp: |
|
144 | if timestamp: | |
145 | # re-enable timestamping |
|
145 | # re-enable timestamping | |
146 | logger.timestamp = True |
|
146 | logger.timestamp = True | |
147 |
|
147 | |||
148 | print ('Activating auto-logging. ' |
|
148 | print ('Activating auto-logging. ' | |
149 | 'Current session state plus future input saved.') |
|
149 | 'Current session state plus future input saved.') | |
150 | logger.logstate() |
|
150 | logger.logstate() | |
151 |
|
151 | |||
152 | @line_magic |
|
152 | @line_magic | |
153 | def logstop(self, parameter_s=''): |
|
153 | def logstop(self, parameter_s=''): | |
154 | """Fully stop logging and close log file. |
|
154 | """Fully stop logging and close log file. | |
155 |
|
155 | |||
156 | In order to start logging again, a new %logstart call needs to be made, |
|
156 | In order to start logging again, a new %logstart call needs to be made, | |
157 | possibly (though not necessarily) with a new filename, mode and other |
|
157 | possibly (though not necessarily) with a new filename, mode and other | |
158 | options.""" |
|
158 | options.""" | |
159 | self.shell.logger.logstop() |
|
159 | self.shell.logger.logstop() | |
160 |
|
160 | |||
161 | @line_magic |
|
161 | @line_magic | |
162 | def logoff(self, parameter_s=''): |
|
162 | def logoff(self, parameter_s=''): | |
163 | """Temporarily stop logging. |
|
163 | """Temporarily stop logging. | |
164 |
|
164 | |||
165 | You must have previously started logging.""" |
|
165 | You must have previously started logging.""" | |
166 | self.shell.logger.switch_log(0) |
|
166 | self.shell.logger.switch_log(0) | |
167 |
|
167 | |||
168 | @line_magic |
|
168 | @line_magic | |
169 | def logon(self, parameter_s=''): |
|
169 | def logon(self, parameter_s=''): | |
170 | """Restart logging. |
|
170 | """Restart logging. | |
171 |
|
171 | |||
172 | This function is for restarting logging which you've temporarily |
|
172 | This function is for restarting logging which you've temporarily | |
173 | stopped with %logoff. For starting logging for the first time, you |
|
173 | stopped with %logoff. For starting logging for the first time, you | |
174 | must use the %logstart function, which allows you to specify an |
|
174 | must use the %logstart function, which allows you to specify an | |
175 | optional log filename.""" |
|
175 | optional log filename.""" | |
176 |
|
176 | |||
177 | self.shell.logger.switch_log(1) |
|
177 | self.shell.logger.switch_log(1) | |
178 |
|
178 | |||
179 | @line_magic |
|
179 | @line_magic | |
180 | def logstate(self, parameter_s=''): |
|
180 | def logstate(self, parameter_s=''): | |
181 | """Print the status of the logging system.""" |
|
181 | """Print the status of the logging system.""" | |
182 |
|
182 | |||
183 | self.shell.logger.logstate() |
|
183 | self.shell.logger.logstate() |
General Comments 0
You need to be logged in to leave comments.
Login now