-
Notifications
You must be signed in to change notification settings - Fork 612
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be loggable There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<>(); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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();
});
} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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); | ||
|
@@ -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]; | ||
|
||
|
@@ -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); | ||
|
@@ -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())) | ||
|
@@ -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( | ||
|
There was a problem hiding this comment.
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
inLoggerGenerator
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.There was a problem hiding this comment.
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)