3 Copyright (c) 2004, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 EFI tools utility functions to display warning, error, and informational
31 #include "EfiUtilityMsgs.h"
33 #define MAX_LINE_LEN 200
36 // Declare module globals for keeping track of the the utility's
37 // name and other settings.
39 static STATUS mStatus
= STATUS_SUCCESS
;
40 static CHAR8 mUtilityName
[50] = { 0 };
41 static UINT32 mDebugMsgMask
= 0;
42 static CHAR8
*mSourceFileName
= NULL
;
43 static UINT32 mSourceFileLineNum
= 0;
44 static UINT32 mErrorCount
= 0;
45 static UINT32 mWarningCount
= 0;
46 static UINT32 mMaxErrors
= 0;
47 static UINT32 mMaxWarnings
= 0;
48 static UINT32 mMaxWarningsPlusErrors
= 0;
49 static INT8 mPrintLimitsSet
= 0;
81 Prints an error message.
84 All arguments are optional, though the printed message may be useless if
85 at least something valid is not specified.
87 FileName - name of the file or application. If not specified, then the
88 utilty name (as set by the utility calling SetUtilityName()
89 earlier) is used. Otherwise "Unknown utility" is used.
91 LineNumber - the line number of error, typically used by parsers. If the
92 utility is not a parser, then 0 should be specified. Otherwise
93 the FileName and LineNumber info can be used to cause
94 MS Visual Studio to jump to the error.
96 MessageCode - an application-specific error code that can be referenced in
99 Text - the text in question, typically used by parsers.
101 MsgFmt - the format string for the error message. Can contain formatting
102 controls for use with the varargs.
108 We print the following (similar to the Warn() and Debug()
110 Typical error/warning message format:
112 bin\VfrCompile.cpp(330) : error C2660: 'AddVfrDataStructField' : function does not take 2 parameters
114 BUGBUG -- these three utility functions are almost identical, and
115 should be modified to share code.
117 Visual Studio does not find error messages with:
131 // If limits have been set, then check that we have not exceeded them
133 if (mPrintLimitsSet
) {
135 // See if we've exceeded our total count
137 if (mMaxWarningsPlusErrors
!= 0) {
138 if (mErrorCount
+ mWarningCount
> mMaxWarningsPlusErrors
) {
139 PrintLimitExceeded ();
144 // See if we've exceeded our error count
146 if (mMaxErrors
!= 0) {
147 if (mErrorCount
> mMaxErrors
) {
148 PrintLimitExceeded ();
155 va_start (List
, MsgFmt
);
156 PrintMessage ("error", FileName
, LineNumber
, MessageCode
, Text
, MsgFmt
, List
);
159 // Set status accordingly
161 if (mStatus
< STATUS_ERROR
) {
162 mStatus
= STATUS_ERROR
;
176 Print a parser error, using the source file name and line number
177 set by a previous call to SetParserPosition().
180 MessageCode - application-specific error code
181 Text - text to print in the error message
182 MsgFmt - format string to print at the end of the error message
191 // If limits have been set, then check them
193 if (mPrintLimitsSet
) {
195 // See if we've exceeded our total count
197 if (mMaxWarningsPlusErrors
!= 0) {
198 if (mErrorCount
+ mWarningCount
> mMaxWarningsPlusErrors
) {
199 PrintLimitExceeded ();
204 // See if we've exceeded our error count
206 if (mMaxErrors
!= 0) {
207 if (mErrorCount
> mMaxErrors
) {
208 PrintLimitExceeded ();
215 va_start (List
, MsgFmt
);
216 PrintMessage ("error", mSourceFileName
, mSourceFileLineNum
, MessageCode
, Text
, MsgFmt
, List
);
219 // Set status accordingly
221 if (mStatus
< STATUS_ERROR
) {
222 mStatus
= STATUS_ERROR
;
229 CHAR8
*OffendingText
,
236 Print a parser warning, using the source file name and line number
237 set by a previous call to SetParserPosition().
240 ErrorCode - application-specific error code
241 OffendingText - text to print in the warning message
242 MsgFmt - format string to print at the end of the warning message
251 // If limits have been set, then check them
253 if (mPrintLimitsSet
) {
255 // See if we've exceeded our total count
257 if (mMaxWarningsPlusErrors
!= 0) {
258 if (mErrorCount
+ mWarningCount
> mMaxWarningsPlusErrors
) {
259 PrintLimitExceeded ();
264 // See if we've exceeded our warning count
266 if (mMaxWarnings
!= 0) {
267 if (mWarningCount
> mMaxWarnings
) {
268 PrintLimitExceeded ();
275 va_start (List
, MsgFmt
);
276 PrintMessage ("warning", mSourceFileName
, mSourceFileLineNum
, ErrorCode
, OffendingText
, MsgFmt
, List
);
279 // Set status accordingly
281 if (mStatus
< STATUS_WARNING
) {
282 mStatus
= STATUS_WARNING
;
298 Print a warning message.
301 FileName - name of the file where the warning was detected, or the name
302 of the application that detected the warning
304 LineNumber - the line number where the warning was detected (parsers).
305 0 should be specified if the utility is not a parser.
307 MessageCode - an application-specific warning code that can be referenced in
310 Text - the text in question (parsers)
312 MsgFmt - the format string for the warning message. Can contain formatting
313 controls for use with varargs.
322 // If limits have been set, then check them
324 if (mPrintLimitsSet
) {
326 // See if we've exceeded our total count
328 if (mMaxWarningsPlusErrors
!= 0) {
329 if (mErrorCount
+ mWarningCount
> mMaxWarningsPlusErrors
) {
330 PrintLimitExceeded ();
335 // See if we've exceeded our warning count
337 if (mMaxWarnings
!= 0) {
338 if (mWarningCount
> mMaxWarnings
) {
339 PrintLimitExceeded ();
346 va_start (List
, MsgFmt
);
347 PrintMessage ("warning", FileName
, LineNumber
, MessageCode
, Text
, MsgFmt
, List
);
350 // Set status accordingly
352 if (mStatus
< STATUS_WARNING
) {
353 mStatus
= STATUS_WARNING
;
369 Print a warning message.
372 FileName - typically the name of the utility printing the debug message, but
373 can be the name of a file being parsed.
375 LineNumber - the line number in FileName (parsers)
377 MsgMask - an application-specific bitmask that, in combination with mDebugMsgMask,
378 determines if the debug message gets printed.
380 Text - the text in question (parsers)
382 MsgFmt - the format string for the debug message. Can contain formatting
383 controls for use with varargs.
392 // If the debug mask is not applicable, then do nothing.
394 if ((MsgMask
!= 0) && ((mDebugMsgMask
& MsgMask
) == 0)) {
398 va_start (List
, MsgFmt
);
399 PrintMessage ("debug", FileName
, LineNumber
, 0, Text
, MsgFmt
, List
);
417 Worker routine for all the utility printing services. Prints the message in
418 a format that Visual Studio will find when scanning build outputs for
422 Type - "warning" or "error" string to insert into the message to be
423 printed. The first character of this string (converted to uppercase)
424 is used to preceed the MessageCode value in the output string.
426 FileName - name of the file where the warning was detected, or the name
427 of the application that detected the warning
429 LineNumber - the line number where the warning was detected (parsers).
430 0 should be specified if the utility is not a parser.
432 MessageCode - an application-specific warning code that can be referenced in
435 Text - part of the message to print
437 MsgFmt - the format string for the message. Can contain formatting
438 controls for use with varargs.
439 List - the variable list.
445 If FileName == NULL then this utility will use the string passed into SetUtilityName().
447 LineNumber is only used if the caller is a parser, in which case FileName refers to the
450 Text and MsgFmt are both optional, though it would be of little use calling this function with
453 Output will typically be of the form:
454 <FileName>(<LineNumber>) : <Type> <Type[0]><MessageCode>: <Text> : <MsgFmt>
456 Parser (LineNumber != 0)
457 VfrCompile.cpp(330) : error E2660: AddVfrDataStructField : function does not take 2 parameters
458 Generic utility (LineNumber == 0)
459 UtilityName : error E1234 : Text string : MsgFmt string and args
463 CHAR8 Line
[MAX_LINE_LEN
];
464 CHAR8 Line2
[MAX_LINE_LEN
];
467 // If given a filename, then add it (and the line number) to the string.
468 // If there's no filename, then use the program name if provided.
470 if (FileName
!= NULL
) {
472 } else if (mUtilityName
[0] != 0) {
475 Cptr
= "Unknown utility";
479 if (LineNumber
!= 0) {
480 sprintf (Line2
, "(%d)", LineNumber
);
481 strcat (Line
, Line2
);
484 // Have to print an error code or Visual Studio won't find the
485 // message for you. It has to be decimal digits too.
487 sprintf (Line2
, " : %s %c%04d", Type
, toupper (Type
[0]), MessageCode
);
488 strcat (Line
, Line2
);
489 fprintf (stdout
, "%s", Line
);
491 // If offending text was provided, then print it
494 fprintf (stdout
, ": %s ", Text
);
497 // Print formatted message if provided
499 if (MsgFmt
!= NULL
) {
500 vsprintf (Line2
, MsgFmt
, List
);
501 fprintf (stdout
, ": %s", Line2
);
504 fprintf (stdout
, "\n");
509 CHAR8
*SourceFileName
,
515 Set the position in a file being parsed. This can be used to
516 print error messages deeper down in a parser.
519 SourceFileName - name of the source file being parsed
520 LineNum - line number of the source file being parsed
527 mSourceFileName
= SourceFileName
;
528 mSourceFileLineNum
= LineNum
;
538 All printed error/warning/debug messages follow the same format, and
539 typically will print a filename or utility name followed by the error
540 text. However if a filename is not passed to the print routines, then
541 they'll print the utility name if you call this function early in your
542 app to set the utility name.
545 UtilityName - name of the utility, which will be printed with all
546 error/warning/debug messags.
554 // Save the name of the utility in our local variable. Make sure its
555 // length does not exceed our buffer.
557 if (UtilityName
!= NULL
) {
558 if (strlen (UtilityName
) >= sizeof (mUtilityName
)) {
559 Error (UtilityName
, 0, 0, "application error", "utility name length exceeds internal buffer size");
560 strncpy (mUtilityName
, UtilityName
, sizeof (mUtilityName
) - 1);
561 mUtilityName
[sizeof (mUtilityName
) - 1] = 0;
564 strcpy (mUtilityName
, UtilityName
);
567 Error (NULL
, 0, 0, "application error", "SetUtilityName() called with NULL utility name");
578 When you call Error() or Warning(), this module keeps track of it and
579 sets a local mStatus to STATUS_ERROR or STATUS_WARNING. When the utility
580 exits, it can call this function to get the status and use it as a return
587 Worst-case status reported, as defined by which print function was called.
601 Set the debug printing mask. This is used by the DebugMsg() function
602 to determine when/if a debug message should be printed.
605 DebugMask - bitmask, specific to the calling application
612 mDebugMsgMask
= DebugMask
;
619 UINT32 MaxWarningsPlusErrors
624 Set the limits of how many errors, warnings, and errors+warnings
628 MaxErrors - maximum number of error messages to print
629 MaxWarnings - maximum number of warning messages to print
630 MaxWarningsPlusErrors
631 - maximum number of errors+warnings to print
638 mMaxErrors
= MaxErrors
;
639 mMaxWarnings
= MaxWarnings
;
640 mMaxWarningsPlusErrors
= MaxWarningsPlusErrors
;
650 static INT8 mPrintLimitExceeded
= 0;
652 // If we've already printed the message, do nothing. Otherwise
653 // temporarily increase our print limits so we can pass one
654 // more message through.
656 if (mPrintLimitExceeded
== 0) {
657 mPrintLimitExceeded
++;
660 mMaxWarningsPlusErrors
++;
661 Error (NULL
, 0, 0, "error/warning print limit exceeded", NULL
);
664 mMaxWarningsPlusErrors
--;
670 TestUtilityMessages (
674 char *ArgStr
= "ArgString";
679 // Test without setting utility name
681 fprintf (stdout
, "* Testing without setting utility name\n");
682 fprintf (stdout
, "** Test debug message not printed\n");
683 DebugMsg (NULL
, 0, 0x00000001, NULL
, NULL
);
684 fprintf (stdout
, "** Test warning with two strings and two args\n");
685 Warning (NULL
, 0, 1234, "Text1", "Text2 %s 0x%X", ArgStr
, ArgInt
);
686 fprintf (stdout
, "** Test error with two strings and two args\n");
687 Warning (NULL
, 0, 1234, "Text1", "Text2 %s 0x%X", ArgStr
, ArgInt
);
688 fprintf (stdout
, "** Test parser warning with nothing\n");
689 ParserWarning (0, NULL
, NULL
);
690 fprintf (stdout
, "** Test parser error with nothing\n");
691 ParserError (0, NULL
, NULL
);
693 // Test with utility name set now
695 fprintf (stdout
, "** Testingin with utility name set\n");
696 SetUtilityName ("MyUtilityName");
701 fprintf (stdout
, "** Test debug message with one string\n");
702 DebugMsg (NULL
, 0, 0x00000002, "Text1", NULL
);
703 fprintf (stdout
, "** Test debug message with one string\n");
704 DebugMsg (NULL
, 0, 0x00000002, NULL
, "Text2");
705 fprintf (stdout
, "** Test debug message with two strings\n");
706 DebugMsg (NULL
, 0, 0x00000002, "Text1", "Text2");
707 fprintf (stdout
, "** Test debug message with two strings and two args\n");
708 DebugMsg (NULL
, 0, 0x00000002, "Text1", "Text2 %s 0x%X", ArgStr
, ArgInt
);
710 // Test warning prints
712 fprintf (stdout
, "** Test warning with no strings\n");
713 Warning (NULL
, 0, 1234, NULL
, NULL
);
714 fprintf (stdout
, "** Test warning with one string\n");
715 Warning (NULL
, 0, 1234, "Text1", NULL
);
716 fprintf (stdout
, "** Test warning with one string\n");
717 Warning (NULL
, 0, 1234, NULL
, "Text2");
718 fprintf (stdout
, "** Test warning with two strings and two args\n");
719 Warning (NULL
, 0, 1234, "Text1", "Text2 %s 0x%X", ArgStr
, ArgInt
);
723 fprintf (stdout
, "** Test error with no strings\n");
724 Error (NULL
, 0, 1234, NULL
, NULL
);
725 fprintf (stdout
, "** Test error with one string\n");
726 Error (NULL
, 0, 1234, "Text1", NULL
);
727 fprintf (stdout
, "** Test error with one string\n");
728 Error (NULL
, 0, 1234, NULL
, "Text2");
729 fprintf (stdout
, "** Test error with two strings and two args\n");
730 Error (NULL
, 0, 1234, "Text1", "Text2 %s 0x%X", ArgStr
, ArgInt
);
732 // Test parser prints
734 fprintf (stdout
, "** Test parser errors\n");
735 ParserSetPosition (__FILE__
, __LINE__
+ 1);
736 ParserError (1234, NULL
, NULL
);
737 ParserSetPosition (__FILE__
, __LINE__
+ 1);
738 ParserError (1234, "Text1", NULL
);
739 ParserSetPosition (__FILE__
, __LINE__
+ 1);
740 ParserError (1234, NULL
, "Text2");
741 ParserSetPosition (__FILE__
, __LINE__
+ 1);
742 ParserError (1234, "Text1", "Text2");
743 ParserSetPosition (__FILE__
, __LINE__
+ 1);
744 ParserError (1234, "Text1", "Text2 %s 0x%X", ArgStr
, ArgInt
);
746 fprintf (stdout
, "** Test parser warnings\n");
747 ParserSetPosition (__FILE__
, __LINE__
+ 1);
748 ParserWarning (4321, NULL
, NULL
);
749 ParserSetPosition (__FILE__
, __LINE__
+ 1);
750 ParserWarning (4321, "Text1", NULL
);
751 ParserSetPosition (__FILE__
, __LINE__
+ 1);
752 ParserWarning (4321, NULL
, "Text2");
753 ParserSetPosition (__FILE__
, __LINE__
+ 1);
754 ParserWarning (4321, "Text1", "Text2");
755 ParserSetPosition (__FILE__
, __LINE__
+ 1);
756 ParserWarning (4321, "Text1", "Text2 %s 0x%X", ArgStr
, ArgInt
);