]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Common/EdkLogger.py
BaseTools: Use absolute import in Common
[mirror_edk2.git] / BaseTools / Source / Python / Common / EdkLogger.py
CommitLineData
f51461c8
LG
1## @file\r
2# This file implements the log mechanism for Python tools.\r
3#\r
183ca964 4# Copyright (c) 2007 - 2015, 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
f3fc5b47 15from __future__ import absolute_import\r
1be2ed90 16import Common.LongFilePathOs as os, sys, logging\r
f51461c8 17import traceback\r
f3fc5b47 18from .BuildToolError import *\r
f51461c8
LG
19\r
20## Log level constants\r
21DEBUG_0 = 1\r
22DEBUG_1 = 2\r
23DEBUG_2 = 3\r
24DEBUG_3 = 4\r
25DEBUG_4 = 5\r
26DEBUG_5 = 6\r
27DEBUG_6 = 7\r
28DEBUG_7 = 8\r
29DEBUG_8 = 9\r
30DEBUG_9 = 10\r
31VERBOSE = 15\r
32INFO = 20\r
33WARN = 30\r
34QUIET = 40\r
35ERROR = 50\r
183ca964 36SILENT = 99\r
f51461c8
LG
37\r
38IsRaiseError = 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
79def 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
104def 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
118def 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
153info = _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
171def 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
201 if RaiseError:\r
202 raise FatalError(ErrorCode)\r
203\r
204# Log information which should be always put out\r
205quiet = _ErrorLogger.error\r
206\r
207## Initialize log system\r
208def Initialize():\r
209 #\r
210 # Since we use different format to log different levels of message into different\r
211 # place (stdout or stderr), we have to use different "Logger" objects to do this.\r
212 #\r
213 # For DEBUG level (All DEBUG_0~9 are applicable)\r
214 _DebugLogger.setLevel(INFO)\r
215 _DebugChannel = logging.StreamHandler(sys.stdout)\r
216 _DebugChannel.setFormatter(_DebugFormatter)\r
217 _DebugLogger.addHandler(_DebugChannel)\r
218\r
219 # For VERBOSE, INFO, WARN level\r
220 _InfoLogger.setLevel(INFO)\r
221 _InfoChannel = logging.StreamHandler(sys.stdout)\r
222 _InfoChannel.setFormatter(_InfoFormatter)\r
223 _InfoLogger.addHandler(_InfoChannel)\r
224\r
225 # For ERROR level\r
226 _ErrorLogger.setLevel(INFO)\r
227 _ErrorCh = logging.StreamHandler(sys.stderr)\r
228 _ErrorCh.setFormatter(_ErrorFormatter)\r
229 _ErrorLogger.addHandler(_ErrorCh)\r
230\r
231## Set log level\r
232#\r
233# @param Level One of log level in _LogLevel\r
234def SetLevel(Level):\r
235 if Level not in _LogLevels:\r
236 info("Not supported log level (%d). Use default level instead." % Level)\r
237 Level = INFO\r
238 _DebugLogger.setLevel(Level)\r
239 _InfoLogger.setLevel(Level)\r
240 _ErrorLogger.setLevel(Level)\r
241\r
183ca964
JJ
242def InitializeForUnitTest():\r
243 Initialize()\r
244 SetLevel(SILENT)\r
245\r
f51461c8
LG
246## Get current log level\r
247def GetLevel():\r
248 return _InfoLogger.getEffectiveLevel()\r
249\r
250## Raise up warning as error\r
251def SetWarningAsError():\r
252 global _WarningAsError\r
253 _WarningAsError = True\r
254\r
255## Specify a file to store the log message as well as put on console\r
256#\r
257# @param LogFile The file path used to store the log message\r
258#\r
259def SetLogFile(LogFile):\r
260 if os.path.exists(LogFile):\r
261 os.remove(LogFile)\r
262\r
263 _Ch = logging.FileHandler(LogFile)\r
264 _Ch.setFormatter(_DebugFormatter)\r
265 _DebugLogger.addHandler(_Ch)\r
266\r
267 _Ch= logging.FileHandler(LogFile)\r
268 _Ch.setFormatter(_InfoFormatter)\r
269 _InfoLogger.addHandler(_Ch)\r
270\r
271 _Ch = logging.FileHandler(LogFile)\r
272 _Ch.setFormatter(_ErrorFormatter)\r
273 _ErrorLogger.addHandler(_Ch)\r
274\r
275if __name__ == '__main__':\r
276 pass\r
277\r