1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 from __future__ import with_statement
28
29 __authors__ = ["Olof Svensson", "Jérôme Kieffer"]
30 __contact__ = "svensson@esrf.fr"
31 __license__ = "LGPLv3+"
32 __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
33 __date__ = "2011-07-29"
34
35 import os, sys, traceback
36 from EDLogFile import EDLogFile
37 from EDThreading import Semaphore
38
39
41 """
42 This class in the same as EDVerbose however it's not static.
43 """
44
46 object.__init__(self)
47 self.__pySemaphoreWrite = Semaphore()
48 self.__bIsVerbose = True
49 self.__bIsVerboseDebug = False
50 self.__bIsTest = False
51 self.__edLogFile = EDLogFile()
52
53
56
59
61 """
62 turn on the test mode: all assertions become verbose (->screen)
63 """
64 self.__bIsTest = True
65
66
68 """
69 turn off the test mode: all assertions become silent (->screen)
70 """
71 self.__bIsTest = False
72
73
75 """
76 This method turns on verbose logging to standard output (stdout)
77 """
78 self.__bIsVerbose = True
79
80
82 """
83 This method turns off verbose logging to standard output (stdout)
84 """
85 self.__bIsVerbose = False
86
87
89 """
90 This method turns on debug messages to standard output and log file
91 """
92 self.__bIsVerboseDebug = True
93
94
96 """
97 This method turns off debug messages to standard output and log file
98 """
99 self.__bIsVerboseDebug = False
100
101
103 """
104 This method returns the current status of debugging
105
106 @return: if debug output to standard output and log file is enabled.
107 @type: boolean
108 """
109 return self.__bIsVerboseDebug
110
111
113 """
114 A private thread safe method for writing a string to standard output and to log file
115
116 @param _strMessage: The string to be written to standard output
117 @type _strMessage: python string
118 """
119 with self.__pySemaphoreWrite:
120 if (self.__bIsVerbose or self.__bIsVerboseDebug):
121 sys.stdout.write(_strMessage)
122 self.__edLogFile.write(_strMessage)
123
124
126 """
127 A private thread safe method for writing a string to standard error and to log file
128
129 @param _strMessage: The string to be written to standard error
130 @type _strMessage: python string
131 """
132 with self.__pySemaphoreWrite:
133 if (self.__bIsVerbose or self.__bIsVerboseDebug):
134 sys.stderr.write(_strMessage)
135 self.__edLogFile.write(_strMessage)
136
137
138 - def log(self, _strMessage=""):
139 """
140 This method writes a message only to the log file.
141
142 @param _strMessage: The string to be written to the log file
143 @type _strMessage: python string
144 """
145 with self.__pySemaphoreWrite:
146 self.__edLogFile.write(" %s%s" % (_strMessage, os.linesep))
147
148
149 - def screen(self, _strMessage=""):
150 """
151 This method writes a message to standard output and to the log file.
152
153 @param _strMessage: The string to be written to the log file
154 @type _strMessage: python string
155 """
156 self.__writeStdout(" %s%s" % (_strMessage, os.linesep))
157
158
159 - def DEBUG(self, _strDebugMessage=""):
160 """
161 This method writes a debug message to standard output and to the log file
162 if debugging is enabled. The message will be written with the prefix [DEBUG]
163
164 @param _strDebugMessage: The debug message to be written to standard output and log file
165 @type _strDebugMessage: python string
166 """
167 if (self.__bIsVerboseDebug):
168 self.__writeStdout(" [DEBUG]: %s%s" % (_strDebugMessage, os.linesep))
169
170
172 """
173 This method is meant to be used by the testing framework. The message will be written
174 to standard output and the log file with the prefix [UnitTest]
175
176 @param _strMessage: The message to be written to standard output and log file
177 @type _strMessage: python string
178 """
179 self.__writeStdout(" [UnitTest]: %s%s" % (_strMessage, os.linesep))
180
181
182 - def ERROR(self, _strMessage=""):
183 """
184 This method writes a message to standard error and the log file with the prefix [ERROR].
185
186 @param _strMessage: The error message to be written to standard output and log file
187 @type _strMessage: python string
188 """
189 self.__writeStderr(" [ERROR]: %s%s" % (_strMessage, os.linesep))
190
191
192 - def error(self, _strMessage=""):
193 """
194 This method writes a message to standard error and the log file with the prefix [ERROR].
195
196 @param _strMessage: The error message to be written to standard output and log file
197 @type _strMessage: python string
198 """
199 self.ERROR(_strMessage)
200
201
202 - def WARNING(self, _strMessage=""):
203 """
204 This method writes a warning message to standard output and the log file with the prefix [Warning].
205
206 @param _strMessage: The error message to be written to standard output and log file
207 @type _strMessage: python string
208 """
209 self.__writeStdout(" [Warning]: %s%s" % (_strMessage, os.linesep))
210
211
212 - def warning(self, _strMessage=""):
213 """
214 This method writes a warning message to standard output and the log file with the prefix [Warning].
215
216 @param _strMessage: The error message to be written to standard output and log file
217 @type _strMessage: python string
218 """
219 self.WARNING(_strMessage)
220
221
222 - def ASSERT(self, _strMessage):
223 """
224 This method writes an assert message to standard output and the log file with the prefix [ASSERT].
225
226 @param _strMessage: The error message to be written to standard output and log file
227 @type _strMessage: python string
228 """
229 if self.__bIsTest:
230 self.screen("[ASSERT]: %s" % _strMessage)
231 else:
232 self.log("[ASSERT]: %s" % _strMessage)
233
234
236 """
237 This method writes an error trace to standard output and the log file. The error trace has
238 the same formatting as normal Python error traces.
239
240 @param _strPrefix: A prefix which can be customized, e.g. the testing framework uses ' [UnitTest]'
241 @type _strPrefix: python string
242 """
243 (exc_type, exc_value, exc_traceback) = sys.exc_info()
244 pyListTrace = traceback.extract_tb(exc_traceback)
245 self.__writeStderr("%s Traceback (most recent call last): %s" % (_strPrefix, os.linesep))
246 for pyListLine in pyListTrace:
247 self.__writeStderr("%s File \"%s\", line %d, in %s%s" % (_strPrefix, pyListLine[0],
248 pyListLine[1],
249 pyListLine[2],
250 os.linesep))
251 self.__writeStderr("%s %s%s" % (_strPrefix, pyListLine[3], os.linesep))
252 strErrorMessage = traceback.format_exception_only(exc_type, exc_value)[0][:-1]
253 self.__writeStderr(_strPrefix + strErrorMessage + os.linesep)
254
255
257 """
258 This method can be used for customising the file name of the log file.
259
260 @param _strLogFileName: A file name for the log file.
261 @type _strLogFileName: python string
262 """
263 self.__edLogFile.setLogFileName(_strLogFileName)
264
265
267 """
268 This method truns off output to the log file.
269 """
270 self.__edLogFile.setLogFileOff()
271
272
274
275
276
277 return self
278
279 EDLoggingVerbose = EDLoggingVerbose()
280