Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Logger to start transition to Sendables #11

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 4 additions & 4 deletions src/main/java/frc/robot/logging/Loggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
/** 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
*/
public abstract void setupLogging(Logger logger);

/**
* Logs data in the given Logger object.
*
* @param logger Logger class to log data to
* @deprecated See {@link Logger#addAttribute(String, Supplier, Consumer)}.
*/
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 @@

@Override
public void setupLogging(Logger logger) {
logger.addAttribute("PH/pressure");
logger.addAttribute("PH/pressure", this::getPressure, null);

Check warning on line 17 in src/main/java/frc/robot/logging/LoggableCompressor.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/LoggableCompressor.java#L17

Added line #L17 was not covered by tests
}

@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 @@

@Override
public void setupLogging(Logger logger) {
logger.addAttribute("PDH/voltage");
logger.addAttribute("PDH/voltage", this::getVoltage, null);

Check warning on line 16 in src/main/java/frc/robot/logging/LoggablePowerDistribution.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/LoggablePowerDistribution.java#L16

Added line #L16 was not covered by tests
}

@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 @@

@Override
public void setupLogging(Logger logger) {
logger.addAttribute("Time");
logger.addAttribute("Time", this::get, null);

Check warning on line 9 in src/main/java/frc/robot/logging/LoggableTimer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/LoggableTimer.java#L9

Added line #L9 was not covered by tests
}

@Override
public void log(Logger logger) {
logger.log("Time", this.get());
}

}
108 changes: 100 additions & 8 deletions src/main/java/frc/robot/logging/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,13 +15,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;

/** 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 @@ -109,16 +126,86 @@

/**
* 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);
TheBitEffect marked this conversation as resolved.
Show resolved Hide resolved
return false; // We already have this attribute

Check warning on line 135 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L134-L135

Added lines #L134 - L135 were not covered by tests
}
fields.put(field, "");

Check warning on line 137 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L137

Added line #L137 was not covered by tests

return true;

Check warning on line 139 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L139

Added line #L139 was not covered by tests
}

/**
* 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);
TheBitEffect marked this conversation as resolved.
Show resolved Hide resolved
return false; // We already have this attribute

Check warning on line 152 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L151-L152

Added lines #L151 - L152 were not covered by tests
}
SmartDashboard.putData((SendableBuilder builder) -> {
builder.setSmartDashboardType(TABLE_PREFIX);

Check warning on line 155 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L154-L155

Added lines #L154 - L155 were not covered by tests
builder.addStringProperty(field, getter != null ? getter::get : null,
setter != null ? setter::accept : null);
});

Check warning on line 158 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L158

Added line #L158 was not covered by tests

suppliers.put(field, getter::get);
fields.put(field, "");

Check warning on line 161 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L160-L161

Added lines #L160 - L161 were not covered by tests

return true;

Check warning on line 163 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L163

Added line #L163 was not covered by tests
}

/**
* 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) {
TheBitEffect marked this conversation as resolved.
Show resolved Hide resolved
if (hasAttribute(field)) {
System.out.printf(ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS, field);

Check warning on line 175 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L175

Added line #L175 was not covered by tests
TheBitEffect marked this conversation as resolved.
Show resolved Hide resolved
return false; // We already have this attribute
}
SmartDashboard.putData((SendableBuilder builder) -> {
builder.setSmartDashboardType(TABLE_PREFIX);

Check warning on line 179 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L178-L179

Added lines #L178 - L179 were not covered by tests
builder.addDoubleProperty(field, getter != null ? getter::getAsDouble : null,
setter != null ? setter::accept : null);
});

Check warning on line 182 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L182

Added line #L182 was not covered by tests

suppliers.put(field, () -> Double.toString(getter.getAsDouble()));
fields.put(field, "");

Check warning on line 185 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L184-L185

Added lines #L184 - L185 were not covered by tests

return true;

Check warning on line 187 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L187

Added line #L187 was not covered by tests
}

/**
* 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) {
TheBitEffect marked this conversation as resolved.
Show resolved Hide resolved
if (hasAttribute(field)) {
System.out.printf(ERROR_MSG_ATTRIBUTE_ALREADY_EXISTS, field);
TheBitEffect marked this conversation as resolved.
Show resolved Hide resolved
return false; // We already have this attribute

Check warning on line 200 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L199-L200

Added lines #L199 - L200 were not covered by tests
}
SmartDashboard.putData((SendableBuilder builder) -> {
builder.setSmartDashboardType(TABLE_PREFIX);

Check warning on line 203 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L202-L203

Added lines #L202 - L203 were not covered by tests
builder.addBooleanProperty(field, getter != null ? getter::getAsBoolean : null,
setter != null ? setter::accept : null);
});

Check warning on line 206 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L206

Added line #L206 was not covered by tests

suppliers.put(field, () -> Boolean.toString(getter.getAsBoolean()));

Check warning on line 208 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L208

Added line #L208 was not covered by tests
fields.put(field, "");

return true;
Expand All @@ -134,11 +221,12 @@

/**
* Logs data to the Logger.
*
* @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) {
if (!hasAttribute(field)) {
return false;
Expand All @@ -150,28 +238,29 @@

/**
* Logs data to the Logger
*
* @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) {
if (!hasAttribute(field)) {
return false;
}

table.getEntry(field).setString(data);
fields.put(field, data);
return true;
}

/**
* Logs data to the Logger
*
* @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) {
if (!hasAttribute(field)) {
return false;
Expand Down Expand Up @@ -250,6 +339,9 @@
* 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());
}

Check warning on line 344 in src/main/java/frc/robot/logging/Logger.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/frc/robot/logging/Logger.java#L343-L344

Added lines #L343 - L344 were not covered by tests
for (Loggable l : loggables) {
l.log(this);
}
Expand Down