Browse Source

logging of lists and dicts

refactor
Ulrich 2 years ago
parent
commit
1d7cdfddac
  1. 99
      basic/message.py
  2. 41
      test/test_11message.py
  3. 0
      tools/job_const.py

99
basic/message.py

@ -207,7 +207,7 @@ class Message:
def getLogLevel(self, tool="", comp=None): def getLogLevel(self, tool="", comp=None):
""" """
gets the decreasing level depending on tool and component gets the increasing level depending on tool and component
if these arguments matches with a job-parameter the level decreases if these arguments matches with a job-parameter the level decreases
:param tool: :param tool:
:param comp: :param comp:
@ -244,8 +244,13 @@ class Message:
else: else:
return False return False
def getFinalReturncode(self):
RETURN_TEXT = ["OK", "WARN", "ERROR", "FATAL"]
return RETURN_TEXT[self.rc]
def getFinalRc(self): def getFinalRc(self):
return int(int(self.rc)) RETURN_TEXT = ["OK", "WARN", "ERROR", "FATAL"]
return int(self.rc)
def setFatal(self, text): def setFatal(self, text):
""" Routine zum Setzen des RC und gleichzeitigem Schreiben des Logs """ """ Routine zum Setzen des RC und gleichzeitigem Schreiben des Logs """
@ -365,33 +370,86 @@ class Message:
self.debug(LIMIT_XTRACE, prio, text) self.debug(LIMIT_XTRACE, prio, text)
def getLoggingArgs(self, mlevel, prio, text): def getLoggingArgs(self, mlevel, prio, text):
verify = self.getLogLevel("msg_tool")
self.logTrace(verify, "getLoggingArgs: " + str(mlevel)+ ", "+self.format2Str(prio)+", "+self.format2Str(text))
out = {} out = {}
prefix = ""
if isinstance(mlevel, int): if isinstance(mlevel, int):
out["mlevel"] = mlevel out["mlevel"] = mlevel
else: else:
raise Exception("argument mlevel is not int "+str(mlevel)) raise Exception("argument mlevel is not int "+str(mlevel))
a = text.split(":") if len(text) < 1:
cat = "" out["mprio"] = 0
txt = "" if isinstance(prio, int):
if len(a[0]) > 1 and a[0].lower() in LIST_MTEXT: txt = str(prio)
cat = a[0] elif isinstance(prio, dict):
if len(a) > 1 and len(a[1]) > 1: txt = self.formatDict2Str(prio)
txt = a[1] elif isinstance(prio, str):
elif len(a[0]) > 1 and a[0].lower() not in LIST_MTEXT:
txt = a[0]
if isinstance(prio, int) and len(txt) > 1:
out["mprio"] = prio
elif len(txt) < 1 and isinstance(prio, str):
txt = prio txt = prio
elif isinstance(prio, list):
txt = self.formatList2Str(prio)
else:
txt = str(prio)
else:
if isinstance(prio, int):
out["mprio"] = prio
else:
out["mprio"] = 0 out["mprio"] = 0
if len(txt) < 1: prefix = text
raise Exception("argument text is not fount " + str(mlevel) + ", " + str(prio) + ", " + str(text)) text = prio
if len(cat) > 1: if isinstance(text, dict):
out["mtext"] = cat + ": " + txt txt = self.formatDict2Str(text)
elif isinstance(text, str):
txt = text
elif isinstance(text, list):
txt = self.formatList2Str(text)
else:
txt = str(text)
if len(prefix) > 1:
if ":" in prefix:
out["mtext"] = prefix.strip() + " " + txt.strip()
else:
out["mtext"] = prefix.strip() + ": " + txt.strip()
else: else:
out["mtext"] = txt out["mtext"] = txt
return out return out
def format2Str(self, elem):
if isinstance(elem, dict):
return self.formatDict2Str(elem)
elif isinstance(elem, str):
return elem
elif isinstance(elem, list):
return self.formatList2Str(elem)
else:
return str(elem)
def formatDict2Str(self, args):
txt = "{"
for k in args:
if isinstance(args[k], dict):
txt += k + ": " + self.formatDict2Str(args[k]) + ", "
elif isinstance(args[k], str):
txt += k + ": " + args[k] + ", "
elif isinstance(args[k], list):
txt += k + ": " + self.formatList2Str(args[k]) + ", "
else:
txt += k + ": " + str(args[k]) + ", "
return txt[0:-2] + "}"
def formatList2Str(self, args):
txt = "["
for k in args:
if isinstance(k, dict):
txt += k + ": {" + self.formatDict2Str(k) + "}, "
elif isinstance(k, str):
txt += k + ", "
elif isinstance(k, list):
txt += self.formatList2Str(k) + ", "
else:
txt += str(k) + ", "
return txt[0:-2] + "]"
def log(self, mlevel, prio, text): def log(self, mlevel, prio, text):
args = self.getLoggingArgs(mlevel, prio, text) args = self.getLoggingArgs(mlevel, prio, text)
""" eigentliche Schreibroutine: hierin wird debug-Level beruecksichtgigt""" """ eigentliche Schreibroutine: hierin wird debug-Level beruecksichtgigt"""
@ -406,8 +464,11 @@ class Message:
self.messages.append(text) self.messages.append(text)
def debug(self, mlevel, prio, text=""): def debug(self, mlevel, prio, text=""):
verify = self.getLogLevel("msg_tool")
args = self.getLoggingArgs(mlevel, prio, text) args = self.getLoggingArgs(mlevel, prio, text)
if (args["mlevel"] + args["mprio"] > int(self.level)): if verify:
self.debugfile.write("m.debug "+self.format2Str(args)+" >? "+str(self.level)+"\n")
if (args["mlevel"] - args["mprio"] > int(self.level)):
return return
if (args["mprio"] + 20) % 2 == 1: if (args["mprio"] + 20) % 2 == 1:
print(args["mtext"]) print(args["mtext"])

