2 # This file implements the log mechanism for Python tools.
4 # Copyright (c) 2007 - 2014, 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
39 _ToolName
= os
.path
.basename(sys
.argv
[0])
41 # For validation purpose
42 _LogLevels
= [DEBUG_0
, DEBUG_1
, DEBUG_2
, DEBUG_3
, DEBUG_4
, DEBUG_5
, DEBUG_6
, DEBUG_7
, DEBUG_8
, DEBUG_9
, VERBOSE
, WARN
, INFO
, ERROR
, QUIET
]
44 # For DEBUG level (All DEBUG_0~9 are applicable)
45 _DebugLogger
= logging
.getLogger("tool_debug")
46 _DebugFormatter
= logging
.Formatter("[%(asctime)s.%(msecs)d]: %(message)s", datefmt
="%H:%M:%S")
48 # For VERBOSE, INFO, WARN level
49 _InfoLogger
= logging
.getLogger("tool_info")
50 _InfoFormatter
= logging
.Formatter("%(message)s")
53 _ErrorLogger
= logging
.getLogger("tool_error")
54 _ErrorFormatter
= logging
.Formatter("%(message)s")
56 # String templates for ERROR/WARN/DEBUG log message
57 _ErrorMessageTemplate
= '\n\n%(tool)s...\n%(file)s(%(line)s): error %(errorcode)04X: %(msg)s\n\t%(extra)s'
58 _ErrorMessageTemplateWithoutFile
= '\n\n%(tool)s...\n : error %(errorcode)04X: %(msg)s\n\t%(extra)s'
59 _WarningMessageTemplate
= '%(tool)s...\n%(file)s(%(line)s): warning: %(msg)s'
60 _WarningMessageTemplateWithoutFile
= '%(tool)s: : warning: %(msg)s'
61 _DebugMessageTemplate
= '%(file)s(%(line)s): debug: \n %(msg)s'
64 # Flag used to take WARN as ERROR.
65 # By default, only ERROR message will break the tools execution.
67 _WarningAsError
= False
71 # @param Level DEBUG level (DEBUG0~9)
72 # @param Message Debug information
73 # @param ExtraData More information associated with "Message"
75 def debug(Level
, Message
, ExtraData
=None):
76 if _DebugLogger
.level
> Level
:
81 # Find out the caller method information
82 CallerStack
= traceback
.extract_stack()[-2]
84 "file" : CallerStack
[0],
85 "line" : CallerStack
[1],
90 LogText
= _DebugMessageTemplate
% TemplateDict
+ "\n %s" % ExtraData
92 LogText
= _DebugMessageTemplate
% TemplateDict
94 _DebugLogger
.log(Level
, LogText
)
96 ## Log verbose message
98 # @param Message Verbose information
100 def verbose(Message
):
101 return _InfoLogger
.log(VERBOSE
, Message
)
103 ## Log warning message
105 # Warning messages are those which might be wrong but won't fail the tool.
107 # @param ToolName The name of the tool. If not given, the name of caller
108 # method will be used.
109 # @param Message Warning information
110 # @param File The name of file which caused the warning.
111 # @param Line The line number in the "File" which caused the warning.
112 # @param ExtraData More information associated with "Message"
114 def warn(ToolName
, Message
, File
=None, Line
=None, ExtraData
=None):
115 if _InfoLogger
.level
> WARN
:
118 # if no tool name given, use caller's source file name as tool name
119 if ToolName
== None or ToolName
== "":
120 ToolName
= os
.path
.basename(traceback
.extract_stack()[-2][0])
135 LogText
= _WarningMessageTemplate
% TemplateDict
137 LogText
= _WarningMessageTemplateWithoutFile
% TemplateDict
139 if ExtraData
!= None:
140 LogText
+= "\n %s" % ExtraData
142 _InfoLogger
.log(WARN
, LogText
)
144 # Raise an execption if indicated
145 if _WarningAsError
== True:
146 raise FatalError(WARNING_AS_ERROR
)
149 info
= _InfoLogger
.info
153 # Once an error messages is logged, the tool's execution will be broken by raising
154 # an execption. If you don't want to break the execution later, you can give
155 # "RaiseError" with "False" value.
157 # @param ToolName The name of the tool. If not given, the name of caller
158 # method will be used.
159 # @param ErrorCode The error code
160 # @param Message Warning information
161 # @param File The name of file which caused the error.
162 # @param Line The line number in the "File" which caused the warning.
163 # @param ExtraData More information associated with "Message"
164 # @param RaiseError Raise an exception to break the tool's executuion if
165 # it's True. This is the default behavior.
167 def error(ToolName
, ErrorCode
, Message
=None, File
=None, Line
=None, ExtraData
=None, RaiseError
=IsRaiseError
):
174 if ErrorCode
in gErrorMessage
:
175 Message
= gErrorMessage
[ErrorCode
]
177 Message
= gErrorMessage
[UNKNOWN_ERROR
]
179 if ExtraData
== None:
186 "errorcode" : ErrorCode
,
192 LogText
= _ErrorMessageTemplate
% TemplateDict
194 LogText
= _ErrorMessageTemplateWithoutFile
% TemplateDict
196 _ErrorLogger
.log(ERROR
, LogText
)
198 raise FatalError(ErrorCode
)
200 # Log information which should be always put out
201 quiet
= _ErrorLogger
.error
203 ## Initialize log system
206 # Since we use different format to log different levels of message into different
207 # place (stdout or stderr), we have to use different "Logger" objects to do this.
209 # For DEBUG level (All DEBUG_0~9 are applicable)
210 _DebugLogger
.setLevel(INFO
)
211 _DebugChannel
= logging
.StreamHandler(sys
.stdout
)
212 _DebugChannel
.setFormatter(_DebugFormatter
)
213 _DebugLogger
.addHandler(_DebugChannel
)
215 # For VERBOSE, INFO, WARN level
216 _InfoLogger
.setLevel(INFO
)
217 _InfoChannel
= logging
.StreamHandler(sys
.stdout
)
218 _InfoChannel
.setFormatter(_InfoFormatter
)
219 _InfoLogger
.addHandler(_InfoChannel
)
222 _ErrorLogger
.setLevel(INFO
)
223 _ErrorCh
= logging
.StreamHandler(sys
.stderr
)
224 _ErrorCh
.setFormatter(_ErrorFormatter
)
225 _ErrorLogger
.addHandler(_ErrorCh
)
229 # @param Level One of log level in _LogLevel
231 if Level
not in _LogLevels
:
232 info("Not supported log level (%d). Use default level instead." % Level
)
234 _DebugLogger
.setLevel(Level
)
235 _InfoLogger
.setLevel(Level
)
236 _ErrorLogger
.setLevel(Level
)
238 ## Get current log level
240 return _InfoLogger
.getEffectiveLevel()
242 ## Raise up warning as error
243 def SetWarningAsError():
244 global _WarningAsError
245 _WarningAsError
= True
247 ## Specify a file to store the log message as well as put on console
249 # @param LogFile The file path used to store the log message
251 def SetLogFile(LogFile
):
252 if os
.path
.exists(LogFile
):
255 _Ch
= logging
.FileHandler(LogFile
)
256 _Ch
.setFormatter(_DebugFormatter
)
257 _DebugLogger
.addHandler(_Ch
)
259 _Ch
= logging
.FileHandler(LogFile
)
260 _Ch
.setFormatter(_InfoFormatter
)
261 _InfoLogger
.addHandler(_Ch
)
263 _Ch
= logging
.FileHandler(LogFile
)
264 _Ch
.setFormatter(_ErrorFormatter
)
265 _ErrorLogger
.addHandler(_Ch
)
267 if __name__
== '__main__':