We are using the RS-232 port on the roboRIO, so we can’t use console out. I’m pretty sure I saw somewhere there’s a way to put errors in the error log in the driver station (such as the ones seen at http://wpilib.screenstepslive.com/s/3120/m/8559/l/96657-frc-driver-station-errors-warnings).
We will be using this just for logging exceptions that are encountered via a try-catch; not like a constant stream of output data.
I’m struggling to find the class/method to do it. Does anyone know what it is?
DriverStation.reportError() is your friend.
Unfortunately, we’ve found that reporterror does not give the stack trace to the error but to the line that reports the error Putting ex.printStackTrace() in our exception handlers is necessary.
We wanted the same thing, so after some digging around I have this code:
public static final void writeToDS(String message) {
final HALControlWord controlWord = FRCNetworkCommunicationsLibrary.HALGetControlWord();
if (controlWord.getDSAttached()) {
FRCNetworkCommunicationsLibrary.HALSetErrorData(message);
}
}
public static final void exceptionToDS(Throwable t) {
final StackTraceElement] stackTrace = t.getStackTrace();
final StringBuilder message = new StringBuilder();
final String separator = "===
";
final Throwable cause = t.getCause();
message.append("Exception of type ").append(t.getClass().getName()).append('
');
message.append("Message: ").append(t.getMessage()).append('
');
message.append(separator);
message.append(" ").append(stackTrace[0]).append('
');
for (int i = 1; i < stackTrace.length; i++) {
message.append(" ").append(stackTrace*).append('
');
}
if (cause != null) {
final StackTraceElement] causeTrace = cause.getStackTrace();
message.append(" ").append("Caused by ").append(cause.getClass().getName()).append('
');
message.append(" ").append("Because: ").append(cause.getMessage()).append('
');
message.append(" ").append(causeTrace[0]).append('
');
message.append(" ").append(causeTrace[2]).append('
');
message.append(" ").append(causeTrace[3]);
}
writeToDS(message.toString());
}
This will send messages directly to the DriverStation LCD.
This can either write to the DS or log a Stack Trace to the DS. If there is a cause it goes one-level deep and logs the cause as well.
It does make use of StringBuilder but this is entirely feasible just appending Strings.
StringBuilder was used just to maintain memory and speed.*