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
29 #include <UefiBaseTypes.h>
30 #include "EfiUtilityMsgs.h"
32 #define MAX_LINE_LEN 200
35 // Declare module globals for keeping track of the the utility's
36 // name and other settings.
38 static STATUS mStatus
= STATUS_SUCCESS
;
39 static INT8 mUtilityName
[50] = { 0 };
40 static INT8
*mSourceFileName
= NULL
;
41 static UINT32 mSourceFileLineNum
= 0;
42 static UINT32 mErrorCount
= 0;
43 static UINT32 mWarningCount
= 0;
44 static UINT32 mDebugMsgMask
= 0;
70 Prints an error message.
73 All arguments are optional, though the printed message may be useless if
74 at least something valid is not specified.
76 FileName - name of the file or application. If not specified, then the
77 utilty name (as set by the utility calling SetUtilityName()
78 earlier) is used. Otherwise "Unknown utility" is used.
80 LineNumber - the line number of error, typically used by parsers. If the
81 utility is not a parser, then 0 should be specified. Otherwise
82 the FileName and LineNumber info can be used to cause
83 MS Visual Studio to jump to the error.
85 MessageCode - an application-specific error code that can be referenced in
88 Text - the text in question, typically used by parsers.
90 MsgFmt - the format string for the error message. Can contain formatting
91 controls for use with the varargs.
97 We print the following (similar to the Warn() and Debug()
99 Typical error/warning message format:
101 bin\VfrCompile.cpp(330) : error C2660: 'AddVfrDataStructField' : function does not take 2 parameters
103 BUGBUG -- these three utility functions are almost identical, and
104 should be modified to share code.
106 Visual Studio does not find error messages with:
120 va_start (List
, MsgFmt
);
121 PrintMessage ("error", FileName
, LineNumber
, MessageCode
, Text
, MsgFmt
, List
);
124 // Set status accordingly
126 if (mStatus
< STATUS_ERROR
) {
127 mStatus
= STATUS_ERROR
;
141 Print a parser error, using the source file name and line number
142 set by a previous call to SetParserPosition().
145 MessageCode - application-specific error code
146 Text - text to print in the error message
147 MsgFmt - format string to print at the end of the error message
157 va_start (List
, MsgFmt
);
158 PrintMessage ("error", mSourceFileName
, mSourceFileLineNum
, MessageCode
, Text
, MsgFmt
, List
);
161 // Set status accordingly
163 if (mStatus
< STATUS_ERROR
) {
164 mStatus
= STATUS_ERROR
;
178 Print a parser warning, using the source file name and line number
179 set by a previous call to SetParserPosition().
182 ErrorCode - application-specific error code
183 OffendingText - text to print in the warning message
184 MsgFmt - format string to print at the end of the warning message
194 va_start (List
, MsgFmt
);
195 PrintMessage ("warning", mSourceFileName
, mSourceFileLineNum
, ErrorCode
, OffendingText
, MsgFmt
, List
);
198 // Set status accordingly
200 if (mStatus
< STATUS_WARNING
) {
201 mStatus
= STATUS_WARNING
;
217 Print a warning message.
220 FileName - name of the file where the warning was detected, or the name
221 of the application that detected the warning
223 LineNumber - the line number where the warning was detected (parsers).
224 0 should be specified if the utility is not a parser.
226 MessageCode - an application-specific warning code that can be referenced in
229 Text - the text in question (parsers)
231 MsgFmt - the format string for the warning message. Can contain formatting
232 controls for use with varargs.
243 va_start (List
, MsgFmt
);
244 PrintMessage ("warning", FileName
, LineNumber
, MessageCode
, Text
, MsgFmt
, List
);
247 // Set status accordingly
249 if (mStatus
< STATUS_WARNING
) {
250 mStatus
= STATUS_WARNING
;
266 Print a warning message.
269 FileName - typically the name of the utility printing the debug message, but
270 can be the name of a file being parsed.
272 LineNumber - the line number in FileName (parsers)
274 MsgMask - an application-specific bitmask that, in combination with mDebugMsgMask,
275 determines if the debug message gets printed.
277 Text - the text in question (parsers)
279 MsgFmt - the format string for the debug message. Can contain formatting
280 controls for use with varargs.
290 // If the debug mask is not applicable, then do nothing.
292 if ((MsgMask
!= 0) && ((mDebugMsgMask
& MsgMask
) == 0)) {
296 va_start (List
, MsgFmt
);
297 PrintMessage ("debug", FileName
, LineNumber
, 0, Text
, MsgFmt
, List
);
315 Worker routine for all the utility printing services. Prints the message in
316 a format that Visual Studio will find when scanning build outputs for
320 Type - "warning" or "error" string to insert into the message to be
321 printed. The first character of this string (converted to uppercase)
322 is used to preceed the MessageCode value in the output string.
324 FileName - name of the file where the warning was detected, or the name
325 of the application that detected the warning
327 LineNumber - the line number where the warning was detected (parsers).
328 0 should be specified if the utility is not a parser.
330 MessageCode - an application-specific warning code that can be referenced in
333 Text - part of the message to print
335 MsgFmt - the format string for the message. Can contain formatting
336 controls for use with varargs.
338 List - Variable function parameter list.
343 If FileName == NULL then this utility will use the string passed into SetUtilityName().
345 LineNumber is only used if the caller is a parser, in which case FileName refers to the
348 Text and MsgFmt are both optional, though it would be of little use calling this function with
351 Output will typically be of the form:
352 <FileName>(<LineNumber>) : <Type> <Type[0]><MessageCode>: <Text> : <MsgFmt>
354 Parser (LineNumber != 0)
355 VfrCompile.cpp(330) : error E2660: AddVfrDataStructField : function does not take 2 parameters
356 Generic utility (LineNumber == 0)
357 UtilityName : error E1234 : Text string : MsgFmt string and args
361 INT8 Line
[MAX_LINE_LEN
];
362 INT8 Line2
[MAX_LINE_LEN
];
365 // If given a filename, then add it (and the line number) to the string.
366 // If there's no filename, then use the program name if provided.
368 if (FileName
!= NULL
) {
370 } else if (mUtilityName
[0] != 0) {
373 Cptr
= "Unknown utility";
377 if (LineNumber
!= 0) {
378 sprintf (Line2
, "(%d)", LineNumber
);
379 strcat (Line
, Line2
);
382 // Have to print an error code or Visual Studio won't find the
383 // message for you. It has to be decimal digits too.
385 sprintf (Line2
, " : %s %c%04d", Type
, toupper (Type
[0]), MessageCode
);
386 strcat (Line
, Line2
);
387 fprintf (stdout
, "%s", Line
);
389 // If offending text was provided, then print it
392 fprintf (stdout
, ": %s ", Text
);
395 // Print formatted message if provided
397 if (MsgFmt
!= NULL
) {
398 vsprintf (Line2
, MsgFmt
, List
);
399 fprintf (stdout
, ": %s", Line2
);
402 fprintf (stdout
, "\n");
407 INT8
*SourceFileName
,
413 Set the position in a file being parsed. This can be used to
414 print error messages deeper down in a parser.
417 SourceFileName - name of the source file being parsed
418 LineNum - line number of the source file being parsed
425 mSourceFileName
= SourceFileName
;
426 mSourceFileLineNum
= LineNum
;
436 All printed error/warning/debug messages follow the same format, and
437 typically will print a filename or utility name followed by the error
438 text. However if a filename is not passed to the print routines, then
439 they'll print the utility name if you call this function early in your
440 app to set the utility name.
443 UtilityName - name of the utility, which will be printed with all
444 error/warning/debug messags.
452 // Save the name of the utility in our local variable. Make sure its
453 // length does not exceed our buffer.
455 if (UtilityName
!= NULL
) {
456 if (strlen (UtilityName
) >= sizeof (mUtilityName
)) {
457 Error (UtilityName
, 0, 0, "application error", "utility name length exceeds internal buffer size");
458 strncpy (mUtilityName
, UtilityName
, sizeof (mUtilityName
) - 1);
459 mUtilityName
[sizeof (mUtilityName
) - 1] = 0;
462 strcpy (mUtilityName
, UtilityName
);
465 Error (NULL
, 0, 0, "application error", "SetUtilityName() called with NULL utility name");
476 When you call Error() or Warning(), this module keeps track of it and
477 sets a local mStatus to STATUS_ERROR or STATUS_WARNING. When the utility
478 exits, it can call this function to get the status and use it as a return
485 Worst-case status reported, as defined by which print function was called.