]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/GuidChk/UtilsMsgs.c
1. Removed the unnecessary #include statements and include files
[mirror_edk2.git] / Tools / Source / TianoTools / GuidChk / UtilsMsgs.c
1 /*++
2
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
8
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.
11
12 Module Name:
13
14 UtilsMsgs.c
15
16 Abstract:
17
18 EFI tools utility functions to display warning, error, and informational
19 messages.
20
21 --*/
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27
28 #include <Common/UefiBaseTypes.h>
29
30 #include "EfiUtilityMsgs.h"
31
32 #define MAX_LINE_LEN 200
33
34 //
35 // Declare module globals for keeping track of the the utility's
36 // name and other settings.
37 //
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;
45
46 static
47 void
48 PrintMessage (
49 INT8 *Type,
50 INT8 *FileName,
51 UINT32 LineNumber,
52 UINT32 MessageCode,
53 INT8 *Text,
54 INT8 *MsgFmt,
55 va_list List
56 );
57
58 void
59 Error (
60 INT8 *FileName,
61 UINT32 LineNumber,
62 UINT32 MessageCode,
63 INT8 *Text,
64 INT8 *MsgFmt,
65 ...
66 )
67 /*++
68
69 Routine Description:
70 Prints an error message.
71
72 Arguments:
73 All arguments are optional, though the printed message may be useless if
74 at least something valid is not specified.
75
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.
79
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.
84
85 MessageCode - an application-specific error code that can be referenced in
86 other documentation.
87
88 Text - the text in question, typically used by parsers.
89
90 MsgFmt - the format string for the error message. Can contain formatting
91 controls for use with the varargs.
92
93 Returns:
94 None.
95
96 Notes:
97 We print the following (similar to the Warn() and Debug()
98 W
99 Typical error/warning message format:
100
101 bin\VfrCompile.cpp(330) : error C2660: 'AddVfrDataStructField' : function does not take 2 parameters
102
103 BUGBUG -- these three utility functions are almost identical, and
104 should be modified to share code.
105
106 Visual Studio does not find error messages with:
107
108 " error :"
109 " error 1:"
110 " error c1:"
111 " error 1000:"
112 " error c100:"
113
114 It does find:
115 " error c1000:"
116 --*/
117 {
118 va_list List;
119 mErrorCount++;
120 va_start (List, MsgFmt);
121 PrintMessage ("error", FileName, LineNumber, MessageCode, Text, MsgFmt, List);
122 va_end (List);
123 //
124 // Set status accordingly
125 //
126 if (mStatus < STATUS_ERROR) {
127 mStatus = STATUS_ERROR;
128 }
129 }
130
131 void
132 ParserError (
133 UINT32 MessageCode,
134 INT8 *Text,
135 INT8 *MsgFmt,
136 ...
137 )
138 /*++
139
140 Routine Description:
141 Print a parser error, using the source file name and line number
142 set by a previous call to SetParserPosition().
143
144 Arguments:
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
148 ...
149
150 Returns:
151 NA
152
153 --*/
154 {
155 va_list List;
156 mErrorCount++;
157 va_start (List, MsgFmt);
158 PrintMessage ("error", mSourceFileName, mSourceFileLineNum, MessageCode, Text, MsgFmt, List);
159 va_end (List);
160 //
161 // Set status accordingly
162 //
163 if (mStatus < STATUS_ERROR) {
164 mStatus = STATUS_ERROR;
165 }
166 }
167
168 void
169 ParserWarning (
170 UINT32 ErrorCode,
171 INT8 *OffendingText,
172 INT8 *MsgFmt,
173 ...
174 )
175 /*++
176
177 Routine Description:
178 Print a parser warning, using the source file name and line number
179 set by a previous call to SetParserPosition().
180
181 Arguments:
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
185 ...
186
187 Returns:
188 NA
189
190 --*/
191 {
192 va_list List;
193 mWarningCount++;
194 va_start (List, MsgFmt);
195 PrintMessage ("warning", mSourceFileName, mSourceFileLineNum, ErrorCode, OffendingText, MsgFmt, List);
196 va_end (List);
197 //
198 // Set status accordingly
199 //
200 if (mStatus < STATUS_WARNING) {
201 mStatus = STATUS_WARNING;
202 }
203 }
204
205 void
206 Warning (
207 INT8 *FileName,
208 UINT32 LineNumber,
209 UINT32 MessageCode,
210 INT8 *Text,
211 INT8 *MsgFmt,
212 ...
213 )
214 /*++
215
216 Routine Description:
217 Print a warning message.
218
219 Arguments:
220 FileName - name of the file where the warning was detected, or the name
221 of the application that detected the warning
222
223 LineNumber - the line number where the warning was detected (parsers).
224 0 should be specified if the utility is not a parser.
225
226 MessageCode - an application-specific warning code that can be referenced in
227 other documentation.
228
229 Text - the text in question (parsers)
230
231 MsgFmt - the format string for the warning message. Can contain formatting
232 controls for use with varargs.
233
234 ...
235
236 Returns:
237 None.
238
239 --*/
240 {
241 va_list List;
242 mWarningCount++;
243 va_start (List, MsgFmt);
244 PrintMessage ("warning", FileName, LineNumber, MessageCode, Text, MsgFmt, List);
245 va_end (List);
246 //
247 // Set status accordingly
248 //
249 if (mStatus < STATUS_WARNING) {
250 mStatus = STATUS_WARNING;
251 }
252 }
253
254 void
255 DebugMsg (
256 INT8 *FileName,
257 UINT32 LineNumber,
258 UINT32 MsgMask,
259 INT8 *Text,
260 INT8 *MsgFmt,
261 ...
262 )
263 /*++
264
265 Routine Description:
266 Print a warning message.
267
268 Arguments:
269 FileName - typically the name of the utility printing the debug message, but
270 can be the name of a file being parsed.
271
272 LineNumber - the line number in FileName (parsers)
273
274 MsgMask - an application-specific bitmask that, in combination with mDebugMsgMask,
275 determines if the debug message gets printed.
276
277 Text - the text in question (parsers)
278
279 MsgFmt - the format string for the debug message. Can contain formatting
280 controls for use with varargs.
281
282 ...
283 Returns:
284 None.
285
286 --*/
287 {
288 va_list List;
289 //
290 // If the debug mask is not applicable, then do nothing.
291 //
292 if ((MsgMask != 0) && ((mDebugMsgMask & MsgMask) == 0)) {
293 return ;
294 }
295
296 va_start (List, MsgFmt);
297 PrintMessage ("debug", FileName, LineNumber, 0, Text, MsgFmt, List);
298 va_end (List);
299 }
300
301 static
302 void
303 PrintMessage (
304 INT8 *Type,
305 INT8 *FileName,
306 UINT32 LineNumber,
307 UINT32 MessageCode,
308 INT8 *Text,
309 INT8 *MsgFmt,
310 va_list List
311 )
312 /*++
313
314 Routine Description:
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
317 errors or warnings.
318
319 Arguments:
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.
323
324 FileName - name of the file where the warning was detected, or the name
325 of the application that detected the warning
326
327 LineNumber - the line number where the warning was detected (parsers).
328 0 should be specified if the utility is not a parser.
329
330 MessageCode - an application-specific warning code that can be referenced in
331 other documentation.
332
333 Text - part of the message to print
334
335 MsgFmt - the format string for the message. Can contain formatting
336 controls for use with varargs.
337
338 List - Variable function parameter list.
339 Returns:
340 None.
341
342 Notes:
343 If FileName == NULL then this utility will use the string passed into SetUtilityName().
344
345 LineNumber is only used if the caller is a parser, in which case FileName refers to the
346 file being parsed.
347
348 Text and MsgFmt are both optional, though it would be of little use calling this function with
349 them both NULL.
350
351 Output will typically be of the form:
352 <FileName>(<LineNumber>) : <Type> <Type[0]><MessageCode>: <Text> : <MsgFmt>
353
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
358
359 --*/
360 {
361 INT8 Line[MAX_LINE_LEN];
362 INT8 Line2[MAX_LINE_LEN];
363 INT8 *Cptr;
364 //
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.
367 //
368 if (FileName != NULL) {
369 Cptr = FileName;
370 } else if (mUtilityName[0] != 0) {
371 Cptr = mUtilityName;
372 } else {
373 Cptr = "Unknown utility";
374 }
375
376 strcpy (Line, Cptr);
377 if (LineNumber != 0) {
378 sprintf (Line2, "(%d)", LineNumber);
379 strcat (Line, Line2);
380 }
381 //
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.
384 //
385 sprintf (Line2, " : %s %c%04d", Type, toupper (Type[0]), MessageCode);
386 strcat (Line, Line2);
387 fprintf (stdout, "%s", Line);
388 //
389 // If offending text was provided, then print it
390 //
391 if (Text != NULL) {
392 fprintf (stdout, ": %s ", Text);
393 }
394 //
395 // Print formatted message if provided
396 //
397 if (MsgFmt != NULL) {
398 vsprintf (Line2, MsgFmt, List);
399 fprintf (stdout, ": %s", Line2);
400 }
401
402 fprintf (stdout, "\n");
403 }
404
405 void
406 ParserSetPosition (
407 INT8 *SourceFileName,
408 UINT32 LineNum
409 )
410 /*++
411
412 Routine Description:
413 Set the position in a file being parsed. This can be used to
414 print error messages deeper down in a parser.
415
416 Arguments:
417 SourceFileName - name of the source file being parsed
418 LineNum - line number of the source file being parsed
419
420 Returns:
421 NA
422
423 --*/
424 {
425 mSourceFileName = SourceFileName;
426 mSourceFileLineNum = LineNum;
427 }
428
429 void
430 SetUtilityName (
431 INT8 *UtilityName
432 )
433 /*++
434
435 Routine Description:
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.
441
442 Arguments:
443 UtilityName - name of the utility, which will be printed with all
444 error/warning/debug messags.
445
446 Returns:
447 NA
448
449 --*/
450 {
451 //
452 // Save the name of the utility in our local variable. Make sure its
453 // length does not exceed our buffer.
454 //
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;
460 return ;
461 } else {
462 strcpy (mUtilityName, UtilityName);
463 }
464 } else {
465 Error (NULL, 0, 0, "application error", "SetUtilityName() called with NULL utility name");
466 }
467 }
468
469 STATUS
470 GetUtilityStatus (
471 VOID
472 )
473 /*++
474
475 Routine Description:
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
479 value.
480
481 Arguments:
482 None.
483
484 Returns:
485 Worst-case status reported, as defined by which print function was called.
486
487 --*/
488 {
489 return mStatus;
490 }