2016-07-24 08:59:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
import logging
|
2018-09-06 20:40:23 +00:00
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
import xbmc
|
|
|
|
|
2018-09-06 08:36:32 +00:00
|
|
|
import database
|
|
|
|
from . import window
|
2016-07-24 08:59:48 +00:00
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
def config():
|
2016-08-22 02:51:23 +00:00
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
logger = logging.getLogger('EMBY')
|
|
|
|
logger.addHandler(LogHandler())
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
class LogHandler(logging.StreamHandler):
|
|
|
|
|
|
|
|
def __init__(self):
|
2016-08-22 02:51:23 +00:00
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
logging.StreamHandler.__init__(self)
|
|
|
|
self.setFormatter(MyFormatter())
|
|
|
|
|
2018-09-06 08:36:32 +00:00
|
|
|
self.sensitive = {'Token': [], 'Server': []}
|
|
|
|
|
|
|
|
for server in database.get_credentials()['Servers']:
|
|
|
|
|
|
|
|
if server.get('AccessToken'):
|
|
|
|
self.sensitive['Token'].append(server['AccessToken'])
|
|
|
|
|
2018-09-08 19:25:15 +00:00
|
|
|
if server.get('LocalAddress'):
|
|
|
|
self.sensitive['Server'].append(server['LocalAddress'].split('://')[1])
|
2018-09-08 07:01:45 +00:00
|
|
|
|
|
|
|
if server.get('RemoteAddress'):
|
|
|
|
self.sensitive['Server'].append(server['RemoteAddress'].split('://')[1])
|
2018-09-06 08:36:32 +00:00
|
|
|
|
|
|
|
if server.get('ManualAddress'):
|
|
|
|
self.sensitive['Server'].append(server['ManualAddress'].split('://')[1])
|
|
|
|
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
def emit(self, record):
|
|
|
|
|
2016-08-22 02:51:23 +00:00
|
|
|
if self._get_log_level(record.levelno):
|
2018-03-03 00:16:43 +00:00
|
|
|
string = self.format(record)
|
2018-09-06 08:36:32 +00:00
|
|
|
|
|
|
|
for server in self.sensitive['Server']:
|
|
|
|
string = string.replace(server or "{server}", "{emby-server}")
|
|
|
|
|
|
|
|
for token in self.sensitive['Token']:
|
|
|
|
string = string.replace(token or "{token}", "{emby-token}")
|
2016-07-24 08:59:48 +00:00
|
|
|
try:
|
2018-03-03 00:16:43 +00:00
|
|
|
xbmc.log(string, level=xbmc.LOGNOTICE)
|
2016-07-24 08:59:48 +00:00
|
|
|
except UnicodeEncodeError:
|
2018-03-03 00:16:43 +00:00
|
|
|
xbmc.log(string.encode('utf-8'), level=xbmc.LOGNOTICE)
|
2016-07-24 08:59:48 +00:00
|
|
|
|
2016-08-22 02:51:23 +00:00
|
|
|
@classmethod
|
|
|
|
def _get_log_level(cls, level):
|
2016-07-24 08:59:48 +00:00
|
|
|
|
|
|
|
levels = {
|
|
|
|
logging.ERROR: 0,
|
|
|
|
logging.WARNING: 0,
|
|
|
|
logging.INFO: 1,
|
2016-07-24 23:46:24 +00:00
|
|
|
logging.DEBUG: 2
|
2016-07-24 08:59:48 +00:00
|
|
|
}
|
|
|
|
try:
|
2016-08-22 02:51:23 +00:00
|
|
|
log_level = int(window('emby_logLevel'))
|
2016-07-24 08:59:48 +00:00
|
|
|
except ValueError:
|
2016-08-22 02:51:23 +00:00
|
|
|
log_level = 0
|
2016-07-24 08:59:48 +00:00
|
|
|
|
2016-08-22 02:51:23 +00:00
|
|
|
return log_level >= levels[level]
|
2016-07-24 08:59:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyFormatter(logging.Formatter):
|
|
|
|
|
|
|
|
def __init__(self, fmt="%(name)s -> %(message)s"):
|
2016-08-22 02:51:23 +00:00
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
logging.Formatter.__init__(self, fmt)
|
|
|
|
|
|
|
|
def format(self, record):
|
|
|
|
|
|
|
|
# Save the original format configured by the user
|
|
|
|
# when the logger formatter was instantiated
|
|
|
|
format_orig = self._fmt
|
|
|
|
|
|
|
|
# Replace the original format with one customized by logging level
|
|
|
|
if record.levelno in (logging.DEBUG, logging.ERROR):
|
|
|
|
self._fmt = '%(name)s -> %(levelname)s:: %(message)s'
|
|
|
|
|
|
|
|
# Call the original formatter class to do the grunt work
|
|
|
|
result = logging.Formatter.format(self, record)
|
|
|
|
|
|
|
|
# Restore the original format configured by the user
|
|
|
|
self._fmt = format_orig
|
|
|
|
|
2016-08-22 02:51:23 +00:00
|
|
|
return result
|