From nobody Sat May 11 23:08:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+93067+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+93067+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=quicinc.com ARC-Seal: i=1; a=rsa-sha256; t=1662069016; cv=none; d=zohomail.com; s=zohoarc; b=AQsBTU25pdOjJBzhmkFNhUEwJpoQJPegjEuw7dHSqnJX9kJqv6Joueq+VpT02SrSgvnLbHKVL7lgCqRcTpAOZnlhT0rLrzrExO9nQTU15zbDPex0D37QcS1fj4yMKV3v4S2N1TMWe2vxP2UP5CgvRGo5dCRNMA0UES30syh96Yw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1662069016; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=ZmJzlj943maoGVHvLoF0146+QgApJTJwKxo8/+ADDVY=; b=TkqJyBwHW6U3REeKzRhGEL/8VEe3X5dJeQ3vj3+ahAcrsy4Hd7hNTt22Feus/yrqns8dWRpRC+HgCepkiV6ygSzZ2pkBWky9P7ZlUFnXnikNJ02UALZ+eoiEzG3uI0/vC/GF7WaP4btJ75MeyskG8EuROcVoK8tXnYq833quWZE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+93067+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1662069016310924.8065418313109; Thu, 1 Sep 2022 14:50:16 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id BcKAYY1788612xyucWyQHPfy; Thu, 01 Sep 2022 14:50:15 -0700 X-Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by mx.groups.io with SMTP id smtpd.web08.18663.1662069015097124532 for ; Thu, 01 Sep 2022 14:50:15 -0700 X-Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 281La4IU002508; Thu, 1 Sep 2022 21:50:07 GMT X-Received: from nalasppmta04.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3jabqk52h3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 01 Sep 2022 21:50:07 +0000 X-Received: from nalasex01a.na.qualcomm.com (nalasex01a.na.qualcomm.com [10.47.209.196]) by NALASPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 281Lo6rH000901 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 1 Sep 2022 21:50:06 GMT X-Received: from linbox.qualcomm.com (10.80.80.8) by nalasex01a.na.qualcomm.com (10.47.209.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.29; Thu, 1 Sep 2022 14:50:05 -0700 From: "Rebecca Cran" To: , Michael D Kinney , Liming Gao , Zhiguang Liu CC: Rebecca Cran Subject: [edk2-devel] [PATCH v2 1/1] MdePkg: Use ANSI colors to indicate debug message severity Date: Thu, 1 Sep 2022 15:49:44 -0600 Message-ID: <20220901214944.162346-2-rebecca@quicinc.com> In-Reply-To: <20220901214944.162346-1-rebecca@quicinc.com> References: <20220901214944.162346-1-rebecca@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nalasex01a.na.qualcomm.com (10.47.209.196) X-QCInternal: smtphost X-Proofpoint-GUID: fpwrTr64Xq_RNBEH6kRNjQZ5G0rOapsP X-Proofpoint-ORIG-GUID: fpwrTr64Xq_RNBEH6kRNjQZ5G0rOapsP Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,quic_rcran@quicinc.com X-Gm-Message-State: oFMiv4sohOk3l1ySZdLplKufx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1662069015; bh=9DExWBIjHmO/drm6IFUaGkZoyJr6tSKk1g14bo5knkY=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=mSpfGIuZvt7in1MmmWQJZDGuwgb8UnQ6bVOKn9Kk7gGvMxUl8MkgVaaPTK31lsf07Ld yTIHkt2/vqbPWEvT/pfrQzsQiPI4DEx4TGQN8ZmsHvzc/jACbKVmYRad1zfNvY4ADpfzE yPwzLvDJqucgVns0kY7pNaGugMgl4vr0Ovc= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1662069016583100005 Content-Type: text/plain; charset="utf-8" There currently isn't a way to differentiate the different levels of DEBUG output: DEBUG_ERROR, DEBUG_WARN, DEBUG_INFO etc. To improve this, wrap DEBUG_ERROR and DEBUG_WARN level messages in ANSI color code escape sequences. DEBUG_ERROR messages will be displayed in red text, and DEBUG_WARN in orange (the escape code is for the yellow color palette entry, but it gets displayed as orange). Only enable this new functionality if the FeatureFlag gEfiMdePkgTokenSpaceGuid.PcdTerminalSupportsAnsiSequences is set to TRUE. By default it's FALSE. Signed-off-by: Rebecca Cran --- MdePkg/MdePkg.dec | 6 + MdePkg/MdePkg.dsc | 2 + MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf | 2 +- MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf | 2 +- MdePkg/Include/Library/PrintLib.h | 39 +++= +++ MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c | 4 + MdePkg/Library/BasePrintLib/PrintLib.c | 128 +++= +++++++++++++++++ MdePkg/Library/UefiDebugLibConOut/DebugLib.c | 4 + 8 files changed, 185 insertions(+), 2 deletions(-) diff --git a/MdePkg/MdePkg.dec b/MdePkg/MdePkg.dec index f1ebf9e251c1..1370755944fc 100644 --- a/MdePkg/MdePkg.dec +++ b/MdePkg/MdePkg.dec @@ -4,6 +4,7 @@ # It also provides the definitions(including PPIs/PROTOCOLs/GUIDs) of # EFI1.10/UEFI2.7/PI1.7 and some Industry Standards. # +# Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved= .
# Copyright (c) 2007 - 2022, Intel Corporation. All rights reserved.
# Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
# (C) Copyright 2016 - 2021 Hewlett Packard Enterprise Development LP
@@ -1973,6 +1974,11 @@ [PcdsFeatureFlag] # @Prompt Validate ORDERED_COLLECTION structure gEfiMdePkgTokenSpaceGuid.PcdValidateOrderedCollection|FALSE|BOOLEAN|0x00= 00002a =20 + ## Indicates if the terminal supports ANSI sequences.

+ # TRUE - Supports ANSI sequences.
+ # FALSE - Does not support ANSI sequences.
+ gEfiMdePkgTokenSpaceGuid.PcdTerminalSupportsAnsiSequences|FALSE|BOOLEAN|= 0x0000002f + [PcdsFixedAtBuild] ## Status code value for indicating a watchdog timer has expired. # EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_TIMER_EXPIRED diff --git a/MdePkg/MdePkg.dsc b/MdePkg/MdePkg.dsc index cc1ac196a931..fe6b476dac0e 100644 --- a/MdePkg/MdePkg.dsc +++ b/MdePkg/MdePkg.dsc @@ -1,6 +1,7 @@ ## @file # EFI/PI MdePkg Package # +# Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved= .
# Copyright (c) 2007 - 2022, Intel Corporation. All rights reserved.
# Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
# (C) Copyright 2020 Hewlett Packard Enterprise Development LP
@@ -25,6 +26,7 @@ [Defines] =20 [PcdsFeatureFlag] gEfiMdePkgTokenSpaceGuid.PcdUgaConsumeSupport|TRUE + gEfiMdePkgTokenSpaceGuid.PcdTerminalSupportsAnsiSequences|FALSE =20 [PcdsFixedAtBuild] gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x0f diff --git a/MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.i= nf b/MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf index 7504faee67f0..c759547cd662 100644 --- a/MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf +++ b/MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.inf @@ -41,4 +41,4 @@ [Pcd] gEfiMdePkgTokenSpaceGuid.PcdDebugClearMemoryValue ## SOMETIMES_CONSUMES gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask ## CONSUMES gEfiMdePkgTokenSpaceGuid.PcdFixedDebugPrintErrorLevel ## CONSUMES - + gEfiMdePkgTokenSpaceGuid.PcdTerminalSupportsAnsiSequences ## CONSUMES diff --git a/MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf b/Mde= Pkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf index 53bbc8ce3f65..fb7bc1f8e728 100644 --- a/MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf +++ b/MdePkg/Library/UefiDebugLibConOut/UefiDebugLibConOut.inf @@ -50,4 +50,4 @@ [Pcd] gEfiMdePkgTokenSpaceGuid.PcdDebugClearMemoryValue ## SOMETIMES_CO= NSUMES gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask ## CONSUMES gEfiMdePkgTokenSpaceGuid.PcdFixedDebugPrintErrorLevel ## CONSUMES - + gEfiMdePkgTokenSpaceGuid.PcdTerminalSupportsAnsiSequences ## CONSUMES diff --git a/MdePkg/Include/Library/PrintLib.h b/MdePkg/Include/Library/Pri= ntLib.h index 8d523cac528d..06d9761df897 100644 --- a/MdePkg/Include/Library/PrintLib.h +++ b/MdePkg/Include/Library/PrintLib.h @@ -2,6 +2,7 @@ Provides services to print a formatted string to a buffer. All combinati= ons of Unicode and ASCII strings are supported. =20 +Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved.<= BR> Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 @@ -931,4 +932,42 @@ SPrintLengthAsciiFormat ( IN VA_LIST Marker ); =20 +/** + Wraps a message with ANSI color escape codes. + + @param String The string to wrap. + @param StringLen The size of the String buffer in characters. + @param ErrorLevel The error level. + + @retval RETURN_SUCCESS The string was successfully updated. + @retval RETURN_BUFFER_TOO_SMALL The buffer is too small. + +**/ +RETURN_STATUS +EFIAPI +AsciiDebugGetColorString ( + IN OUT CHAR8 *String, + IN UINTN StringLen, + IN UINTN ErrorLevel + ); + +/** + Wraps a message with ANSI color escape codes. + + @param String The string to wrap. + @param StringLen The size of the String buffer in Unicode characters. + @param ErrorLevel The error level. + + @retval RETURN_SUCCESS The string was successfully updated. + @retval RETURN_BUFFER_TOO_SMALL The buffer is too small. + +**/ +RETURN_STATUS +EFIAPI +UnicodeDebugGetColorString ( + IN OUT CHAR16 *String, + IN UINTN StringLen, + IN UINTN ErrorLevel + ); + #endif diff --git a/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c b/MdePkg/Libr= ary/BaseDebugLibSerialPort/DebugLib.c index bd5686947712..d7714a011244 100644 --- a/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c +++ b/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c @@ -125,6 +125,10 @@ DebugPrintMarker ( AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker); } =20 + if (FeaturePcdGet (PcdTerminalSupportsAnsiSequences)) { + AsciiDebugGetColorString (Buffer, MAX_DEBUG_MESSAGE_LENGTH, ErrorLevel= ); + } + // // Send the print string to a Serial Port // diff --git a/MdePkg/Library/BasePrintLib/PrintLib.c b/MdePkg/Library/BasePr= intLib/PrintLib.c index e6f4042bb90b..95135d45aff9 100644 --- a/MdePkg/Library/BasePrintLib/PrintLib.c +++ b/MdePkg/Library/BasePrintLib/PrintLib.c @@ -1,12 +1,15 @@ /** @file Base Print Library instance implementation. =20 + Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved= .
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ =20 +#include + #include "PrintLibInternal.h" =20 // @@ -19,6 +22,19 @@ VA_LIST gNullVaList; =20 #define ASSERT_UNICODE_BUFFER(Buffer) ASSERT ((((UINTN) (Buffer)) & 0x01)= =3D=3D 0) =20 +// +// Define the maximum debug and assert message length that this library su= pports +// +#define MAX_DEBUG_MESSAGE_LENGTH 0x100 + +#define ASCII_RED_ESC_SEQ "\033[31m" +#define ASCII_YELLOW_ESC_SEQ "\033[33m" +#define ASCII_END_ESC_SEQ "\033[0m" + +#define RED_ESC_SEQ L"\033[31m" +#define YELLOW_ESC_SEQ L"\033[33m" +#define END_ESC_SEQ L"\033[0m" + /** Produces a Null-terminated Unicode string in an output buffer based on a Null-terminated Unicode format string and a VA_LIST argument list. @@ -834,3 +850,115 @@ SPrintLengthAsciiFormat ( { return BasePrintLibSPrintMarker (NULL, 0, OUTPUT_UNICODE | COUNT_ONLY_NO= _PRINT, (CHAR8 *)FormatString, Marker, NULL); } + +/** + Wraps a message with ANSI color escape codes. + + @param String The string to wrap. + @param StringLen The size of the String buffer in characters. + @param ErrorLevel The error level. + + @retval RETURN_SUCCESS The string was successfully updated. + @retval RETURN_BUFFER_TOO_SMALL The buffer is too small. + +**/ +RETURN_STATUS +EFIAPI +AsciiDebugGetColorString ( + IN OUT CHAR8 *String, + IN UINTN StringLen, + IN UINTN ErrorLevel + ) +{ + CHAR8 Buffer[MAX_DEBUG_MESSAGE_LENGTH]; + UINTN ReqBufferLen; + + ReqBufferLen =3D AsciiStrLen (String) + + AsciiStrLen (ASCII_RED_ESC_SEQ) + + AsciiStrLen (ASCII_END_ESC_SEQ) + + 1; + + if (StringLen < ReqBufferLen) { + return RETURN_BUFFER_TOO_SMALL; + } + + ZeroMem (Buffer, sizeof (Buffer)); + + switch (ErrorLevel) { + case DEBUG_WARN: + AsciiStrCpyS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, ASCII_YELLOW_ESC_SEQ= ); + break; + case DEBUG_ERROR: + AsciiStrCpyS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, ASCII_RED_ESC_SEQ); + break; + } + + AsciiStrCatS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, String); + + switch (ErrorLevel) { + case DEBUG_WARN: + case DEBUG_ERROR: + AsciiStrCatS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, ASCII_END_ESC_SEQ); + break; + } + + AsciiStrCpyS (String, StringLen, Buffer); + + return RETURN_SUCCESS; +} + +/** + Wraps a message with ANSI color escape codes. + + @param String The string to wrap. + @param StringLen The size of the String buffer in Unicode characters. + @param ErrorLevel The error level. + + @retval RETURN_SUCCESS The string was successfully updated. + @retval RETURN_BUFFER_TOO_SMALL The buffer is too small. + +**/ +RETURN_STATUS +EFIAPI +UnicodeDebugGetColorString ( + IN OUT CHAR16 *String, + IN UINTN StringLen, + IN UINTN ErrorLevel + ) +{ + CHAR16 Buffer[MAX_DEBUG_MESSAGE_LENGTH]; + UINTN ReqBufferLen; + + ReqBufferLen =3D StrLen (String) + + StrLen (RED_ESC_SEQ) + + StrLen (END_ESC_SEQ) + + 1; + + if (StringLen < ReqBufferLen) { + return RETURN_BUFFER_TOO_SMALL; + } + + ZeroMem (Buffer, sizeof (Buffer)); + + switch (ErrorLevel) { + case DEBUG_WARN: + StrCpyS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, YELLOW_ESC_SEQ); + break; + case DEBUG_ERROR: + StrCpyS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, RED_ESC_SEQ); + break; + } + + StrCatS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, String); + + switch (ErrorLevel) { + case DEBUG_WARN: + case DEBUG_ERROR: + StrCatS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, END_ESC_SEQ); + break; + } + + StrCpyS (String, StringLen, Buffer); + + return RETURN_SUCCESS; +} diff --git a/MdePkg/Library/UefiDebugLibConOut/DebugLib.c b/MdePkg/Library/= UefiDebugLibConOut/DebugLib.c index 65c8dc2b4654..eea24edc0b77 100644 --- a/MdePkg/Library/UefiDebugLibConOut/DebugLib.c +++ b/MdePkg/Library/UefiDebugLibConOut/DebugLib.c @@ -108,6 +108,10 @@ DebugPrintMarker ( UnicodeBSPrintAsciiFormat (Buffer, sizeof (Buffer), Format, BaseList= Marker); } =20 + if (FeaturePcdGet (PcdTerminalSupportsAnsiSequences)) { + UnicodeDebugGetColorString (Buffer, MAX_DEBUG_MESSAGE_LENGTH, ErrorL= evel); + } + // // Send the print string to the Console Output device // --=20 2.30.2 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#93067): https://edk2.groups.io/g/devel/message/93067 Mute This Topic: https://groups.io/mt/93406439/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-