Added logging decorator

This commit is contained in:
tomkat83 2016-01-26 17:20:13 +01:00
parent aa849f7457
commit 0d4c8ae53e
23 changed files with 54 additions and 214 deletions

View file

@ -60,17 +60,8 @@ try:
except ImportError:
import xml.etree.ElementTree as etree
# from Version import __VERSION__
# from Debug import * # dprint(), prettyXML()
"""
storage for PMS addresses and additional information - now per aTV! (replaces global PMS_list)
syntax: PMS[<ATV_UDID>][PMS_UUID][<data>]
data: name, ip, ...type (local, myplex)
"""
@utils.logging
class PlexAPI():
# CONSTANTS
# Timeout for POST/GET commands, I guess in seconds
@ -81,7 +72,6 @@ class PlexAPI():
self.__language__ = xbmcaddon.Addon().getLocalizedString
self.g_PMS = {}
client = clientinfo.ClientInfo()
self.addonName = client.getAddonName()
self.addonId = client.getAddonId()
self.clientId = client.getDeviceId()
self.deviceName = client.getDeviceName()
@ -93,10 +83,6 @@ class PlexAPI():
self.doUtils = downloadutils.DownloadUtils()
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def GetPlexLoginFromSettings(self):
"""
Returns empty strings if not found.
@ -1521,6 +1507,7 @@ class PlexAPI():
return xml
@utils.logging
class API():
"""
API(item)
@ -1537,7 +1524,6 @@ class API():
# which media part in the XML response shall we look at?
self.part = 0
self.clientinfo = clientinfo.ClientInfo()
self.addonName = self.clientinfo.getAddonName()
self.clientId = self.clientinfo.getDeviceId()
self.userId = utils.window('emby_currUser')
self.server = utils.window('emby_server%s' % self.userId)
@ -1545,10 +1531,6 @@ class API():
self.jumpback = int(utils.settings('resumeJumpBack'))
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def setChildNumber(self, number=0):
"""
Which child in the XML response shall we look at and work with?

View file

@ -12,12 +12,12 @@ from plexbmchelper import listener, plexgdm, subscribers
from plexbmchelper.settings import settings
@utils.logging
@utils.ThreadMethods
class PlexCompanion(threading.Thread):
def __init__(self):
self.port = int(utils.settings('companionPort'))
ci = clientinfo.ClientInfo()
self.addonName = ci.getAddonName()
self.clientId = ci.getDeviceId()
self.deviceName = ci.getDeviceName()
self.logMsg("----===## Starting PlexBMC Helper ##===----", 1)
@ -34,10 +34,6 @@ class PlexCompanion(threading.Thread):
threading.Thread.__init__(self)
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def run(self):
start_count = 0
while True:

View file

@ -12,12 +12,12 @@ import xbmcgui
import xbmcvfs
import utils
import clientinfo
import image_cache_thread
#################################################################################################
@utils.logging
class Artwork():
xbmc_host = 'localhost'
@ -29,8 +29,6 @@ class Artwork():
imageCacheLimitThreads = 0
def __init__(self):
self.clientinfo = clientinfo.ClientInfo()
self.addonName = self.clientinfo.getAddonName()
self.enableTextureCache = utils.settings('enableTextureCache') == "true"
self.imageCacheLimitThreads = int(utils.settings("imageCacheLimit"))
@ -43,11 +41,6 @@ class Artwork():
self.userId = utils.window('emby_currUser')
self.server = utils.window('emby_server%s' % self.userId)
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def double_urlencode(self, text):
text = self.single_urlencode(text)
text = self.single_urlencode(text)
@ -503,6 +496,7 @@ class Artwork():
% (self.server, itemid, itemtype))
return image
def getAllArtwork(self, item, parentInfo=False):
server = self.server

View file

@ -14,19 +14,13 @@ import utils
#################################################################################################
@utils.logging
class ClientInfo():
def __init__(self):
self.addon = xbmcaddon.Addon()
self.addonName = self.getAddonName()
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def getAddonName(self):
# Used for logging

View file

@ -2,11 +2,11 @@
##################################################################################################
import json
# import json
import requests
import logging
# import logging
import xbmc
# import xbmc
import xbmcgui
import utils
@ -28,28 +28,21 @@ requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
##################################################################################################
@utils.logging
class DownloadUtils():
# Borg - multiple instances, shared state
_shared_state = {}
clientInfo = clientinfo.ClientInfo()
addonName = clientInfo.getAddonName()
# Requests session
s = None
timeout = 30
def __init__(self):
self.__dict__ = self._shared_state
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def setUsername(self, username):
# Reserved for userclient only
self.username = username

View file

@ -3,26 +3,17 @@
#################################################################################################
import utils
import clientinfo
#################################################################################################
@utils.logging
class Embydb_Functions():
def __init__(self, embycursor):
self.embycursor = embycursor
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def getViews(self):
embycursor = self.embycursor

View file

@ -21,7 +21,6 @@ import downloadutils
import librarysync
import read_embyserver as embyserver
import embydb_functions as embydb
import playlist
import playbackutils as pbutils
import playutils
import api

View file

@ -3,6 +3,8 @@ import utils
import xbmc
import requests
@utils.logging
class image_cache_thread(threading.Thread):
urlToProcess = None
@ -16,11 +18,7 @@ class image_cache_thread(threading.Thread):
def __init__(self):
self.monitor = xbmc.Monitor()
threading.Thread.__init__(self)
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s" % className, msg, lvl)
def setUrl(self, url):
self.urlToProcess = url

View file

@ -16,6 +16,7 @@ import PlexAPI
#################################################################################################
@utils.logging
class InitialSetup():
def __init__(self):
@ -24,16 +25,10 @@ class InitialSetup():
self.__language__ = self.addon.getLocalizedString
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.addonId = self.clientInfo.getAddonId()
self.doUtils = downloadutils.DownloadUtils()
self.userClient = userclient.UserClient()
self.plx = PlexAPI.PlexAPI()
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def setup(self):
"""

