TestCenter Reference
Logging.py
Go to the documentation of this file.
2# Copyright 2009, MeVis Medical Solutions AG
3#
4# The user may use this file in accordance with the license agreement provided with
5# the Software or, alternatively, in accordance with the terms contained in a
6# written agreement between the user and MeVis Medical Solutions AG.
7#
8# For further information use the contact form at https://www.mevislab.de/contact
9#
10
11
13
14# -- system imports ----------------------------------------------------------------------------{{{-
15import os
16import sys
17import traceback
18# ----------------------------------------------------------------------------------------------}}}-
19
20# -- local imports -----------------------------------------------------------------------------{{{-
21import mevis
22
23from TestSupport.TestHelper import TestHelper, CancelTestException, emitSpecialMessage, emitSpecialCommand
25
26# ----------------------------------------------------------------------------------------------}}}-
27
28# Global flag to stop on first error that occurs, indicated by gStopped.
29gStopOnFirstError = False
30# Global flag that stores if the test should be stopped right now.
31gStopped = False
32
33def setStopped(stopped):
34 global gStopped
35 gStopped = stopped
36
37# -- def LoggingDecorator ----------------------------------------------------------------------{{{-
38# @cond
39
40def LoggingDecorator (func):
41 def wrapper (*args, **kwds):
42 loggingMethod, message, type, depth, escapeHtml = func(*args, **kwds)
43 isInfoMessage = loggingMethod == mevis.MLAB.logHTML
44 if TestSupport.TestHelper.gLogInfoMessages or not isInfoMessage:
45 message = str(message)
46 if TestHelper.getInstance().hasCallerStackFrame():
47 callerStackFrame = TestHelper.getInstance().getCallerStackFrame()
48 else:
49 callerStackFrame = sys._getframe(1 + depth)
50 if isInfoMessage:
51 stackLine = traceback.extract_stack(callerStackFrame, 1)[0]
52 formattedStack = ''
53 else:
54 stackTrace = traceback.extract_stack(callerStackFrame)
55 # Skip test runner traceback levels
56 for i, frame in enumerate(stackTrace):
57 if frame[2] == '__callTestFunction':
58 stackTrace = stackTrace[i+1:]
59 break
60 stackLine = stackTrace[-1]
61 formattedStack = ''.join(traceback.format_list(stackTrace))
62 emitSpecialMessage(loggingMethod, stackLine[0], stackLine[1], type, message, escapeHtml, formattedStack)
63
64 if gStopOnFirstError and gStopped:
65 raise CancelTestException
66 return wrapper
67# @endcond
68# ----------------------------------------------------------------------------------------------}}}-
69
70# -- def info ----------------------------------------------------------------------------------{{{-
71
75@LoggingDecorator
76def info (message, stack=False, type='', depth=0):
77 if stack:
78 mevis.MLAB.logError("The stack option is deprecated!")
79 return mevis.MLAB.logHTML, message, type, depth, True
80# ----------------------------------------------------------------------------------------------}}}-
81
82# -- def infoHTML ----------------------------------------------------------------------------------{{{-
83
86@LoggingDecorator
87def infoHTML (message, type='', depth=0):
88 return mevis.MLAB.logHTML, message, type, depth, False
89# ----------------------------------------------------------------------------------------------}}}-
90
91# -- def warning -------------------------------------------------------------------------------{{{-
92
96@LoggingDecorator
97def warning (message, stack=False, type='', depth=0):
98 if stack:
99 mevis.MLAB.logError("The stack option is deprecated!")
100 return mevis.MLAB.logWarningHTML, message, type, depth, True
101# ----------------------------------------------------------------------------------------------}}}-
102
103# -- def warningHTML -------------------------------------------------------------------------------{{{-
104
107@LoggingDecorator
108def warningHTML (message, type='', depth=0):
109 return mevis.MLAB.logWarningHTML, message, type, depth, False
110# ----------------------------------------------------------------------------------------------}}}-
111
112
113# -- def error ---------------------------------------------------------------------------------{{{-
114
118@LoggingDecorator
119def error (message, stack=False, type='', depth=0):
120 if stack:
121 mevis.MLAB.logError("The stack option is deprecated!")
122 return mevis.MLAB.logErrorHTML, message, type, depth, True
123# ----------------------------------------------------------------------------------------------}}}-
124
125# -- def errorHTML ---------------------------------------------------------------------------------{{{-
126
129@LoggingDecorator
130def errorHTML (message, type='', depth=0):
131 return mevis.MLAB.logErrorHTML, message, type, depth, False
132# ----------------------------------------------------------------------------------------------}}}-
133
134
135# -- def showImage -----------------------------------------------------------------------------{{{-
136
140def showImage (description, *files):
141 fileList = []
142 for filename in files:
143 if not os.path.isfile(filename):
144 error("Image (%s) does not exist" % (filename), depth=1)
145 else:
146 hash = Base_getHash(filename)
147 fileList.append("%s|%s" % (hash, filename))
148 if len(fileList) == 0:
149 error("No images given!", depth=1)
150 else:
151 stackLine = traceback.extract_stack(sys._getframe(1), 1)[0]
152 emitSpecialCommand(stackLine[0], stackLine[1], "ImageShow", fileList, description)
153# ----------------------------------------------------------------------------------------------}}}-
154
155# -- def showFile ------------------------------------------------------------------------------{{{-
156
161def showFile (description, *files):
162 fileList = []
163 for filename in files:
164 if not os.path.isfile(filename):
165 error("File (%s) does not exist" % (filename), depth=1)
166 else:
167 hash = Base_getHash(filename)
168 fileList.append("%s|%s" % (hash, filename))
169 if len(fileList) == 0:
170 error("No files given!", depth=1)
171 else:
172 stackLine = traceback.extract_stack(sys._getframe(1), 1)[0]
173 emitSpecialCommand(stackLine[0], stackLine[1], "FileShow", fileList, description)
174# ----------------------------------------------------------------------------------------------}}}-
175
176# -- def showDiff ------------------------------------------------------------------------------{{{-
177
185def showDiff (description, fromLines, toLines, showOnlyContextOfDiff=False, numberOfContextLines=10):
186 if type(fromLines) != list:
187 with open(fromLines) as fh:
188 fromLines = fh.readlines()
189 if type(toLines) != list:
190 with open(toLines) as fh:
191 toLines = fh.readlines()
192 diff = Base_createHtmlDiff(fromLines, toLines, showOnlyContextOfDiff, numberOfContextLines)
193 baseDiffFileName = os.path.join(Base_getResultDirectory(), "diff%s.html")
194 diffFileName = baseDiffFileName % ""
195 i = 0
196 while os.path.exists(diffFileName):
197 i += 1
198 diffFileName = baseDiffFileName % i
199 if not os.path.exists(Base_getResultDirectory()):
200 os.makedirs(Base_getResultDirectory())
201 with open(diffFileName, "w") as f:
202 f.write(diff)
203 showFile(description, diffFileName)
204# ----------------------------------------------------------------------------------------------}}}-
205
206# -- def getStack ------------------------------------------------------------------------------{{{-
207
213def getStack (depth=0):
214 content = ""
215 for line in traceback.extract_stack(sys._getframe(2+depth)):
216 content += "[[%s][%d][%s][%s]]" % (line[0], line[1], line[2], line[3])
217 return "##S[%s]" % (content)
218# ----------------------------------------------------------------------------------------------}}}-
219
220from .Base import getResultDirectory as Base_getResultDirectory, createHtmlDiff as Base_createHtmlDiff, getHash as Base_getHash
getStack(depth=0)
Get the current stack.
Definition Logging.py:213
warningHTML(message, type='', depth=0)
Put a warning to the log while interpreting HTML.
Definition Logging.py:108
error(message, stack=False, type='', depth=0)
Put an error to the log.
Definition Logging.py:119
showFile(description, *files)
Put the FileShow command to the log.
Definition Logging.py:161
errorHTML(message, type='', depth=0)
Put an error to the log while interpreting HTML.
Definition Logging.py:130
showImage(description, *files)
Put the ImageShow command to the log.
Definition Logging.py:140
showDiff(description, fromLines, toLines, showOnlyContextOfDiff=False, numberOfContextLines=10)
Creates a HTML diff from the two files and puts the FileShow command to the log.
Definition Logging.py:185
infoHTML(message, type='', depth=0)
Put an info message to the log while interpreting HTML.
Definition Logging.py:87
warning(message, stack=False, type='', depth=0)
Put a warning to the log.
Definition Logging.py:97
info(message, stack=False, type='', depth=0)
Put an info message to the log.
Definition Logging.py:76
setStopped(stopped)
Definition Logging.py:33