2018-07-13 02:46:02 +10:00
|
|
|
#!/usr/bin/env python
|
2016-07-24 18:59:48 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-07-13 02:46:02 +10:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2016-07-24 18:59:48 +10:00
|
|
|
import logging
|
|
|
|
import xbmc
|
2017-09-06 21:31:15 +10:00
|
|
|
###############################################################################
|
|
|
|
LEVELS = {
|
|
|
|
logging.ERROR: xbmc.LOGERROR,
|
|
|
|
logging.WARNING: xbmc.LOGWARNING,
|
|
|
|
logging.INFO: xbmc.LOGNOTICE,
|
|
|
|
logging.DEBUG: xbmc.LOGDEBUG
|
|
|
|
}
|
|
|
|
###############################################################################
|
2016-07-24 18:59:48 +10:00
|
|
|
|
|
|
|
|
2018-02-11 22:59:04 +11:00
|
|
|
def try_encode(uniString, encoding='utf-8'):
|
2017-09-10 23:12:53 +10:00
|
|
|
"""
|
|
|
|
Will try to encode uniString (in unicode) to encoding. This possibly
|
|
|
|
fails with e.g. Android TV's Python, which does not accept arguments for
|
|
|
|
string.encode()
|
|
|
|
"""
|
|
|
|
if isinstance(uniString, str):
|
|
|
|
# already encoded
|
|
|
|
return uniString
|
|
|
|
try:
|
|
|
|
uniString = uniString.encode(encoding, "ignore")
|
|
|
|
except TypeError:
|
|
|
|
uniString = uniString.encode()
|
|
|
|
return uniString
|
|
|
|
|
|
|
|
|
2016-07-24 18:59:48 +10:00
|
|
|
def config():
|
2016-08-30 03:31:41 +10:00
|
|
|
logger = logging.getLogger('PLEX')
|
2016-07-24 18:59:48 +10:00
|
|
|
logger.addHandler(LogHandler())
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
class LogHandler(logging.StreamHandler):
|
|
|
|
def __init__(self):
|
|
|
|
logging.StreamHandler.__init__(self)
|
2018-08-05 22:44:24 +10:00
|
|
|
self.setFormatter(logging.Formatter(fmt=b"%(name)s: %(message)s"))
|
2016-07-24 18:59:48 +10:00
|
|
|
|
|
|
|
def emit(self, record):
|
2020-12-19 03:10:20 +11:00
|
|
|
if isinstance(record.msg, str):
|
2018-08-05 22:44:24 +10:00
|
|
|
record.msg = record.msg.encode('utf-8')
|
2016-07-24 18:59:48 +10:00
|
|
|
try:
|
2017-09-06 21:31:15 +10:00
|
|
|
xbmc.log(self.format(record), level=LEVELS[record.levelno])
|
|
|
|
except UnicodeEncodeError:
|
2018-02-11 22:59:04 +11:00
|
|
|
xbmc.log(try_encode(self.format(record)),
|
2017-09-06 21:31:15 +10:00
|
|
|
level=LEVELS[record.levelno])
|