View file

@ -12,7 +12,6 @@ import xbmcvfs
import api
import artwork
import clientinfo
import downloadutils
import utils
import embydb_functions as embydb
@ -20,11 +19,11 @@ import kodidb_functions as kodidb
import read_embyserver as embyserver
import musicutils as musicutils
import PlexAPI
import sys
##################################################################################################
@utils.logging
class Items(object):
"""
Items to be called with "with Items as xxx:" to ensure that __enter__
@ -32,8 +31,6 @@ class Items(object):
"""
def __init__(self):
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.doUtils = downloadutils.DownloadUtils()
self.kodiversion = int(xbmc.getInfoLabel("System.BuildVersion")[:2])
@ -66,12 +63,6 @@ class Items(object):
self.kodiconn.close()
return self
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def itemsbyId(self, items, process, pdialog=None):
# Process items by itemid. Process can be added, update, userdata, remove
emby = self.emby

View file

@ -4,7 +4,6 @@
import xbmc
import api
import artwork
import clientinfo
import utils
@ -12,25 +11,18 @@ import utils
##################################################################################################
@utils.logging
class Kodidb_Functions():
kodiversion = int(xbmc.getInfoLabel("System.BuildVersion")[:2])
def __init__(self, cursor):
self.cursor = cursor
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.artwork = artwork.Artwork()
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def addPath(self, path):
cursor = self.cursor

View file

@ -7,7 +7,6 @@ import json
import xbmc
import xbmcgui
import clientinfo
import downloadutils
import embydb_functions as embydb
import playbackutils as pbutils
@ -18,23 +17,16 @@ from urllib import urlencode
#################################################################################################
@utils.logging
class KodiMonitor(xbmc.Monitor):
def __init__(self):
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.doUtils = downloadutils.DownloadUtils()
self.logMsg("Kodi monitor started.", 1)
def logMsg(self, msg, lvl=1):
self.className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, self.className), msg, lvl)
def onScanStarted(self, library):
self.logMsg("Kodi library scan %s running." % library, 2)
if library == "video":

View file

@ -177,6 +177,7 @@ class ThreadedShowSyncInfo(threading.Thread):
self.dialog.close()
@utils.logging
@utils.ThreadMethodsStopsync
@utils.ThreadMethods
class LibrarySync(threading.Thread):
@ -196,7 +197,6 @@ class LibrarySync(threading.Thread):
self.__dict__ = self._shared_state
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.doUtils = downloadutils.DownloadUtils()
self.user = userclient.UserClient()
self.emby = embyserver.Read_EmbyServer()
@ -206,12 +206,6 @@ class LibrarySync(threading.Thread):
threading.Thread.__init__(self)
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def progressDialog(self, title, forced=False):
dialog = None

View file

