efiutilitymsgs.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 757 行 · 第 1/2 页

C
757
字号
  MsgFmt      - the format string for the debug message. Can contain formatting
                controls for use with varargs.
           
Returns:
  None.

--*/
{
  va_list List;
  //
  // If the debug mask is not applicable, then do nothing.
  //
  if ((MsgMask != 0) && ((mDebugMsgMask & MsgMask) == 0)) {
    return ;
  }

  va_start (List, MsgFmt);
  PrintMessage ("debug", FileName, LineNumber, 0, Text, MsgFmt, List);
  va_end (List);
}

static
void
PrintMessage (
  INT8    *Type,
  INT8    *FileName,
  UINT32  LineNumber,
  UINT32  MessageCode,
  INT8    *Text,
  INT8    *MsgFmt,
  va_list List
  )
/*++

Routine Description:
  Worker routine for all the utility printing services. Prints the message in
  a format that Visual Studio will find when scanning build outputs for
  errors or warnings.

Arguments:
  Type        - "warning" or "error" string to insert into the message to be
                printed. The first character of this string (converted to uppercase)
                is used to preceed the MessageCode value in the output string.

  FileName    - name of the file where the warning was detected, or the name
                of the application that detected the warning
  
  LineNumber  - the line number where the warning was detected (parsers).
                0 should be specified if the utility is not a parser.
               
  MessageCode - an application-specific warning code that can be referenced in
                other documentation. 

  Text        - part of the message to print
  
  MsgFmt      - the format string for the message. Can contain formatting
                controls for use with varargs.
  List        - the variable list.
           
Returns:
  None.

Notes:
  If FileName == NULL then this utility will use the string passed into SetUtilityName(). 
  
  LineNumber is only used if the caller is a parser, in which case FileName refers to the
  file being parsed.

  Text and MsgFmt are both optional, though it would be of little use calling this function with
  them both NULL.

  Output will typically be of the form:
    <FileName>(<LineNumber>) : <Type> <Type[0]><MessageCode>: <Text> : <MsgFmt>

    Parser (LineNumber != 0)
      VfrCompile.cpp(330) : error E2660: AddVfrDataStructField : function does not take 2 parameters
    Generic utility (LineNumber == 0) 
      UtilityName : error E1234 : Text string : MsgFmt string and args

--*/
{
  INT8  Line[MAX_LINE_LEN];
  INT8  Line2[MAX_LINE_LEN];
  INT8  *Cptr;
  //
  // If given a filename, then add it (and the line number) to the string.
  // If there's no filename, then use the program name if provided.
  //
  if (FileName != NULL) {
    Cptr = FileName;
  } else if (mUtilityName[0] != 0) {
    Cptr = mUtilityName;
  } else {
    Cptr = "Unknown utility";
  }

  strcpy (Line, Cptr);
  if (LineNumber != 0) {
    sprintf (Line2, "(%d)", LineNumber);
    strcat (Line, Line2);
  }
  //
  // Have to print an error code or Visual Studio won't find the
  // message for you. It has to be decimal digits too.
  //
  sprintf (Line2, " : %s %c%04d", Type, toupper (Type[0]), MessageCode);
  strcat (Line, Line2);
  fprintf (stdout, "%s", Line);
  //
  // If offending text was provided, then print it
  //
  if (Text != NULL) {
    fprintf (stdout, ": %s ", Text);
  }
  //
  // Print formatted message if provided
  //
  if (MsgFmt != NULL) {
    vsprintf (Line2, MsgFmt, List);
    fprintf (stdout, ": %s", Line2);
  }

  fprintf (stdout, "\n");
}

void
ParserSetPosition (
  INT8    *SourceFileName,
  UINT32  LineNum
  )
/*++

Routine Description:
  Set the position in a file being parsed. This can be used to 
  print error messages deeper down in a parser.

Arguments:
  SourceFileName - name of the source file being parsed
  LineNum        - line number of the source file being parsed

Returns:
  NA

--*/
{
  mSourceFileName     = SourceFileName;
  mSourceFileLineNum  = LineNum;
}

void
SetUtilityName (
  INT8    *UtilityName
  )
/*++

Routine Description:
  All printed error/warning/debug messages follow the same format, and
  typically will print a filename or utility name followed by the error
  text. However if a filename is not passed to the print routines, then
  they'll print the utility name if you call this function early in your
  app to set the utility name.
  
Arguments:
  UtilityName  -  name of the utility, which will be printed with all
                  error/warning/debug messags.

Returns:
  NA
  
--*/
{
  //
  // Save the name of the utility in our local variable. Make sure its
  // length does not exceed our buffer.
  //
  if (UtilityName != NULL) {
    if (strlen (UtilityName) >= sizeof (mUtilityName)) {
      Error (UtilityName, 0, 0, "application error", "utility name length exceeds internal buffer size");
      strncpy (mUtilityName, UtilityName, sizeof (mUtilityName) - 1);
      mUtilityName[sizeof (mUtilityName) - 1] = 0;
      return ;
    } else {
      strcpy (mUtilityName, UtilityName);
    }
  } else {
    Error (NULL, 0, 0, "application error", "SetUtilityName() called with NULL utility name");
  }
}

STATUS
GetUtilityStatus (
  VOID
  )
/*++

Routine Description:
  When you call Error() or Warning(), this module keeps track of it and
  sets a local mStatus to STATUS_ERROR or STATUS_WARNING. When the utility
  exits, it can call this function to get the status and use it as a return
  value.
  
Arguments:
  None.

Returns:
  Worst-case status reported, as defined by which print function was called.
  
--*/
{
  return mStatus;
}

