core/exception.c

Go to the documentation of this file.
00001 
00054 #include <string.h>
00055 #include "exception.h"
00056 #include "output.h"
00057 
00058 EXCEPTIONHANDLER *handlers = NULL;
00059 
00063 EXCEPTIONHANDLER *create_exception_handler(void)
00064 {
00065     EXCEPTIONHANDLER *ptr = malloc(sizeof(EXCEPTIONHANDLER));
00066     ptr->next = handlers;
00067     ptr->id = (handlers==NULL?0:handlers->id)+1;
00068     memset(ptr->msg,0,sizeof(ptr->msg));
00069     handlers = ptr;
00070     return ptr;
00071 }
00072 
00075 void delete_exception_handler(EXCEPTIONHANDLER *ptr) 
00076 {
00077     EXCEPTIONHANDLER *target = ptr->next;
00078     while (handlers!=target)
00079     {
00080         ptr = handlers;
00081         handlers=ptr->next;
00082         free(ptr);
00083         /* if(handlers == NULL) break; */
00084     }
00085 }
00086 
00089 void throw_exception(char *format, 
00090                      ...) 
00091 {
00092     char buffer[1024];
00093     va_list ptr;
00094     va_start(ptr,format);
00095     vsprintf(buffer,format,ptr);
00096     va_end(ptr);
00097 
00098     if (handlers)
00099     {
00100         strncpy(handlers->msg,buffer,sizeof(handlers->msg));
00101         longjmp(handlers->buf,handlers->id);
00102     }
00103     else
00104     {
00105         output_fatal("unhandled exception: %s", buffer);
00106         exit(-1);
00107     }
00108 }
00109 
00113 char *exception_msg(void)
00114 {
00115     return handlers->msg;
00116 }
00117 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy