]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkCompatibilityPkg/Sample/Tools/Source/ProcessDsc/Exceptions.c
Add in the 1st version of ECP.
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / ProcessDsc / Exceptions.c
diff --git a/EdkCompatibilityPkg/Sample/Tools/Source/ProcessDsc/Exceptions.c b/EdkCompatibilityPkg/Sample/Tools/Source/ProcessDsc/Exceptions.c
new file mode 100644 (file)
index 0000000..bc50183
--- /dev/null
@@ -0,0 +1,141 @@
+/*++\r
+\r
+Copyright (c) 2004, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  Exceptions.c\r
+\r
+Abstract:\r
+\r
+  Exception logging routines.\r
+\r
+--*/\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h> // for memset()\r
+#include "Exceptions.h"\r
+\r
+//\r
+// Max length of a saved exception message\r
+//\r
+#define MAX_EXCEPTION_MSG 200\r
+\r
+//\r
+// We use this structure to track exceptions thrown. We nest deeper on\r
+// TryException() calls, and come back out on CatchException() calls.\r
+// We save off the first exception message for a given exception level,\r
+// but we save the count of how many were thrown.\r
+//\r
+typedef struct {\r
+  int   ExceptionCount;\r
+  char  ExceptionMsg[MAX_EXCEPTION_MSG];\r
+} EXCEPTION_LOG;\r
+\r
+static EXCEPTION_LOG  ExceptionLog[MAX_EXCEPTION_NESTING + 1];\r
+static int            ExceptionLevel;\r
+\r
+//\r
+// Initialize our data and structures for tracking exceptions.\r
+//\r
+int\r
+InitExceptions (\r
+  VOID\r
+  )\r
+{\r
+  ExceptionLevel = -1;\r
+  memset ((char *) &ExceptionLog, 0, sizeof (ExceptionLog));\r
+  return 0;\r
+}\r
+//\r
+// This function replaces the _try() exception macro. It sets the\r
+// nesting level.\r
+//\r
+int\r
+TryException (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Boost our exception level if we would not go out of range\r
+  //\r
+  ExceptionLevel++;\r
+  if (ExceptionLevel >= MAX_EXCEPTION_NESTING) {\r
+    fprintf (stderr, "ERROR: Max exception nesting level exceeded\n");\r
+    ExceptionLevel--;\r
+    return 1;\r
+  }\r
+\r
+  return 0;\r
+}\r
+//\r
+// This function replaces the _catch() exception macro. It's used to decrement\r
+// the nesting level and return any exeption error messages that were\r
+// thrown at the current nesting level.\r
+//\r
+char *\r
+CatchException (\r
+  VOID\r
+  )\r
+{\r
+  //\r
+  // Return a pointer to exception message. NULL if no exceptions at this level\r
+  //\r
+  if (ExceptionLevel >= 0) {\r
+    ExceptionLevel--;\r
+    if (ExceptionLog[ExceptionLevel + 1].ExceptionMsg[0]) {\r
+      return ExceptionLog[ExceptionLevel + 1].ExceptionMsg;\r
+    } else {\r
+      return NULL;\r
+    }\r
+  } else {\r
+    fprintf (stderr, "ERROR: Invalid nesting level call to CatchException()\n");\r
+    return NULL;\r
+  }\r
+}\r
+//\r
+// This function can be used to test for exceptions between the TryException()\r
+// and CatchException() calls in a given function.\r
+//\r
+int\r
+ExceptionThrown (\r
+  VOID\r
+  )\r
+{\r
+  return ExceptionLog[ExceptionLevel].ExceptionCount;\r
+}\r
+//\r
+// This function replaces the _throw() exception macro. It saves off the\r
+// given error message at the current exeption level nesting.\r
+//\r
+int\r
+ThrowException (\r
+  char *Msg\r
+  )\r
+{\r
+  if (ExceptionLevel < 0) {\r
+    //\r
+    // fprintf (stderr, "ERROR: Exception thrown out of scope");\r
+    // Haven't yet enabled handling of exceptions, so just emit the message.\r
+    //\r
+    fprintf (stderr, Msg);\r
+    return 1;\r
+  }\r
+  //\r
+  // Only log the first\r
+  //\r
+  if (ExceptionLog[ExceptionLevel].ExceptionMsg[0] == 0) {\r
+    strncpy (ExceptionLog[ExceptionLevel].ExceptionMsg, Msg, MAX_EXCEPTION_MSG);\r
+  }\r
+\r
+  ExceptionLog[ExceptionLevel].ExceptionCount++;\r
+  return 0;\r
+}\r