From 4ec6360fee6b0ce46a6c17d925081050d48dc2e5 Mon Sep 17 00:00:00 2001 From: theblindbandet Date: Mon, 24 Jan 2022 14:45:26 -0500 Subject: [PATCH 1/9] Change Logger to start transition to Sendables --- src/main/java/frc/robot/logging/Loggable.java | 6 +- src/main/java/frc/robot/logging/Logger.java | 99 ++++++++++++++++++- 2 files changed, 98 insertions(+), 7 deletions(-) diff --git a/src/main/java/frc/robot/logging/Loggable.java b/src/main/java/frc/robot/logging/Loggable.java index 6301f02..da79c47 100644 --- a/src/main/java/frc/robot/logging/Loggable.java +++ b/src/main/java/frc/robot/logging/Loggable.java @@ -3,7 +3,7 @@ /** A class for a loggable subsystem. */ public interface Loggable { /** - * Sets up all the keys in the given Logger object. + * Sets up all the keys, getters, and setters in the given Logger object. * * @param logger Logger class to setup keys in */ @@ -12,7 +12,9 @@ public interface Loggable { /** * Logs data in the given Logger object. * + * @deprecated See {@link Logger#addAttribute(String, Supplier, Consumer)}. * @param logger Logger class to log data to */ - public abstract void log(Logger logger); + @Deprecated(forRemoval = true) + public void log(Logger logger); } diff --git a/src/main/java/frc/robot/logging/Logger.java b/src/main/java/frc/robot/logging/Logger.java index 4e7fd5b..1055d24 100644 --- a/src/main/java/frc/robot/logging/Logger.java +++ b/src/main/java/frc/robot/logging/Logger.java @@ -9,16 +9,27 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.function.BooleanSupplier; +import java.util.function.Consumer; +import java.util.function.DoubleConsumer; +import java.util.function.DoubleSupplier; +import java.util.function.Supplier; import edu.wpi.first.networktables.NetworkTable; import edu.wpi.first.networktables.NetworkTableInstance; +import edu.wpi.first.util.function.BooleanConsumer; +import edu.wpi.first.util.sendable.SendableBuilder; +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; /** Manages NetworkTable and file logging. */ public class Logger { + private static final String TABLE_PREFIX = "Logging"; private String filename; private BufferedWriter log = null; + private Map> suppliers; private Map fields; private List loggables; + @Deprecated(forRemoval = true) private NetworkTable table; public Logger() { @@ -102,15 +113,86 @@ public boolean hasAttribute(String name) { /** * Adds an attribute to the logger. - * @param field - * @return + * @param field The key to add to the logger + * @return Whether the attribute was added */ public boolean addAttribute(String field) { if (hasAttribute(field)) { - // TODO: Output warning + System.out.printf("Attribute \"%s\" already exists! Skipping!%n", field); return false; // We already have this attribute } + fields.put(field, ""); + + return true; + } + + /** + * Adds an attribute to the logger. + * @param field The key to add to the logger + * @param getter The function to get the value of the attribute + * @param setter The function to set the value of the attribute + * @return Whether the attribute was added + */ + public boolean addAttribute(String field, Supplier getter, Consumer setter) { + if (hasAttribute(field)) { + System.out.printf("Attribute \"%s\" already exists! Skipping!%n", field); + return false; // We already have this attribute + } + SmartDashboard.putData((SendableBuilder builder) -> { + builder.setSmartDashboardType(TABLE_PREFIX); + builder.addStringProperty(field, getter != null ? getter::get : null, + setter != null ? setter::accept : null); + }); + + suppliers.put(field, getter::get); + fields.put(field, ""); + + return true; + } + + /** + * Adds an attribute to the logger. + * @param field The key to add to the logger + * @param getter The function to get the value of the attribute + * @param setter The function to set the value of the attribute + * @return Whether the attribute was added + */ + public boolean addAttribute(String field, DoubleSupplier getter, DoubleConsumer setter) { + if (hasAttribute(field)) { + System.out.printf("Attribute \"%s\" already exists! Skipping!%n", field); + return false; // We already have this attribute + } + SmartDashboard.putData((SendableBuilder builder) -> { + builder.setSmartDashboardType(TABLE_PREFIX); + builder.addDoubleProperty(field, getter != null ? getter::getAsDouble : null, + setter != null ? setter::accept : null); + }); + + suppliers.put(field, () -> Double.toString(getter.getAsDouble())); + fields.put(field, ""); + + return true; + } + /** + * Adds an attribute to the logger. + * @param field The key to add to the logger + * @param getter The function to get the value of the attribute + * @param setter The function to set the value of the attribute + * @return Whether the attribute was added + */ + public boolean addAttribute(String field, BooleanSupplier getter, BooleanConsumer setter) { + if (hasAttribute(field)) { + System.out.printf("Attribute \"%s\" already exists! Skipping!%n", field); + return false; // We already have this attribute + } + SmartDashboard.putData((SendableBuilder builder) -> { + builder.setSmartDashboardType(TABLE_PREFIX); + builder.addBooleanProperty(field, getter != null ? getter::getAsBoolean : null, + setter != null ? setter::accept : null); + }); + + suppliers.put(field, () -> Boolean.toString(getter.getAsBoolean())); fields.put(field, ""); return true; @@ -118,39 +200,43 @@ public boolean addAttribute(String field) { /** * Logs data to the Logger. + * @deprecated Use {@link #addAttribute(String, DoubleSupplier, DoubleConsumer)} instead * @param field Key being logged * @param data Number data to log * @return Whether the operation succeeded */ + @Deprecated(forRemoval = true) public boolean log(String field, double d) { if (!hasAttribute(field)) return false; - table.getEntry(field).setDouble(d); fields.put(field, Double.toString(d)); return true; } /** * Logs data to the Logger + * @deprecated Use {@link #addAttribute(String, Supplier, Consumer)} instead * @param field key being logged * @param data String data to log * @return whether the operation succeeded */ + @Deprecated(forRemoval = true) public boolean log(String field, String data) { if (!hasAttribute(field)) return false; - table.getEntry(field).setString(data); fields.put(field, data); return true; } /** * Logs data to the Logger + * @deprecated Use {@link #addAttribute(String, Supplier, Consumer)} instead * @param field key being logged * @param data data to log * @return whether the operation succeeded */ + @Deprecated(forRemoval = true) public boolean log(String field, Object data) { if (!hasAttribute(field)) return false; @@ -232,6 +318,9 @@ public void setup() { * Calls the log method of all currently registered Loggables. */ public void log() { + for (Map.Entry> entry : this.suppliers.entrySet()) { + this.fields.put(entry.getKey(), entry.getValue().get()); + } for (Loggable l : loggables) { l.log(this); } From e0944101d78c8f245eb383f5c45e3a6a50e2c685 Mon Sep 17 00:00:00 2001 From: theblindbandet Date: Mon, 24 Jan 2022 15:00:37 -0500 Subject: [PATCH 2/9] Fix deprecated JavaDoc comment link --- src/main/java/frc/robot/logging/Logger.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/frc/robot/logging/Logger.java b/src/main/java/frc/robot/logging/Logger.java index 1055d24..0cf321c 100644 --- a/src/main/java/frc/robot/logging/Logger.java +++ b/src/main/java/frc/robot/logging/Logger.java @@ -215,7 +215,7 @@ public boolean log(String field, double d) { /** * Logs data to the Logger - * @deprecated Use {@link #addAttribute(String, Supplier, Consumer)} instead + * @deprecated Use {@link #addAttribute(String, Supplier, Consumer)} instead * @param field key being logged * @param data String data to log * @return whether the operation succeeded From 9b6fa4dd361c16466a0b537ecba9dba74b55d90d Mon Sep 17 00:00:00 2001 From: theblindbandet Date: Mon, 24 Jan 2022 15:02:21 -0500 Subject: [PATCH 3/9] Fix other deprecated JavaDoc comment link --- src/main/java/frc/robot/logging/Loggable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/frc/robot/logging/Loggable.java b/src/main/java/frc/robot/logging/Loggable.java index da79c47..90ba0ab 100644 --- a/src/main/java/frc/robot/logging/Loggable.java +++ b/src/main/java/frc/robot/logging/Loggable.java @@ -12,7 +12,7 @@ public interface Loggable { /** * Logs data in the given Logger object. * - * @deprecated See {@link Logger#addAttribute(String, Supplier, Consumer)}. + * @deprecated See {@link Logger#addAttribute(String, Supplier, Consumer)}. * @param logger Logger class to log data to */ @Deprecated(forRemoval = true) From 1845eab29ec8d7521f1c7cf12f9601c1778268b7 Mon Sep 17 00:00:00 2001 From: theblindbandet Date: Mon, 24 Jan 2022 15:11:47 -0500 Subject: [PATCH 4/9] Convert the base Loggables to the new format --- src/main/java/frc/robot/logging/LoggableCompressor.java | 5 +++-- .../java/frc/robot/logging/LoggablePowerDistribution.java | 5 ++--- src/main/java/frc/robot/logging/LoggableTimer.java | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/frc/robot/logging/LoggableCompressor.java b/src/main/java/frc/robot/logging/LoggableCompressor.java index d0496b8..1821d70 100644 --- a/src/main/java/frc/robot/logging/LoggableCompressor.java +++ b/src/main/java/frc/robot/logging/LoggableCompressor.java @@ -7,16 +7,17 @@ public class LoggableCompressor extends Compressor implements Loggable { public LoggableCompressor(int module, PneumaticsModuleType moduleType) { super(module, moduleType); } + public LoggableCompressor(PneumaticsModuleType moduleType) { super(moduleType); } @Override public void setupLogging(Logger logger) { - logger.addAttribute("PH/pressure"); + logger.addAttribute("PH/pressure", this::getPressure, null); } + @Override public void log(Logger logger) { - logger.log("PH/pressure", this.getPressure()); } } diff --git a/src/main/java/frc/robot/logging/LoggablePowerDistribution.java b/src/main/java/frc/robot/logging/LoggablePowerDistribution.java index edd6319..2301fe1 100644 --- a/src/main/java/frc/robot/logging/LoggablePowerDistribution.java +++ b/src/main/java/frc/robot/logging/LoggablePowerDistribution.java @@ -6,19 +6,18 @@ public class LoggablePowerDistribution extends PowerDistribution implements Logg public LoggablePowerDistribution() { super(); } + public LoggablePowerDistribution(int module, ModuleType moduleType) { super(module, moduleType); } - @Override public void setupLogging(Logger logger) { - logger.addAttribute("PDH/voltage"); + logger.addAttribute("PDH/voltage", this::getVoltage, null); } @Override public void log(Logger logger) { - logger.log("PDH/voltage", this.getVoltage()); } } diff --git a/src/main/java/frc/robot/logging/LoggableTimer.java b/src/main/java/frc/robot/logging/LoggableTimer.java index 0cb8d81..536fbb7 100644 --- a/src/main/java/frc/robot/logging/LoggableTimer.java +++ b/src/main/java/frc/robot/logging/LoggableTimer.java @@ -6,12 +6,11 @@ public class LoggableTimer extends Timer implements Loggable { @Override public void setupLogging(Logger logger) { - logger.addAttribute("Time"); + logger.addAttribute("Time", this::get, null); } @Override public void log(Logger logger) { - logger.log("Time", this.get()); } } From 7496049899a6482627bf2209459ce119fe683635 Mon Sep 17 00:00:00 2001 From: theblindbandet Date: Mon, 24 Jan 2022 18:09:20 -0500 Subject: [PATCH 5/9] Give CodeCoverage less things to complain about --- src/main/java/frc/robot/logging/Logger.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/frc/robot/logging/Logger.java b/src/main/java/frc/robot/logging/Logger.java index 0cf321c..8b51624 100644 --- a/src/main/java/frc/robot/logging/Logger.java +++ b/src/main/java/frc/robot/logging/Logger.java @@ -23,12 +23,17 @@ /** Manages NetworkTable and file logging. */ public class Logger { + private static final String ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS = "Attribute \"%s\" already exists! Skipping!%n"; private static final String TABLE_PREFIX = "Logging"; private String filename; private BufferedWriter log = null; private Map> suppliers; private Map fields; private List loggables; + /** + * Our NetworkTable instance. + * @deprecated Use SmartDashboard Sendables instead. + */ @Deprecated(forRemoval = true) private NetworkTable table; @@ -118,7 +123,7 @@ public boolean hasAttribute(String name) { */ public boolean addAttribute(String field) { if (hasAttribute(field)) { - System.out.printf("Attribute \"%s\" already exists! Skipping!%n", field); + System.out.printf(ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS, field); return false; // We already have this attribute } fields.put(field, ""); @@ -135,7 +140,7 @@ public boolean addAttribute(String field) { */ public boolean addAttribute(String field, Supplier getter, Consumer setter) { if (hasAttribute(field)) { - System.out.printf("Attribute \"%s\" already exists! Skipping!%n", field); + System.out.printf(ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS, field); return false; // We already have this attribute } SmartDashboard.putData((SendableBuilder builder) -> { @@ -159,7 +164,7 @@ public boolean addAttribute(String field, Supplier getter, Consumer { @@ -183,7 +188,7 @@ public boolean addAttribute(String field, DoubleSupplier getter, DoubleConsumer */ public boolean addAttribute(String field, BooleanSupplier getter, BooleanConsumer setter) { if (hasAttribute(field)) { - System.out.printf("Attribute \"%s\" already exists! Skipping!%n", field); + System.out.printf(ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS, field); return false; // We already have this attribute } SmartDashboard.putData((SendableBuilder builder) -> { From 638192b7560bd83c025e633413e55e98b340d555 Mon Sep 17 00:00:00 2001 From: "[INSERT_NAME_HERE]" Date: Tue, 1 Feb 2022 15:07:55 +0000 Subject: [PATCH 6/9] Fix variable name in log(String, double) Sent from my iPad --- src/main/java/frc/robot/logging/Logger.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/java/frc/robot/logging/Logger.java b/src/main/java/frc/robot/logging/Logger.java index 9fc4cc5..d01b2b5 100644 --- a/src/main/java/frc/robot/logging/Logger.java +++ b/src/main/java/frc/robot/logging/Logger.java @@ -2,6 +2,9 @@ import edu.wpi.first.networktables.NetworkTable; import edu.wpi.first.networktables.NetworkTableInstance; +import edu.wpi.first.util.function.BooleanConsumer; +import edu.wpi.first.util.sendable.SendableBuilder; +import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; import java.io.BufferedWriter; import java.io.File; @@ -18,16 +21,10 @@ import java.util.function.DoubleSupplier; import java.util.function.Supplier; -import edu.wpi.first.networktables.NetworkTable; -import edu.wpi.first.networktables.NetworkTableInstance; -import edu.wpi.first.util.function.BooleanConsumer; -import edu.wpi.first.util.sendable.SendableBuilder; -import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; - - /** Manages NetworkTable and file logging. */ public class Logger { - private static final String ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS = "Attribute \"%s\" already exists! Skipping!%n"; + private static final String ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS = + "Attribute \"%s\" already exists! Skipping!%n"; private static final String TABLE_PREFIX = "Logging"; private String filename; private BufferedWriter log = null; @@ -230,12 +227,12 @@ public void setup() { * @return Whether the operation succeeded */ @Deprecated(forRemoval = true) - public boolean log(String field, double d) { + public boolean log(String field, double data) { if (!hasAttribute(field)) { return false; } table.getEntry(field).setDouble(data); - fields.put(field, Double.toString(d)); + fields.put(field, Double.toString(data)); return true; } From 6eed5d23c39cbacf4e2ff1ff87c9d8a28da3a586 Mon Sep 17 00:00:00 2001 From: theblindbandet Date: Tue, 1 Feb 2022 19:03:19 -0500 Subject: [PATCH 7/9] Move all deprecated below return in Javadocs --- src/main/java/frc/robot/logging/Logger.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/frc/robot/logging/Logger.java b/src/main/java/frc/robot/logging/Logger.java index d01b2b5..c54cca4 100644 --- a/src/main/java/frc/robot/logging/Logger.java +++ b/src/main/java/frc/robot/logging/Logger.java @@ -221,10 +221,10 @@ public void setup() { /** * Logs data to the Logger. - * @deprecated Use {@link #addAttribute(String, DoubleSupplier, DoubleConsumer)} instead * @param field Key being logged * @param data Number data to log * @return Whether the operation succeeded + * @deprecated Use {@link #addAttribute(String, DoubleSupplier, DoubleConsumer)} instead */ @Deprecated(forRemoval = true) public boolean log(String field, double data) { @@ -238,10 +238,10 @@ public boolean log(String field, double data) { /** * Logs data to the Logger - * @deprecated Use {@link #addAttribute(String, Supplier, Consumer)} instead * @param field key being logged * @param data String data to log * @return whether the operation succeeded + * @deprecated Use {@link #addAttribute(String, Supplier, Consumer)} instead */ @Deprecated(forRemoval = true) public boolean log(String field, String data) { @@ -255,10 +255,10 @@ public boolean log(String field, String data) { /** * Logs data to the Logger - * @deprecated Use {@link #addAttribute(String, Supplier, Consumer)} instead * @param field key being logged * @param data to log * @return whether the operation succeeded + * @deprecated Use {@link #addAttribute(String, Supplier, Consumer)} instead */ @Deprecated(forRemoval = true) public boolean log(String field, Object data) { From 20a4ad7055afec19346f13404f30a9623a6ad558 Mon Sep 17 00:00:00 2001 From: theblindbandet Date: Tue, 1 Feb 2022 19:12:13 -0500 Subject: [PATCH 8/9] =?UTF-8?q?=E2=80=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/frc/robot/logging/Loggable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/frc/robot/logging/Loggable.java b/src/main/java/frc/robot/logging/Loggable.java index 0565c28..12a13f1 100644 --- a/src/main/java/frc/robot/logging/Loggable.java +++ b/src/main/java/frc/robot/logging/Loggable.java @@ -12,8 +12,8 @@ public interface Loggable { /** * Logs data in the given Logger object. - * @deprecated See {@link Logger#addAttribute(String, Supplier, Consumer)}. * @param logger Logger class to log data to + * @deprecated See {@link Logger#addAttribute(String, Supplier, Consumer)}. */ @Deprecated(forRemoval = true) public void log(Logger logger); From 3d912375cd6efe7e0be579f74541f325600abae0 Mon Sep 17 00:00:00 2001 From: theblindbandet Date: Sat, 5 Feb 2022 11:07:07 -0500 Subject: [PATCH 9/9] Fix bad merge (likely by me) in Loggable --- src/main/java/frc/robot/logging/Loggable.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/frc/robot/logging/Loggable.java b/src/main/java/frc/robot/logging/Loggable.java index 12a13f1..8c5b84d 100644 --- a/src/main/java/frc/robot/logging/Loggable.java +++ b/src/main/java/frc/robot/logging/Loggable.java @@ -4,8 +4,6 @@ public interface Loggable { /** * Sets up all the keys, getters, and setters in the given Logger object. - * - * Sets up all the keys in the given Logger object. * @param logger Logger class to setup keys in */ public abstract void setupLogging(Logger logger);