/[ascend]/trunk/base/generic/utilities/ascPanic.c
ViewVC logotype

Contents of /trunk/base/generic/utilities/ascPanic.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 427 - (show annotations) (download) (as text)
Tue Apr 4 04:13:34 2006 UTC (15 years, 7 months ago) by johnpye
File MIME type: text/x-csrc
File size: 4636 byte(s)
tentative: Disabled ascDynaload functions when DYNAMIC_PACKAGES is not defined. 
Altered error.[ch] so that MS VC++ variadic macros are used when available. 
Disabled Windows MessageBox in ascPanic, so that compilation of base engine is possible with the Platform SDK.
Added SConscript files to build FORTRAN components.
Removed some autoconf-related stuff from the pygtk/interface directory.
1 /*
2 ASCEND Language Interpreter
3 AscPanic by Mark Thomas, created 15 May 1997
4 Copyright (C) 2005 Carnegie-Mellon University
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 This file is part of the SLV solver.
20 */
21
22 /* removed changelog -- please consult commit comments in ViewCVS */
23
24 #include <stdarg.h>
25 #include "ascConfig.h"
26 #include "ascPanic.h"
27
28 #define PANIC_MSG_MAXLEN 2047
29 /**< The maximum length of the panic message.
30 Used to create a buffer to hold the message.
31 */
32
33 static PanicCallbackFunc f_panic_callback_func = NULL;
34 /**< Holds a pointer to a callback function if registered using
35 Asc_PanicSetCallback(). If NULL (the default), nothing is called.
36 */
37
38 /*
39 disable the windows message box...
40 #if defined(__WIN32__)
41 # define USE_WIN32_FATAL_MSGBOX
42 #endif
43 */
44
45 #ifdef USE_WIN32_FATAL_MSGBOX
46 static int f_display_MessageBox = TRUE;
47 /**< On Windows only, flag to enable/disable display of the MessageBox
48 in Asc_Panic().
49 */
50 #endif
51
52 static char g_panic_outfile[PATH_MAX];
53 /**< Holds the name of the file in which to write panic messages.
54 Use the Asc_PanicSetOutfile(filename) function to set it.
55 */
56
57
58 void Asc_Panic(CONST int status, CONST char *function,
59 CONST char *format, ...)
60 {
61 char msg[PANIC_MSG_MAXLEN]; /* The message that will be printed */
62 size_t p; /* The current length of the msg array */
63 FILE *outfile; /* The file to save the message into */
64 va_list args; /* The arguments to print */
65 int cancel = FALSE; /* If non-zero, do not call exit(). Default is to exit() */
66
67 /* Fail loudly if ASCERR isn't set to a file pointer -- can't use asc_assert here! */
68 assert(NULL != ASCERR);
69
70 /* Give the name of the function where the panic occurred */
71 if( function != NULL ) {
72 snprintf( msg, PANIC_MSG_MAXLEN-2, "function '%s':", function );
73 }else{
74 snprintf(msg, PANIC_MSG_MAXLEN-2, " ");
75 }
76 p = strlen(msg);
77
78 /* Add the variable args to the panic message using the format "format" */
79 va_start(args, format);
80 vsnprintf( (msg+p), PANIC_MSG_MAXLEN-p-2, format, args );
81 va_end(args);
82
83 p = strlen(msg);
84 msg[p++] = '\n';
85 msg[p++] = '\0';
86
87 /*
88 Ensure that our messages don't get left in the GUI
89 that is about to vanish...
90 */
91 error_reporter_set_callback(NULL);
92
93 /* Print the message to ASCERR */
94 ERROR_REPORTER_NOLINE(ASC_PROG_FATAL,msg);
95
96 /*
97 Write the message to g_panic_outfile if it is not empty
98 and we can actually write to that location. Print a
99 message on ASCERR (if valid) saying that we did that.
100 */
101 if(( g_panic_outfile[0] != '\0' )
102 && ( (outfile=fopen(g_panic_outfile,"w")) != NULL ))
103 {
104 FPRINTF(outfile, msg);
105 if( ASCERR != NULL ) {
106 CONSOLE_DEBUG("Error message written to %s\n", g_panic_outfile);
107 }
108 fclose(outfile);
109 }
110
111 /* Call the registered callback function, if any. */
112 if (NULL != f_panic_callback_func) {
113 cancel = (*f_panic_callback_func)(status);
114 }
115
116 /* Display msg in a MessageBox under Windows unless turned off */
117 #ifdef USE_WIN32_FATAL_MSGBOX
118 if (FALSE != f_display_MessageBox) {
119 (void)MessageBeep(MB_ICONEXCLAMATION);
120 MessageBox(NULL, msg, "Fatal Error in ASCEND",
121 (UINT)(MB_ICONSTOP | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND));
122 }
123 if (0 == cancel)
124 ExitProcess((UINT)status);
125 #endif
126
127 if (0 == cancel)
128 exit(status);
129 }
130
131
132 void Asc_PanicSetOutfile(CONST char *filename)
133 {
134 if( filename != NULL ) {
135 strncpy( g_panic_outfile, filename, PATH_MAX-1 );
136 g_panic_outfile[PATH_MAX-1] = '\0';
137 } else {
138 g_panic_outfile[0] = '\0';
139 }
140 }
141
142 PanicCallbackFunc Asc_PanicSetCallback(PanicCallbackFunc func)
143 {
144 PanicCallbackFunc old_func = f_panic_callback_func;
145 f_panic_callback_func = func;
146 return old_func;
147 }
148
149
150 void Asc_PanicDisplayMessageBox(int is_displayed)
151 {
152 #ifdef USE_WIN32_FATAL_MSGBOX
153 f_display_MessageBox = is_displayed;
154 #else
155 UNUSED_PARAMETER(is_displayed);
156 #endif
157 }
158
159 /* removed superceded test 'main' */

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22