From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ma1-aaemail-dr-lapp01.apple.com (ma1-aaemail-dr-lapp01.apple.com [17.171.2.60]) by mx.groups.io with SMTP id smtpd.web09.18884.1662070920763564476 for ; Thu, 01 Sep 2022 15:22:01 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@apple.com header.s=20180706 header.b=iZQi/+RE; spf=pass (domain: apple.com, ip: 17.171.2.60, mailfrom: afish@apple.com) Received: from pps.filterd (ma1-aaemail-dr-lapp01.apple.com [127.0.0.1]) by ma1-aaemail-dr-lapp01.apple.com (8.16.0.42/8.16.0.42) with SMTP id 281MJ9wS021084; Thu, 1 Sep 2022 15:21:52 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=apple.com; h=from : message-id : content-type : mime-version : subject : date : in-reply-to : cc : to : references; s=20180706; bh=R8UnCjceOLik5MA583DhdFZcSTPFBcyrUn2uRcMzzX0=; b=iZQi/+REM6ZDH/OWQMvoxULMxuHDXGH+4MOlI3/4MGg0AU2bsfOIJcoH6z/crQ31Tz2u xsnjc5Nu1HPdMswsbtX86c4uF+FTjP8xo4dUgDYEKANvdw1Tbx1TTYv7Xhm3DR6WIE8s mQOO+dR1NVNeaoAJFpLxSBL9EpBzWm/8NITyxYifxRH/SmYiOdNG0PSsYLlacm6CIFla Ar98tZaaGapRUmJQYZTGfoq3KSduNKPhEDkBBFHCuH+rS1TBYxHHZNKBMWAI9Z0kR+Nv 3LMNQMp1+AB26jKqWEfIVFrYEmlJNlwS4e9ZmQro4EngzLzeTtRMgfeAQ/fvVkzMaMdd iw== Received: from ma-mailsvcp-mta-lapp03.corp.apple.com (ma-mailsvcp-mta-lapp03.corp.apple.com [10.226.18.135]) by ma1-aaemail-dr-lapp01.apple.com with ESMTP id 3j7hx3beq8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO); Thu, 01 Sep 2022 15:21:52 -0700 Received: from ma-mailsvcp-mmp-lapp02.apple.com (ma-mailsvcp-mmp-lapp02.apple.com [17.32.222.15]) by ma-mailsvcp-mta-lapp03.corp.apple.com (Oracle Communications Messaging Server 8.1.0.19.20220711 64bit (built Jul 11 2022)) with ESMTPS id <0RHJ00MK5Y4FBU40@ma-mailsvcp-mta-lapp03.corp.apple.com>; Thu, 01 Sep 2022 15:21:51 -0700 (PDT) Received: from process_milters-daemon.ma-mailsvcp-mmp-lapp02.apple.com by ma-mailsvcp-mmp-lapp02.apple.com (Oracle Communications Messaging Server 8.1.0.19.20220711 64bit (built Jul 11 2022)) id <0RHJ00W00XQ97M00@ma-mailsvcp-mmp-lapp02.apple.com>; Thu, 01 Sep 2022 15:21:51 -0700 (PDT) X-Va-A: X-Va-T-CD: 934eb4426ee8da9c69d7781819503c1d X-Va-E-CD: b37800727ad22a5a409a63c90e470963 X-Va-R-CD: 86ee0474ae0f9020248f3a84f1c4a04b X-Va-CD: 0 X-Va-ID: bfa5ee9c-72a3-4931-bbd3-5e630e0a4454 X-V-A: X-V-T-CD: 934eb4426ee8da9c69d7781819503c1d X-V-E-CD: b37800727ad22a5a409a63c90e470963 X-V-R-CD: 86ee0474ae0f9020248f3a84f1c4a04b X-V-CD: 0 X-V-ID: c10b8139-4f91-4d12-9a7f-c03548ccfa97 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.895 definitions=2022-09-01_12:2022-08-31,2022-09-01 signatures=0 Received: from smtpclient.apple (unknown [17.244.130.120]) by ma-mailsvcp-mmp-lapp02.apple.com (Oracle Communications Messaging Server 8.1.0.19.20220711 64bit (built Jul 11 2022)) with ESMTPSA id <0RHJ00PL8Y4D7L00@ma-mailsvcp-mmp-lapp02.apple.com>; Thu, 01 Sep 2022 15:21:50 -0700 (PDT) From: "Andrew Fish" Message-id: MIME-version: 1.0 (Mac OS X Mail 16.0 \(3731.200.22\)) Subject: Re: [edk2-devel] [PATCH 1/1] MdePkg: Use ANSI colors to indicate debug message severity Date: Thu, 01 Sep 2022 17:21:38 -0500 In-reply-to: Cc: Mike Kinney , "Gao, Liming" , "Liu, Zhiguang" To: devel@edk2.groups.io, quic_rcran@quicinc.com References: <20220829221211.71032-1-rebecca@quicinc.com> X-Mailer: Apple Mail (2.3731.200.22) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.517,18.0.895 definitions=2022-09-01_12:2022-08-31,2022-09-01 signatures=0 Content-type: multipart/alternative; boundary="Apple-Mail=_13F6ED5F-EB59-4C11-9187-E621BE3B8CCE" --Apple-Mail=_13F6ED5F-EB59-4C11-9187-E621BE3B8CCE Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Sep 1, 2022, at 4:52 PM, Rebecca Cran wrote: >=20 > Thanks. I've just sent out a v2 that introduces a >=20 > PcdTerminalSupportsAnsiSequences FeatureFlag. >=20 > Let me know if you'd prefer a different name: I wondered about something = like PcdConOutAnsiSeqSupport for example. >=20 Rebecca, I think this feature flag is going to control the output of edk2 DEBUG. In = an abstract sense DEBUG is not Terminal or ConOut. It could be mapped to th= e same device, but it does not have to be.=20 So maybe PcdDebugAnsiSeqSupport or DebugLibAnsiSeqSupport?=20 Thanks, Andrew Fish > --=20 > Rebecca Cran >=20 > On 8/29/22 17:29, Kinney, Michael D wrote: >> Hi Rebecca, >>=20 >> I think this is a good idea to improve readability. >>=20 >> However, I think there is an assumption today that debug >> message output is just ASCII text with no assumptions on >> a terminal type and no assumption on screen width or screen >> height and no support for cursor control. >>=20 >> This change would only make sense if the device the >> serial messages are sent supports ANSI color codes. >>=20 >> I think we should make this a build time configuration >> option to add the ANSI sequences. I suggest a FeatureFlag >> PCD with a default of disabled to preserve current behavior >> and platforms can opt into ANSI color coded debug messages >> in their DSC file. >>=20 >> Thanks, >>=20 >> Mike >>=20 >>> -----Original Message----- >>> From: Rebecca Cran >>> Sent: Monday, August 29, 2022 3:12 PM >>> To: devel@edk2.groups.io; Kinney, Michael D ; Gao, Liming ; Liu, Zhiguang >>> >>> Cc: Rebecca Cran >>> Subject: [PATCH 1/1] MdePkg: Use ANSI colors to indicate debug message = severity >>>=20 >>> There currently isn't a way to differentiate the different >>> levels of DEBUG output: DEBUG_ERROR, DEBUG_WARN, DEBUG_INFO >>> etc. >>>=20 >>> 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). >>>=20 >>> 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(+) >>>=20 >>> 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 combi= nations of >>> Unicode and ASCII strings are supported. >>>=20 >>> +Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserv= ed.
>>> 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 character= s. >>> + @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); >>> } >>>=20 >>> + AsciiDebugGetColorString (Buffer, MAX_DEBUG_MESSAGE_LENGTH, ErrorLev= el); >>> + >>> // >>> // Send the print string to a Serial Port >>> // >>> diff --git a/MdePkg/Library/BasePrintLib/PrintLib.c b/MdePkg/Library/Ba= sePrintLib/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 rese= rved.
>>> Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved. >>> Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<= BR> >>> 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)) & 0= x01) =3D=3D 0) >>>=20 >>> +// >>> +// Define the maximum debug and assert message length that this librar= y 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_ONL= Y_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_SE= Q); >>> + 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_SE= Q); >>> + 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 character= s. >>> + @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/Libr= ary/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, Base= ListMarker); >>> } >>>=20 >>> + UnicodeDebugGetColorString (Buffer, MAX_DEBUG_MESSAGE_LENGTH, Erro= rLevel); >>> + >>> // >>> // Send the print string to the Console Output device >>> // >>> -- >>> 2.30.2 >=20 >=20 >=20 >=20 --Apple-Mail=_13F6ED5F-EB59-4C11-9187-E621BE3B8CCE Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii

