riscritto logger
This commit is contained in:
@@ -2,113 +2,41 @@
|
||||
# --------------------------------------------------------------------------------
|
||||
# Logger (kodi)
|
||||
# --------------------------------------------------------------------------------
|
||||
|
||||
import inspect, sys, os, xbmc
|
||||
from __future__ import unicode_literals
|
||||
import inspect, os, xbmc, sys
|
||||
from platformcode import config
|
||||
|
||||
PY3 = False
|
||||
if sys.version_info[0] >= 3: PY3 = True; unicode = str; unichr = chr; long = int
|
||||
|
||||
loggeractive = (config.get_setting("debug") == True)
|
||||
LOG_FORMAT = '{addname}[{filename}.{function}:{line}]{sep} {message}'
|
||||
DEBUG_ENABLED = config.get_setting("debug")
|
||||
DEF_LEVEL = xbmc.LOGINFO if sys.version_info[0] >= 3 else xbmc.LOGNOTICE
|
||||
|
||||
|
||||
def log_enable(active):
|
||||
global loggeractive
|
||||
loggeractive = active
|
||||
def info(*args):
|
||||
log(*args)
|
||||
|
||||
|
||||
def encode_log(message=""):
|
||||
|
||||
# Unicode to utf8
|
||||
if isinstance(message, unicode):
|
||||
message = message.encode("utf8")
|
||||
if PY3: message = message.decode("utf8")
|
||||
|
||||
# All encodings to utf8
|
||||
elif not PY3 and isinstance(message, str):
|
||||
message = unicode(message, "utf8", errors="replace").encode("utf8")
|
||||
|
||||
# Bytes encodings to utf8
|
||||
elif PY3 and isinstance(message, bytes):
|
||||
message = message.decode("utf8")
|
||||
|
||||
# Objects to string
|
||||
else:
|
||||
message = str(message)
|
||||
|
||||
return message
|
||||
def debug(*args):
|
||||
if DEBUG_ENABLED:
|
||||
log(*args)
|
||||
|
||||
|
||||
def get_caller(message=None):
|
||||
|
||||
if message and isinstance(message, unicode):
|
||||
message = message.encode("utf8")
|
||||
if PY3: message = message.decode("utf8")
|
||||
elif message and PY3 and isinstance(message, bytes):
|
||||
message = message.decode("utf8")
|
||||
elif message and not PY3:
|
||||
message = unicode(message, "utf8", errors="replace").encode("utf8")
|
||||
elif message:
|
||||
message = str(message)
|
||||
|
||||
module = inspect.getmodule(inspect.currentframe().f_back.f_back)
|
||||
|
||||
if module == None:
|
||||
module = "None"
|
||||
else:
|
||||
module = module.__name__
|
||||
|
||||
function = inspect.currentframe().f_back.f_back.f_code.co_name
|
||||
|
||||
if module == "__main__":
|
||||
module = "kod"
|
||||
else:
|
||||
module = "kod." + module
|
||||
if message:
|
||||
if module not in message:
|
||||
if function == "<module>":
|
||||
return module + " " + message
|
||||
else:
|
||||
return module + " [" + function + "] " + message
|
||||
else:
|
||||
return message
|
||||
else:
|
||||
if function == "<module>":
|
||||
return module
|
||||
else:
|
||||
return module + "." + function
|
||||
def error(*args):
|
||||
log("######## ERROR #########", level=xbmc.LOGERROR)
|
||||
log(*args, level=xbmc.LOGERROR)
|
||||
|
||||
|
||||
def info(texto=""):
|
||||
if loggeractive:
|
||||
xbmc.log(get_caller(encode_log(texto)), xbmc.LOGNOTICE)
|
||||
|
||||
|
||||
def debug(texto=""):
|
||||
if loggeractive:
|
||||
texto = " [" + get_caller() + "] " + encode_log(texto)
|
||||
|
||||
xbmc.log("######## DEBUG #########", xbmc.LOGNOTICE)
|
||||
xbmc.log(texto, xbmc.LOGNOTICE)
|
||||
|
||||
|
||||
def error(texto=""):
|
||||
texto = " [" + get_caller() + "] " + encode_log(texto)
|
||||
|
||||
xbmc.log("######## ERROR #########", xbmc.LOGERROR)
|
||||
xbmc.log(texto, xbmc.LOGERROR)
|
||||
|
||||
|
||||
def log(*args):
|
||||
# Function to simplify the log
|
||||
# Automatically returns File Name and Function Name
|
||||
if loggeractive:
|
||||
string = ''
|
||||
for arg in args: string += ' '+str(arg)
|
||||
frame = inspect.stack()[1]
|
||||
filename = frame[0].f_code.co_filename
|
||||
filename = os.path.basename(filename)
|
||||
xbmc.log("[" + filename + "] [" + inspect.stack()[1][3] + "] " + string, xbmc.LOGNOTICE)
|
||||
def log(*args, **kwargs):
|
||||
msg = ''
|
||||
for arg in args: msg += ' ' + str(arg)
|
||||
frame = inspect.currentframe().f_back.f_back
|
||||
filename = frame.f_code.co_filename
|
||||
filename = os.path.basename(filename).split('.')[0]
|
||||
xbmc.log(LOG_FORMAT.format(addname=config.PLUGIN_NAME,
|
||||
filename=filename,
|
||||
line=frame.f_lineno,
|
||||
sep=':' if msg else '',
|
||||
function=frame.f_code.co_name,
|
||||
message=msg), kwargs.get('level', DEF_LEVEL))
|
||||
|
||||
|
||||
class WebErrorException(Exception):
|
||||
|
||||
Reference in New Issue
Block a user