]>
Commit | Line | Data |
---|---|---|
f51461c8 LG |
1 | ## @file\r |
2 | # This file implements the log mechanism for Python tools.\r | |
3 | #\r | |
855698fb | 4 | # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r |
f51461c8 LG |
5 | # This program and the accompanying materials\r |
6 | # are licensed and made available under the terms and conditions of the BSD License\r | |
7 | # which accompanies this distribution. The full text of the license may be found at\r | |
8 | # http://opensource.org/licenses/bsd-license.php\r | |
9 | #\r | |
10 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | #\r | |
13 | \r | |
14 | ## Import modules\r | |
1ccc4d89 | 15 | from __future__ import absolute_import\r |
1be2ed90 | 16 | import Common.LongFilePathOs as os, sys, logging\r |
f51461c8 | 17 | import traceback\r |
f3fc5b47 | 18 | from .BuildToolError import *\r |
f51461c8 LG |
19 | \r |
20 | ## Log level constants\r | |
21 | DEBUG_0 = 1\r | |
22 | DEBUG_1 = 2\r | |
23 | DEBUG_2 = 3\r | |
24 | DEBUG_3 = 4\r | |
25 | DEBUG_4 = 5\r | |
26 | DEBUG_5 = 6\r | |
27 | DEBUG_6 = 7\r | |
28 | DEBUG_7 = 8\r | |
29 | DEBUG_8 = 9\r | |
30 | DEBUG_9 = 10\r | |
31 | VERBOSE = 15\r | |
32 | INFO = 20\r | |
33 | WARN = 30\r | |
34 | QUIET = 40\r | |
35 | ERROR = 50\r | |
183ca964 | 36 | SILENT = 99\r |
f51461c8 LG |
37 | \r |
38 | IsRaiseError = True\r | |
39 | \r | |
40 | # Tool name\r | |
41 | _ToolName = os.path.basename(sys.argv[0])\r | |
42 | \r | |
43 | # For validation purpose\r | |
183ca964 JJ |
44 | _LogLevels = [DEBUG_0, DEBUG_1, DEBUG_2, DEBUG_3, DEBUG_4, DEBUG_5,\r |
45 | DEBUG_6, DEBUG_7, DEBUG_8, DEBUG_9, VERBOSE, WARN, INFO,\r | |
46 | ERROR, QUIET, SILENT]\r | |
f51461c8 LG |
47 | \r |
48 | # For DEBUG level (All DEBUG_0~9 are applicable)\r | |
49 | _DebugLogger = logging.getLogger("tool_debug")\r | |
50 | _DebugFormatter = logging.Formatter("[%(asctime)s.%(msecs)d]: %(message)s", datefmt="%H:%M:%S")\r | |
51 | \r | |
52 | # For VERBOSE, INFO, WARN level\r | |
53 | _InfoLogger = logging.getLogger("tool_info")\r | |
54 | _InfoFormatter = logging.Formatter("%(message)s")\r | |
55 | \r | |
56 | # For ERROR level\r | |
57 | _ErrorLogger = logging.getLogger("tool_error")\r | |
58 | _ErrorFormatter = logging.Formatter("%(message)s")\r | |
59 | \r | |
60 | # String templates for ERROR/WARN/DEBUG log message\r | |
61 | _ErrorMessageTemplate = '\n\n%(tool)s...\n%(file)s(%(line)s): error %(errorcode)04X: %(msg)s\n\t%(extra)s'\r | |
62 | _ErrorMessageTemplateWithoutFile = '\n\n%(tool)s...\n : error %(errorcode)04X: %(msg)s\n\t%(extra)s'\r | |
63 | _WarningMessageTemplate = '%(tool)s...\n%(file)s(%(line)s): warning: %(msg)s'\r | |
64 | _WarningMessageTemplateWithoutFile = '%(tool)s: : warning: %(msg)s'\r | |
65 | _DebugMessageTemplate = '%(file)s(%(line)s): debug: \n %(msg)s'\r | |
66 | \r | |
67 | #\r | |
68 | # Flag used to take WARN as ERROR.\r | |
69 | # By default, only ERROR message will break the tools execution.\r | |
70 | #\r | |
71 | _WarningAsError = False\r | |
72 | \r | |
73 | ## Log debug message\r | |
74 | #\r | |
75 | # @param Level DEBUG level (DEBUG0~9)\r | |
76 | # @param Message Debug information\r | |
77 | # @param ExtraData More information associated with "Message"\r | |
78 | #\r | |
79 | def debug(Level, Message, ExtraData=None):\r | |
80 | if _DebugLogger.level > Level:\r | |
81 | return\r | |
82 | if Level > DEBUG_9:\r | |
83 | return\r | |
84 | \r | |
85 | # Find out the caller method information\r | |
86 | CallerStack = traceback.extract_stack()[-2]\r | |
87 | TemplateDict = {\r | |
88 | "file" : CallerStack[0],\r | |
89 | "line" : CallerStack[1],\r | |
90 | "msg" : Message,\r | |
91 | }\r | |
92 | \r | |
4231a819 | 93 | if ExtraData is not None:\r |
f51461c8 LG |
94 | LogText = _DebugMessageTemplate % TemplateDict + "\n %s" % ExtraData\r |
95 | else:\r | |
96 | LogText = _DebugMessageTemplate % TemplateDict\r | |
97 | \r | |
98 | _DebugLogger.log(Level, LogText)\r | |
99 | \r | |
100 | ## Log verbose message\r | |
101 | #\r | |
102 | # @param Message Verbose information\r | |
103 | #\r | |
104 | def verbose(Message):\r | |
105 | return _InfoLogger.log(VERBOSE, Message)\r | |
106 | \r | |
107 | ## Log warning message\r | |
108 | #\r | |
109 | # Warning messages are those which might be wrong but won't fail the tool.\r | |
110 | #\r | |
111 | # @param ToolName The name of the tool. If not given, the name of caller\r | |
112 | # method will be used.\r | |
113 | # @param Message Warning information\r | |
114 | # @param File The name of file which caused the warning.\r | |
115 | # @param Line The line number in the "File" which caused the warning.\r | |
116 | # @param ExtraData More information associated with "Message"\r | |
117 | #\r | |
118 | def warn(ToolName, Message, File=None, Line=None, ExtraData=None):\r | |
119 | if _InfoLogger.level > WARN:\r | |
120 | return\r | |
121 | \r | |
122 | # if no tool name given, use caller's source file name as tool name\r | |
4231a819 | 123 | if ToolName is None or ToolName == "":\r |
f51461c8 LG |
124 | ToolName = os.path.basename(traceback.extract_stack()[-2][0])\r |
125 | \r | |
4231a819 | 126 | if Line is None:\r |
f51461c8 LG |
127 | Line = "..."\r |
128 | else:\r | |
129 | Line = "%d" % Line\r | |
130 | \r | |
131 | TemplateDict = {\r | |
132 | "tool" : ToolName,\r | |
133 | "file" : File,\r | |
134 | "line" : Line,\r | |
135 | "msg" : Message,\r | |
136 | }\r | |
137 | \r | |
4231a819 | 138 | if File is not None:\r |
f51461c8 LG |
139 | LogText = _WarningMessageTemplate % TemplateDict\r |
140 | else:\r | |
141 | LogText = _WarningMessageTemplateWithoutFile % TemplateDict\r | |
142 | \r | |
4231a819 | 143 | if ExtraData is not None:\r |
f51461c8 LG |
144 | LogText += "\n %s" % ExtraData\r |
145 | \r | |
146 | _InfoLogger.log(WARN, LogText)\r | |
147 | \r | |
148 | # Raise an execption if indicated\r | |
149 | if _WarningAsError == True:\r | |
150 | raise FatalError(WARNING_AS_ERROR)\r | |
151 | \r | |
152 | ## Log INFO message\r | |
153 | info = _InfoLogger.info\r | |
154 | \r | |
155 | ## Log ERROR message\r | |
156 | #\r | |
157 | # Once an error messages is logged, the tool's execution will be broken by raising\r | |
158 | # an execption. If you don't want to break the execution later, you can give\r | |
159 | # "RaiseError" with "False" value.\r | |
160 | #\r | |
161 | # @param ToolName The name of the tool. If not given, the name of caller\r | |
162 | # method will be used.\r | |
163 | # @param ErrorCode The error code\r | |
164 | # @param Message Warning information\r | |
165 | # @param File The name of file which caused the error.\r | |
166 | # @param Line The line number in the "File" which caused the warning.\r | |
167 | # @param ExtraData More information associated with "Message"\r | |
168 | # @param RaiseError Raise an exception to break the tool's executuion if\r | |
169 | # it's True. This is the default behavior.\r | |
170 | #\r | |
171 | def error(ToolName, ErrorCode, Message=None, File=None, Line=None, ExtraData=None, RaiseError=IsRaiseError):\r | |
4231a819 | 172 | if Line is None:\r |
f51461c8 LG |
173 | Line = "..."\r |
174 | else:\r | |
175 | Line = "%d" % Line\r | |
176 | \r | |
4231a819 | 177 | if Message is None:\r |
f51461c8 LG |
178 | if ErrorCode in gErrorMessage:\r |
179 | Message = gErrorMessage[ErrorCode]\r | |
180 | else:\r | |
181 | Message = gErrorMessage[UNKNOWN_ERROR]\r | |
182 | \r | |
4231a819 | 183 | if ExtraData is None:\r |
f51461c8 LG |
184 | ExtraData = ""\r |
185 | \r | |
186 | TemplateDict = {\r | |
187 | "tool" : _ToolName,\r | |
188 | "file" : File,\r | |
189 | "line" : Line,\r | |
190 | "errorcode" : ErrorCode,\r | |
191 | "msg" : Message,\r | |
192 | "extra" : ExtraData\r | |
193 | }\r | |
194 | \r | |
4231a819 | 195 | if File is not None:\r |
f51461c8 LG |
196 | LogText = _ErrorMessageTemplate % TemplateDict\r |
197 | else:\r | |
198 | LogText = _ErrorMessageTemplateWithoutFile % TemplateDict\r | |
199 | \r | |
200 | _ErrorLogger.log(ERROR, LogText)\r | |
4423f0bc YZ |
201 | \r |
202 | if RaiseError and IsRaiseError:\r | |
f51461c8 LG |
203 | raise FatalError(ErrorCode)\r |
204 | \r | |
205 | # Log information which should be always put out\r | |
206 | quiet = _ErrorLogger.error\r | |
207 | \r | |
208 | ## Initialize log system\r | |
209 | def Initialize():\r | |
210 | #\r | |
211 | # Since we use different format to log different levels of message into different\r | |
212 | # place (stdout or stderr), we have to use different "Logger" objects to do this.\r | |
213 | #\r | |
214 | # For DEBUG level (All DEBUG_0~9 are applicable)\r | |
215 | _DebugLogger.setLevel(INFO)\r | |
216 | _DebugChannel = logging.StreamHandler(sys.stdout)\r | |
217 | _DebugChannel.setFormatter(_DebugFormatter)\r | |
218 | _DebugLogger.addHandler(_DebugChannel)\r | |
219 | \r | |
220 | # For VERBOSE, INFO, WARN level\r | |
221 | _InfoLogger.setLevel(INFO)\r | |
222 | _InfoChannel = logging.StreamHandler(sys.stdout)\r | |
223 | _InfoChannel.setFormatter(_InfoFormatter)\r | |
224 | _InfoLogger.addHandler(_InfoChannel)\r | |
225 | \r | |
226 | # For ERROR level\r | |
227 | _ErrorLogger.setLevel(INFO)\r | |
228 | _ErrorCh = logging.StreamHandler(sys.stderr)\r | |
229 | _ErrorCh.setFormatter(_ErrorFormatter)\r | |
230 | _ErrorLogger.addHandler(_ErrorCh)\r | |
231 | \r | |
232 | ## Set log level\r | |
233 | #\r | |
234 | # @param Level One of log level in _LogLevel\r | |
235 | def SetLevel(Level):\r | |
236 | if Level not in _LogLevels:\r | |
237 | info("Not supported log level (%d). Use default level instead." % Level)\r | |
238 | Level = INFO\r | |
239 | _DebugLogger.setLevel(Level)\r | |
240 | _InfoLogger.setLevel(Level)\r | |
241 | _ErrorLogger.setLevel(Level)\r | |
242 | \r | |
183ca964 JJ |
243 | def InitializeForUnitTest():\r |
244 | Initialize()\r | |
245 | SetLevel(SILENT)\r | |
246 | \r | |
f51461c8 LG |
247 | ## Get current log level\r |
248 | def GetLevel():\r | |
249 | return _InfoLogger.getEffectiveLevel()\r | |
250 | \r | |
251 | ## Raise up warning as error\r | |
252 | def SetWarningAsError():\r | |
253 | global _WarningAsError\r | |
254 | _WarningAsError = True\r | |
255 | \r | |
256 | ## Specify a file to store the log message as well as put on console\r | |
257 | #\r | |
258 | # @param LogFile The file path used to store the log message\r | |
259 | #\r | |
260 | def SetLogFile(LogFile):\r | |
261 | if os.path.exists(LogFile):\r | |
262 | os.remove(LogFile)\r | |
263 | \r | |
264 | _Ch = logging.FileHandler(LogFile)\r | |
265 | _Ch.setFormatter(_DebugFormatter)\r | |
266 | _DebugLogger.addHandler(_Ch)\r | |
267 | \r | |
268 | _Ch= logging.FileHandler(LogFile)\r | |
269 | _Ch.setFormatter(_InfoFormatter)\r | |
270 | _InfoLogger.addHandler(_Ch)\r | |
271 | \r | |
272 | _Ch = logging.FileHandler(LogFile)\r | |
273 | _Ch.setFormatter(_ErrorFormatter)\r | |
274 | _ErrorLogger.addHandler(_Ch)\r | |
275 | \r | |
276 | if __name__ == '__main__':\r | |
277 | pass\r | |
278 | \r |