Skip to content

Commit

Permalink
attempted fix for logging unicode under windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xnova committed Feb 3, 2014
1 parent e9b1a59 commit bdd77c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion counterpartyd.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def format_feed (feed):
datefmt='%Y-%m-%d-T%H:%M:%S%z')

# Log also to stderr.
console = logging.StreamHandler()
console = util.SanitizedStreamHandler() if os.name == 'nt' else logging.StreamHandler()
console.setLevel(logging.DEBUG if args.verbose else logging.INFO)
formatter = logging.Formatter('%(message)s')
console.setFormatter(formatter)
Expand Down
18 changes: 18 additions & 0 deletions lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
D = decimal.Decimal
import sys
import logging
import copy
import unicodedata
import operator
from operator import itemgetter
import apsw
Expand All @@ -25,6 +27,22 @@
'>=': operator.ge,
}

class SanitizedStreamHandler(logging.StreamHandler):
"""cleans up stdout data for window's cmd.exe (which has broken unicode support out of the box)"""
def emit(self, record):
# If the message doesn't need to be rendered we take a shortcut.
if record.levelno < self.level:
return
# Make sure the message is a string.
message = record.msg
#Sanitize and clean up the message
message = unicodedata.normalize('NFKD', message).encode('ascii', 'ignore').decode()
# Copy the original record so we don't break other handlers.
record = copy.copy(record)
record.msg = message
# Use the built-in stream handler to handle output.
logging.StreamHandler.emit(self, record)

def rowtracer(cursor, sql):
"""Converts fetched SQL data into dict-style"""
dictionary = {}
Expand Down

0 comments on commit bdd77c0

Please sign in to comment.