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