##// END OF EJS Templates
Make logging unicode-aware...
Thomas Kluyver -
Show More
@@ -14,9 +14,12 b''
14
14
15 # Python standard modules
15 # Python standard modules
16 import glob
16 import glob
17 import io
17 import os
18 import os
18 import time
19 import time
19
20
21 from IPython.utils.py3compat import str_to_unicode
22
20 #****************************************************************************
23 #****************************************************************************
21 # FIXME: This class isn't a mixin anymore, but it still needs attributes from
24 # FIXME: This class isn't a mixin anymore, but it still needs attributes from
22 # ipython and does input cache management. Finish cleanup later...
25 # ipython and does input cache management. Finish cleanup later...
@@ -24,7 +27,7 b' import time'
24 class Logger(object):
27 class Logger(object):
25 """A Logfile class with different policies for file creation"""
28 """A Logfile class with different policies for file creation"""
26
29
27 def __init__(self, home_dir, logfname='Logger.log', loghead='',
30 def __init__(self, home_dir, logfname='Logger.log', loghead=u'',
28 logmode='over'):
31 logmode='over'):
29
32
30 # this is the full ipython instance, we need some attributes from it
33 # this is the full ipython instance, we need some attributes from it
@@ -84,7 +87,7 b' class Logger(object):'
84 logmode = self.logmode
87 logmode = self.logmode
85
88
86 if logmode == 'append':
89 if logmode == 'append':
87 self.logfile = open(self.logfname,'a')
90 self.logfile = io.open(self.logfname, 'a', encoding='utf-8')
88
91
89 elif logmode == 'backup':
92 elif logmode == 'backup':
90 if isfile(self.logfname):
93 if isfile(self.logfname):
@@ -94,16 +97,16 b' class Logger(object):'
94 if isfile(backup_logname):
97 if isfile(backup_logname):
95 os.remove(backup_logname)
98 os.remove(backup_logname)
96 os.rename(self.logfname,backup_logname)
99 os.rename(self.logfname,backup_logname)
97 self.logfile = open(self.logfname,'w')
100 self.logfile = io.open(self.logfname, 'w', encoding='utf-8')
98
101
99 elif logmode == 'global':
102 elif logmode == 'global':
100 self.logfname = os.path.join(self.home_dir,self.logfname)
103 self.logfname = os.path.join(self.home_dir,self.logfname)
101 self.logfile = open(self.logfname, 'a')
104 self.logfile = io.open(self.logfname, 'a', encoding='utf-8')
102
105
103 elif logmode == 'over':
106 elif logmode == 'over':
104 if isfile(self.logfname):
107 if isfile(self.logfname):
105 os.remove(self.logfname)
108 os.remove(self.logfname)
106 self.logfile = open(self.logfname,'w')
109 self.logfile = io.open(self.logfname,'w', encoding='utf-8')
107
110
108 elif logmode == 'rotate':
111 elif logmode == 'rotate':
109 if isfile(self.logfname):
112 if isfile(self.logfname):
@@ -116,7 +119,7 b' class Logger(object):'
116 num = int(ext[1:-1])+1
119 num = int(ext[1:-1])+1
117 os.rename(f, root+'.'+`num`.zfill(3)+'~')
120 os.rename(f, root+'.'+`num`.zfill(3)+'~')
118 os.rename(self.logfname, self.logfname+'.001~')
121 os.rename(self.logfname, self.logfname+'.001~')
119 self.logfile = open(self.logfname,'w')
122 self.logfile = io.open(self.logfname, 'w', encoding='utf-8')
120
123
121 if logmode != 'append':
124 if logmode != 'append':
122 self.logfile.write(self.loghead)
125 self.logfile.write(self.loghead)
@@ -190,13 +193,13 b' which already exists. But you must first start the logging process with'
190 write = self.logfile.write
193 write = self.logfile.write
191 if kind=='input':
194 if kind=='input':
192 if self.timestamp:
195 if self.timestamp:
193 write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
196 write(str_to_unicode(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
194 time.localtime()))
197 time.localtime())))
195 write(data)
198 write(data)
196 elif kind=='output' and self.log_output:
199 elif kind=='output' and self.log_output:
197 odata = '\n'.join(['#[Out]# %s' % s
200 odata = u'\n'.join([u'#[Out]# %s' % s
198 for s in data.splitlines()])
201 for s in data.splitlines()])
199 write('%s\n' % odata)
202 write(u'%s\n' % odata)
200 self.logfile.flush()
203 self.logfile.flush()
201
204
202 def logstop(self):
205 def logstop(self):
@@ -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 IPython.utils.warn import warn
21 from IPython.utils.warn import warn
22 from IPython.utils.py3compat import str_to_unicode
22
23
23 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
24 # Magic implementation classes
25 # Magic implementation classes
@@ -96,7 +97,7 b' class LoggingMagics(Magics):'
96 logfname = os.path.expanduser(logfname)
97 logfname = os.path.expanduser(logfname)
97 self.shell.logfile = logfname
98 self.shell.logfile = logfname
98
99
99 loghead = '# IPython log file\n\n'
100 loghead = u'# IPython log file\n\n'
100 try:
101 try:
101 logger.logstart(logfname, loghead, logmode, log_output, timestamp,
102 logger.logstart(logfname, loghead, logmode, log_output, timestamp,
102 log_raw_input)
103 log_raw_input)
@@ -121,12 +122,12 b' class LoggingMagics(Magics):'
121 log_write = logger.log_write
122 log_write = logger.log_write
122 output_hist = self.shell.history_manager.output_hist
123 output_hist = self.shell.history_manager.output_hist
123 for n in range(1,len(input_hist)-1):
124 for n in range(1,len(input_hist)-1):
124 log_write(input_hist[n].rstrip() + '\n')
125 log_write(input_hist[n].rstrip() + u'\n')
125 if n in output_hist:
126 if n in output_hist:
126 log_write(repr(output_hist[n]),'output')
127 log_write(str_to_unicode(repr(output_hist[n])),'output')
127 else:
128 else:
128 logger.log_write('\n'.join(input_hist[1:]))
129 logger.log_write(u'\n'.join(input_hist[1:]))
129 logger.log_write('\n')
130 logger.log_write(u'\n')
130 if timestamp:
131 if timestamp:
131 # re-enable timestamping
132 # re-enable timestamping
132 logger.timestamp = True
133 logger.timestamp = True
@@ -142,7 +143,7 b' class LoggingMagics(Magics):'
142 In order to start logging again, a new %logstart call needs to be made,
143 In order to start logging again, a new %logstart call needs to be made,
143 possibly (though not necessarily) with a new filename, mode and other
144 possibly (though not necessarily) with a new filename, mode and other
144 options."""
145 options."""
145 self.logger.logstop()
146 self.shell.logger.logstop()
146
147
147 @line_magic
148 @line_magic
148 def logoff(self, parameter_s=''):
149 def logoff(self, parameter_s=''):
General Comments 0
You need to be logged in to leave comments. Login now