Skip to content

Commit

Permalink
Make logging unicode-aware
Browse files Browse the repository at this point in the history
Closes gh-1777
  • Loading branch information
takluyver committed May 30, 2012
1 parent e0e173d commit 0eeff21
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
23 changes: 13 additions & 10 deletions IPython/core/logger.py
Expand Up @@ -14,17 +14,20 @@

# Python standard modules
import glob
import io
import os
import time

from IPython.utils.py3compat import str_to_unicode

#****************************************************************************
# FIXME: This class isn't a mixin anymore, but it still needs attributes from
# ipython and does input cache management. Finish cleanup later...

class Logger(object):
"""A Logfile class with different policies for file creation"""

def __init__(self, home_dir, logfname='Logger.log', loghead='',
def __init__(self, home_dir, logfname='Logger.log', loghead=u'',
logmode='over'):

# this is the full ipython instance, we need some attributes from it
Expand Down Expand Up @@ -84,7 +87,7 @@ def logstart(self,logfname=None,loghead=None,logmode=None,
logmode = self.logmode

if logmode == 'append':
self.logfile = open(self.logfname,'a')
self.logfile = io.open(self.logfname, 'a', encoding='utf-8')

elif logmode == 'backup':
if isfile(self.logfname):
Expand All @@ -94,16 +97,16 @@ def logstart(self,logfname=None,loghead=None,logmode=None,
if isfile(backup_logname):
os.remove(backup_logname)
os.rename(self.logfname,backup_logname)
self.logfile = open(self.logfname,'w')
self.logfile = io.open(self.logfname, 'w', encoding='utf-8')

elif logmode == 'global':
self.logfname = os.path.join(self.home_dir,self.logfname)
self.logfile = open(self.logfname, 'a')
self.logfile = io.open(self.logfname, 'a', encoding='utf-8')

elif logmode == 'over':
if isfile(self.logfname):
os.remove(self.logfname)
self.logfile = open(self.logfname,'w')
self.logfile = io.open(self.logfname,'w', encoding='utf-8')

elif logmode == 'rotate':
if isfile(self.logfname):
Expand All @@ -116,7 +119,7 @@ def logstart(self,logfname=None,loghead=None,logmode=None,
num = int(ext[1:-1])+1
os.rename(f, root+'.'+`num`.zfill(3)+'~')
os.rename(self.logfname, self.logfname+'.001~')
self.logfile = open(self.logfname,'w')
self.logfile = io.open(self.logfname, 'w', encoding='utf-8')

if logmode != 'append':
self.logfile.write(self.loghead)
Expand Down Expand Up @@ -190,13 +193,13 @@ def log_write(self, data, kind='input'):
write = self.logfile.write
if kind=='input':
if self.timestamp:
write(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
time.localtime()))
write(str_to_unicode(time.strftime('# %a, %d %b %Y %H:%M:%S\n',
time.localtime())))
write(data)
elif kind=='output' and self.log_output:
odata = '\n'.join(['#[Out]# %s' % s
odata = u'\n'.join([u'#[Out]# %s' % s
for s in data.splitlines()])
write('%s\n' % odata)
write(u'%s\n' % odata)
self.logfile.flush()

def logstop(self):
Expand Down
13 changes: 7 additions & 6 deletions IPython/core/magics/logging.py
Expand Up @@ -19,6 +19,7 @@
# Our own packages
from IPython.core.magic import Magics, magics_class, line_magic
from IPython.utils.warn import warn
from IPython.utils.py3compat import str_to_unicode

#-----------------------------------------------------------------------------
# Magic implementation classes
Expand Down Expand Up @@ -96,7 +97,7 @@ def logstart(self, parameter_s=''):
logfname = os.path.expanduser(logfname)
self.shell.logfile = logfname

loghead = '# IPython log file\n\n'
loghead = u'# IPython log file\n\n'
try:
logger.logstart(logfname, loghead, logmode, log_output, timestamp,
log_raw_input)
Expand All @@ -121,12 +122,12 @@ def logstart(self, parameter_s=''):
log_write = logger.log_write
output_hist = self.shell.history_manager.output_hist
for n in range(1,len(input_hist)-1):
log_write(input_hist[n].rstrip() + '\n')
log_write(input_hist[n].rstrip() + u'\n')
if n in output_hist:
log_write(repr(output_hist[n]),'output')
log_write(str_to_unicode(repr(output_hist[n])),'output')
else:
logger.log_write('\n'.join(input_hist[1:]))
logger.log_write('\n')
logger.log_write(u'\n'.join(input_hist[1:]))
logger.log_write(u'\n')
if timestamp:
# re-enable timestamping
logger.timestamp = True
Expand All @@ -142,7 +143,7 @@ def logstop(self, parameter_s=''):
In order to start logging again, a new %logstart call needs to be made,
possibly (though not necessarily) with a new filename, mode and other
options."""
self.logger.logstop()
self.shell.logger.logstop()

@line_magic
def logoff(self, parameter_s=''):
Expand Down

0 comments on commit 0eeff21

Please sign in to comment.