From: Gregoire Henry <Gregoire.Henry@pps.jussieu.fr>
Date: Sat, 28 Nov 2009 10:15:23 +0100
Subject: debugging facility for Java exceptions

debugging is togglable at runtime, without needing to recompile CamlJava
---
 lib/jni.mli    |  2 ++
 lib/jni.mlp    |  2 ++
 lib/jnistubs.c | 15 +++++++++++----
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/lib/jni.mli b/lib/jni.mli
index 2a593f0..ad829b3 100644
--- a/lib/jni.mli
+++ b/lib/jni.mli
@@ -14,6 +14,8 @@
 
 (* Low-level Java interface (JNI level) *)
 
+external set_debug: bool -> unit = "camljava_set_debug"
+
 (* Object operations *)
 
 type obj
diff --git a/lib/jni.mlp b/lib/jni.mlp
index 9005e65..d1febac 100644
--- a/lib/jni.mlp
+++ b/lib/jni.mlp
@@ -14,6 +14,8 @@
 
 (* Low-level Java interface (JNI level) *)
 
+external set_debug: bool -> unit = "camljava_set_debug"
+
 external init: string -> unit = "camljava_Init"
 external shutdown: unit -> unit = "camljava_Shutdown"
 
diff --git a/lib/jnistubs.c b/lib/jnistubs.c
index e1659f8..27ff365 100644
--- a/lib/jnistubs.c
+++ b/lib/jnistubs.c
@@ -74,6 +74,13 @@ value camljava_IsNull(value vobj)
 
 /*********** Reflecting Java exceptions as Caml exceptions *************/
 
+static int debug = 0;
+
+value camljava_set_debug(value v) {
+  debug = Bool_val(v);
+  return Val_unit;
+}
+
 static void check_java_exception(void)
 {
   jthrowable exn;
@@ -82,10 +89,10 @@ static void check_java_exception(void)
 
   exn = (*jenv)->ExceptionOccurred(jenv);
   if (exn != NULL) {
-#if 0
-    /* For debugging */
-    (*jenv)->ExceptionDescribe(jenv);
-#endif
+    if(debug) {
+      /* For debugging */
+      (*jenv)->ExceptionDescribe(jenv);
+    }
     (*jenv)->ExceptionClear(jenv);
     /* TODO: check Caml exception embedded into Java exception */
     if (camljava_raise_exception == NULL) {
