TestCenter Reference
Logging.py
Go to the documentation of this file.
1 #
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 from builtins import str
12 
13 # Already ported to Python 3
14 
16 
17 # -- system imports ----------------------------------------------------------------------------{{{-
18 import os
19 import sys
20 import traceback
21 # ----------------------------------------------------------------------------------------------}}}-
22 
23 # -- local imports -----------------------------------------------------------------------------{{{-
24 import mevis
25 
26 from TestSupport.TestHelper import TestHelper, CancelTestException, emitSpecialMessage, emitSpecialCommand
28 
29 # ----------------------------------------------------------------------------------------------}}}-
30 
31 # Global flag to stop on first error that occurs, indicated by gStopped.
32 gStopOnFirstError = False
33 # Global flag that stores if the test should be stopped right now.
34 gStopped = False
35 
36 def setStopped(stopped):
37  global gStopped
38  gStopped = stopped
39 
40 # -- def LoggingDecorator ----------------------------------------------------------------------{{{-
41 # @cond
42 
43 def 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  return wrapper
70 # @endcond
71 # ----------------------------------------------------------------------------------------------}}}-
72 
73 # -- def info ----------------------------------------------------------------------------------{{{-
74 
78 @LoggingDecorator
79 def info (message, stack=False, type='', depth=0):
80  if stack:
81  mevis.MLAB.logError("The stack option is deprecated!")
82  return mevis.MLAB.logHTML, message, type, depth, True
83 # ----------------------------------------------------------------------------------------------}}}-
84 
85 # -- def infoHTML ----------------------------------------------------------------------------------{{{-
86 
89 @LoggingDecorator
90 def infoHTML (message, type='', depth=0):
91  return mevis.MLAB.logHTML, message, type, depth, False
92 # ----------------------------------------------------------------------------------------------}}}-
93 
94 # -- def warning -------------------------------------------------------------------------------{{{-
95 
99 @LoggingDecorator
100 def warning (message, stack=False, type='', depth=0):
101  if stack:
102  mevis.MLAB.logError("The stack option is deprecated!")
103  return mevis.MLAB.logWarningHTML, message, type, depth, True
104 # ----------------------------------------------------------------------------------------------}}}-
105 
106 # -- def warningHTML -------------------------------------------------------------------------------{{{-
107 
110 @LoggingDecorator
111 def warningHTML (message, type='', depth=0):
112  return mevis.MLAB.logWarningHTML, message, type, depth, False
113 # ----------------------------------------------------------------------------------------------}}}-
114 
115 
116 # -- def error ---------------------------------------------------------------------------------{{{-
117 
121 @LoggingDecorator
122 def error (message, stack=False, type='', depth=0):
123  if stack:
124  mevis.MLAB.logError("The stack option is deprecated!")
125  return mevis.MLAB.logErrorHTML, message, type, depth, True
126 # ----------------------------------------------------------------------------------------------}}}-
127 
128 # -- def errorHTML ---------------------------------------------------------------------------------{{{-
129 
132 @LoggingDecorator
133 def errorHTML (message, type='', depth=0):
134  return mevis.MLAB.logErrorHTML, message, type, depth, False
135 # ----------------------------------------------------------------------------------------------}}}-
136 
137 
138 # -- def showImage -----------------------------------------------------------------------------{{{-
139 
143 def showImage (description, *files):
144  fileList = []
145  for filename in files:
146  if not os.path.isfile(filename):
147  error("Image (%s) does not exist" % (filename), depth=1)
148  else:
149  hash = Base_getHash(filename)
150  fileList.append("%s|%s" % (hash, filename))
151  if len(fileList) == 0:
152  error("No images given!", depth=1)
153  else:
154  stackLine = traceback.extract_stack(sys._getframe(1), 1)[0]
155  emitSpecialCommand(stackLine[0], stackLine[1], "ImageShow", fileList, description)
156 # ----------------------------------------------------------------------------------------------}}}-
157 
158 # -- def showFile ------------------------------------------------------------------------------{{{-
159 
164 def showFile (description, *files):
165  fileList = []
166  for filename in files:
167  if not os.path.isfile(filename):
168  error("File (%s) does not exist" % (filename), depth=1)
169  else:
170  hash = Base_getHash(filename)
171  fileList.append("%s|%s" % (hash, filename))
172  if len(fileList) == 0:
173  error("No files given!", depth=1)
174  else:
175  stackLine = traceback.extract_stack(sys._getframe(1), 1)[0]
176  emitSpecialCommand(stackLine[0], stackLine[1], "FileShow", fileList, description)
177 # ----------------------------------------------------------------------------------------------}}}-
178 
179 # -- def showDiff ------------------------------------------------------------------------------{{{-
180 
188 def showDiff (description, fromLines, toLines, showOnlyContextOfDiff=False, numberOfContextLines=10):
189  if type(fromLines) != list:
190  with open(fromLines) as fh:
191  fromLines = fh.readlines()
192  if type(toLines) != list:
193  with open(toLines) as fh:
194  toLines = fh.readlines()
195  diff = Base_createHtmlDiff(fromLines, toLines, showOnlyContextOfDiff, numberOfContextLines)
196  baseDiffFileName = os.path.join(Base_getResultDirectory(), "diff%s.html")
197  diffFileName = baseDiffFileName % ""
198  i = 0
199  while os.path.exists(diffFileName):
200  i += 1
201  diffFileName = baseDiffFileName % i
202  if not os.path.exists(Base_getResultDirectory()):
203  os.makedirs(Base_getResultDirectory())
204  with open(diffFileName, "w") as f:
205  f.write(diff)
206  showFile(description, diffFileName)
207 # ----------------------------------------------------------------------------------------------}}}-
208 
209 # -- def getStack ------------------------------------------------------------------------------{{{-
210 
216 def getStack (depth=0):
217  content = ""
218  for line in traceback.extract_stack(sys._getframe(2+depth)):
219  content += "[[%s][%d][%s][%s]]" % (line[0], line[1], line[2], line[3])
220  return "##S[%s]" % (content)
221 # ----------------------------------------------------------------------------------------------}}}-
222 
223 from .Base import getResultDirectory as Base_getResultDirectory, createHtmlDiff as Base_createHtmlDiff, getHash as Base_getHash
def LoggingDecorator(func)
Internal decorator used for creating messages in compare methods.
Definition: Base.py:162
def warningHTML(message, type='', depth=0)
Put a warning to the log while interpreting HTML.
Definition: Logging.py:110
def getStack(depth=0)
Get the current stack.
Definition: Logging.py:215
def error(message, stack=False, type='', depth=0)
Put an error to the log.
Definition: Logging.py:121
def warning(message, stack=False, type='', depth=0)
Put a warning to the log.
Definition: Logging.py:99
def info(message, stack=False, type='', depth=0)
Put an info message to the log.
Definition: Logging.py:78
def showFile(description, *files)
Put the FileShow command to the log.
Definition: Logging.py:163
def errorHTML(message, type='', depth=0)
Put an error to the log while interpreting HTML.
Definition: Logging.py:132
def showImage(description, *files)
Put the ImageShow command to the log.
Definition: Logging.py:142
def setStopped(stopped)
Definition: Logging.py:36
def 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:187
def infoHTML(message, type='', depth=0)
Put an info message to the log while interpreting HTML.
Definition: Logging.py:89
def emitSpecialCommand(file, line, type, fileList, message, escapeHtml=True)
Definition: TestHelper.py:75
def emitSpecialMessage(loggingMethod, file, line, type, message, escapeHtml=True, formattedStack='')
Definition: TestHelper.py:52