On Sep 1, 2022, at 4:52 PM, Rebecca Cran <quic_rcran@quic= inc.com> wrote:

Thanks. I've just sent out a v2 that introduces a

PcdTerminalSupportsAnsiSequences Feature= Flag.

Let me know if you= 'd prefer a different name: I wondered about something like PcdConOutAnsiSe= qSupport for example.


Rebecca,

I think this feature flag is going to control the output of edk2 DEBUG. In= an abstract sense DEBUG is not Terminal or ConOut. It could be mapped to t= he same device, but it does not have to be. 

= So maybe PcdDebugAnsiSeqSupport or DebugLibAnsiSeqSupport? 
=
Thanks,

Andrew Fish

-- 
Rebecca Cran

On 8/29/22 17:29, Kinney, Michael D wrote:
Hi Rebecca,

I think this is a good idea to improve rea= dability.

However, I think there is an assumption today that debugmessage output is just ASCII text with no assumptions on
a terminal ty= pe and no assumption on screen width or screen
height and no support for= cursor control.

This change would only make sense if the device the=
serial messages are sent supports ANSI color codes.

I think we s= hould make this a build time configuration
option to add the ANSI sequen= ces.  I suggest a FeatureFlag
PCD with a default of disabled to pre= serve current behavior
and platforms can opt into ANSI color coded debug= messages
in their DSC file.

