Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
8 changes: 5 additions & 3 deletions src/main/java/frc/robot/logging/Loggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
/** A class for a loggable subsystem. */
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);

/**
* 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);
}
3 changes: 1 addition & 2 deletions src/main/java/frc/robot/logging/LoggableCompressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ public LoggableCompressor(PneumaticsModuleType 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 @@ -13,12 +13,11 @@ public LoggablePowerDistribution(int module, ModuleType 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());
}

}
112 changes: 103 additions & 9 deletions src/main/java/frc/robot/logging/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,33 @@
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 @@ -106,59 +126,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 @@ -245,6 +336,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