Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/frc/robot/logging/Loggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
}
5 changes: 3 additions & 2 deletions src/main/java/frc/robot/logging/LoggableCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
3 changes: 1 addition & 2 deletions src/main/java/frc/robot/logging/LoggableTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
104 changes: 99 additions & 5 deletions src/main/java/frc/robot/logging/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,32 @@
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 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<String, Supplier<String>> suppliers;
private Map<String, String> fields;
private List<Loggable> loggables;
/**
* Our NetworkTable instance.
* @deprecated Use SmartDashboard Sendables instead.
*/
@Deprecated(forRemoval = true)
private NetworkTable table;

public Logger() {
Expand Down Expand Up @@ -102,55 +118,130 @@ 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(ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS, 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<String> getter, Consumer<String> setter) {
if (hasAttribute(field)) {
System.out.printf(ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS, 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(ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS, 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(ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS, 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;
}

/**
* 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;
Expand Down Expand Up @@ -232,6 +323,9 @@ public void setup() {
* Calls the log method of all currently registered Loggables.
*/
public void log() {
for (Map.Entry<String, Supplier<String>> entry : this.suppliers.entrySet()) {
this.fields.put(entry.getKey(), entry.getValue().get());
}
for (Loggable l : loggables) {
l.log(this);
}
Expand Down