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

[epilogue] Backend refactoring and interface fixes #7548

Open
wants to merge 4 commits into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,19 @@ private Set<TypeElement> getLoggedTypes(RoundEnvironment roundEnv) {
annotatedElements.stream()
.filter(e -> e instanceof TypeElement)
.map(e -> (TypeElement) e),
// 2. All type elements containing a field or method with the @Logged annotation
// 2. All type elements containing a field or method with the @Logged annotation,
// or interfaces which have an implementing class with an @Logged annotation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be better to change methodsToLog in LoggerGenerator to look at superclass and interface methods. This has the side effect of making a plain interface loggable if it happens to have a loggable implementation, which is completely invisible to anyone looking at the interface declaration.

Copy link
Contributor Author

@Daniel1464 Daniel1464 Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was the intention. Currently, the "logger file" generated by epilogue for said interface is blank(so there's no side effects). The idea is that if you put @Logged on a IO impl but not the IO, then fields on said impl will still be logged(while not logging any default members of the IO interface)

annotatedElements.stream()
.filter(e -> e instanceof VariableElement || e instanceof ExecutableElement)
.map(Element::getEnclosingElement)
.filter(e -> e instanceof TypeElement)
.map(e -> (TypeElement) e))
.flatMap(
e ->
Stream.concat(
Stream.of(e),
e.getInterfaces().stream()
.map(i -> (TypeElement) ((DeclaredType) i).asElement())))
.sorted(Comparator.comparing(e -> e.getSimpleName().toString()))
.collect(
Collectors.toCollection(LinkedHashSet::new)); // Collect to a set to avoid duplicates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,61 @@ public void update(EpilogueBackend backend, Example object) {
assertLoggerGenerates(source, expectedGeneratedSource);
}

@Test
void optInInheritance() {
String source =
"""
package edu.wpi.first.epilogue;

class Example {
@Logged public ABC inst = new Base2();
}

class Base implements ABC {
double x;
}

class Base2 implements ABC {
@Logged double x;
}

interface ABC {
default double a() { return 2.0; }
}
Comment on lines +120 to +122
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be loggable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to still generate a logger for the interface ABC because it has classes that inherit from it(that can be logged)

""";

String expectedGeneratedSource =
"""
package edu.wpi.first.epilogue;

import edu.wpi.first.epilogue.Logged;
import edu.wpi.first.epilogue.Epilogue;
import edu.wpi.first.epilogue.logging.ClassSpecificLogger;
import edu.wpi.first.epilogue.logging.EpilogueBackend;

public class ExampleLogger extends ClassSpecificLogger<Example> {
public ExampleLogger() {
super(Example.class);
}

@Override
public void update(EpilogueBackend backend, Example object) {
if (Epilogue.shouldLog(Logged.Importance.DEBUG)) {
var $$inst = object.inst;
if ($$inst instanceof edu.wpi.first.epilogue.Base2 edu_wpi_first_epilogue_Base2) {
Epilogue.base2Logger.tryUpdate(backend.getNested("inst"), edu_wpi_first_epilogue_Base2, Epilogue.getConfig().errorHandler);
} else {
// Base type edu.wpi.first.epilogue.ABC
Epilogue.abcLogger.tryUpdate(backend.getNested("inst"), $$inst, Epilogue.getConfig().errorHandler);
};
}
}
}
""";

assertLoggerGenerates(source, expectedGeneratedSource);
}

