Skip to content

Commit

Permalink
Made sure that logs always end up in the log file even on encoding er…
Browse files Browse the repository at this point in the history
…rors
  • Loading branch information
jrosdahl committed Aug 24, 2011
1 parent 80bf19e commit 3eedcd5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions maildirproc
Expand Up @@ -99,6 +99,14 @@ def sha1sum(fp):
return sha_obj.hexdigest()


def safe_write(fp, s):
line = u"%s\n" % s
try:
fp.write(line)
except UnicodeEncodeError:
fp.write(ascii(line))


class MailHeader(object):
def __init__(self, mail, name, text):
assert isinstance(text, unicode)
Expand Down Expand Up @@ -532,7 +540,7 @@ class MaildirProcessor(object):

def log(self, text, level=1):
if level <= self._log_level:
self._log_fp.write(u"%s\n" % text)
safe_write(self._log_fp, text)
self._log_fp.flush()

def log_debug(self, text):
Expand All @@ -546,7 +554,7 @@ class MaildirProcessor(object):

def fatal_error(self, text):
self.log_error(text)
sys.stderr.write(u"%s\n" % text)
safe_write(sys.stderr, text)
sys.exit(1)

# ----------------------------------------------------------------
Expand Down

0 comments on commit 3eedcd5

Please sign in to comment.