Skip to content

Commit

Permalink
Add ability to enable and disable logging for MonitoredSubsystem and …
Browse files Browse the repository at this point in the history
…for each individual Monitor
  • Loading branch information
aidnem committed Nov 25, 2024
1 parent b216d81 commit 1e4ae8c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
54 changes: 52 additions & 2 deletions monitors/src/main/java/coppercore/monitors/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ public class Monitor {
/** Function to call when the fault happens */
Runnable faultCallback;

/**
* Should the monitor be logged by the MonitoredSubsystem?
*
* <p>Changing this value doesn't change the behavior of the monitor, it exists only to tell
* MonitoredSubsystem whether or not to log the monitor.
*/
boolean loggingEnabled;

/**
* Timestamp when the monitor was first triggered, or -1.0 if the monitor has been triggered for
* less than 1 loop.
Expand Down Expand Up @@ -53,20 +61,25 @@ public class Monitor {
* fault is triggered.
* @param faultCallback a function called on every periodic loop while the monitor is in a
* faulted state.
* @param loggingEnabled whether or not the monitor should be logged. This value is only used by
* MonitoredSubsystem to enable or disable logging for each monitor.
* @see MonitorBuilder
*/
public Monitor(
String name,
boolean sticky,
BooleanSupplier isStateValid,
double timeToFault,
Runnable faultCallback) {
Runnable faultCallback,
boolean loggingEnabled) {
this.name = name;
this.sticky = sticky;
this.timeToFault = timeToFault;
this.isStateValid = isStateValid;

this.faultCallback = faultCallback;

this.loggingEnabled = loggingEnabled;
}

/**
Expand Down Expand Up @@ -151,6 +164,26 @@ public String getName() {
return name;
}

/**
* Set whether or not the monitor should be logged.
*
* <p>Changing this value doesn't change the behavior of the monitor, it exists only to tell
* MonitoredSubsystem whether or not to log the monitor.
*/
public void setLoggingEnabled(boolean loggingEnabled) {
this.loggingEnabled = loggingEnabled;
}

/**
* Get whether or not the monitor should be logged.
*
* <p>Changing this value doesn't change the behavior of the monitor, it exists only to tell
* MonitoredSubsystem whether or not to log the monitor.
*/
public boolean getLoggingEnabled() {
return loggingEnabled;
}

/**
* This class is meant to build a fault monitor. Create a builder, then call withName,
* withStickyness, withTimeToFault, and withIsStateValid, and withFaultCallback to configure its
Expand All @@ -164,6 +197,7 @@ public static class MonitorBuilder {
BooleanSupplier
isStateValid; // Supplier with which to check whether the value is acceptable
Runnable faultCallback; // Function to call when the fault happens
boolean loggingEnabled = true; // Whether or not to log the monitor. Defaults to true.

/**
* Sets the name of the monitor. This name will be used when the monitor is logged by
Expand Down Expand Up @@ -227,14 +261,30 @@ public MonitorBuilder withFaultCallback(Runnable faultCallback) {
return this;
}

/**
* Sets whether or not the monitor should be logged.
*
* <p>This value is only used to tell the MonitoredSubsystem whether or not to log each
* monitor. This value defaults to true unless withLoggingEnabled(false) is called on the
* builder!
*
* @param loggingEnabled whether or not the monitor should be logged
* @return the monitor builder, so that successive builder calls can be chained.
*/
public MonitorBuilder withLoggingEnabled(boolean loggingEnabled) {
this.loggingEnabled = loggingEnabled;
return this;
}

/**
* Instantiates a monitor and returns it. This method should be called after all of the
* fields of the monitor are configured using with[Field] methods.
*
* @return a monitor with the fields set by the builder.
*/
public Monitor build() {
return new Monitor(name, sticky, isStateValid, timeToFault, faultCallback);
return new Monitor(
name, sticky, isStateValid, timeToFault, faultCallback, loggingEnabled);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
public class MonitoredSubsystem extends SubsystemBase {
private List<Monitor> registeredMonitors = new ArrayList<Monitor>();

private boolean loggingEnabled = true;

public void addMonitor(Monitor monitor) {
registeredMonitors.add(monitor);
}
Expand Down Expand Up @@ -38,10 +40,24 @@ private void runMonitors() {
monitor -> {
monitor.periodic(Timer.getFPGATimestamp());

Logger.recordOutput(
"monitors/" + monitor.getName() + "/triggered", monitor.isTriggered());
Logger.recordOutput(
"monitors/" + monitor.getName() + "/faulted", monitor.isFaulted());
if (loggingEnabled && monitor.getLoggingEnabled()) {
Logger.recordOutput(
"monitors/" + monitor.getName() + "/triggered",
monitor.isTriggered());
Logger.recordOutput(
"monitors/" + monitor.getName() + "/faulted", monitor.isFaulted());
}
});
}

/**
* Set whether or not the monitored subsystem should log its monitors. This is enabled by
* default, but can be disabled if there are RAM issues stemming from too many strings in
* logging.
*
* @param loggingEnabled
*/
public void setLoggingEnabled(boolean loggingEnabled) {
this.loggingEnabled = loggingEnabled;
}
}

0 comments on commit 1e4ae8c

Please sign in to comment.