Skip to content

Commit

Permalink
Fix message formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaeschke committed Apr 28, 2024
1 parent 8be545d commit 7e7fcf5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 31 additions & 16 deletions core/src/main/java/org/ode4j/ode/internal/ErrorHdl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
*
Expand Down Expand Up @@ -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);
}

Expand All @@ -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));
}


Expand All @@ -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();
}
}

Expand Down

0 comments on commit 7e7fcf5

Please sign in to comment.