Format: application/x-python-code
1 o
2
3 ��h� � @ sX d Z ddlZddlZddlmZmZmZ ddlmZ ddl m
4 Z
5 eG dd� de��ZdS )z=Implementation of magic functions for IPython's own logging.
6 � N)�Magics�magics_class�
7 line_magic)�warn)�Boolc @ sj e Zd ZdZeddd�jdd�Zeddd ��Zedd
8 d��Z eddd
9 ��Z
10 eddd��Zeddd��ZdS )�
11 LoggingMagicsz(Magics related to all logging machinery.FzF
12 Suppress output of log state when logging is enabled
13 )�helpT)�config� c C s� | � |d�\}}d|v }d|v }d|v }d|v }| jj}|r0z|�� \} }
14 W n |} d}
15 Y n|j} |j}
16 | jj}| rBtj� | �} | | j_d}z|�
17 | ||
18 |||� W n || j_tdt�
19 � d � Y d
20 S |rnd
21 |_|rv| jjj}
22 n| jjj}
23 |r�|j}| jjj}td t|
24 �d �D ]}||
25 | �� d � ||v r�|t|| �d� q�n|�d�|
26 d d
27 � �� |�d� |r�d|_| js�|s�td� |�� d
28 S d
29 S d
30 S )a� Start logging anywhere in a session.
31
32 %logstart [-o|-r|-t|-q] [log_name [log_mode]]
33
34 If no name is given, it defaults to a file named 'ipython_log.py' in your
35 current directory, in 'rotate' mode (see below).
36
37 '%logstart name' saves to file 'name' in 'backup' mode. It saves your
38 history up to that point and then continues logging.
39
40 %logstart takes a second optional parameter: logging mode. This can be one
41 of (note that the modes are given unquoted):
42
43 append
44 Keep logging at the end of any existing file.
45
46 backup
47 Rename any existing file to name~ and start name.
48
49 global
50 Append to a single logfile in your home directory.
51
52 over
53 Overwrite any existing log.
54
55 rotate
56 Create rotating logs: name.1~, name.2~, etc.
57
58 Options:
59
60 -o
61 log also IPython's output. In this mode, all commands which
62 generate an Out[NN] prompt are recorded to the logfile, right after
63 their corresponding input line. The output lines are always
64 prepended with a '#[Out]# ' marker, so that the log remains valid
65 Python code.
66
67 Since this marker is always the same, filtering only the output from
68 a log is very easy, using for example a simple awk call::
69
70 awk -F'#\[Out\]# ' '{if($2) {print $2}}' ipython_log.py
71
72 -r
73 log 'raw' input. Normally, IPython's logs contain the processed
74 input, so that user lines are logged in their final form, converted
75 into valid Python. For example, %Exit is logged as
76 _ip.run_line_magic("Exit"). If the -r flag is given, all input is logged
77 exactly as typed, with no transformations applied.
78
79 -t
80 put timestamps before each input line logged (these are put in
81 comments).
82
83 -q
84 suppress output of logstate message when logging is invoked
85 Zortq�o�r�t�q�backupz# IPython log file
86
87 zCouldn't start log: %s� F�
88 �outputNTzGActivating auto-logging. Current session state plus future input saved.)Z
89 parse_options�shell�logger�split�logfname�logmodeZlogfile�os�path�
90 expanduser�logstartr �sys�exc_info� timestampZhistory_managerZinput_hist_rawZinput_hist_parsed� log_write�output_hist�range�len�rstrip�repr�join�quiet�print�logstate)�self�parameter_s�opts�parZ
91 log_outputZ
92 log_raw_inputr r&