From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by mx.groups.io with SMTP id smtpd.web11.4670.1661811492235824868 for ; Mon, 29 Aug 2022 15:18:12 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=C0jVm0b/; spf=permerror, err=parse error for token &{10 18 %{ir}.%{v}.%{d}.spf.has.pphosted.com}: invalid domain name (domain: quicinc.com, ip: 205.220.180.131, mailfrom: quic_rcran@quicinc.com) Received: from pps.filterd (m0279869.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 27TJkLiU020298; Mon, 29 Aug 2022 22:18:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=HJI63Xhpb+n+W/1CIGy7GUyi6+rJEcTkNz8/f9lGRdE=; b=C0jVm0b/9tgsGoyFZ8p6knNwjCtc9a9Qf2F3jqwRD53kpSG51VvSjvYsLbPj6RRtRZZw X/odVOmyFB2rEsn5h5/9RMhJns4swDk+/12wL3/7tVhpYIu2kydgzSxoKWKShBpDqawa IIxpWAUfZxrwlo6Id3VWoDGSKimm0HPU68atvu7mJM1z6c6Bn1nLqhVcIbXQIrqb1aDE 7kYyBG0HA6Yc2CXR3VYUC0lOwmIJ97OP6543s/TC6/oYo1/Mx4fZGoWREw7NmvwdL9JK jDA0kbF7hfirGkQ5PKkQUY8YC0xQH4JmZoj/LeyU06kmaAYvaqvPoIPRleWz36UK2ATC 8A== Received: from nalasppmta02.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3j8x1j1duu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 29 Aug 2022 22:18:01 +0000 Received: from nalasex01a.na.qualcomm.com (nalasex01a.na.qualcomm.com [10.47.209.196]) by NALASPPMTA02.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 27TMD0WV026273 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 29 Aug 2022 22:13:00 GMT 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; Mon, 29 Aug 2022 15:12:59 -0700 From: "Rebecca Cran" To: , Michael D Kinney , Liming Gao , Zhiguang Liu CC: Rebecca Cran Subject: [PATCH 1/1] MdePkg: Use ANSI colors to indicate debug message severity Date: Mon, 29 Aug 2022 16:12:11 -0600 Message-ID: <20220829221211.71032-1-rebecca@quicinc.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01b.na.qualcomm.com (10.46.141.250) To nalasex01a.na.qualcomm.com (10.47.209.196) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: a9TbzmnkxLiGgZGfGDARYiYpInMM8-jb X-Proofpoint-ORIG-GUID: a9TbzmnkxLiGgZGfGDARYiYpInMM8-jb X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.517,FMLib:17.11.122.1 definitions=2022-08-29_11,2022-08-25_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 clxscore=1011 bulkscore=0 mlxscore=0 mlxlogscore=999 lowpriorityscore=0 adultscore=0 impostorscore=0 suspectscore=0 priorityscore=1501 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2208290098 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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). Signed-off-by: Rebecca Cran --- MdePkg/Include/Library/PrintLib.h | 39 ++++++ MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c | 2 + MdePkg/Library/BasePrintLib/PrintLib.c | 128 ++++++++++++++++++++ MdePkg/Library/UefiDebugLibConOut/DebugLib.c | 2 + 4 files changed, 171 insertions(+) diff --git a/MdePkg/Include/Library/PrintLib.h b/MdePkg/Include/Library/PrintLib.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 combinations of Unicode and ASCII strings are supported. +Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved.
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent @@ -931,4 +932,42 @@ SPrintLengthAsciiFormat ( IN VA_LIST Marker ); +/** + 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/Library/BaseDebugLibSerialPort/DebugLib.c index bd5686947712..d599d16f5b7a 100644 --- a/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c +++ b/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c @@ -125,6 +125,8 @@ DebugPrintMarker ( AsciiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker); } + 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/BasePrintLib/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. + 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 **/ +#include + #include "PrintLibInternal.h" // @@ -19,6 +22,19 @@ VA_LIST gNullVaList; #define ASSERT_UNICODE_BUFFER(Buffer) ASSERT ((((UINTN) (Buffer)) & 0x01) == 0) +// +// Define the maximum debug and assert message length that this library supports +// +#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 = 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 = 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..bc902b0e60c6 100644 --- a/MdePkg/Library/UefiDebugLibConOut/DebugLib.c +++ b/MdePkg/Library/UefiDebugLibConOut/DebugLib.c @@ -108,6 +108,8 @@ DebugPrintMarker ( UnicodeBSPrintAsciiFormat (Buffer, sizeof (Buffer), Format, BaseListMarker); } + UnicodeDebugGetColorString (Buffer, MAX_DEBUG_MESSAGE_LENGTH, ErrorLevel); + // // Send the print string to the Console Output device // -- 2.30.2