1 /*************************************************************************
2  *                                                                       *
3  * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
4  * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
5  *                                                                       *
6  * This library is free software; you can redistribute it and/or         *
7  * modify it under the terms of EITHER:                                  *
8  *   (1) The GNU Lesser General Public License as published by the Free  *
9  *       Software Foundation; either version 2.1 of the License, or (at  *
10  *       your option) any later version. The text of the GNU Lesser      *
11  *       General Public License is included with this library in the     *
12  *       file LICENSE.TXT.                                               *
13  *   (2) The BSD-style license that is included with this library in     *
14  *       the file LICENSE-BSD.TXT.                                       *
15  *                                                                       *
16  * This library is distributed in the hope that it will be useful,       *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
19  * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
20  *                                                                       *
21  *************************************************************************/
22 
23 #include <ode/odeconfig.h>
24 #include <ode/error.h>
25 #include "config.h"
26 
27 
28 static dMessageFunction *error_function = 0;
29 static dMessageFunction *debug_function = 0;
30 static dMessageFunction *message_function = 0;
31 
32 
dSetErrorHandler(dMessageFunction * fn)33 extern "C" void dSetErrorHandler (dMessageFunction *fn)
34 {
35     error_function = fn;
36 }
37 
38 
dSetDebugHandler(dMessageFunction * fn)39 extern "C" void dSetDebugHandler (dMessageFunction *fn)
40 {
41     debug_function = fn;
42 }
43 
44 
dSetMessageHandler(dMessageFunction * fn)45 extern "C" void dSetMessageHandler (dMessageFunction *fn)
46 {
47     message_function = fn;
48 }
49 
50 
dGetErrorHandler()51 extern "C" dMessageFunction *dGetErrorHandler()
52 {
53     return error_function;
54 }
55 
56 
dGetDebugHandler()57 extern "C" dMessageFunction *dGetDebugHandler()
58 {
59     return debug_function;
60 }
61 
62 
dGetMessageHandler()63 extern "C" dMessageFunction *dGetMessageHandler()
64 {
65     return message_function;
66 }
67 
68 
printMessage(int num,const char * msg1,const char * msg2,va_list ap)69 static void printMessage (int num, const char *msg1, const char *msg2,
70                           va_list ap)
71 {
72     fflush (stderr);
73     fflush (stdout);
74     if (num) fprintf (stderr,"\n%s %d: ",msg1,num);
75     else fprintf (stderr,"\n%s: ",msg1);
76     vfprintf (stderr,msg2,ap);
77     fprintf (stderr,"\n");
78     fflush (stderr);
79 }
80 
81 //****************************************************************************
82 // unix
83 
84 #ifndef WIN32
85 
dError(int num,const char * msg,...)86 extern "C" void dError (int num, const char *msg, ...)
87 {
88     va_list ap;
89     va_start (ap,msg);
90     if (error_function) error_function (num,msg,ap);
91     else printMessage (num,"ODE Error",msg,ap);
92     va_end (ap);
93     exit (1);
94 }
95 
96 
dDebug(int num,const char * msg,...)97 extern "C" void dDebug (int num, const char *msg, ...)
98 {
99     va_list ap;
100     va_start (ap,msg);
101     if (debug_function) debug_function (num,msg,ap);
102     else printMessage (num,"ODE INTERNAL ERROR",msg,ap);
103     va_end (ap);
104     // *((char *)0) = 0;   ... commit SEGVicide
105     abort();
106 }
107 
108 
dMessage(int num,const char * msg,...)109 extern "C" void dMessage (int num, const char *msg, ...)
110 {
111     va_list ap;
112     va_start (ap,msg);
113     if (message_function) message_function (num,msg,ap);
114     else printMessage (num,"ODE Message",msg,ap);
115     va_end (ap);
116 }
117 
118 #endif
119 
120 //****************************************************************************
121 // windows
122 
123 #ifdef WIN32
124 
125 // isn't cygwin annoying!
126 #ifdef CYGWIN
127 #define _snprintf snprintf
128 #define _vsnprintf vsnprintf
129 #endif
130 
131 
132 #include "windows.h"
133 
134 
dError(int num,const char * msg,...)135 extern "C" void dError (int num, const char *msg, ...)
136 {
137     va_list ap;
138     va_start (ap,msg);
139     if (error_function) error_function (num,msg,ap);
140     else {
141         char s[1000],title[100];
142         _snprintf (title,sizeof(title),"ODE Error %d",num);
143         _vsnprintf (s,sizeof(s),msg,ap);
144         s[sizeof(s)-1] = 0;
145         MessageBox(0,s,title,MB_OK | MB_ICONWARNING);
146     }
147     va_end (ap);
148     exit (1);
149 }
150 
151 
dDebug(int num,const char * msg,...)152 extern "C" void dDebug (int num, const char *msg, ...)
153 {
154     va_list ap;
155     va_start (ap,msg);
156     if (debug_function) debug_function (num,msg,ap);
157     else {
158         char s[1000],title[100];
159         _snprintf (title,sizeof(title),"ODE INTERNAL ERROR %d",num);
160         _vsnprintf (s,sizeof(s),msg,ap);
161         s[sizeof(s)-1] = 0;
162         MessageBox(0,s,title,MB_OK | MB_ICONSTOP);
163     }
164     va_end (ap);
165     abort();
166 }
167 
168 
dMessage(int num,const char * msg,...)169 extern "C" void dMessage (int num, const char *msg, ...)
170 {
171     va_list ap;
172     va_start (ap,msg);
173     if (message_function) message_function (num,msg,ap);
174     else printMessage (num,"ODE Message",msg,ap);
175     va_end (ap);
176 }
177 
178 
179 #endif
180