2 # This file implements the log mechanism for Python tools.
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
9 from __future__
import absolute_import
10 import Common
.LongFilePathOs
as os
, sys
, logging
12 from .BuildToolError
import *
14 ## Log level constants
35 _ToolName
= os
.path
.basename(sys
.argv
[0])
37 # For validation purpose
38 _LogLevels
= [DEBUG_0
, DEBUG_1
, DEBUG_2
, DEBUG_3
, DEBUG_4
, DEBUG_5
,
39 DEBUG_6
, DEBUG_7
, DEBUG_8
, DEBUG_9
, VERBOSE
, WARN
, INFO
,
42 # For DEBUG level (All DEBUG_0~9 are applicable)
43 _DebugLogger
= logging
.getLogger("tool_debug")
44 _DebugFormatter
= logging
.Formatter("[%(asctime)s.%(msecs)d]: %(message)s", datefmt
="%H:%M:%S")
46 # For VERBOSE, INFO, WARN level
47 _InfoLogger
= logging
.getLogger("tool_info")
48 _InfoFormatter
= logging
.Formatter("%(message)s")
51 _ErrorLogger
= logging
.getLogger("tool_error")
52 _ErrorFormatter
= logging
.Formatter("%(message)s")
54 # String templates for ERROR/WARN/DEBUG log message
55 _ErrorMessageTemplate
= '\n\n%(tool)s...\n%(file)s(%(line)s): error %(errorcode)04X: %(msg)s\n\t%(extra)s'
56 _ErrorMessageTemplateWithoutFile
= '\n\n%(tool)s...\n : error %(errorcode)04X: %(msg)s\n\t%(extra)s'
57 _WarningMessageTemplate
= '%(tool)s...\n%(file)s(%(line)s): warning: %(msg)s'
58 _WarningMessageTemplateWithoutFile
= '%(tool)s: : warning: %(msg)s'
59 _DebugMessageTemplate
= '%(file)s(%(line)s): debug: \n %(msg)s'
62 # Flag used to take WARN as ERROR.
63 # By default, only ERROR message will break the tools execution.
65 _WarningAsError
= False
69 # @param Level DEBUG level (DEBUG0~9)
70 # @param Message Debug information
71 # @param ExtraData More information associated with "Message"
73 def debug(Level
, Message
, ExtraData
=None):
74 if _DebugLogger
.level
> Level
:
79 # Find out the caller method information
80 CallerStack
= traceback
.extract_stack()[-2]
82 "file" : CallerStack
[0],
83 "line" : CallerStack
[1],
87 if ExtraData
is not None:
88 LogText
= _DebugMessageTemplate
% TemplateDict
+ "\n %s" % ExtraData
90 LogText
= _DebugMessageTemplate
% TemplateDict
92 _DebugLogger
.log(Level
, LogText
)
94 ## Log verbose message
96 # @param Message Verbose information
99 return _InfoLogger
.log(VERBOSE
, Message
)
101 ## Log warning message
103 # Warning messages are those which might be wrong but won't fail the tool.
105 # @param ToolName The name of the tool. If not given, the name of caller
106 # method will be used.
107 # @param Message Warning information
108 # @param File The name of file which caused the warning.
109 # @param Line The line number in the "File" which caused the warning.
110 # @param ExtraData More information associated with "Message"
112 def warn(ToolName
, Message
, File
=None, Line
=None, ExtraData
=None):
113 if _InfoLogger
.level
> WARN
:
116 # if no tool name given, use caller's source file name as tool name
117 if ToolName
is None or ToolName
== "":
118 ToolName
= os
.path
.basename(traceback
.extract_stack()[-2][0])
133 LogText
= _WarningMessageTemplate
% TemplateDict
135 LogText
= _WarningMessageTemplateWithoutFile
% TemplateDict
137 if ExtraData
is not None:
138 LogText
+= "\n %s" % ExtraData
140 _InfoLogger
.log(WARN
, LogText
)
142 # Raise an exception if indicated
143 if _WarningAsError
== True:
144 raise FatalError(WARNING_AS_ERROR
)
147 info
= _InfoLogger
.info
151 # Once an error messages is logged, the tool's execution will be broken by raising
152 # an exception. If you don't want to break the execution later, you can give
153 # "RaiseError" with "False" value.
155 # @param ToolName The name of the tool. If not given, the name of caller
156 # method will be used.
157 # @param ErrorCode The error code
158 # @param Message Warning information
159 # @param File The name of file which caused the error.
160 # @param Line The line number in the "File" which caused the warning.
161 # @param ExtraData More information associated with "Message"
162 # @param RaiseError Raise an exception to break the tool's execution if
163 # it's True. This is the default behavior.
165 def error(ToolName
, ErrorCode
, Message
=None, File
=None, Line
=None, ExtraData
=None, RaiseError
=IsRaiseError
):
172 if ErrorCode
in gErrorMessage
:
173 Message
= gErrorMessage
[ErrorCode
]
175 Message
= gErrorMessage
[UNKNOWN_ERROR
]
177 if ExtraData
is None:
184 "errorcode" : ErrorCode
,
190 LogText
= _ErrorMessageTemplate
% TemplateDict
192 LogText
= _ErrorMessageTemplateWithoutFile
% TemplateDict
194 _ErrorLogger
.log(ERROR
, LogText
)
196 if RaiseError
and IsRaiseError
:
197 raise FatalError(ErrorCode
)
199 # Log information which should be always put out
200 quiet
= _ErrorLogger
.error
202 ## Initialize log system
205 # Since we use different format to log different levels of message into different
206 # place (stdout or stderr), we have to use different "Logger" objects to do this.
208 # For DEBUG level (All DEBUG_0~9 are applicable)
209 _DebugLogger
.setLevel(INFO
)
210 _DebugChannel
= logging
.StreamHandler(sys
.stdout
)
211 _DebugChannel
.setFormatter(_DebugFormatter
)
212 _DebugLogger
.addHandler(_DebugChannel
)
214 # For VERBOSE, INFO, WARN level
215 _InfoLogger
.setLevel(INFO
)
216 _InfoChannel
= logging
.StreamHandler(sys
.stdout
)
217 _InfoChannel
.setFormatter(_InfoFormatter
)
218 _InfoLogger
.addHandler(_InfoChannel
)
221 _ErrorLogger
.setLevel(INFO
)
222 _ErrorCh
= logging
.StreamHandler(sys
.stderr
)
223 _ErrorCh
.setFormatter(_ErrorFormatter
)
224 _ErrorLogger
.addHandler(_ErrorCh
)
228 # @param Level One of log level in _LogLevel
230 if Level
not in _LogLevels
:
231 info("Not supported log level (%d). Use default level instead." % Level
)
233 _DebugLogger
.setLevel(Level
)
234 _InfoLogger
.setLevel(Level
)
235 _ErrorLogger
.setLevel(Level
)
237 def InitializeForUnitTest():
241 ## Get current log level
243 return _InfoLogger
.getEffectiveLevel()
245 ## Raise up warning as error
246 def SetWarningAsError():
247 global _WarningAsError
248 _WarningAsError
= True
250 ## Specify a file to store the log message as well as put on console
252 # @param LogFile The file path used to store the log message
254 def SetLogFile(LogFile
):
255 if os
.path
.exists(LogFile
):
258 _Ch
= logging
.FileHandler(LogFile
)
259 _Ch
.setFormatter(_DebugFormatter
)
260 _DebugLogger
.addHandler(_Ch
)
262 _Ch
= logging
.FileHandler(LogFile
)
263 _Ch
.setFormatter(_InfoFormatter
)
264 _InfoLogger
.addHandler(_Ch
)
266 _Ch
= logging
.FileHandler(LogFile
)
267 _Ch
.setFormatter(_ErrorFormatter
)
268 _ErrorLogger
.addHandler(_Ch
)
270 if __name__
== '__main__':