2020-03-19 16:45:31 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Log handler for Kodi"""
|
|
|
|
|
2020-03-20 13:53:21 +01:00
|
|
|
from __future__ import absolute_import, division, unicode_literals
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcaddon
|
|
|
|
|
2020-10-26 17:25:37 +01:00
|
|
|
from resources.lib import kodiutils
|
|
|
|
|
2020-05-11 14:12:13 +02:00
|
|
|
ADDON = xbmcaddon.Addon()
|
|
|
|
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
class KodiLogHandler(logging.StreamHandler):
|
|
|
|
""" A log handler for Kodi """
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
logging.StreamHandler.__init__(self)
|
2020-05-11 14:12:13 +02:00
|
|
|
formatter = logging.Formatter("[{}] [%(name)s] %(message)s".format(ADDON.getAddonInfo("id")))
|
2020-03-19 16:45:31 +01:00
|
|
|
self.setFormatter(formatter)
|
2020-10-26 17:25:37 +01:00
|
|
|
# xbmc.LOGNOTICE is deprecated in Kodi 19 Matrix
|
|
|
|
if kodiutils.kodi_version_major() > 18:
|
|
|
|
self.info_level = xbmc.LOGINFO
|
|
|
|
else:
|
|
|
|
self.info_level = xbmc.LOGNOTICE
|
2020-03-19 16:45:31 +01:00
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
""" Emit a log message """
|
|
|
|
levels = {
|
|
|
|
logging.CRITICAL: xbmc.LOGFATAL,
|
|
|
|
logging.ERROR: xbmc.LOGERROR,
|
|
|
|
logging.WARNING: xbmc.LOGWARNING,
|
2020-10-26 17:25:37 +01:00
|
|
|
logging.INFO: self.info_level,
|
2020-03-19 16:45:31 +01:00
|
|
|
logging.DEBUG: xbmc.LOGDEBUG,
|
|
|
|
logging.NOTSET: xbmc.LOGNONE,
|
|
|
|
}
|
2020-05-11 14:12:13 +02:00
|
|
|
|
2020-10-26 17:25:37 +01:00
|
|
|
# Map DEBUG level to info_level if debug logging setting has been activated
|
2020-05-11 14:12:13 +02:00
|
|
|
# This is for troubleshooting only
|
|
|
|
if ADDON.getSetting('debug_logging') == 'true':
|
2020-10-26 17:25:37 +01:00
|
|
|
levels[logging.DEBUG] = self.info_level
|
2020-05-11 14:12:13 +02:00
|
|
|
|
2020-03-19 16:45:31 +01:00
|
|
|
try:
|
|
|
|
xbmc.log(self.format(record), levels[record.levelno])
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
xbmc.log(self.format(record).encode('utf-8', 'ignore'), levels[record.levelno])
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
""" Flush the messages """
|
|
|
|
|
|
|
|
|
|
|
|
def config():
|
|
|
|
""" Setup the logger with this handler """
|
|
|
|
logger = logging.getLogger()
|
2020-09-10 21:05:31 +02:00
|
|
|
logger.setLevel(logging.DEBUG) # Make sure we pass all messages, Kodi will do some filtering itself.
|
2020-03-19 16:45:31 +01:00
|
|
|
logger.addHandler(KodiLogHandler())
|