Thanks,

Mike

-----Original Message-----
From: Rebecca Cran <rebec= ca@quicinc.com>
Sent: Monday, August 29, 2022 3:12 PM
To: devel@ed= k2.groups.io; Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Li= ming <gaoliming@byosoft.com.cn>; Liu, Zhiguang
<zhiguang.liu@in= tel.com>
Cc: Rebecca Cran <rebecca@quicinc.com>
Subject: [PA= TCH 1/1] MdePkg: Use ANSI colors to indicate debug message severity

= There currently isn't a way to differentiate the different
levels of DEB= UG output: DEBUG_ERROR, DEBUG_WARN, DEBUG_INFO
etc.

To improve th= is, wrap DEBUG_ERROR and DEBUG_WARN level
messages in ANSI color code es= cape sequences. DEBUG_ERROR
messages will be displayed in red text, and = DEBUG_WARN
in orange (the escape code is for the yellow color paletteentry, but it gets displayed as orange).

Signed-off-by: Rebecca Cra= n <rebecca@quicinc.com>
---
 MdePkg/Include/Library/PrintL= ib.h            &nbs= p;   |  39 ++++++
 MdePkg/Library/BaseDebugLibS= erialPort/DebugLib.c |   2 +
 MdePkg/Library/BasePrintLib= /PrintLib.c           | 1= 28 ++++++++++++++++++++
 MdePkg/Library/UefiDebugLibConOut/DebugLib= .c     |   2 +
 4 files changed, 171 = insertions(+)

diff --git a/MdePkg/Include/Library/PrintLib.h b/MdePk= g/Include/Library/PrintLib.h
index 8d523cac528d..06d9761df897 100644
= --- a/MdePkg/Include/Library/PrintLib.h
+++ b/MdePkg/Include/Library/Pri= ntLib.h
@@ -2,6 +2,7 @@
   Provides services to print = a formatted string to a buffer. All combinations of
   Un= icode and ASCII strings are supported.

