Show More
@@ -19,6 +19,7 b' import sys' | |||||
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 | from traitlets import Bool | |||
22 |
|
23 | |||
23 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
24 | # Magic implementation classes |
|
25 | # Magic implementation classes | |
@@ -28,11 +29,17 b' from warnings import warn' | |||||
28 | class LoggingMagics(Magics): |
|
29 | class LoggingMagics(Magics): | |
29 | """Magics related to all logging machinery.""" |
|
30 | """Magics related to all logging machinery.""" | |
30 |
|
31 | |||
|
32 | quiet = Bool(False, help= | |||
|
33 | """ | |||
|
34 | Suppress output of log state when logging is enabled | |||
|
35 | """ | |||
|
36 | ).tag(config=True) | |||
|
37 | ||||
31 | @line_magic |
|
38 | @line_magic | |
32 | def logstart(self, parameter_s=''): |
|
39 | def logstart(self, parameter_s=''): | |
33 | """Start logging anywhere in a session. |
|
40 | """Start logging anywhere in a session. | |
34 |
|
41 | |||
35 | %logstart [-o|-r|-t] [log_name [log_mode]] |
|
42 | %logstart [-o|-r|-t|-q] [log_name [log_mode]] | |
36 |
|
43 | |||
37 | If no name is given, it defaults to a file named 'ipython_log.py' in your |
|
44 | If no name is given, it defaults to a file named 'ipython_log.py' in your | |
38 | current directory, in 'rotate' mode (see below). |
|
45 | current directory, in 'rotate' mode (see below). | |
@@ -82,12 +89,16 b' class LoggingMagics(Magics):' | |||||
82 | -t |
|
89 | -t | |
83 | put timestamps before each input line logged (these are put in |
|
90 | put timestamps before each input line logged (these are put in | |
84 | comments). |
|
91 | comments). | |
|
92 | ||||
|
93 | -q | |||
|
94 | suppress output of logstate message when logging is invoked | |||
85 | """ |
|
95 | """ | |
86 |
|
96 | |||
87 | opts,par = self.parse_options(parameter_s,'ort') |
|
97 | opts,par = self.parse_options(parameter_s,'ortq') | |
88 | log_output = 'o' in opts |
|
98 | log_output = 'o' in opts | |
89 | log_raw_input = 'r' in opts |
|
99 | log_raw_input = 'r' in opts | |
90 | timestamp = 't' in opts |
|
100 | timestamp = 't' in opts | |
|
101 | quiet = 'q' in opts | |||
91 |
|
102 | |||
92 | logger = self.shell.logger |
|
103 | logger = self.shell.logger | |
93 |
|
104 | |||
@@ -145,9 +156,10 b' class LoggingMagics(Magics):' | |||||
145 | # re-enable timestamping |
|
156 | # re-enable timestamping | |
146 | logger.timestamp = True |
|
157 | logger.timestamp = True | |
147 |
|
158 | |||
148 | print ('Activating auto-logging. ' |
|
159 | if not (self.quiet or quiet): | |
149 | 'Current session state plus future input saved.') |
|
160 | print ('Activating auto-logging. ' | |
150 | logger.logstate() |
|
161 | 'Current session state plus future input saved.') | |
|
162 | logger.logstate() | |||
151 |
|
163 | |||
152 | @line_magic |
|
164 | @line_magic | |
153 | def logstop(self, parameter_s=''): |
|
165 | def logstop(self, parameter_s=''): |
@@ -6,6 +6,7 b' Needs to be run by nose (to make ipython session available).' | |||||
6 |
|
6 | |||
7 | import io |
|
7 | import io | |
8 | import os |
|
8 | import os | |
|
9 | import re | |||
9 | import sys |
|
10 | import sys | |
10 | import warnings |
|
11 | import warnings | |
11 | from unittest import TestCase |
|
12 | from unittest import TestCase | |
@@ -20,7 +21,7 b' from IPython.core.error import UsageError' | |||||
20 | from IPython.core.magic import (Magics, magics_class, line_magic, |
|
21 | from IPython.core.magic import (Magics, magics_class, line_magic, | |
21 | cell_magic, |
|
22 | cell_magic, | |
22 | register_line_magic, register_cell_magic) |
|
23 | register_line_magic, register_cell_magic) | |
23 | from IPython.core.magics import execution, script, code |
|
24 | from IPython.core.magics import execution, script, code, logging | |
24 | from IPython.testing import decorators as dec |
|
25 | from IPython.testing import decorators as dec | |
25 | from IPython.testing import tools as tt |
|
26 | from IPython.testing import tools as tt | |
26 | from IPython.utils import py3compat |
|
27 | from IPython.utils import py3compat | |
@@ -977,3 +978,35 b' def test_strip_initial_indent():' | |||||
977 | nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2") |
|
978 | nt.assert_equal(sii(" a = 1\nb = 2"), "a = 1\nb = 2") | |
978 | nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc") |
|
979 | nt.assert_equal(sii(" a\n b\nc"), "a\n b\nc") | |
979 | nt.assert_equal(sii("a\n b"), "a\n b") |
|
980 | nt.assert_equal(sii("a\n b"), "a\n b") | |
|
981 | ||||
|
982 | def test_logging_magic_quiet_from_arg(): | |||
|
983 | _ip.config.LoggingMagics.quiet = False | |||
|
984 | lm = logging.LoggingMagics(shell=_ip) | |||
|
985 | with TemporaryDirectory() as td: | |||
|
986 | try: | |||
|
987 | with tt.AssertNotPrints(re.compile("Activating.*")): | |||
|
988 | lm.logstart('-q {}'.format( | |||
|
989 | os.path.join(td, "quiet_from_arg.log"))) | |||
|
990 | finally: | |||
|
991 | _ip.logger.logstop() | |||
|
992 | ||||
|
993 | def test_logging_magic_quiet_from_config(): | |||
|
994 | _ip.config.LoggingMagics.quiet = True | |||
|
995 | lm = logging.LoggingMagics(shell=_ip) | |||
|
996 | with TemporaryDirectory() as td: | |||
|
997 | try: | |||
|
998 | with tt.AssertNotPrints(re.compile("Activating.*")): | |||
|
999 | lm.logstart(os.path.join(td, "quiet_from_config.log")) | |||
|
1000 | finally: | |||
|
1001 | _ip.logger.logstop() | |||
|
1002 | ||||
|
1003 | def test_logging_magic_not_quiet(): | |||
|
1004 | _ip.config.LoggingMagics.quiet = False | |||
|
1005 | lm = logging.LoggingMagics(shell=_ip) | |||
|
1006 | with TemporaryDirectory() as td: | |||
|
1007 | try: | |||
|
1008 | with tt.AssertPrints(re.compile("Activating.*")): | |||
|
1009 | lm.logstart(os.path.join(td, "not_quiet.log")) | |||
|
1010 | finally: | |||
|
1011 | _ip.logger.logstop() | |||
|
1012 |
@@ -25,7 +25,9 b' from IPython.core.history import HistoryManager' | |||||
25 | from IPython.core.application import ( |
|
25 | from IPython.core.application import ( | |
26 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases |
|
26 | ProfileDir, BaseIPythonApplication, base_flags, base_aliases | |
27 | ) |
|
27 | ) | |
28 |
from IPython.core.magics import |
|
28 | from IPython.core.magics import ( | |
|
29 | ScriptMagics, LoggingMagics | |||
|
30 | ) | |||
29 | from IPython.core.shellapp import ( |
|
31 | from IPython.core.shellapp import ( | |
30 | InteractiveShellApp, shell_flags, shell_aliases |
|
32 | InteractiveShellApp, shell_flags, shell_aliases | |
31 | ) |
|
33 | ) | |
@@ -202,6 +204,7 b' class TerminalIPythonApp(BaseIPythonApplication, InteractiveShellApp):' | |||||
202 | PlainTextFormatter, |
|
204 | PlainTextFormatter, | |
203 | IPCompleter, |
|
205 | IPCompleter, | |
204 | ScriptMagics, |
|
206 | ScriptMagics, | |
|
207 | LoggingMagics, | |||
205 | StoreMagics, |
|
208 | StoreMagics, | |
206 | ] |
|
209 | ] | |
207 |
|
210 |
General Comments 0
You need to be logged in to leave comments.
Login now