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