2 # This file implements the log mechanism for Python tools.
4 # Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 import Common
.LongFilePathOs
as os
, sys
, logging
17 from BuildToolError
import *
19 ## Log level constants
40 _ToolName
= os
.path
.basename(sys
.argv
[0])
42 # For validation purpose
43 _LogLevels
= [DEBUG_0
, DEBUG_1
, DEBUG_2
, DEBUG_3
, DEBUG_4
, DEBUG_5
,
44 DEBUG_6
, DEBUG_7
, DEBUG_8
, DEBUG_9
, VERBOSE
, WARN
, INFO
,
47 # For DEBUG level (All DEBUG_0~9 are applicable)
48 _DebugLogger
= logging
.getLogger("tool_debug")
49 _DebugFormatter
= logging
.Formatter("[%(asctime)s.%(msecs)d]: %(message)s", datefmt
="%H:%M:%S")
51 # For VERBOSE, INFO, WARN level
52 _InfoLogger
= logging
.getLogger("tool_info")
53 _InfoFormatter
= logging
.Formatter("%(message)s")
56 _ErrorLogger
= logging
.getLogger("tool_error")
57 _ErrorFormatter
= logging
.Formatter("%(message)s")
59 # String templates for ERROR/WARN/DEBUG log message
60 _ErrorMessageTemplate
= '\n\n%(tool)s...\n%(file)s(%(line)s): error %(errorcode)04X: %(msg)s\n\t%(extra)s'
61 _ErrorMessageTemplateWithoutFile
= '\n\n%(tool)s...\n : error %(errorcode)04X: %(msg)s\n\t%(extra)s'
62 _WarningMessageTemplate
= '%(tool)s...\n%(file)s(%(line)s): warning: %(msg)s'
63 _WarningMessageTemplateWithoutFile
= '%(tool)s: : warning: %(msg)s'
64 _DebugMessageTemplate
= '%(file)s(%(line)s): debug: \n %(msg)s'
67 # Flag used to take WARN as ERROR.
68 # By default, only ERROR message will break the tools execution.
70 _WarningAsError
= False
74 # @param Level DEBUG level (DEBUG0~9)
75 # @param Message Debug information
76 # @param ExtraData More information associated with "Message"
78 def debug(Level
, Message
, ExtraData
=None):
79 if _DebugLogger
.level
> Level
:
84 # Find out the caller method information
85 CallerStack
= traceback
.extract_stack()[-2]
87 "file" : CallerStack
[0],
88 "line" : CallerStack
[1],
93 LogText
= _DebugMessageTemplate
% TemplateDict
+ "\n %s" % ExtraData
95 LogText
= _DebugMessageTemplate
% TemplateDict
97 _DebugLogger
.log(Level
, LogText
)
99 ## Log verbose message
101 # @param Message Verbose information
103 def verbose(Message
):
104 return _InfoLogger
.log(VERBOSE
, Message
)
106 ## Log warning message
108 # Warning messages are those which might be wrong but won't fail the tool.
110 # @param ToolName The name of the tool. If not given, the name of caller
111 # method will be used.
112 # @param Message Warning information
113 # @param File The name of file which caused the warning.
114 # @param Line The line number in the "File" which caused the warning.
115 # @param ExtraData More information associated with "Message"
117 def warn(ToolName
, Message
, File
=None, Line
=None, ExtraData
=None):
118 if _InfoLogger
.level
> WARN
:
121 # if no tool name given, use caller's source file name as tool name
122 if ToolName
== None or ToolName
== "":
123 ToolName
= os
.path
.basename(traceback
.extract_stack()[-2][0])
138 LogText
= _WarningMessageTemplate
% TemplateDict
140 LogText
= _WarningMessageTemplateWithoutFile
% TemplateDict
142 if ExtraData
!= None:
143 LogText
+= "\n %s" % ExtraData
145 _InfoLogger
.log(WARN
, LogText
)
147 # Raise an execption if indicated
148 if _WarningAsError
== True:
149 raise FatalError(WARNING_AS_ERROR
)
152 info
= _InfoLogger
.info
156 # Once an error messages is logged, the tool's execution will be broken by raising
157 # an execption. If you don't want to break the execution later, you can give
158 # "RaiseError" with "False" value.
160 # @param ToolName The name of the tool. If not given, the name of caller
161 # method will be used.
162 # @param ErrorCode The error code
163 # @param Message Warning information
164 # @param File The name of file which caused the error.
165 # @param Line The line number in the "File" which caused the warning.
166 # @param ExtraData More information associated with "Message"
167 # @param RaiseError Raise an exception to break the tool's executuion if
168 # it's True. This is the default behavior.
170 def error(ToolName
, ErrorCode
, Message
=None, File
=None, Line
=None, ExtraData
=None, RaiseError
=IsRaiseError
):
177 if ErrorCode
in gErrorMessage
:
178 Message
= gErrorMessage
[ErrorCode
]
180 Message
= gErrorMessage
[UNKNOWN_ERROR
]
182 if ExtraData
== None:
189 "errorcode" : ErrorCode
,
195 LogText
= _ErrorMessageTemplate
% TemplateDict
197 LogText
= _ErrorMessageTemplateWithoutFile
% TemplateDict
199 _ErrorLogger
.log(ERROR
, LogText
)
201 raise FatalError(ErrorCode
)
203 # Log information which should be always put out
204 quiet
= _ErrorLogger
.error
206 ## Initialize log system
209 # Since we use different format to log different levels of message into different
210 # place (stdout or stderr), we have to use different "Logger" objects to do this.
212 # For DEBUG level (All DEBUG_0~9 are applicable)
213 _DebugLogger
.setLevel(INFO
)
214 _DebugChannel
= logging
.StreamHandler(sys
.stdout
)
215 _DebugChannel
.setFormatter(_DebugFormatter
)
216 _DebugLogger
.addHandler(_DebugChannel
)
218 # For VERBOSE, INFO, WARN level
219 _InfoLogger
.setLevel(INFO
)
220 _InfoChannel
= logging
.StreamHandler(sys
.stdout
)
221 _InfoChannel
.setFormatter(_InfoFormatter
)
222 _InfoLogger
.addHandler(_InfoChannel
)
225 _ErrorLogger
.setLevel(INFO
)
226 _ErrorCh
= logging
.StreamHandler(sys
.stderr
)
227 _ErrorCh
.setFormatter(_ErrorFormatter
)
228 _ErrorLogger
.addHandler(_ErrorCh
)
232 # @param Level One of log level in _LogLevel
234 if Level
not in _LogLevels
:
235 info("Not supported log level (%d). Use default level instead." % Level
)
237 _DebugLogger
.setLevel(Level
)
238 _InfoLogger
.setLevel(Level
)
239 _ErrorLogger
.setLevel(Level
)
241 def InitializeForUnitTest():
245 ## Get current log level
247 return _InfoLogger
.getEffectiveLevel()
249 ## Raise up warning as error
250 def SetWarningAsError():
251 global _WarningAsError
252 _WarningAsError
= True
254 ## Specify a file to store the log message as well as put on console
256 # @param LogFile The file path used to store the log message
258 def SetLogFile(LogFile
):
259 if os
.path
.exists(LogFile
):
262 _Ch
= logging
.FileHandler(LogFile
)
263 _Ch
.setFormatter(_DebugFormatter
)
264 _DebugLogger
.addHandler(_Ch
)
266 _Ch
= logging
.FileHandler(LogFile
)
267 _Ch
.setFormatter(_InfoFormatter
)
268 _InfoLogger
.addHandler(_Ch
)
270 _Ch
= logging
.FileHandler(LogFile
)
271 _Ch
.setFormatter(_ErrorFormatter
)
272 _ErrorLogger
.addHandler(_Ch
)
274 if __name__
== '__main__':