@ -10,7 +10,6 @@ import xbmcgui
import xbmcplugin
import artwork
import clientinfo
import downloadutils
import playutils as putils
import playlist
@ -22,6 +21,7 @@ import PlexAPI
#################################################################################################
@utils.logging
class PlaybackUtils():
@ -30,8 +30,6 @@ class PlaybackUtils():
self.item = item
self.API = PlexAPI.API(self.item)
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.doUtils = downloadutils.DownloadUtils()
self.userid = utils.window('emby_currUser')
@ -42,11 +40,6 @@ class PlaybackUtils():
self.emby = embyserver.Read_EmbyServer()
self.pl = playlist.Playlist()
def logMsg(self, msg, lvl=1):
self.className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, self.className), msg, lvl)
def play(self, itemid, dbid=None, seektime=None):
self.logMsg("Play called.", 1)

View file

@ -10,7 +10,6 @@ import xbmcgui
import utils
import clientinfo
import downloadutils
import kodidb_functions as kodidb
import websocket_client as wsc
from urllib import urlencode
@ -18,6 +17,7 @@ from urllib import urlencode
#################################################################################################
@utils.logging
class Player(xbmc.Player):
# Borg - multiple instances, shared state
@ -27,25 +27,17 @@ class Player(xbmc.Player):
playStats = {}
currentFile = None
def __init__(self):
self.__dict__ = self._shared_state
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.doUtils = downloadutils.DownloadUtils()
self.ws = wsc.WebSocket_Client()
self.xbmcplayer = xbmc.Player()
self.logMsg("Starting playback monitor.", 2)
def logMsg(self, msg, lvl=1):
self.className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, self.className), msg, lvl)
def GetPlayStats(self):
return self.playStats

View file

@ -6,9 +6,7 @@ import json
import xbmc
import xbmcgui
import xbmcplugin
import clientinfo
import playutils
import playbackutils
import embydb_functions as embydb
@ -18,25 +16,15 @@ import utils
#################################################################################################
@utils.logging
class Playlist():
def __init__(self):
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.userid = utils.window('emby_currUser')
self.server = utils.window('emby_server%s' % self.userid)
self.emby = embyserver.Read_EmbyServer()
def logMsg(self, msg, lvl=1):
self.className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, self.className), msg, lvl)
def playAll(self, itemids, startat):
embyconn = utils.kodiSQL('emby')

View file

@ -14,6 +14,7 @@ import PlexAPI
#################################################################################################
@utils.logging
class PlayUtils():
@ -22,7 +23,6 @@ class PlayUtils():
self.item = item
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.userid = utils.window('emby_currUser')
self.server = utils.window('emby_server%s' % self.userid)
@ -30,12 +30,6 @@ class PlayUtils():
self.API = PlexAPI.API(item)
def logMsg(self, msg, lvl=1):
self.className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, self.className), msg, lvl)
def getPlayUrl(self, child=0, partIndex=None):
item = self.item
# NO, I am not very fond of this construct!

View file

@ -3,32 +3,23 @@
#################################################################################################
import utils
import clientinfo
import downloadutils
#################################################################################################
@utils.logging
class Read_EmbyServer():
limitIndex = int(utils.settings('limitindex'))
def __init__(self):
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.doUtils = downloadutils.DownloadUtils()
self.userId = utils.window('emby_currUser')
self.server = utils.window('emby_server%s' % self.userId)
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def split_list(self, itemlist, size):
# Split up list in pieces of size. Will generate a list of lists
return [itemlist[i:i+size] for i in range(0, len(itemlist), size)]

View file

@ -10,7 +10,6 @@ import xbmcaddon
import xbmcvfs
import utils
import clientinfo
import downloadutils
import PlexAPI
@ -19,6 +18,7 @@ import librarysync
##################################################################################################
@utils.logging
@utils.ThreadMethods
class UserClient(threading.Thread):
@ -37,23 +37,15 @@ class UserClient(threading.Thread):
userSettings = None
def __init__(self):
self.__dict__ = self.__shared_state
self.addon = xbmcaddon.Addon()
self.addonName = clientinfo.ClientInfo().getAddonName()
self.doUtils = downloadutils.DownloadUtils()
threading.Thread.__init__(self)
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def getAdditionalUsers(self):
additionalUsers = utils.settings('additionalUsers')

View file

