Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f99b40c2f3 | ||
|
|
ae00e83c9d | ||
|
|
d4f2f12924 |
@@ -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)
|
||||
|
||||
@@ -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):
|
||||
|
||||
|
||||
51
couchpotato/core/helpers/path.py
Normal file
51
couchpotato/core/helpers/path.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import os
|
||||
from chardet import detect
|
||||
from couchpotato import Env
|
||||
|
||||
fs_enc = Env.get('fs_encoding')
|
||||
|
||||
|
||||
def list_dir(path, full_path = True):
|
||||
"""
|
||||
List directory don't error when it doesn't exist
|
||||
"""
|
||||
|
||||
path = unicode_path(path)
|
||||
|
||||
if os.path.isdir(path):
|
||||
for f in os.listdir(path):
|
||||
if full_path:
|
||||
yield join(path, f)
|
||||
else:
|
||||
yield f
|
||||
|
||||
|
||||
def join(*args):
|
||||
"""
|
||||
Join path, encode properly before joining
|
||||
"""
|
||||
|
||||
return os.path.join(*[safe(x) for x in args])
|
||||
|
||||
|
||||
def unicode_path(path):
|
||||
"""
|
||||
Convert back to unicode
|
||||
:param path: path string
|
||||
"""
|
||||
|
||||
if isinstance(path, str):
|
||||
detected = detect(path)
|
||||
print detected
|
||||
path = path.decode(detected.get('encoding'))
|
||||
path = path.decode('unicode_escape')
|
||||
|
||||
return path
|
||||
|
||||
|
||||
def safe(path):
|
||||
|
||||
if isinstance(path, unicode):
|
||||
return path.encode('unicode_escape')
|
||||
|
||||
return path
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -14,6 +14,7 @@ class Env(object):
|
||||
''' Environment variables '''
|
||||
_app = None
|
||||
_encoding = 'UTF-8'
|
||||
_fs_encoding = 'UTF-8'
|
||||
_debug = False
|
||||
_dev = False
|
||||
_settings = Settings()
|
||||
|
||||
@@ -86,6 +86,7 @@ def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, En
|
||||
encoding = 'UTF-8'
|
||||
|
||||
Env.set('encoding', encoding)
|
||||
Env.set('fs_encoding', sys.getfilesystemencoding())
|
||||
|
||||
# Do db stuff
|
||||
db_path = sp(os.path.join(data_dir, 'database'))
|
||||
@@ -204,7 +205,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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user