3 Copyright (c) 2004, Intel Corporation. All rights reserved.<BR>
4 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
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.
18 Exception logging routines.
24 #include <string.h> // for memset()
25 #include "Exceptions.h"
28 // Max length of a saved exception message
30 #define MAX_EXCEPTION_MSG 200
33 // We use this structure to track exceptions thrown. We nest deeper on
34 // TryException() calls, and come back out on CatchException() calls.
35 // We save off the first exception message for a given exception level,
36 // but we save the count of how many were thrown.
40 char ExceptionMsg
[MAX_EXCEPTION_MSG
];
43 static EXCEPTION_LOG ExceptionLog
[MAX_EXCEPTION_NESTING
+ 1];
44 static int ExceptionLevel
;
47 // Initialize our data and structures for tracking exceptions.
55 memset ((char *) &ExceptionLog
, 0, sizeof (ExceptionLog
));
59 // This function replaces the _try() exception macro. It sets the
68 // Boost our exception level if we would not go out of range
71 if (ExceptionLevel
>= MAX_EXCEPTION_NESTING
) {
72 fprintf (stderr
, "ERROR: Max exception nesting level exceeded\n");
80 // This function replaces the _catch() exception macro. It's used to decrement
81 // the nesting level and return any exeption error messages that were
82 // thrown at the current nesting level.
90 // Return a pointer to exception message. NULL if no exceptions at this level
92 if (ExceptionLevel
>= 0) {
94 if (ExceptionLog
[ExceptionLevel
+ 1].ExceptionMsg
[0]) {
95 return ExceptionLog
[ExceptionLevel
+ 1].ExceptionMsg
;
100 fprintf (stderr
, "ERROR: Invalid nesting level call to CatchException()\n");
105 // This function can be used to test for exceptions between the TryException()
106 // and CatchException() calls in a given function.
113 return ExceptionLog
[ExceptionLevel
].ExceptionCount
;
116 // This function replaces the _throw() exception macro. It saves off the
117 // given error message at the current exeption level nesting.
124 if (ExceptionLevel
< 0) {
126 // fprintf (stderr, "ERROR: Exception thrown out of scope");
127 // Haven't yet enabled handling of exceptions, so just emit the message.
129 fprintf (stderr
, Msg
);
133 // Only log the first
135 if (ExceptionLog
[ExceptionLevel
].ExceptionMsg
[0] == 0) {
136 strncpy (ExceptionLog
[ExceptionLevel
].ExceptionMsg
, Msg
, MAX_EXCEPTION_MSG
);
139 ExceptionLog
[ExceptionLevel
].ExceptionCount
++;