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 "config.h"
25 #include <ode/error.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   exit (1);
93 }
94 
95 
dDebug(int num,const char * msg,...)96 extern "C" void dDebug (int num, const char *msg, ...)
97 {
98   va_list ap;
99   va_start (ap,msg);
100   if (debug_function) debug_function (num,msg,ap);
101   else printMessage (num,"ODE INTERNAL ERROR",msg,ap);
102   // *((char *)0) = 0;   ... commit SEGVicide
103   abort();
104 }
105 
106 
dMessage(int num,const char * msg,...)107 extern "C" void dMessage (int num, const char *msg, ...)
108 {
109   va_list ap;
110   va_start (ap,msg);
111   if (message_function) message_function (num,msg,ap);
112   else printMessage (num,"ODE Message",msg,ap);
113 }
114 
115 #endif
116 
117 //****************************************************************************
118 // windows
119 
120 #ifdef WIN32
121 
122 // isn't cygwin annoying!
123 #ifdef CYGWIN
124 #define _snprintf snprintf
125 #define _vsnprintf vsnprintf
126 #endif
127 
128 
129 #include "windows.h"
130 
131 
dError(int num,const char * msg,...)132 extern "C" void dError (int num, const char *msg, ...)
133 {
134   va_list ap;
135   va_start (ap,msg);
136   if (error_function) error_function (num,msg,ap);
137   else {
138     char s[1000],title[100];
139     _snprintf (title,sizeof(title),"ODE Error %d",num);
140     _vsnprintf (s,sizeof(s),msg,ap);
141     s[sizeof(s)-1] = 0;
142     MessageBox(0,s,title,MB_OK | MB_ICONWARNING);
143   }
144   exit (1);
145 }
146 
147 
dDebug(int num,const char * msg,...)148 extern "C" void dDebug (int num, const char *msg, ...)
149 {
150   va_list ap;
151   va_start (ap,msg);
152   if (debug_function) debug_function (num,msg,ap);
153   else {
154     char s[1000],title[100];
155     _snprintf (title,sizeof(title),"ODE INTERNAL ERROR %d",num);
156     _vsnprintf (s,sizeof(s),msg,ap);
157     s[sizeof(s)-1] = 0;
158     MessageBox(0,s,title,MB_OK | MB_ICONSTOP);
159   }
160   abort();
161 }
162 
163 
dMessage(int num,const char * msg,...)164 extern "C" void dMessage (int num, const char *msg, ...)
165 {
166   va_list ap;
167   va_start (ap,msg);
168   if (message_function) message_function (num,msg,ap);
169   else printMessage (num,"ODE Message",msg,ap);
170 }
171 
172 
173 #endif
174