@ -9,16 +9,18 @@ import sqlite3
import time
import unicodedata
import xml.etree.ElementTree as etree
from functools import wraps, update_wrapper
import xbmc
import xbmcaddon
import xbmcgui
import xbmcvfs
import clientinfo
#################################################################################################
addonName = xbmcaddon.Addon().getAddonInfo('name')
def ThreadMethodsStopsync(cls):
"""
@ -82,32 +84,29 @@ def ThreadMethods(cls):
return cls
class logDecor(object):
def logging(cls):
"""
A decorator adding logging capabilities.
A decorator adding logging capabilities to classes. Also adds
self.addonName to the class
Syntax: self.logMsg(message, loglevel)
Loglevel: -2 (Error) to 2 (DB debug)
"""
def __init__(self, f):
"""
If there are no decorator arguments, the function to be decorated is
passed to the constructor.
"""
self.f = f
self.addonName = clientinfo.ClientInfo().getAddonName()
# Attach new attributes to class
cls.addonName = addonName
def __call__(self, *args, **kwargs):
"""
The __call__ method is not called until the
decorated function is called.
"""
def decorLog(self, msg, lvl=1):
className = self.__class__.__name__
logMsg("%s %s" % (self.addonName, className), msg, lvl)
# The function itself:
self.f(*args, **kwargs)
# Define new class methods and attach them to class
def newFunction(self, msg, lvl=0):
title = "%s %s" % (self.addonName, cls.__name__)
logMsg(title, msg, lvl)
cls.logMsg = newFunction
# Override the name, we don't want the decorators name showing up
__name__ = cls.__name__
# Return class to render this a decorator
return cls
def logMsg(title, msg, level=1):
@ -120,17 +119,21 @@ def logMsg(title, msg, level=1):
if logLevel >= level:
if logLevel == 2: # inspect.stack() is expensive
if logLevel == 2: # inspect is expensive
func = inspect.currentframe().f_back.f_back.f_code
try:
xbmc.log("%s -> %s : %s" % (title, inspect.stack()[1][3], msg))
xbmc.log("%s -> %s : %s" % (
title, func.co_name, msg))
except UnicodeEncodeError:
xbmc.log("%s -> %s : %s" % (title, inspect.stack()[1][3], msg.encode('utf-8')))
xbmc.log("%s -> %s : %s" % (
title, func.co_name, msg.encode('utf-8')))
else:
try:
xbmc.log("%s -> %s" % (title, msg))
except UnicodeEncodeError:
xbmc.log("%s -> %s" % (title, msg.encode('utf-8')))
def window(property, value=None, clear=False, windowid=10000):
# Get or set window property
WINDOW = xbmcgui.Window(windowid)

View file

@ -6,31 +6,19 @@ import shutil
import xml.etree.ElementTree as etree
import xbmc
import xbmcaddon
import xbmcvfs
import clientinfo
import utils
#################################################################################################
@utils.logging
class VideoNodes(object):
def __init__(self):
clientInfo = clientinfo.ClientInfo()
self.addonName = clientInfo.getAddonName()
self.kodiversion = int(xbmc.getInfoLabel("System.BuildVersion")[:2])
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def commonRoot(self, order, label, tagname, roottype=1):
if roottype == 0:

View file

@ -22,6 +22,7 @@ logging.basicConfig()
#################################################################################################
@utils.logging
class WebSocket_Client(threading.Thread):
_shared_state = {}
@ -37,18 +38,11 @@ class WebSocket_Client(threading.Thread):
self.doUtils = downloadutils.DownloadUtils()
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
self.deviceId = self.clientInfo.getDeviceId()
self.librarySync = librarysync.LibrarySync()
threading.Thread.__init__(self)
def logMsg(self, msg, lvl=1):
self.className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, self.className), msg, lvl)
def sendProgressUpdate(self, data):
self.logMsg("sendProgressUpdate", 2)
try:

View file

@ -37,6 +37,7 @@ import PlexCompanion
#################################################################################################
@utils.logging
class Service():
welcome_msg = True
@ -53,7 +54,6 @@ class Service():
def __init__(self):
self.clientInfo = clientinfo.ClientInfo()
self.addonName = self.clientInfo.getAddonName()
logLevel = userclient.UserClient().getLogLevel()
self.monitor = xbmc.Monitor()
@ -89,12 +89,6 @@ class Service():
# Set the minimum database version
utils.window('emby_minDBVersion', value="1.1.63")
def logMsg(self, msg, lvl=1):
className = self.__class__.__name__
utils.logMsg("%s %s" % (self.addonName, className), msg, lvl)
def ServiceEntryPoint(self):
# Important: Threads depending on abortRequest will not trigger
# if profile switch happens more than once.