]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/UPT/Logger/Log.py
Sync BaseTool trunk (version r2649) into EDKII BaseTools.
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Logger / Log.py
index 0915bb72639c1e77f0ce706285798d5741161c1c..407a1b32b6eeb8f653d90623e6c834831d83d081 100644 (file)
-## @file
-# This file implements the log mechanism for Python tools.
-#
-# Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
-#
-# This program and the accompanying materials are licensed and made available 
-# under the terms and conditions of the BSD License which accompanies this 
-# distribution. The full text of the license may be found at 
-# http://opensource.org/licenses/bsd-license.php
-#
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-#
-
-'''
-Logger
-'''
-
-## Import modules
-from sys import argv
-from sys import stdout
-from sys import stderr
-import os.path
-from os import remove
-from logging import getLogger
-from logging import Formatter
-from logging import StreamHandler
-from logging import FileHandler
-from traceback import extract_stack
-
-from Logger.ToolError import FatalError
-from Logger.ToolError import WARNING_AS_ERROR
-from Logger.ToolError import gERROR_MESSAGE
-from Logger.ToolError import UNKNOWN_ERROR
-from Library import GlobalData
-
-#
-# Log level constants
-#
-DEBUG_0 = 1
-DEBUG_1 = 2
-DEBUG_2 = 3
-DEBUG_3 = 4
-DEBUG_4 = 5
-DEBUG_5 = 6
-DEBUG_6 = 7
-DEBUG_7 = 8
-DEBUG_8 = 9
-DEBUG_9 = 10
-VERBOSE = 15
-INFO    = 20
-WARN    = 30
-QUIET   = 40
-QUIET_1 = 41
-ERROR   = 50
-SILENT  = 60
-
-IS_RAISE_ERROR = True
-SUPRESS_ERROR = False
-
-#
-# Tool name
-#
-_TOOL_NAME = os.path.basename(argv[0])
-#
-# For validation purpose
-#
-_LOG_LEVELS = [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, \
-              QUIET_1, SILENT]
-#
-# For DEBUG level (All DEBUG_0~9 are applicable)
-#
-_DEBUG_LOGGER = getLogger("tool_debug")
-_DEBUG_FORMATTER = Formatter("[%(asctime)s.%(msecs)d]: %(message)s", \
-                            datefmt="%H:%M:%S")
-#
-# For VERBOSE, INFO, WARN level
-#
-_INFO_LOGGER = getLogger("tool_info")
-_INFO_FORMATTER = Formatter("%(message)s")
-#
-# For ERROR level
-#
-_ERROR_LOGGER = getLogger("tool_error")
-_ERROR_FORMATTER = Formatter("%(message)s")
-
-#
-# String templates for ERROR/WARN/DEBUG log message
-#
-_ERROR_MESSAGE_TEMPLATE = \
-('\n\n%(tool)s...\n%(file)s(%(line)s): error %(errorcode)04X: %(msg)s\n\t%(extra)s')
-
-__ERROR_MESSAGE_TEMPLATE_WITHOUT_FILE = \
-'\n\n%(tool)s...\n : error %(errorcode)04X: %(msg)s\n\t%(extra)s'
-
-_WARNING_MESSAGE_TEMPLATE = '%(tool)s...\n%(file)s(%(line)s): warning: %(msg)s'
-_WARNING_MESSAGE_TEMPLATE_WITHOUT_FILE = '%(tool)s: : warning: %(msg)s'
-_DEBUG_MESSAGE_TEMPLATE = '%(file)s(%(line)s): debug: \n    %(msg)s'
-
-
-#
-# Log INFO message
-#
-#Info    = _INFO_LOGGER.info
-
-def Info(msg, *args, **kwargs):
-    _INFO_LOGGER.info(msg, *args, **kwargs)
-
-#
-# Log information which should be always put out
-#
-def Quiet(msg, *args, **kwargs):
-    _ERROR_LOGGER.error(msg, *args, **kwargs)
-
-## Log debug message
-#
-#   @param  Level       DEBUG level (DEBUG0~9)
-#   @param  Message     Debug information
-#   @param  ExtraData   More information associated with "Message"
-#
-def Debug(Level, Message, ExtraData=None):
-    if _DEBUG_LOGGER.level > Level:
-        return
-    if Level > DEBUG_9:
-        return
-    #
-    # Find out the caller method information
-    #
-    CallerStack = extract_stack()[-2]
-    TemplateDict = {
-        "file"      : CallerStack[0],
-        "line"      : CallerStack[1],
-        "msg"       : Message,
-    }
-
-    if ExtraData != None:
-        LogText = _DEBUG_MESSAGE_TEMPLATE % TemplateDict + "\n    %s" % ExtraData
-    else:
-        LogText = _DEBUG_MESSAGE_TEMPLATE % TemplateDict
-
-    _DEBUG_LOGGER.log(Level, LogText)
-
-## Log verbose message
-#
-#   @param  Message     Verbose information
-#
-def Verbose(Message):
-    return _INFO_LOGGER.log(VERBOSE, Message)
-
-## Log warning message
-#
-#   Warning messages are those which might be wrong but won't fail the tool.
-#
-#   @param  ToolName    The name of the tool. If not given, the name of caller
-#                       method will be used.
-#   @param  Message     Warning information
-#   @param  File        The name of file which caused the warning.
-#   @param  Line        The line number in the "File" which caused the warning.
-#   @param  ExtraData   More information associated with "Message"
-#
-def Warn(ToolName, Message, File=None, Line=None, ExtraData=None):
-    if _INFO_LOGGER.level > WARN:
-        return
-    #
-    # if no tool name given, use caller's source file name as tool name
-    #
-    if ToolName == None or ToolName == "":
-        ToolName = os.path.basename(extract_stack()[-2][0])
-
-    if Line == None:
-        Line = "..."
-    else:
-        Line = "%d" % Line
-
-    TemplateDict = {
-        "tool"      : ToolName,
-        "file"      : File,
-        "line"      : Line,
-        "msg"       : Message,
-    }
-
-    if File != None:
-        LogText = _WARNING_MESSAGE_TEMPLATE % TemplateDict
-    else:
-        LogText = _WARNING_MESSAGE_TEMPLATE_WITHOUT_FILE % TemplateDict
-
-    if ExtraData != None:
-        LogText += "\n    %s" % ExtraData
-
-    _INFO_LOGGER.log(WARN, LogText)
-    #
-    # Raise an execption if indicated
-    #
-    if GlobalData.gWARNING_AS_ERROR == True:
-        raise FatalError(WARNING_AS_ERROR)
-
-## Log ERROR message
-#
-# Once an error messages is logged, the tool's execution will be broken by 
-# raising an execption. If you don't want to break the execution later, you 
-# can give "RaiseError" with "False" value.
-#
-#   @param  ToolName    The name of the tool. If not given, the name of caller
-#                       method will be used.
-#   @param  ErrorCode   The error code
-#   @param  Message     Warning information
-#   @param  File        The name of file which caused the error.
-#   @param  Line        The line number in the "File" which caused the warning.
-#   @param  ExtraData   More information associated with "Message"
-#   @param  RaiseError  Raise an exception to break the tool's executuion if
-#                       it's True. This is the default behavior.
-#
-def Error(ToolName, ErrorCode, Message=None, File=None, Line=None, \
-          ExtraData=None, RaiseError=IS_RAISE_ERROR):
-    if ToolName:
-        pass
-    if Line == None:
-        Line = "..."
-    else:
-        Line = "%d" % Line
-
-    if Message == None:
-        if ErrorCode in gERROR_MESSAGE:
-            Message = gERROR_MESSAGE[ErrorCode]
-        else:
-            Message = gERROR_MESSAGE[UNKNOWN_ERROR]
-
-    if ExtraData == None:
-        ExtraData = ""
-
-    TemplateDict = {
-        "tool"      : _TOOL_NAME,
-        "file"      : File,
-        "line"      : Line,
-        "errorcode" : ErrorCode,
-        "msg"       : Message,
-        "extra"     : ExtraData
-    }
-
-    if File != None:
-        LogText =  _ERROR_MESSAGE_TEMPLATE % TemplateDict
-    else:
-        LogText = __ERROR_MESSAGE_TEMPLATE_WITHOUT_FILE % TemplateDict
-
-    if not SUPRESS_ERROR:
-        _ERROR_LOGGER.log(ERROR, LogText)
-    if RaiseError:
-        raise FatalError(ErrorCode)
-
-    
-## Initialize log system
-#
-def Initialize():
-    #
-    # Since we use different format to log different levels of message into 
-    # different place (stdout or stderr), we have to use different "Logger" 
-    # objects to do this.
-    #
-    # For DEBUG level (All DEBUG_0~9 are applicable)
-    _DEBUG_LOGGER.setLevel(INFO)
-    _DebugChannel = StreamHandler(stdout)
-    _DebugChannel.setFormatter(_DEBUG_FORMATTER)
-    _DEBUG_LOGGER.addHandler(_DebugChannel)
-    #
-    # For VERBOSE, INFO, WARN level
-    #
-    _INFO_LOGGER.setLevel(INFO)
-    _InfoChannel = StreamHandler(stdout)
-    _InfoChannel.setFormatter(_INFO_FORMATTER)
-    _INFO_LOGGER.addHandler(_InfoChannel)
-    #
-    # For ERROR level
-    #
-    _ERROR_LOGGER.setLevel(INFO)
-    _ErrorCh = StreamHandler(stderr)
-    _ErrorCh.setFormatter(_ERROR_FORMATTER)
-    _ERROR_LOGGER.addHandler(_ErrorCh)
-
-
-## Set log level
-#
-#   @param  Level   One of log level in _LogLevel
-#
-def SetLevel(Level):
-    if Level not in _LOG_LEVELS:
-        Info("Not supported log level (%d). Use default level instead." % \
-             Level)
-        Level = INFO
-    _DEBUG_LOGGER.setLevel(Level)
-    _INFO_LOGGER.setLevel(Level)
-    _ERROR_LOGGER.setLevel(Level)
-
-## Get current log level
-#
-def GetLevel():
-    return _INFO_LOGGER.getEffectiveLevel()
-
-## Raise up warning as error
-#
-def SetWarningAsError():
-    GlobalData.gWARNING_AS_ERROR = True
-
-## Specify a file to store the log message as well as put on console
-#
-#   @param  LogFile     The file path used to store the log message
-#
-def SetLogFile(LogFile):
-    if os.path.exists(LogFile):
-        remove(LogFile)
-
-    _Ch = FileHandler(LogFile)
-    _Ch.setFormatter(_DEBUG_FORMATTER)
-    _DEBUG_LOGGER.addHandler(_Ch)
-
-    _Ch = FileHandler(LogFile)
-    _Ch.setFormatter(_INFO_FORMATTER)
-    _INFO_LOGGER.addHandler(_Ch)
-
-    _Ch = FileHandler(LogFile)
-    _Ch.setFormatter(_ERROR_FORMATTER)
-    _ERROR_LOGGER.addHandler(_Ch)
-
-
-
+## @file\r
+# This file implements the log mechanism for Python tools.\r
+#\r
+# Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+#\r
+# This program and the accompanying materials are licensed and made available \r
+# under the terms and conditions of the BSD License which accompanies this \r
+# distribution. The full text of the license may be found at \r
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+'''\r
+Logger\r
+'''\r
+\r
+## Import modules\r
+from sys import argv\r
+from sys import stdout\r
+from sys import stderr\r
+import os.path\r
+from os import remove\r
+from logging import getLogger\r
+from logging import Formatter\r
+from logging import StreamHandler\r
+from logging import FileHandler\r
+from traceback import extract_stack\r
+\r
+from Logger.ToolError import FatalError\r
+from Logger.ToolError import WARNING_AS_ERROR\r
+from Logger.ToolError import gERROR_MESSAGE\r
+from Logger.ToolError import UNKNOWN_ERROR\r
+from Library import GlobalData\r
+\r
+#\r
+# Log level constants\r
+#\r
+DEBUG_0 = 1\r
+DEBUG_1 = 2\r
+DEBUG_2 = 3\r
+DEBUG_3 = 4\r
+DEBUG_4 = 5\r
+DEBUG_5 = 6\r
+DEBUG_6 = 7\r
+DEBUG_7 = 8\r
+DEBUG_8 = 9\r
+DEBUG_9 = 10\r
+VERBOSE = 15\r
+INFO    = 20\r
+WARN    = 30\r
+QUIET   = 40\r
+QUIET_1 = 41\r
+ERROR   = 50\r
+SILENT  = 60\r
+\r
+IS_RAISE_ERROR = True\r
+SUPRESS_ERROR = False\r
+\r
+#\r
+# Tool name\r
+#\r
+_TOOL_NAME = os.path.basename(argv[0])\r
+#\r
+# For validation purpose\r
+#\r
+_LOG_LEVELS = [DEBUG_0, DEBUG_1, DEBUG_2, DEBUG_3, DEBUG_4, DEBUG_5, DEBUG_6, \\r
+              DEBUG_7, DEBUG_8, DEBUG_9, VERBOSE, WARN, INFO, ERROR, QUIET, \\r
+              QUIET_1, SILENT]\r
+#\r
+# For DEBUG level (All DEBUG_0~9 are applicable)\r
+#\r
+_DEBUG_LOGGER = getLogger("tool_debug")\r
+_DEBUG_FORMATTER = Formatter("[%(asctime)s.%(msecs)d]: %(message)s", \\r
+                            datefmt="%H:%M:%S")\r
+#\r
+# For VERBOSE, INFO, WARN level\r
+#\r
+_INFO_LOGGER = getLogger("tool_info")\r
+_INFO_FORMATTER = Formatter("%(message)s")\r
+#\r
+# For ERROR level\r
+#\r
+_ERROR_LOGGER = getLogger("tool_error")\r
+_ERROR_FORMATTER = Formatter("%(message)s")\r
+\r
+#\r
+# String templates for ERROR/WARN/DEBUG log message\r
+#\r
+_ERROR_MESSAGE_TEMPLATE = \\r
+('\n\n%(tool)s...\n%(file)s(%(line)s): error %(errorcode)04X: %(msg)s\n\t%(extra)s')\r
+\r
+__ERROR_MESSAGE_TEMPLATE_WITHOUT_FILE = \\r
+'\n\n%(tool)s...\n : error %(errorcode)04X: %(msg)s\n\t%(extra)s'\r
+\r
+_WARNING_MESSAGE_TEMPLATE = '%(tool)s...\n%(file)s(%(line)s): warning: %(msg)s'\r
+_WARNING_MESSAGE_TEMPLATE_WITHOUT_FILE = '%(tool)s: : warning: %(msg)s'\r
+_DEBUG_MESSAGE_TEMPLATE = '%(file)s(%(line)s): debug: \n    %(msg)s'\r
+\r
+\r
+#\r
+# Log INFO message\r
+#\r
+#Info    = _INFO_LOGGER.info\r
+\r
+def Info(msg, *args, **kwargs):\r
+    _INFO_LOGGER.info(msg, *args, **kwargs)\r
+\r
+#\r
+# Log information which should be always put out\r
+#\r
+def Quiet(msg, *args, **kwargs):\r
+    _ERROR_LOGGER.error(msg, *args, **kwargs)\r
+\r
+## Log debug message\r
+#\r
+#   @param  Level       DEBUG level (DEBUG0~9)\r
+#   @param  Message     Debug information\r
+#   @param  ExtraData   More information associated with "Message"\r
+#\r
+def Debug(Level, Message, ExtraData=None):\r
+    if _DEBUG_LOGGER.level > Level:\r
+        return\r
+    if Level > DEBUG_9:\r
+        return\r
+    #\r
+    # Find out the caller method information\r
+    #\r
+    CallerStack = extract_stack()[-2]\r
+    TemplateDict = {\r
+        "file"      : CallerStack[0],\r
+        "line"      : CallerStack[1],\r
+        "msg"       : Message,\r
+    }\r
+\r
+    if ExtraData != None:\r
+        LogText = _DEBUG_MESSAGE_TEMPLATE % TemplateDict + "\n    %s" % ExtraData\r
+    else:\r
+        LogText = _DEBUG_MESSAGE_TEMPLATE % TemplateDict\r
+\r
+    _DEBUG_LOGGER.log(Level, LogText)\r
+\r
+## Log verbose message\r
+#\r
+#   @param  Message     Verbose information\r
+#\r
+def Verbose(Message):\r
+    return _INFO_LOGGER.log(VERBOSE, Message)\r
+\r
+## Log warning message\r
+#\r
+#   Warning messages are those which might be wrong but won't fail the tool.\r
+#\r
+#   @param  ToolName    The name of the tool. If not given, the name of caller\r
+#                       method will be used.\r
+#   @param  Message     Warning information\r
+#   @param  File        The name of file which caused the warning.\r
+#   @param  Line        The line number in the "File" which caused the warning.\r
+#   @param  ExtraData   More information associated with "Message"\r
+#\r
+def Warn(ToolName, Message, File=None, Line=None, ExtraData=None):\r
+    if _INFO_LOGGER.level > WARN:\r
+        return\r
+    #\r
+    # if no tool name given, use caller's source file name as tool name\r
+    #\r
+    if ToolName == None or ToolName == "":\r
+        ToolName = os.path.basename(extract_stack()[-2][0])\r
+\r
+    if Line == None:\r
+        Line = "..."\r
+    else:\r
+        Line = "%d" % Line\r
+\r
+    TemplateDict = {\r
+        "tool"      : ToolName,\r
+        "file"      : File,\r
+        "line"      : Line,\r
+        "msg"       : Message,\r
+    }\r
+\r
+    if File != None:\r
+        LogText = _WARNING_MESSAGE_TEMPLATE % TemplateDict\r
+    else:\r
+        LogText = _WARNING_MESSAGE_TEMPLATE_WITHOUT_FILE % TemplateDict\r
+\r
+    if ExtraData != None:\r
+        LogText += "\n    %s" % ExtraData\r
+\r
+    _INFO_LOGGER.log(WARN, LogText)\r
+    #\r
+    # Raise an execption if indicated\r
+    #\r
+    if GlobalData.gWARNING_AS_ERROR == True:\r
+        raise FatalError(WARNING_AS_ERROR)\r
+\r
+## Log ERROR message\r
+#\r
+# Once an error messages is logged, the tool's execution will be broken by \r
+# raising an execption. If you don't want to break the execution later, you \r
+# can give "RaiseError" with "False" value.\r
+#\r
+#   @param  ToolName    The name of the tool. If not given, the name of caller\r
+#                       method will be used.\r
+#   @param  ErrorCode   The error code\r
+#   @param  Message     Warning information\r
+#   @param  File        The name of file which caused the error.\r
+#   @param  Line        The line number in the "File" which caused the warning.\r
+#   @param  ExtraData   More information associated with "Message"\r
+#   @param  RaiseError  Raise an exception to break the tool's executuion if\r
+#                       it's True. This is the default behavior.\r
+#\r
+def Error(ToolName, ErrorCode, Message=None, File=None, Line=None, \\r
+          ExtraData=None, RaiseError=IS_RAISE_ERROR):\r
+    if ToolName:\r
+        pass\r
+    if Line == None:\r
+        Line = "..."\r
+    else:\r
+        Line = "%d" % Line\r
+\r
+    if Message == None:\r
+        if ErrorCode in gERROR_MESSAGE:\r
+            Message = gERROR_MESSAGE[ErrorCode]\r
+        else:\r
+            Message = gERROR_MESSAGE[UNKNOWN_ERROR]\r
+\r
+    if ExtraData == None:\r
+        ExtraData = ""\r
+\r
+    TemplateDict = {\r
+        "tool"      : _TOOL_NAME,\r
+        "file"      : File,\r
+        "line"      : Line,\r
+        "errorcode" : ErrorCode,\r
+        "msg"       : Message,\r
+        "extra"     : ExtraData\r
+    }\r
+\r
+    if File != None:\r
+        LogText =  _ERROR_MESSAGE_TEMPLATE % TemplateDict\r
+    else:\r
+        LogText = __ERROR_MESSAGE_TEMPLATE_WITHOUT_FILE % TemplateDict\r
+\r
+    if not SUPRESS_ERROR:\r
+        _ERROR_LOGGER.log(ERROR, LogText)\r
+    if RaiseError:\r
+        raise FatalError(ErrorCode)\r
+\r
+    \r
+## Initialize log system\r
+#\r
+def Initialize():\r
+    #\r
+    # Since we use different format to log different levels of message into \r
+    # different place (stdout or stderr), we have to use different "Logger" \r
+    # objects to do this.\r
+    #\r
+    # For DEBUG level (All DEBUG_0~9 are applicable)\r
+    _DEBUG_LOGGER.setLevel(INFO)\r
+    _DebugChannel = StreamHandler(stdout)\r
+    _DebugChannel.setFormatter(_DEBUG_FORMATTER)\r
+    _DEBUG_LOGGER.addHandler(_DebugChannel)\r
+    #\r
+    # For VERBOSE, INFO, WARN level\r
+    #\r
+    _INFO_LOGGER.setLevel(INFO)\r
+    _InfoChannel = StreamHandler(stdout)\r
+    _InfoChannel.setFormatter(_INFO_FORMATTER)\r
+    _INFO_LOGGER.addHandler(_InfoChannel)\r
+    #\r
+    # For ERROR level\r
+    #\r
+    _ERROR_LOGGER.setLevel(INFO)\r
+    _ErrorCh = StreamHandler(stderr)\r
+    _ErrorCh.setFormatter(_ERROR_FORMATTER)\r
+    _ERROR_LOGGER.addHandler(_ErrorCh)\r
+\r
+\r
+## Set log level\r
+#\r
+#   @param  Level   One of log level in _LogLevel\r
+#\r
+def SetLevel(Level):\r
+    if Level not in _LOG_LEVELS:\r
+        Info("Not supported log level (%d). Use default level instead." % \\r
+             Level)\r
+        Level = INFO\r
+    _DEBUG_LOGGER.setLevel(Level)\r
+    _INFO_LOGGER.setLevel(Level)\r
+    _ERROR_LOGGER.setLevel(Level)\r
+\r
+## Get current log level\r
+#\r
+def GetLevel():\r
+    return _INFO_LOGGER.getEffectiveLevel()\r
+\r
+## Raise up warning as error\r
+#\r
+def SetWarningAsError():\r
+    GlobalData.gWARNING_AS_ERROR = True\r
+\r
+## Specify a file to store the log message as well as put on console\r
+#\r
+#   @param  LogFile     The file path used to store the log message\r
+#\r
+def SetLogFile(LogFile):\r
+    if os.path.exists(LogFile):\r
+        remove(LogFile)\r
+\r
+    _Ch = FileHandler(LogFile)\r
+    _Ch.setFormatter(_DEBUG_FORMATTER)\r
+    _DEBUG_LOGGER.addHandler(_Ch)\r
+\r
+    _Ch = FileHandler(LogFile)\r
+    _Ch.setFormatter(_INFO_FORMATTER)\r
+    _INFO_LOGGER.addHandler(_Ch)\r
+\r
+    _Ch = FileHandler(LogFile)\r
+    _Ch.setFormatter(_ERROR_FORMATTER)\r
+    _ERROR_LOGGER.addHandler(_Ch)\r
+\r
+\r
+\r