]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Sample/Tools/Source/GuidChk/UtilsMsgs.c
Add in the 1st version of ECP.
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / GuidChk / UtilsMsgs.c
CommitLineData
3eb9473e 1/*++\r
2\r
3Copyright (c) 2004, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name: \r
13\r
14 UtilsMsgs.c\r
15 \r
16Abstract:\r
17\r
18 EFI tools utility functions to display warning, error, and informational\r
19 messages.\r
20 \r
21--*/\r
22\r
23#include <stdio.h>\r
24#include <string.h>\r
25#include <ctype.h>\r
26#include <stdarg.h>\r
27\r
28#include "Tiano.h"\r
29#include "EfiUtilityMsgs.h"\r
30\r
31#define MAX_LINE_LEN 200\r
32\r
33//\r
34// Declare module globals for keeping track of the the utility's\r
35// name and other settings.\r
36//\r
37static STATUS mStatus = STATUS_SUCCESS;\r
38static INT8 mUtilityName[50] = { 0 };\r
39static INT8 *mSourceFileName = NULL;\r
40static UINT32 mSourceFileLineNum = 0;\r
41static UINT32 mErrorCount = 0;\r
42static UINT32 mWarningCount = 0;\r
43static UINT32 mDebugMsgMask = 0;\r
44\r
45static\r
46void\r
47PrintMessage (\r
48 INT8 *Type,\r
49 INT8 *FileName,\r
50 UINT32 LineNumber,\r
51 UINT32 MessageCode,\r
52 INT8 *Text,\r
53 INT8 *MsgFmt,\r
54 va_list List\r
55 );\r
56\r
57void\r
58Error (\r
59 INT8 *FileName,\r
60 UINT32 LineNumber,\r
61 UINT32 MessageCode,\r
62 INT8 *Text,\r
63 INT8 *MsgFmt,\r
64 ...\r
65 )\r
66/*++\r
67\r
68Routine Description:\r
69 Prints an error message.\r
70 \r
71Arguments:\r
72 All arguments are optional, though the printed message may be useless if\r
73 at least something valid is not specified.\r
74 \r
75 FileName - name of the file or application. If not specified, then the\r
76 utilty name (as set by the utility calling SetUtilityName()\r
77 earlier) is used. Otherwise "Unknown utility" is used.\r
78 \r
79 LineNumber - the line number of error, typically used by parsers. If the\r
80 utility is not a parser, then 0 should be specified. Otherwise\r
81 the FileName and LineNumber info can be used to cause\r
82 MS Visual Studio to jump to the error.\r
83 \r
84 MessageCode - an application-specific error code that can be referenced in\r
85 other documentation. \r
86\r
87 Text - the text in question, typically used by parsers.\r
88 \r
89 MsgFmt - the format string for the error message. Can contain formatting\r
90 controls for use with the varargs.\r
91 \r
92Returns:\r
93 None.\r
94 \r
95Notes:\r
96 We print the following (similar to the Warn() and Debug() \r
97 W\r
98 Typical error/warning message format:\r
99\r
100 bin\VfrCompile.cpp(330) : error C2660: 'AddVfrDataStructField' : function does not take 2 parameters\r
101\r
102 BUGBUG -- these three utility functions are almost identical, and\r
103 should be modified to share code.\r
104\r
105 Visual Studio does not find error messages with:\r
106 \r
107 " error :"\r
108 " error 1:"\r
109 " error c1:"\r
110 " error 1000:"\r
111 " error c100:"\r
112\r
113 It does find:\r
114 " error c1000:" \r
115--*/\r
116{\r
117 va_list List;\r
118 mErrorCount++;\r
119 va_start (List, MsgFmt);\r
120 PrintMessage ("error", FileName, LineNumber, MessageCode, Text, MsgFmt, List);\r
121 va_end (List);\r
122 //\r
123 // Set status accordingly\r
124 //\r
125 if (mStatus < STATUS_ERROR) {\r
126 mStatus = STATUS_ERROR;\r
127 }\r
128}\r
129\r
130void\r
131ParserError (\r
132 UINT32 MessageCode,\r
133 INT8 *Text,\r
134 INT8 *MsgFmt,\r
135 ...\r
136 )\r
137/*++\r
138\r
139Routine Description:\r
140 Print a parser error, using the source file name and line number\r
141 set by a previous call to SetParserPosition().\r
142\r
143Arguments:\r
144 MessageCode - application-specific error code\r
145 Text - text to print in the error message\r
146 MsgFmt - format string to print at the end of the error message\r
147 ...\r
148\r
149Returns:\r
150 NA\r
151\r
152--*/\r
153{\r
154 va_list List;\r
155 mErrorCount++;\r
156 va_start (List, MsgFmt);\r
157 PrintMessage ("error", mSourceFileName, mSourceFileLineNum, MessageCode, Text, MsgFmt, List);\r
158 va_end (List);\r
159 //\r
160 // Set status accordingly\r
161 //\r
162 if (mStatus < STATUS_ERROR) {\r
163 mStatus = STATUS_ERROR;\r
164 }\r
165}\r
166\r
167void\r
168ParserWarning (\r
169 UINT32 ErrorCode,\r
170 INT8 *OffendingText,\r
171 INT8 *MsgFmt,\r
172 ...\r
173 )\r
174/*++\r
175\r
176Routine Description:\r
177 Print a parser warning, using the source file name and line number\r
178 set by a previous call to SetParserPosition().\r
179\r
180Arguments:\r
181 ErrorCode - application-specific error code\r
182 OffendingText - text to print in the warning message\r
183 MsgFmt - format string to print at the end of the warning message\r
184 ...\r
185\r
186Returns:\r
187 NA\r
188\r
189--*/\r
190{\r
191 va_list List;\r
192 mWarningCount++;\r
193 va_start (List, MsgFmt);\r
194 PrintMessage ("warning", mSourceFileName, mSourceFileLineNum, ErrorCode, OffendingText, MsgFmt, List);\r
195 va_end (List);\r
196 //\r
197 // Set status accordingly\r
198 //\r
199 if (mStatus < STATUS_WARNING) {\r
200 mStatus = STATUS_WARNING;\r
201 }\r
202}\r
203\r
204void\r
205Warning (\r
206 INT8 *FileName,\r
207 UINT32 LineNumber,\r
208 UINT32 MessageCode,\r
209 INT8 *Text,\r
210 INT8 *MsgFmt,\r
211 ...\r
212 )\r
213/*++\r
214\r
215Routine Description:\r
216 Print a warning message.\r
217\r
218Arguments:\r
219 FileName - name of the file where the warning was detected, or the name\r
220 of the application that detected the warning\r
221 \r
222 LineNumber - the line number where the warning was detected (parsers).\r
223 0 should be specified if the utility is not a parser.\r
224 \r
225 MessageCode - an application-specific warning code that can be referenced in\r
226 other documentation. \r
227\r
228 Text - the text in question (parsers)\r
229 \r
230 MsgFmt - the format string for the warning message. Can contain formatting\r
231 controls for use with varargs.\r
232 \r
233 ...\r
234 \r
235Returns:\r
236 None.\r
237\r
238--*/\r
239{\r
240 va_list List;\r
241 mWarningCount++;\r
242 va_start (List, MsgFmt);\r
243 PrintMessage ("warning", FileName, LineNumber, MessageCode, Text, MsgFmt, List);\r
244 va_end (List);\r
245 //\r
246 // Set status accordingly\r
247 //\r
248 if (mStatus < STATUS_WARNING) {\r
249 mStatus = STATUS_WARNING;\r
250 }\r
251}\r
252\r
253void\r
254DebugMsg (\r
255 INT8 *FileName,\r
256 UINT32 LineNumber,\r
257 UINT32 MsgMask,\r
258 INT8 *Text,\r
259 INT8 *MsgFmt,\r
260 ...\r
261 )\r
262/*++\r
263\r
264Routine Description:\r
265 Print a warning message.\r
266\r
267Arguments:\r
268 FileName - typically the name of the utility printing the debug message, but\r
269 can be the name of a file being parsed.\r
270 \r
271 LineNumber - the line number in FileName (parsers) \r
272 \r
273 MsgMask - an application-specific bitmask that, in combination with mDebugMsgMask,\r
274 determines if the debug message gets printed.\r
275\r
276 Text - the text in question (parsers)\r
277 \r
278 MsgFmt - the format string for the debug message. Can contain formatting\r
279 controls for use with varargs.\r
280 \r
281 ... \r
282Returns:\r
283 None.\r
284\r
285--*/\r
286{\r
287 va_list List;\r
288 //\r
289 // If the debug mask is not applicable, then do nothing.\r
290 //\r
291 if ((MsgMask != 0) && ((mDebugMsgMask & MsgMask) == 0)) {\r
292 return ;\r
293 }\r
294\r
295 va_start (List, MsgFmt);\r
296 PrintMessage ("debug", FileName, LineNumber, 0, Text, MsgFmt, List);\r
297 va_end (List);\r
298}\r
299\r
300static\r
301void\r
302PrintMessage (\r
303 INT8 *Type,\r
304 INT8 *FileName,\r
305 UINT32 LineNumber,\r
306 UINT32 MessageCode,\r
307 INT8 *Text,\r
308 INT8 *MsgFmt,\r
309 va_list List\r
310 )\r
311/*++\r
312\r
313Routine Description:\r
314 Worker routine for all the utility printing services. Prints the message in\r
315 a format that Visual Studio will find when scanning build outputs for\r
316 errors or warnings.\r
317\r
318Arguments:\r
319 Type - "warning" or "error" string to insert into the message to be\r
320 printed. The first character of this string (converted to uppercase)\r
321 is used to preceed the MessageCode value in the output string.\r
322\r
323 FileName - name of the file where the warning was detected, or the name\r
324 of the application that detected the warning\r
325 \r
326 LineNumber - the line number where the warning was detected (parsers).\r
327 0 should be specified if the utility is not a parser.\r
328 \r
329 MessageCode - an application-specific warning code that can be referenced in\r
330 other documentation. \r
331\r
332 Text - part of the message to print\r
333 \r
334 MsgFmt - the format string for the message. Can contain formatting\r
335 controls for use with varargs.\r
336\r
337 List - Variable function parameter list. \r
338Returns:\r
339 None.\r
340\r
341Notes:\r
342 If FileName == NULL then this utility will use the string passed into SetUtilityName(). \r
343 \r
344 LineNumber is only used if the caller is a parser, in which case FileName refers to the\r
345 file being parsed.\r
346\r
347 Text and MsgFmt are both optional, though it would be of little use calling this function with\r
348 them both NULL.\r
349\r
350 Output will typically be of the form:\r
351 <FileName>(<LineNumber>) : <Type> <Type[0]><MessageCode>: <Text> : <MsgFmt>\r
352\r
353 Parser (LineNumber != 0)\r
354 VfrCompile.cpp(330) : error E2660: AddVfrDataStructField : function does not take 2 parameters\r
355 Generic utility (LineNumber == 0) \r
356 UtilityName : error E1234 : Text string : MsgFmt string and args\r
357\r
358--*/\r
359{\r
360 INT8 Line[MAX_LINE_LEN];\r
361 INT8 Line2[MAX_LINE_LEN];\r
362 INT8 *Cptr;\r
363 //\r
364 // If given a filename, then add it (and the line number) to the string.\r
365 // If there's no filename, then use the program name if provided.\r
366 //\r
367 if (FileName != NULL) {\r
368 Cptr = FileName;\r
369 } else if (mUtilityName[0] != 0) {\r
370 Cptr = mUtilityName;\r
371 } else {\r
372 Cptr = "Unknown utility";\r
373 }\r
374\r
375 strcpy (Line, Cptr);\r
376 if (LineNumber != 0) {\r
377 sprintf (Line2, "(%d)", LineNumber);\r
378 strcat (Line, Line2);\r
379 }\r
380 //\r
381 // Have to print an error code or Visual Studio won't find the\r
382 // message for you. It has to be decimal digits too.\r
383 //\r
384 sprintf (Line2, " : %s %c%04d", Type, toupper (Type[0]), MessageCode);\r
385 strcat (Line, Line2);\r
386 fprintf (stdout, "%s", Line);\r
387 //\r
388 // If offending text was provided, then print it\r
389 //\r
390 if (Text != NULL) {\r
391 fprintf (stdout, ": %s ", Text);\r
392 }\r
393 //\r
394 // Print formatted message if provided\r
395 //\r
396 if (MsgFmt != NULL) {\r
397 vsprintf (Line2, MsgFmt, List);\r
398 fprintf (stdout, ": %s", Line2);\r
399 }\r
400\r
401 fprintf (stdout, "\n");\r
402}\r
403\r
404void\r
405ParserSetPosition (\r
406 INT8 *SourceFileName,\r
407 UINT32 LineNum\r
408 )\r
409/*++\r
410\r
411Routine Description:\r
412 Set the position in a file being parsed. This can be used to \r
413 print error messages deeper down in a parser.\r
414\r
415Arguments:\r
416 SourceFileName - name of the source file being parsed\r
417 LineNum - line number of the source file being parsed\r
418\r
419Returns:\r
420 NA\r
421\r
422--*/\r
423{\r
424 mSourceFileName = SourceFileName;\r
425 mSourceFileLineNum = LineNum;\r
426}\r
427\r
428void\r
429SetUtilityName (\r
430 INT8 *UtilityName\r
431 )\r
432/*++\r
433\r
434Routine Description:\r
435 All printed error/warning/debug messages follow the same format, and\r
436 typically will print a filename or utility name followed by the error\r
437 text. However if a filename is not passed to the print routines, then\r
438 they'll print the utility name if you call this function early in your\r
439 app to set the utility name.\r
440 \r
441Arguments:\r
442 UtilityName - name of the utility, which will be printed with all\r
443 error/warning/debug messags.\r
444\r
445Returns:\r
446 NA\r
447 \r
448--*/\r
449{\r
450 //\r
451 // Save the name of the utility in our local variable. Make sure its\r
452 // length does not exceed our buffer.\r
453 //\r
454 if (UtilityName != NULL) {\r
455 if (strlen (UtilityName) >= sizeof (mUtilityName)) {\r
456 Error (UtilityName, 0, 0, "application error", "utility name length exceeds internal buffer size");\r
457 strncpy (mUtilityName, UtilityName, sizeof (mUtilityName) - 1);\r
458 mUtilityName[sizeof (mUtilityName) - 1] = 0;\r
459 return ;\r
460 } else {\r
461 strcpy (mUtilityName, UtilityName);\r
462 }\r
463 } else {\r
464 Error (NULL, 0, 0, "application error", "SetUtilityName() called with NULL utility name");\r
465 }\r
466}\r
467\r
468STATUS\r
469GetUtilityStatus (\r
470 VOID\r
471 )\r
472/*++\r
473\r
474Routine Description:\r
475 When you call Error() or Warning(), this module keeps track of it and\r
476 sets a local mStatus to STATUS_ERROR or STATUS_WARNING. When the utility\r
477 exits, it can call this function to get the status and use it as a return\r
478 value.\r
479 \r
480Arguments:\r
481 None.\r
482\r
483Returns:\r
484 Worst-case status reported, as defined by which print function was called.\r
485 \r
486--*/\r
487{\r
488 return mStatus;\r
489}\r