41
test/test_11message.py

@ -12,7 +12,7 @@ import basic.message
HOME_PATH = test.constants.HOME_PATH HOME_PATH = test.constants.HOME_PATH
PYTHON_CMD = "python" PYTHON_CMD = "python"
TEST_FUNCTIONS = ["test_00init", "test_04logLevel", "test_05loginigArgs", "test_10set", "test_11log", "test_20close"] TEST_FUNCTIONS = ["test_00init", "test_04logLevel", "test_05loginigArgs", "test_10set", "test_11log", "test_20close"]
#TEST_FUNCTIONS = ["test_00init", "test_04logLevel"] TEST_FUNCTIONS = ["test_00init", "test_05loginigArgs"]
PROGRAM_NAME = "clean_workspace" PROGRAM_NAME = "clean_workspace"
NOT_TO_LOG = ["xx1xx", "xx4xx"] NOT_TO_LOG = ["xx1xx", "xx4xx"]
@ -87,6 +87,7 @@ class MyTestCase(unittest.TestCase):
cnttest = 0 cnttest = 0
if actfunction not in TEST_FUNCTIONS: if actfunction not in TEST_FUNCTIONS:
return return
# setattr(msgObject.job.par, "tool", "msg_tool")
res = msgObject.getLoggingArgs(12, 1, "text") res = msgObject.getLoggingArgs(12, 1, "text")
self.assertEqual(res["mlevel"], 12) self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 1) self.assertEqual(res["mprio"], 1)
@ -98,8 +99,38 @@ class MyTestCase(unittest.TestCase):
res = msgObject.getLoggingArgs(12, "text", "ERROR: ") res = msgObject.getLoggingArgs(12, "text", "ERROR: ")
self.assertEqual(res["mlevel"], 12) self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0) self.assertEqual(res["mprio"], 0)
self.assertEqual(res["mtext"], "ERROR: text") self.assertEqual("ERROR: text", res["mtext"])
cnttest += 9
args = ["a", "b", "c"]
res = msgObject.getLoggingArgs(12, args, "")
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0)
self.assertEqual("[a, b, c]", res["mtext"])
res = msgObject.getLoggingArgs(12, 1, args)
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 1)
self.assertEqual("[a, b, c]", res["mtext"])
args = {"a": "x", "b": "y", "c": "z"}
res = msgObject.getLoggingArgs(12, args, "")
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0)
self.assertEqual("{a: x, b: y, c: z}", res["mtext"])
res = msgObject.getLoggingArgs(12, 3, args)
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 3)
self.assertEqual("{a: x, b: y, c: z}", res["mtext"])
args = {"a": "x", "b": {"b1": "x", "b2": "y"}, "c": ["x", "y", "z"] }
res = msgObject.getLoggingArgs(12, args, "")
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0)
self.assertEqual("{a: x, b: {b1: x, b2: y}, c: [x, y, z]}", res["mtext"])
res = msgObject.getLoggingArgs(12, 0, args)
self.assertEqual(res["mlevel"], 12)
self.assertEqual(res["mprio"], 0)
self.assertEqual("{a: x, b: {b1: x, b2: y}, c: [x, y, z]}", res["mtext"])
cnttest += 18
MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest) MyTestCase.mymsg += "\n----- "+actfunction+" : "+str(cnttest)
def test_10set(self): def test_10set(self):
@ -190,8 +221,8 @@ class MyTestCase(unittest.TestCase):
self.assertEqual(True, msgObject.logfile.closed) self.assertEqual(True, msgObject.logfile.closed)
self.assertEqual(True, msgObject.debugfile.closed) self.assertEqual(True, msgObject.debugfile.closed)
# now check what is logged # now check what is logged
logtext = tools.file_tool.readFileText(jobObject, msgObject.logpath, None) logtext = tools.file_tool.read_file_text(jobObject, msgObject.logpath, None)
debugtext = tools.file_tool.readFileText(jobObject, msgObject.debugpath, None) debugtext = tools.file_tool.read_file_text(jobObject, msgObject.debugpath, None)
for x in TO_LOG: for x in TO_LOG:
regex = r".*" + x + "" regex = r".*" + x + ""
self.assertIn(x, logtext) self.assertIn(x, logtext)

0
tools/job_const.py

Loading…
Cancel
Save