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 "EfiUtilityMsgs.h"
31 #define MAX_LINE_LEN 200
34 // Declare module globals for keeping track of the the utility's
35 // name and other settings.
37 static STATUS mStatus
= STATUS_SUCCESS
;
38 static INT8 mUtilityName
[50] = { 0 };
39 static INT8
*mSourceFileName
= NULL
;
40 static UINT32 mSourceFileLineNum
= 0;
41 static UINT32 mErrorCount
= 0;
42 static UINT32 mWarningCount
= 0;
43 static UINT32 mDebugMsgMask
= 0;
69 Prints an error message.
72 All arguments are optional, though the printed message may be useless if
73 at least something valid is not specified.
75 FileName - name of the file or application. If not specified, then the
76 utilty name (as set by the utility calling SetUtilityName()
77 earlier) is used. Otherwise "Unknown utility" is used.
79 LineNumber - the line number of error, typically used by parsers. If the
80 utility is not a parser, then 0 should be specified. Otherwise
81 the FileName and LineNumber info can be used to cause
82 MS Visual Studio to jump to the error.
84 MessageCode - an application-specific error code that can be referenced in
87 Text - the text in question, typically used by parsers.
89 MsgFmt - the format string for the error message. Can contain formatting
90 controls for use with the varargs.
96 We print the following (similar to the Warn() and Debug()
98 Typical error/warning message format:
100 bin\VfrCompile.cpp(330) : error C2660: 'AddVfrDataStructField' : function does not take 2 parameters
102 BUGBUG -- these three utility functions are almost identical, and
103 should be modified to share code.
105 Visual Studio does not find error messages with:
119 va_start (List
, MsgFmt
);
120 PrintMessage ("error", FileName
, LineNumber
, MessageCode
, Text
, MsgFmt
, List
);
123 // Set status accordingly
125 if (mStatus
< STATUS_ERROR
) {
126 mStatus
= STATUS_ERROR
;
140 Print a parser error, using the source file name and line number
141 set by a previous call to SetParserPosition().
144 MessageCode - application-specific error code
145 Text - text to print in the error message
146 MsgFmt - format string to print at the end of the error message
156 va_start (List
, MsgFmt
);
157 PrintMessage ("error", mSourceFileName
, mSourceFileLineNum
, MessageCode
, Text
, MsgFmt
, List
);
160 // Set status accordingly
162 if (mStatus
< STATUS_ERROR
) {
163 mStatus
= STATUS_ERROR
;
177 Print a parser warning, using the source file name and line number
178 set by a previous call to SetParserPosition().
181 ErrorCode - application-specific error code
182 OffendingText - text to print in the warning message
183 MsgFmt - format string to print at the end of the warning message
193 va_start (List
, MsgFmt
);
194 PrintMessage ("warning", mSourceFileName
, mSourceFileLineNum
, ErrorCode
, OffendingText
, MsgFmt
, List
);
197 // Set status accordingly
199 if (mStatus
< STATUS_WARNING
) {
200 mStatus
= STATUS_WARNING
;
216 Print a warning message.
219 FileName - name of the file where the warning was detected, or the name
220 of the application that detected the warning
222 LineNumber - the line number where the warning was detected (parsers).
223 0 should be specified if the utility is not a parser.
225 MessageCode - an application-specific warning code that can be referenced in
228 Text - the text in question (parsers)
230 MsgFmt - the format string for the warning message. Can contain formatting
231 controls for use with varargs.
242 va_start (List
, MsgFmt
);
243 PrintMessage ("warning", FileName
, LineNumber
, MessageCode
, Text
, MsgFmt
, List
);
246 // Set status accordingly
248 if (mStatus
< STATUS_WARNING
) {
249 mStatus
= STATUS_WARNING
;
265 Print a warning message.
268 FileName - typically the name of the utility printing the debug message, but
269 can be the name of a file being parsed.
271 LineNumber - the line number in FileName (parsers)
273 MsgMask - an application-specific bitmask that, in combination with mDebugMsgMask,
274 determines if the debug message gets printed.
276 Text - the text in question (parsers)
278 MsgFmt - the format string for the debug message. Can contain formatting
279 controls for use with varargs.
289 // If the debug mask is not applicable, then do nothing.
291 if ((MsgMask
!= 0) && ((mDebugMsgMask
& MsgMask
) == 0)) {
295 va_start (List
, MsgFmt
);
296 PrintMessage ("debug", FileName
, LineNumber
, 0, Text
, MsgFmt
, List
);
314 Worker routine for all the utility printing services. Prints the message in
315 a format that Visual Studio will find when scanning build outputs for
319 Type - "warning" or "error" string to insert into the message to be
320 printed. The first character of this string (converted to uppercase)
321 is used to preceed the MessageCode value in the output string.
323 FileName - name of the file where the warning was detected, or the name
324 of the application that detected the warning
326 LineNumber - the line number where the warning was detected (parsers).
327 0 should be specified if the utility is not a parser.
329 MessageCode - an application-specific warning code that can be referenced in
332 Text - part of the message to print
334 MsgFmt - the format string for the message. Can contain formatting
335 controls for use with varargs.
337 List - Variable function parameter list.
342 If FileName == NULL then this utility will use the string passed into SetUtilityName().
344 LineNumber is only used if the caller is a parser, in which case FileName refers to the
347 Text and MsgFmt are both optional, though it would be of little use calling this function with
350 Output will typically be of the form:
351 <FileName>(<LineNumber>) : <Type> <Type[0]><MessageCode>: <Text> : <MsgFmt>
353 Parser (LineNumber != 0)
354 VfrCompile.cpp(330) : error E2660: AddVfrDataStructField : function does not take 2 parameters
355 Generic utility (LineNumber == 0)
356 UtilityName : error E1234 : Text string : MsgFmt string and args
360 INT8 Line
[MAX_LINE_LEN
];
361 INT8 Line2
[MAX_LINE_LEN
];
364 // If given a filename, then add it (and the line number) to the string.
365 // If there's no filename, then use the program name if provided.
367 if (FileName
!= NULL
) {
369 } else if (mUtilityName
[0] != 0) {
372 Cptr
= "Unknown utility";
376 if (LineNumber
!= 0) {
377 sprintf (Line2
, "(%d)", LineNumber
);
378 strcat (Line
, Line2
);
381 // Have to print an error code or Visual Studio won't find the
382 // message for you. It has to be decimal digits too.
384 sprintf (Line2
, " : %s %c%04d", Type
, toupper (Type
[0]), MessageCode
);
385 strcat (Line
, Line2
);
386 fprintf (stdout
, "%s", Line
);
388 // If offending text was provided, then print it
391 fprintf (stdout
, ": %s ", Text
);
394 // Print formatted message if provided
396 if (MsgFmt
!= NULL
) {
397 vsprintf (Line2
, MsgFmt
, List
);
398 fprintf (stdout
, ": %s", Line2
);
401 fprintf (stdout
, "\n");
406 INT8
*SourceFileName
,
412 Set the position in a file being parsed. This can be used to
413 print error messages deeper down in a parser.
416 SourceFileName - name of the source file being parsed
417 LineNum - line number of the source file being parsed
424 mSourceFileName
= SourceFileName
;
425 mSourceFileLineNum
= LineNum
;
435 All printed error/warning/debug messages follow the same format, and
436 typically will print a filename or utility name followed by the error
437 text. However if a filename is not passed to the print routines, then
438 they'll print the utility name if you call this function early in your
439 app to set the utility name.
442 UtilityName - name of the utility, which will be printed with all
443 error/warning/debug messags.
451 // Save the name of the utility in our local variable. Make sure its
452 // length does not exceed our buffer.
454 if (UtilityName
!= NULL
) {
455 if (strlen (UtilityName
) >= sizeof (mUtilityName
)) {
456 Error (UtilityName
, 0, 0, "application error", "utility name length exceeds internal buffer size");
457 strncpy (mUtilityName
, UtilityName
, sizeof (mUtilityName
) - 1);
458 mUtilityName
[sizeof (mUtilityName
) - 1] = 0;
461 strcpy (mUtilityName
, UtilityName
);
464 Error (NULL
, 0, 0, "application error", "SetUtilityName() called with NULL utility name");
475 When you call Error() or Warning(), this module keeps track of it and
476 sets a local mStatus to STATUS_ERROR or STATUS_WARNING. When the utility
477 exits, it can call this function to get the status and use it as a return
484 Worst-case status reported, as defined by which print function was called.