diff --git a/CouchPotato.py b/CouchPotato.py index afc46aa..85d885c 100755 --- a/CouchPotato.py +++ b/CouchPotato.py @@ -61,7 +61,7 @@ class Loader(object): self.log = CPLog(__name__) formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%H:%M:%S') - hdlr = handlers.RotatingFileHandler(os.path.join(self.log_dir, 'error.log'), 'a', 500000, 10) + hdlr = handlers.RotatingFileHandler(os.path.join(self.log_dir, 'error.log'), 'a', 500000, 10, encoding = 'utf-8') hdlr.setLevel(logging.CRITICAL) hdlr.setFormatter(formatter) self.log.logger.addHandler(hdlr) diff --git a/couchpotato/core/helpers/encoding.py b/couchpotato/core/helpers/encoding.py index c65fe87..6c4faf8 100644 --- a/couchpotato/core/helpers/encoding.py +++ b/couchpotato/core/helpers/encoding.py @@ -47,6 +47,17 @@ def toUnicode(original, *args): ascii_text = str(original).encode('string_escape') return toUnicode(ascii_text) +def toUTF8(original): + try: + if isinstance(original, str) and len(original) > 0: + # Try to detect + detected = detect(original) + return original.decode(detected.get('encoding')).encode('utf-8') + else: + return original + except: + #log.error('Failed encoding to UTF8: %s', traceback.format_exc()) + raise def ss(original, *args): diff --git a/couchpotato/core/logger.py b/couchpotato/core/logger.py index a1b5e7d..338dca6 100644 --- a/couchpotato/core/logger.py +++ b/couchpotato/core/logger.py @@ -1,5 +1,6 @@ import logging import re +import traceback class CPLog(object): @@ -54,19 +55,19 @@ class CPLog(object): def safeMessage(self, msg, replace_tuple = ()): - from couchpotato.core.helpers.encoding import ss, toUnicode + from couchpotato.core.helpers.encoding import ss, toUTF8 - msg = ss(msg) + msg = toUTF8(msg) try: if isinstance(replace_tuple, tuple): - msg = msg % tuple([ss(x) if not isinstance(x, (int, float)) else x for x in list(replace_tuple)]) + msg = msg % tuple([toUTF8(x) for x in list(replace_tuple)]) elif isinstance(replace_tuple, dict): - msg = msg % dict((k, ss(v)) for k, v in replace_tuple.iteritems()) + msg = msg % dict((k, toUTF8(v)) for k, v in replace_tuple.iteritems()) else: - msg = msg % ss(replace_tuple) - except Exception as e: - self.logger.error('Failed encoding stuff to log "%s": %s' % (msg, e)) + msg = msg % toUTF8(replace_tuple) + except: + self.logger.error('Failed encoding stuff to log "%s": %s' % (msg, traceback.format_exc())) self.setup() if not self.is_develop: @@ -83,4 +84,4 @@ class CPLog(object): except: pass - return toUnicode(msg) + return toUTF8(msg) diff --git a/couchpotato/core/plugins/log/main.py b/couchpotato/core/plugins/log/main.py index 003529b..ca1e54e 100644 --- a/couchpotato/core/plugins/log/main.py +++ b/couchpotato/core/plugins/log/main.py @@ -1,9 +1,9 @@ +import codecs import os import re import traceback from couchpotato.api import addApiView -from couchpotato.core.helpers.encoding import toUnicode from couchpotato.core.helpers.variable import tryInt, splitString from couchpotato.core.logger import CPLog from couchpotato.core.plugins.base import Plugin @@ -103,9 +103,8 @@ class Logging(Plugin): if not os.path.isfile(path): break - f = open(path, 'r') - log_content = toUnicode(f.read()) - raw_lines = self.toList(log_content) + f = codecs.open(path, 'r', 'utf-8') + raw_lines = self.toList(f.read()) raw_lines.reverse() brk = False @@ -131,7 +130,7 @@ class Logging(Plugin): def toList(self, log_content = ''): - logs_raw = toUnicode(log_content).split('[0m\n') + logs_raw = log_content.split('[0m\n') logs = [] for log_line in logs_raw: diff --git a/couchpotato/runner.py b/couchpotato/runner.py index 36fc366..93958ba 100644 --- a/couchpotato/runner.py +++ b/couchpotato/runner.py @@ -204,7 +204,7 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En logger.addHandler(hdlr) # To file - hdlr2 = handlers.RotatingFileHandler(Env.get('log_path'), 'a', 500000, 10, encoding = Env.get('encoding')) + hdlr2 = handlers.RotatingFileHandler(Env.get('log_path'), 'a', 500000, 10, encoding = 'utf-8') hdlr2.setFormatter(formatter) logger.addHandler(hdlr2)