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
21# -- local imports -----------------------------------------------------------------------------{{{-
22import mevis
23
24from TestSupport.TestHelper import TestHelper, CancelTestException, emitSpecialMessage, emitSpecialCommand
26
27# ----------------------------------------------------------------------------------------------}}}-
28
29# Global flag to stop on first error that occurs, indicated by gStopped.
30gStopOnFirstError = False
31# Global flag that stores if the test should be stopped right now.
32gStopped = False
33
34
35def setStopped(stopped):
36 global gStopped
37 gStopped = stopped
38
39
40# -- def LoggingDecorator ----------------------------------------------------------------------{{{-
41# @cond
42
43def LoggingDecorator(func):
44 def wrapper(*args, **kwds):
45 loggingMethod, message, type, depth, escapeHtml = func(*args, **kwds)
46 isInfoMessage = loggingMethod == mevis.MLAB.logHTML
47 if TestSupport.TestHelper.gLogInfoMessages or not isInfoMessage:
48 message = str(message)
49 if TestHelper.getInstance().hasCallerStackFrame():
50 callerStackFrame = TestHelper.getInstance().getCallerStackFrame()
51 else:
52 callerStackFrame = sys._getframe(1 + depth)
53 if isInfoMessage:
54 stackLine = traceback.extract_stack(callerStackFrame, 1)[0]
55 formattedStack = ""
56 else:
57 stackTrace = traceback.extract_stack(callerStackFrame)
58 # Skip test runner traceback levels
59 for i, frame in enumerate(stackTrace):
60 if frame[2] == "__callTestFunction":
61 stackTrace = stackTrace[i + 1 :]
62 break
63 stackLine = stackTrace[-1]
64 formattedStack = "".join(traceback.format_list(stackTrace))
65 emitSpecialMessage(loggingMethod, stackLine[0], stackLine[1], type, message, escapeHtml, formattedStack)
66
67 if gStopOnFirstError and gStopped:
68 raise CancelTestException
69
70 return wrapper
71
72
73# @endcond
74# ----------------------------------------------------------------------------------------------}}}-
75
76
77# -- def info ----------------------------------------------------------------------------------{{{-
78
82@LoggingDecorator
83def info(message, stack=False, type="", depth=0):
84 if stack:
85 mevis.MLAB.logError("The stack option is deprecated!")
86 return mevis.MLAB.logHTML, message, type, depth, True
87
88
89# ----------------------------------------------------------------------------------------------}}}-
90
91
92# -- def infoHTML ----------------------------------------------------------------------------------{{{-
93
96@LoggingDecorator
97def infoHTML(message, type="", depth=0):
98 return mevis.MLAB.logHTML, message, type, depth, False
99
100
101# ----------------------------------------------------------------------------------------------}}}-
102
103
104# -- def warning -------------------------------------------------------------------------------{{{-
105
109@LoggingDecorator
110def warning(message, stack=False, type="", depth=0):
111 if stack:
112 mevis.MLAB.logError("The stack option is deprecated!")
113 return mevis.MLAB.logWarningHTML, message, type, depth, True
114
115
116# ----------------------------------------------------------------------------------------------}}}-
117
118
119# -- def warningHTML -------------------------------------------------------------------------------{{{-
120
123@LoggingDecorator
124def warningHTML(message, type="", depth=0):
125 return mevis.MLAB.logWarningHTML, message, type, depth, False
126
127
128# ----------------------------------------------------------------------------------------------}}}-
129
130
131# -- def error ---------------------------------------------------------------------------------{{{-
132
136@LoggingDecorator
137def error(message, stack=False, type="", depth=0):
138 if stack:
139 mevis.MLAB.logError("The stack option is deprecated!")
140 return mevis.MLAB.logErrorHTML, message, type, depth, True
141
142
143# ----------------------------------------------------------------------------------------------}}}-
144
145
146# -- def errorHTML ---------------------------------------------------------------------------------{{{-
147
150@LoggingDecorator
151def errorHTML(message, type="", depth=0):
152 return mevis.MLAB.logErrorHTML, message, type, depth, False
153
154
155# ----------------------------------------------------------------------------------------------}}}-
156
157
158# -- def showImage -----------------------------------------------------------------------------{{{-
159
163def showImage(description, *files):
164 fileList = []
165 for filename in files:
166 if not os.path.isfile(filename):
167 error("Image (%s) does not exist" % (filename), depth=1)
168 else:
169 hash = Base_getHash(filename)
170 fileList.append("%s|%s" % (hash, filename))
171 if len(fileList) == 0:
172 error("No images given!", depth=1)
173 else:
174 stackLine = traceback.extract_stack(sys._getframe(1), 1)[0]
175 emitSpecialCommand(stackLine[0], stackLine[1], "ImageShow", fileList, description)
176
177
178# ----------------------------------------------------------------------------------------------}}}-
179
180
181# -- def showFile ------------------------------------------------------------------------------{{{-
182
187def showFile(description, *files):
188 fileList = []
189 for filename in files:
190 if not os.path.isfile(filename):
191 error("File (%s) does not exist" % (filename), depth=1)
192 else:
193 hash = Base_getHash(filename)
194 fileList.append("%s|%s" % (hash, filename))
195 if len(fileList) == 0:
196 error("No files given!", depth=1)
197 else:
198 stackLine = traceback.extract_stack(sys._getframe(1), 1)[0]
199 emitSpecialCommand(stackLine[0], stackLine[1], "FileShow", fileList, description)
200
201
202# ----------------------------------------------------------------------------------------------}}}-
203
204
205# -- def showDiff ------------------------------------------------------------------------------{{{-
206
214def showDiff(description, fromLines, toLines, showOnlyContextOfDiff=False, numberOfContextLines=10):
215 if type(fromLines) != list:
216 with open(fromLines) as fh:
217 fromLines = fh.readlines()
218 if type(toLines) != list:
219 with open(toLines) as fh:
220 toLines = fh.readlines()
221 diff = Base_createHtmlDiff(fromLines, toLines, showOnlyContextOfDiff, numberOfContextLines)
222 baseDiffFileName = os.path.join(Base_getResultDirectory(), "diff%s.html")
223 diffFileName = baseDiffFileName % ""
224 i = 0
225 while os.path.exists(diffFileName):
226 i += 1
227 diffFileName = baseDiffFileName % i
228 if not os.path.exists(Base_getResultDirectory()):
229 os.makedirs(Base_getResultDirectory())
230 with open(diffFileName, "w") as f:
231 f.write(diff)
232 showFile(description, diffFileName)
233
234
235# ----------------------------------------------------------------------------------------------}}}-
236
237
238# -- def getStack ------------------------------------------------------------------------------{{{-
239
245def getStack(depth=0):
246 content = ""
247 for line in traceback.extract_stack(sys._getframe(2 + depth)):
248 content += "[[%s][%d][%s][%s]]" % (line[0], line[1], line[2], line[3])
249 return "##S[%s]" % (content)
250
251
252# ----------------------------------------------------------------------------------------------}}}-
253
254from .Base import (
255 getResultDirectory as Base_getResultDirectory,
256 createHtmlDiff as Base_createHtmlDiff,
257 getHash as Base_getHash,
258)
getStack(depth=0)
Get the current stack.
Definition Logging.py:245
errorHTML(message, type="", depth=0)
Put an error to the log while interpreting HTML.
Definition Logging.py:151
showFile(description, *files)
Put the FileShow command to the log.
Definition Logging.py:187
warningHTML(message, type="", depth=0)
Put a warning to the log while interpreting HTML.
Definition Logging.py:124
infoHTML(message, type="", depth=0)
Put an info message to the log while interpreting HTML.
Definition Logging.py:97
info(message, stack=False, type="", depth=0)
Put an info message to the log.
Definition Logging.py:83
showImage(description, *files)
Put the ImageShow command to the log.
Definition Logging.py:163
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:214
error(message, stack=False, type="", depth=0)
Put an error to the log.
Definition Logging.py:137
warning(message, stack=False, type="", depth=0)
Put a warning to the log.
Definition Logging.py:110
setStopped(stopped)
Definition Logging.py:35