Change HAL messages to be formatted by the logger

This commit is contained in:
nathanrsxtn
2022-04-04 17:00:20 -06:00
parent d3d7157802
commit 2c75485018
2 changed files with 25 additions and 4 deletions
@@ -584,9 +584,7 @@ public class DriverStation {
logRecord.setThrown(throwable); logRecord.setThrown(throwable);
}); });
if (!frc4388.utility.AnsiLogging.halLoggerHandler.isLoggable(logRecord)) return; if (!frc4388.utility.AnsiLogging.halLoggerHandler.isLoggable(logRecord)) return;
// java.util.logging.Logger.getLogger(HAL.class.getSimpleName()).log(logRecord); frc4388.utility.AnsiLogging.halLoggerHandler.publish(logRecord);
String msg = frc4388.utility.AnsiLogging.halLoggerHandler.getFormatter().format(logRecord);
HAL.sendError(isError, code, false, msg.substring(0, msg.length() - 1), "", "", true);
} else { } else {
String locString; String locString;
if (stackTrace.length >= stackTraceFirst + 1) { if (stackTrace.length >= stackTraceFirst + 1) {
+24 -1
View File
@@ -4,9 +4,11 @@ import static org.fusesource.jansi.Ansi.ansi;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.Map; import java.util.Map;
@@ -25,6 +27,9 @@ import com.diffplug.common.base.Errors;
import org.fusesource.jansi.Ansi.Attribute; import org.fusesource.jansi.Ansi.Attribute;
import org.fusesource.jansi.Ansi.Color; import org.fusesource.jansi.Ansi.Color;
import edu.wpi.first.hal.HAL;
import org.fusesource.jansi.AnsiConsole; import org.fusesource.jansi.AnsiConsole;
import org.fusesource.jansi.AnsiPrintStream; import org.fusesource.jansi.AnsiPrintStream;
@@ -56,7 +61,7 @@ public class AnsiLogging {
// This is registering a plugin that will log Durian errors to the console using a logger. // This is registering a plugin that will log Durian errors to the console using a logger.
DurianPlugins.register(Errors.Plugins.Log.class, e -> Logger.getLogger(e.getStackTrace()[0].getClassName().substring(e.getStackTrace()[0].getClassName().lastIndexOf('.') + 1)).log(Level.SEVERE, e, e::getLocalizedMessage)); DurianPlugins.register(Errors.Plugins.Log.class, e -> Logger.getLogger(e.getStackTrace()[0].getClassName().substring(e.getStackTrace()[0].getClassName().lastIndexOf('.') + 1)).log(Level.SEVERE, e, e::getLocalizedMessage));
// Store the handler for HAL to use when sending errors to DriverStation. // Store the handler for HAL to use when sending errors to DriverStation.
halLoggerHandler = new LoggingAnsiConsoleHandler(); halLoggerHandler = new LoggingAnsiConsoleHandler(new HalOutputStream());
} catch (IOException exception) { } catch (IOException exception) {
exception.printStackTrace(AnsiConsole.sysErr()); exception.printStackTrace(AnsiConsole.sysErr());
} }
@@ -70,6 +75,10 @@ public class AnsiLogging {
super(ANSI_CONSOLE_STREAM, new LoggingAnsiFormatter()); super(ANSI_CONSOLE_STREAM, new LoggingAnsiFormatter());
setLevel(LEVEL); setLevel(LEVEL);
} }
public LoggingAnsiConsoleHandler(OutputStream out) {
super(out, new LoggingAnsiFormatter());
setLevel(LEVEL);
}
private static class LoggingAnsiFormatter extends Formatter { private static class LoggingAnsiFormatter extends Formatter {
private static final ZoneId ZONE_ID = ZoneId.systemDefault(); private static final ZoneId ZONE_ID = ZoneId.systemDefault();
@@ -129,4 +138,18 @@ public class AnsiLogging {
} }
}, true); }, true);
} }
private static class HalOutputStream extends ByteArrayOutputStream {
@Override
public synchronized void write(int b) {
if (b == '\n') flush();
else super.write(b);
}
@Override
public void flush() {
String s = toString();
HAL.sendError(false, 0, false, s.substring(0, s.length() - 1), "", "", true);
reset();
}
}
} }