@Test
void optInMethods() {
String source =
Expand Down
1 change: 1 addition & 0 deletions epilogue-runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ dependencies {
api(project(':ntcore'))
api(project(':wpiutil'))
api(project(':wpiunits'))
api(project(':wpilibj'))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
package edu.wpi.first.epilogue;

import edu.wpi.first.epilogue.logging.EpilogueBackend;
import edu.wpi.first.epilogue.logging.NTEpilogueBackend;
import edu.wpi.first.epilogue.logging.NTBackend;
import edu.wpi.first.epilogue.logging.errors.ErrorHandler;
import edu.wpi.first.epilogue.logging.errors.ErrorPrinter;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.units.measure.Time;

/**
Expand All @@ -22,7 +21,7 @@ public class EpilogueConfiguration {
* NetworkTables. NetworkTable data can be mirrored to a log file on disk by calling {@code
* DataLogManager.start()} in your {@code robotInit} method.
*/
public EpilogueBackend backend = new NTEpilogueBackend(NetworkTableInstance.getDefault());
public EpilogueBackend backend = new NTBackend();

/**
* The period Epilogue will log at. By default this is the period that the robot runs at. This is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import edu.wpi.first.util.datalog.StructArrayLogEntry;
import edu.wpi.first.util.datalog.StructLogEntry;
import edu.wpi.first.util.struct.Struct;
import edu.wpi.first.wpilibj.DataLogManager;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
Expand All @@ -41,6 +42,11 @@ public FileBackend(DataLog dataLog) {
this.m_dataLog = requireNonNullParam(dataLog, "dataLog", "FileBackend");
}

/** Creates a new file-based backend. */
public FileBackend() {
this(DataLogManager.getLog());
}

@Override
public EpilogueBackend getNested(String path) {
return m_subLoggers.computeIfAbsent(path, k -> new NestedBackend(k, this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import edu.wpi.first.util.struct.Struct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BooleanSupplier;

/**
* A backend implementation that sends data over network tables. Be careful when using this, since
* sending too much data may cause bandwidth or CPU starvation.
*/
public class NTEpilogueBackend implements EpilogueBackend {
public class NTBackend implements EpilogueBackend {
private final NetworkTableInstance m_nt;
private BooleanSupplier m_disabledSupplier;

private final Map<String, Publisher> m_publishers = new HashMap<>();
private final Map<String, NestedBackend> m_nestedBackends = new HashMap<>();
Expand All @@ -38,52 +40,92 @@ public class NTEpilogueBackend implements EpilogueBackend {
*
* @param nt the NetworkTable instance to use to send data to
*/
public NTEpilogueBackend(NetworkTableInstance nt) {
public NTBackend(NetworkTableInstance nt) {
this.m_nt = nt;
}

/** Creates a logging backend that sends information to NetworkTables. */
public NTBackend() {
this.m_nt = NetworkTableInstance.getDefault();
}

/**
* Creates a new NTBackend that is disabled whenever the disabledSupplier returns true.
*
* @param disabledSupplier the disable condition for NT logging
* @return a new NTBackend
*/
public NTBackend disableWhen(BooleanSupplier disabledSupplier) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this better than just changing the backend on the fly?

if (condition()) {
  Epilogue.configure(config -> {
    config.backend = new NullBackend();
  });
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more succint, and prevents you from having to cache different kinds of backends yourself.
For instance, if you only want NT logging enabled when fms is not attached, you would have to cache a file only backend, a file and nt backend, and use an addPeriodic call.

In addition, the configure() method accepts a lambda, and if it is called every loop, it will spawn a new object every loop as well

var newBackend = new NTBackend(this.m_nt);
if (m_disabledSupplier == null) {
newBackend.m_disabledSupplier = disabledSupplier;
} else {
newBackend.m_disabledSupplier =
() -> m_disabledSupplier.getAsBoolean() || disabledSupplier.getAsBoolean();
}
return newBackend;
}

@Override
public EpilogueBackend getNested(String path) {
return m_nestedBackends.computeIfAbsent(path, k -> new NestedBackend(k, this));
}

@Override
public void log(String identifier, int value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((IntegerPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getIntegerTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, long value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((IntegerPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getIntegerTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, float value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((FloatPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getFloatTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, double value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((DoublePublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getDoubleTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, boolean value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((BooleanPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getBooleanTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, byte[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((RawPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getRawTopic(k).publish("raw")))
.set(value);
Expand All @@ -92,6 +134,9 @@ public void log(String identifier, byte[] value) {
@Override
@SuppressWarnings("PMD.UnnecessaryCastRule")
public void log(String identifier, int[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
// NT backend only supports int64[], so we have to manually widen to 64 bits before sending
long[] widened = new long[value.length];

Expand All @@ -106,41 +151,59 @@ public void log(String identifier, int[] value) {

@Override
public void log(String identifier, long[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((IntegerArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getIntegerArrayTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, float[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((FloatArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getFloatArrayTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, double[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((DoubleArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getDoubleArrayTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, boolean[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((BooleanArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getBooleanArrayTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, String value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((StringPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getStringTopic(k).publish()))
.set(value);
}

@Override
public void log(String identifier, String[] value) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
((StringArrayPublisher)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getStringArrayTopic(k).publish()))
.set(value);
Expand All @@ -149,6 +212,9 @@ public void log(String identifier, String[] value) {
@Override
@SuppressWarnings("unchecked")
public <S> void log(String identifier, S value, Struct<S> struct) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
m_nt.addSchema(struct);
((StructPublisher<S>)
m_publishers.computeIfAbsent(identifier, k -> m_nt.getStructTopic(k, struct).publish()))
Expand All @@ -158,6 +224,9 @@ public <S> void log(String identifier, S value, Struct<S> struct) {
@Override
@SuppressWarnings("unchecked")
public <S> void log(String identifier, S[] value, Struct<S> struct) {
if (m_disabledSupplier != null && m_disabledSupplier.getAsBoolean()) {
return;
}
m_nt.addSchema(struct);
((StructArrayPublisher<S>)
m_publishers.computeIfAbsent(
Expand Down
Loading