]> git.proxmox.com Git - mirror_edk2.git/blame - EdkCompatibilityPkg/Sample/Tools/Source/ProcessDsc/Exceptions.c
Update the copyright notice format
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / ProcessDsc / Exceptions.c
CommitLineData
3eb9473e 1/*++\r
2\r
4b1e1121
HT
3Copyright (c) 2004, Intel Corporation. All rights reserved.<BR>\r
4This program and the accompanying materials \r
3eb9473e 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 Exceptions.c\r
15\r
16Abstract:\r
17\r
18 Exception logging routines.\r
19\r
20--*/\r
21\r
22#include <stdio.h>\r
23#include <stdlib.h>\r
24#include <string.h> // for memset()\r
25#include "Exceptions.h"\r
26\r
27//\r
28// Max length of a saved exception message\r
29//\r
30#define MAX_EXCEPTION_MSG 200\r
31\r
32//\r
33// We use this structure to track exceptions thrown. We nest deeper on\r
34// TryException() calls, and come back out on CatchException() calls.\r
35// We save off the first exception message for a given exception level,\r
36// but we save the count of how many were thrown.\r
37//\r
38typedef struct {\r
39 int ExceptionCount;\r
40 char ExceptionMsg[MAX_EXCEPTION_MSG];\r
41} EXCEPTION_LOG;\r
42\r
43static EXCEPTION_LOG ExceptionLog[MAX_EXCEPTION_NESTING + 1];\r
44static int ExceptionLevel;\r
45\r
46//\r
47// Initialize our data and structures for tracking exceptions.\r
48//\r
49int\r
50InitExceptions (\r
51 VOID\r
52 )\r
53{\r
54 ExceptionLevel = -1;\r
55 memset ((char *) &ExceptionLog, 0, sizeof (ExceptionLog));\r
56 return 0;\r
57}\r
58//\r
59// This function replaces the _try() exception macro. It sets the\r
60// nesting level.\r
61//\r
62int\r
63TryException (\r
64 VOID\r
65 )\r
66{\r
67 //\r
68 // Boost our exception level if we would not go out of range\r
69 //\r
70 ExceptionLevel++;\r
71 if (ExceptionLevel >= MAX_EXCEPTION_NESTING) {\r
72 fprintf (stderr, "ERROR: Max exception nesting level exceeded\n");\r
73 ExceptionLevel--;\r
74 return 1;\r
75 }\r
76\r
77 return 0;\r
78}\r
79//\r
80// This function replaces the _catch() exception macro. It's used to decrement\r
81// the nesting level and return any exeption error messages that were\r
82// thrown at the current nesting level.\r
83//\r
84char *\r
85CatchException (\r
86 VOID\r
87 )\r
88{\r
89 //\r
90 // Return a pointer to exception message. NULL if no exceptions at this level\r
91 //\r
92 if (ExceptionLevel >= 0) {\r
93 ExceptionLevel--;\r
94 if (ExceptionLog[ExceptionLevel + 1].ExceptionMsg[0]) {\r
95 return ExceptionLog[ExceptionLevel + 1].ExceptionMsg;\r
96 } else {\r
97 return NULL;\r
98 }\r
99 } else {\r
100 fprintf (stderr, "ERROR: Invalid nesting level call to CatchException()\n");\r
101 return NULL;\r
102 }\r
103}\r
104//\r
105// This function can be used to test for exceptions between the TryException()\r
106// and CatchException() calls in a given function.\r
107//\r
108int\r
109ExceptionThrown (\r
110 VOID\r
111 )\r
112{\r
113 return ExceptionLog[ExceptionLevel].ExceptionCount;\r
114}\r
115//\r
116// This function replaces the _throw() exception macro. It saves off the\r
117// given error message at the current exeption level nesting.\r
118//\r
119int\r
120ThrowException (\r
121 char *Msg\r
122 )\r
123{\r
124 if (ExceptionLevel < 0) {\r
125 //\r
126 // fprintf (stderr, "ERROR: Exception thrown out of scope");\r
127 // Haven't yet enabled handling of exceptions, so just emit the message.\r
128 //\r
129 fprintf (stderr, Msg);\r
130 return 1;\r
131 }\r
132 //\r
133 // Only log the first\r
134 //\r
135 if (ExceptionLog[ExceptionLevel].ExceptionMsg[0] == 0) {\r
136 strncpy (ExceptionLog[ExceptionLevel].ExceptionMsg, Msg, MAX_EXCEPTION_MSG);\r
137 }\r
138\r
139 ExceptionLog[ExceptionLevel].ExceptionCount++;\r
140 return 0;\r
141}\r