+Copyright (c) 2022, Qualcomm= Innovation Center, Inc. All rights reserved.<BR>
 Copyright = (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
 = ;SPDX-License-Identifier: BSD-2-Clause-Patent

@@ -931,4 +932,42 @@ S= PrintLengthAsciiFormat (
   IN  VA_LIST   =    Marker
   );

+/**
+  Wra= ps a message with ANSI color escape codes.
+
+  @param String &n= bsp;   The string to wrap.
+  @param StringLen  = ;The size of the String buffer in characters.
+  @param ErrorLevel = The error level.
+
+  @retval RETURN_SUCCESS    &= nbsp;     The string was successfully updated.
= +  @retval RETURN_BUFFER_TOO_SMALL The buffer is too small.
+
+*= */
+RETURN_STATUS
+EFIAPI
+AsciiDebugGetColorString (
+  I= N OUT CHAR8  *String,
+  IN UINTN     &nbs= p;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 U= nicode characters.
+  @param ErrorLevel The error level.
+
+ =  @retval RETURN_SUCCESS        &nbs= p; The string was successfully updated.
+  @retval RETURN_BUFF= ER_TOO_SMALL The buffer is too small.
+
+**/
+RETURN_STATUS
+EF= IAPI
+UnicodeDebugGetColorString (
+  IN OUT CHAR16  *Strin= g,
+  IN UINTN       StringLen,
+ =  IN UINTN       ErrorLevel
+  );=
+
 #endif
diff --git a/MdePkg/Library/BaseDebugLibSerialPort= /DebugLib.c b/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c
index bd5= 686947712..d599d16f5b7a 100644
--- a/MdePkg/Library/BaseDebugLibSerialPo= rt/DebugLib.c
+++ b/MdePkg/Library/BaseDebugLibSerialPort/DebugLib.c
= @@ -125,6 +125,8 @@ DebugPrintMarker (
     Asc= iiBSPrint (Buffer, sizeof (Buffer), Format, BaseListMarker);
  = ; }

+  AsciiDebugGetColorString (Buffer, MAX_DEBUG_MESSAGE= _LENGTH, ErrorLevel);
+
   //
   // = Send the print string to a Serial Port
   //
diff --gi= t a/MdePkg/Library/BasePrintLib/PrintLib.c b/MdePkg/Library/BasePrintLib/Pr= intLib.c
index e6f4042bb90b..95135d45aff9 100644
--- a/MdePkg/Library= /BasePrintLib/PrintLib.c
+++ b/MdePkg/Library/BasePrintLib/PrintLib.c@@ -1,12 +1,15 @@
 /** @file
   Base Print Libra= ry instance implementation.

+  Copyright (c) 2022, Qualcomm Inn= ovation Center, Inc. All rights reserved.<BR>
   Co= pyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR><= br>   Portions copyright (c) 2008 - 2009, Apple Inc. All rig= hts reserved.<BR>
   SPDX-License-Identifier: BSD-2= -Clause-Patent

 **/

+#include <Library/BaseMemoryLib.= h>
+
 #include "PrintLibInternal.h"

 //
@@ -19= ,6 +22,19 @@ VA_LIST  gNullVaList;

 #define ASSERT_UNICODE= _BUFFER(Buffer)  ASSERT ((((UINTN) (Buffer)) & 0x01) =3D=3D 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"
+#d= efine ASCII_YELLOW_ESC_SEQ  "\033[33m"
+#define ASCII_END_ESC_SEQ &= nbsp;   "\033[0m"
+
+#define RED_ESC_SEQ   &= nbsp; L"\033[31m"
+#define YELLOW_ESC_SEQ  L"\033[33m"
+#de= fine END_ESC_SEQ     L"\033[0m"
+
 /**
&n= bsp;  Produces a Null-terminated Unicode string in an output buff= er based on
   a Null-terminated Unicode format string an= d 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 escap= e codes.
+
+  @param String     The string t= o 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,
+ &n= bsp;IN UINTN      StringLen,
+  IN UINTN &= nbsp;    ErrorLevel
+  )
+{
+  CHAR8=  Buffer[MAX_DEBUG_MESSAGE_LENGTH];
+  UINTN  ReqBufferLe= n;
+
+  ReqBufferLen =3D AsciiStrLen (String) +
+   = ;            &n= bsp; AsciiStrLen (ASCII_RED_ESC_SEQ) +
+     &n= bsp;           Ascii= StrLen (ASCII_END_ESC_SEQ) +
+        = ;         1;
+
+  i= f (StringLen < ReqBufferLen) {
+    return RETURN_BUFF= ER_TOO_SMALL;
+  }
+
+  ZeroMem (Buffer, sizeof (Buffer)= );
+
+  switch (ErrorLevel) {
+    case DEBUG_= WARN:
+      AsciiStrCpyS (Buffer, MAX_DEBUG_ME= SSAGE_LENGTH, ASCII_YELLOW_ESC_SEQ);
+      bre= ak;
+    case DEBUG_ERROR:
+     &= nbsp;AsciiStrCpyS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, ASCII_RED_ESC_SEQ);+      break;
+  }
+
+  AsciiS= trCatS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, String);
+
+  switch (= ErrorLevel) {
+    case DEBUG_WARN:
+   &nbs= p;case DEBUG_ERROR:
+      AsciiStrCatS (Buffer= , MAX_DEBUG_MESSAGE_LENGTH, ASCII_END_ESC_SEQ);
+    &nbs= p; break;
+  }
+
+  AsciiStrCpyS (String, StringLen= , Buffer);
+
+  return RETURN_SUCCESS;
+}
+
+/**
+ &= nbsp;Wraps a message with ANSI color escape codes.
+
+  @param S= tring     The string to wrap.
+  @param StringL= en  The size of the String buffer in Unicode characters.
+  @p= aram ErrorLevel The error level.
+
+  @retval RETURN_SUCCESS &nb= sp;        The string was successfu= lly updated.
+  @retval RETURN_BUFFER_TOO_SMALL The buffer is too s= mall.
+
+**/
+RETURN_STATUS
+EFIAPI
+UnicodeDebugGetColorStr= ing (
+  IN OUT CHAR16  *String,
+  IN UINTN  &nb= sp;    StringLen,
+  IN UINTN    = ;   ErrorLevel
+  )
+{
+  CHAR16  Bu= ffer[MAX_DEBUG_MESSAGE_LENGTH];
+  UINTN   ReqBufferLen;<= br>+
+  ReqBufferLen =3D StrLen (String) +
+    &= nbsp;           &nbs= p;StrLen (RED_ESC_SEQ) +
+        &nb= sp;        StrLen (END_ESC_SEQ) ++             = ;    1;
+
+  if (StringLen < ReqBufferLen= ) {
+    return RETURN_BUFFER_TOO_SMALL;
+  }
= +
+  ZeroMem (Buffer, sizeof (Buffer));
+
+  switch (Err= orLevel) {
+    case DEBUG_WARN:
+    &= nbsp; StrCpyS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, YELLOW_ESC_SEQ);
+=      break;
+    case DEBUG_ERR= OR:
+      StrCpyS (Buffer, MAX_DEBUG_MESSAGE_L= ENGTH, RED_ESC_SEQ);
+      break;
+  }=
+
+  StrCatS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, String);
++  switch (ErrorLevel) {
+    case DEBUG_WARN:
= +    case DEBUG_ERROR:
+      St= rCatS (Buffer, MAX_DEBUG_MESSAGE_LENGTH, END_ESC_SEQ);
+   &nb= sp;  break;
+  }
+
+  StrCpyS (String, StringL= en, Buffer);
+
+  return RETURN_SUCCESS;
+}
diff --git a/M= dePkg/Library/UefiDebugLibConOut/DebugLib.c b/MdePkg/Library/UefiDebugLibCo= nOut/DebugLib.c
index 65c8dc2b4654..bc902b0e60c6 100644
--- a/MdePkg/= Library/UefiDebugLibConOut/DebugLib.c
+++ b/MdePkg/Library/UefiDebugLibC= onOut/DebugLib.c
@@ -108,6 +108,8 @@ DebugPrintMarker (
  &= nbsp;    UnicodeBSPrintAsciiFormat (Buffer, sizeof (Buf= fer), Format, BaseListMarker);
     }

+ =    UnicodeDebugGetColorString (Buffer, MAX_DEBUG_MESSAGE_LEN= GTH, ErrorLevel);
+
     //
  &= nbsp;  // Send the print string to the Console Output device
&= nbsp;    //
--
2.30.2




--Apple-Mail=_13F6ED5F-EB59-4C11-9187-E621BE3B8CCE--