diff --git a/CHANGELOG.md b/CHANGELOG.md index 044f7722..7a2e9dc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,8 @@ ## 0.5.3 - unreleased - Fix INTERNAL ERROR in `FastLSolve.solveL1Straight()` caused by bug in `DLCP.solve1()`. [#133](https://github.com/tzaeschke/ode4j/issues/133) +- Fix formatting of degub/info/error message. + [#135](https://github.com/tzaeschke/ode4j/issues/135) ## 0.5.2 - 2023-10-07 - Fix DVector3.cross() returning always 0. [#128](https://github.com/tzaeschke/ode4j/issues/128) diff --git a/core/src/main/java/org/ode4j/ode/internal/ErrorHdl.java b/core/src/main/java/org/ode4j/ode/internal/ErrorHdl.java index 3dc70596..b964f65c 100644 --- a/core/src/main/java/org/ode4j/ode/internal/ErrorHdl.java +++ b/core/src/main/java/org/ode4j/ode/internal/ErrorHdl.java @@ -25,9 +25,12 @@ package org.ode4j.ode.internal; import org.ode4j.ode.internal.ErrorHandler.dMessageFunction; +import org.ode4j.ode.internal.cpp4j.Cstdio; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.IllegalFormatException; + /** * @@ -115,14 +118,12 @@ public static dMessageFunction dGetMessageHandler() { * @param ap objects */ public static void dError (int num, String msg, Object ... ap) { - // va_list ap; - // va_start (ap,msg); if (error_function != null) { - error_function.call (num,msg,ap); + error_function.call(num, msg, ap); } else { - logger.error("ODE Error " + num + ": " + msg, ap); + logger.error("ODE Error {}: {}", num, format(msg, ap)); } - throw new RuntimeException("#"+num + ": " + msg); + throw new RuntimeException("#" + num + ": " + format(msg, ap)); //exit (1); } @@ -134,16 +135,15 @@ public static void dError (int num, String msg, Object ... ap) { * @param ap objects */ public static void dDebug (int num, String msg, Object ... ap) { - // va_list ap; - // va_start (ap,msg); if (debug_function != null) { - debug_function.call (num,msg,ap); + debug_function.call(num, msg, ap); } else { - logger.debug("ODE INTERNAL ERROR " + " " + num + ": " + msg, ap); + if (logger.isDebugEnabled()) { + logger.debug("ODE INTERNAL ERROR {}: {}", num, format(msg, ap)); + } } - // *((char *)0) = 0; ... commit SEGVicide //abort(); - throw new RuntimeException("#"+num + ": " + String.format(msg, ap)); + throw new RuntimeException("#" + num + ": " + format(msg, ap)); } @@ -154,13 +154,28 @@ public static void dDebug (int num, String msg, Object ... ap) { * @param ap objects */ public static void dMessage (int num, String msg, Object ... ap) { - // va_list ap; - // va_start (ap,msg); - if (message_function!=null) { - message_function.call (num,msg,ap); + if (message_function != null) { + message_function.call(num, msg, ap); } else { //printMessage (num,"ODE Message",msg,ap); - logger.info("ODE Message " + num + ": " + msg, ap); + if (logger.isInfoEnabled()) { + logger.info("ODE Message {}: {}", num, format(msg, ap)); + } + } + } + + private static String format(String msg, Object... ap) { + try { + return String.format(msg, ap); + } catch (IllegalFormatException e) { + // Ensure that we have an error message even if the formatting is bad + StringBuilder sb = new StringBuilder(); + sb.append(msg).append(" "); + for (Object o: ap) { + sb.append(o).append(","); + } + sb.append(e.getMessage()); + return sb.toString(); } }