void
SetDebugMsgMask (
  UINT32  DebugMask
  )
/*++

Routine Description:
  Set the debug printing mask. This is used by the DebugMsg() function
  to determine when/if a debug message should be printed.

Arguments:
  DebugMask  - bitmask, specific to the calling application

Returns:
  NA

--*/
{
  mDebugMsgMask = DebugMask;
}

void
SetPrintLimits (
  UINT32  MaxErrors,
  UINT32  MaxWarnings,
  UINT32  MaxWarningsPlusErrors
  )
/*++

Routine Description:
  Set the limits of how many errors, warnings, and errors+warnings
  we will print.

Arguments:
  MaxErrors       - maximum number of error messages to print
  MaxWarnings     - maximum number of warning messages to print
  MaxWarningsPlusErrors 
                  - maximum number of errors+warnings to print

Returns:
  NA

--*/
{
  mMaxErrors              = MaxErrors;
  mMaxWarnings            = MaxWarnings;
  mMaxWarningsPlusErrors  = MaxWarningsPlusErrors;
  mPrintLimitsSet         = 1;
}

static
void
PrintLimitExceeded (
  VOID
  )
{
  static INT8 mPrintLimitExceeded = 0;
  //
  // If we've already printed the message, do nothing. Otherwise
  // temporarily increase our print limits so we can pass one
  // more message through.
  //
  if (mPrintLimitExceeded == 0) {
    mPrintLimitExceeded++;
    mMaxErrors++;
    mMaxWarnings++;
    mMaxWarningsPlusErrors++;
    Error (NULL, 0, 0, "error/warning print limit exceeded", NULL);
    mMaxErrors--;
    mMaxWarnings--;
    mMaxWarningsPlusErrors--;
  }
}

#if 0
void
TestUtilityMessages (
  VOID
  )
{
  char *ArgStr = "ArgString";
  int  ArgInt;

  ArgInt  = 0x12345678;
  //
  // Test without setting utility name
  //
  fprintf (stdout, "* Testing without setting utility name\n");
  fprintf (stdout, "** Test debug message not printed\n");
  DebugMsg (NULL, 0, 0x00000001, NULL, NULL);
  fprintf (stdout, "** Test warning with two strings and two args\n");
  Warning (NULL, 0, 1234, "Text1", "Text2 %s 0x%X", ArgStr, ArgInt);
  fprintf (stdout, "** Test error with two strings and two args\n");
  Warning (NULL, 0, 1234, "Text1", "Text2 %s 0x%X", ArgStr, ArgInt);
  fprintf (stdout, "** Test parser warning with nothing\n");
  ParserWarning (0, NULL, NULL);
  fprintf (stdout, "** Test parser error with nothing\n");
  ParserError (0, NULL, NULL);
  //
  // Test with utility name set now
  //
  fprintf (stdout, "** Testingin with utility name set\n");
  SetUtilityName ("MyUtilityName");
  //
  // Test debug prints
  //
  SetDebugMsgMask (2);
  fprintf (stdout, "** Test debug message with one string\n");
  DebugMsg (NULL, 0, 0x00000002, "Text1", NULL);
  fprintf (stdout, "** Test debug message with one string\n");
  DebugMsg (NULL, 0, 0x00000002, NULL, "Text2");
  fprintf (stdout, "** Test debug message with two strings\n");
  DebugMsg (NULL, 0, 0x00000002, "Text1", "Text2");
  fprintf (stdout, "** Test debug message with two strings and two args\n");
  DebugMsg (NULL, 0, 0x00000002, "Text1", "Text2 %s 0x%X", ArgStr, ArgInt);
  //
  // Test warning prints
  //
  fprintf (stdout, "** Test warning with no strings\n");
  Warning (NULL, 0, 1234, NULL, NULL);
  fprintf (stdout, "** Test warning with one string\n");
  Warning (NULL, 0, 1234, "Text1", NULL);
  fprintf (stdout, "** Test warning with one string\n");
  Warning (NULL, 0, 1234, NULL, "Text2");
  fprintf (stdout, "** Test warning with two strings and two args\n");
  Warning (NULL, 0, 1234, "Text1", "Text2 %s 0x%X", ArgStr, ArgInt);
  //
  // Test error prints
  //
  fprintf (stdout, "** Test error with no strings\n");
  Error (NULL, 0, 1234, NULL, NULL);
  fprintf (stdout, "** Test error with one string\n");
  Error (NULL, 0, 1234, "Text1", NULL);
  fprintf (stdout, "** Test error with one string\n");
  Error (NULL, 0, 1234, NULL, "Text2");
  fprintf (stdout, "** Test error with two strings and two args\n");
  Error (NULL, 0, 1234, "Text1", "Text2 %s 0x%X", ArgStr, ArgInt);
  //
  // Test parser prints
  //
  fprintf (stdout, "** Test parser errors\n");
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserError (1234, NULL, NULL);
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserError (1234, "Text1", NULL);
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserError (1234, NULL, "Text2");
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserError (1234, "Text1", "Text2");
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserError (1234, "Text1", "Text2 %s 0x%X", ArgStr, ArgInt);

  fprintf (stdout, "** Test parser warnings\n");
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserWarning (4321, NULL, NULL);
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserWarning (4321, "Text1", NULL);
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserWarning (4321, NULL, "Text2");
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserWarning (4321, "Text1", "Text2");
  ParserSetPosition (__FILE__, __LINE__ + 1);
  ParserWarning (4321, "Text1", "Text2 %s 0x%X", ArgStr, ArgInt);
}
#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?