Skip to content

Commit

Permalink
Fix array lazy logging
Browse files Browse the repository at this point in the history
  • Loading branch information
shueja committed Jan 12, 2024
1 parent 6064048 commit 975c676
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 98 deletions.
33 changes: 25 additions & 8 deletions monologue/src/generate/java/DataLogger.java.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.networktables.NTSendable;
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilderImpl;
import edu.wpi.first.util.datalog.*;
import monologue.NTIntegerArrayLogEntry;
import edu.wpi.first.util.sendable.Sendable;
import edu.wpi.first.wpilibj.DataLogManager;
import java.util.Arrays;
Expand Down Expand Up @@ -34,8 +35,11 @@ class DataLogger extends GenericLogger {
if (value == null) {return;}
{%endif%}
{%if t.TypeName == 'IntegerArray'%}
new {{t.java.EntryName}}LogEntry(log, entryName)
new NTIntegerArrayLogEntry(log, entryName)
.append(toLongArray(value));
{%elif t.TypeName == 'LongArray'%}
new NTIntegerArrayLogEntry(log, entryName)
.append(value);
{%else%}
new {{t.java.EntryName}}LogEntry(log, entryName).append(value);
{%endif%}
Expand All @@ -45,9 +49,12 @@ class DataLogger extends GenericLogger {
public void add{{t.TypeName}}(String entryName,
{{t.java.Supplier}}valueSupplier,
LogLevel level)
{
{
{%if t.TypeName == 'IntegerArray' or t.TypeName == 'LongArray'%}
var entry = new NTIntegerArrayLogEntry(log, entryName);
{%else %}
var entry = new {{t.java.EntryName}}LogEntry(log, entryName);

{%endif%}
LongConsumer consumer;
{%if t.TypeName == 'IntegerArray'%}
if (this.isLazy()) {
Expand All @@ -60,7 +67,10 @@ class DataLogger extends GenericLogger {
var value = toLongArray(valueSupplier.get());
if (!(Arrays.equals(value, lastValue))) {
entry.append(value, timestamp);
lastValue = value;
if (lastValue.length != value.length) {
lastValue = new long[value.length];
}
System.arraycopy(value, 0, lastValue, 0, value.length);
}
}
};
Expand Down Expand Up @@ -92,7 +102,10 @@ class DataLogger extends GenericLogger {
if (!(Arrays.equals(value, lastValue))) {
{%endif%}
entry.append(value, timestamp);
lastValue = value;
if (lastValue.length != value.length) {
lastValue = new {{t.java.ComponentType}}[value.length];
}
System.arraycopy(value, 0, lastValue, 0, value.length);
}
}
};
Expand Down Expand Up @@ -171,17 +184,21 @@ class DataLogger extends GenericLogger {

if (this.isLazy()) {
consumer = new LongConsumer() {
private R lastValue = null;
private byte[] lastValue = new byte[] {};

@Override
public void accept(long timestamp) {
var value = valueSupplier.get();
if (value == null) {
return;
}
if (!(value.equals(lastValue))) {
var packed = struct.pack(value);
if (!(Arrays.equals(packed, lastValue))) {
entry.append(value, timestamp);
lastValue = value;
if (lastValue.length != value.length) {
lastValue = new byte[value.length];
}
lastValue = packed;
}
}
};
Expand Down
160 changes: 80 additions & 80 deletions monologue/src/generate/java/NTLogger.java.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,29 @@ class NTLogger extends GenericLogger {
{%endif%}
LongConsumer consumer;
{%if t.TypeName == 'IntegerArray'%}
if (this.isLazy()) {
consumer = new LongConsumer() {
private long[] lastValue = new long[] {};
@Override
public void accept(long timestamp) {
var intarr = valueSupplier.get();
if (intarr == null) { return;}
var value = toLongArray(valueSupplier.get());
if (!(Arrays.equals(value, lastValue))) {
entry.set(value, timestamp);
lastValue = value;
}
// if (this.isLazy()) {
// consumer = new LongConsumer() {
// private long[] lastValue = new long[] {};
// @Override
// public void accept(long timestamp) {
// var intarr = valueSupplier.get();
// if (intarr == null) { return;}
// var value = toLongArray(valueSupplier.get());
// if (!(Arrays.equals(value, lastValue))) {
// entry.set(value, timestamp);
// lastValue = value;
// }


}
};
} else {
// }
// };
// } else {
consumer = (timestamp) -> {
var value = valueSupplier.get();
if (value == null) { return;}
entry.set(toLongArray(value), timestamp);
};
}
// }

addField(
entryName,
Expand All @@ -125,53 +125,53 @@ class NTLogger extends GenericLogger {
);
{%else%}
{%if t.java.IsArray == true%}
if (this.isLazy()) {
consumer = new LongConsumer() {
private {{t.java.ValueType}} lastValue = new {{t.java.ValueType}} {};
@Override
public void accept(long timestamp) {
var value = valueSupplier.get();
if (value == null) { return;}
{%if t.TypeName == 'StringArray'%}
if (!(Arrays.deepEquals(value, lastValue))) {
{%else%}
if (!(Arrays.equals(value, lastValue))) {
{%endif%}
entry.set(value, timestamp);
lastValue = value;
}
}
};
} else {
// if (this.isLazy()) {
// consumer = new LongConsumer() {
// private {{t.java.ValueType}} lastValue = new {{t.java.ValueType}} {};
// @Override
// public void accept(long timestamp) {
// var value = valueSupplier.get();
// if (value == null) { return;}
// {%if t.TypeName == 'StringArray'%}
// if (!(Arrays.deepEquals(value, lastValue))) {
// {%else%}
// if (!(Arrays.equals(value, lastValue))) {
// {%endif%}
// entry.set(value, timestamp);
// lastValue = value;
// }
// }
// };
// } else {
consumer = (timestamp) -> {
var value = valueSupplier.get();
if (value != null) {
entry.set(value, timestamp);
}
};
}
// }
{%else%}
if (this.isLazy()) {
consumer = new LongConsumer() {
private {{t.java.ValueType}} lastValue = {{t.java.EmptyValue}};
@Override
public void accept(long timestamp) {
// if (this.isLazy()) {
// consumer = new LongConsumer() {
// private {{t.java.ValueType}} lastValue = {{t.java.EmptyValue}};
// @Override
// public void accept(long timestamp) {

var value = valueSupplier.get();
if (value == null) { return; }
if (value != lastValue) {
entry.set(value, timestamp);
lastValue = value;
}
}
};
} else {
// var value = valueSupplier.get();
// if (value == null) { return; }
// if (value != lastValue) {
// entry.set(value, timestamp);
// lastValue = value;
// }
// }
// };
// } else {
consumer = (timestamp) -> {
var value = valueSupplier.get();
if (value == null) { return; }
entry.set(value, timestamp);
};
}
// }
{%endif%}

addField(
Expand All @@ -196,27 +196,27 @@ class NTLogger extends GenericLogger {

LongConsumer consumer;

if (this.isLazy()) {
consumer = new LongConsumer() {
private R lastValue = null;
@Override
public void accept(long timestamp) {
// if (this.isLazy()) {
// consumer = new LongConsumer() {
// private R lastValue = null;
// @Override
// public void accept(long timestamp) {

var value = valueSupplier.get();
if (value == null) { return;}
if (!(value.equals(lastValue))) {
publisher.set(value, timestamp);
lastValue = value;
}
}
};
} else {
// var value = valueSupplier.get();
// if (value == null) { return;}
// if (!(value.equals(lastValue))) {
// publisher.set(value, timestamp);
// lastValue = value;
// }
// }
// };
// } else {
consumer = (timestamp) -> {
var value = valueSupplier.get();
if (value == null) { return;}
publisher.set(value, timestamp);
};
}
// }

addField(
entryName,
Expand All @@ -238,26 +238,26 @@ class NTLogger extends GenericLogger {

LongConsumer consumer;

if (this.isLazy()) {
consumer = new LongConsumer() {
private R[] lastValue = null;
@Override
public void accept(long timestamp) {
var value = (R[]) valueSupplier.get();
if (value == null) {return;}
if (!(Arrays.deepEquals(value, lastValue))) {
publisher.set(value, timestamp);
lastValue = value;
}
}
};
} else {
// if (this.isLazy()) {
// consumer = new LongConsumer() {
// private R[] lastValue = null;
// @Override
// public void accept(long timestamp) {
// var value = (R[]) valueSupplier.get();
// if (value == null) {return;}
// if (!(Arrays.deepEquals(value, lastValue))) {
// publisher.set(value, timestamp);
// lastValue = value;
// }
// }
// };
// } else {
consumer = (timestamp) -> {
var value = valueSupplier.get();
if (value == null) {return;}
publisher.set(value, timestamp);
};
}
// }

addField(
entryName,
Expand Down
21 changes: 14 additions & 7 deletions monologue/src/generate/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
"FunctionTypeSuffix": "<byte[]>",
"EntryName": "Raw",
"SupplierGet" : "get",
"IsArray" : true
"IsArray" : true,
"ComponentType": "byte"
}
},
{
Expand All @@ -111,7 +112,8 @@
"FunctionTypeSuffix": "<boolean[]>",
"EntryName": "BooleanArray",
"SupplierGet" : "get",
"IsArray" : true
"IsArray" : true,
"ComponentType": "boolean"
}
},
{
Expand All @@ -125,7 +127,8 @@
"FunctionTypeSuffix": "<int[]>",
"EntryName": "IntegerArray",
"SupplierGet" : "get",
"IsArray" : true
"IsArray" : true,
"ComponentType": "int"
}
},
{
Expand All @@ -139,7 +142,8 @@
"FunctionTypeSuffix": "<long[]>",
"EntryName": "IntegerArray",
"SupplierGet" : "get",
"IsArray" : true
"IsArray" : true,
"ComponentType" : "long"
}
},
{
Expand All @@ -153,7 +157,8 @@
"FunctionTypeSuffix": "<float[]>",
"EntryName": "FloatArray",
"SupplierGet" : "get",
"IsArray" : true
"IsArray" : true,
"ComponentType": "float"
}
},
{
Expand All @@ -167,7 +172,8 @@
"FunctionTypeSuffix": "<double[]>",
"EntryName": "DoubleArray",
"SupplierGet" : "get",
"IsArray" : true
"IsArray" : true,
"ComponentType": "double"
}
},
{
Expand All @@ -180,7 +186,8 @@
"FunctionTypeSuffix": "<String[]>",
"EntryName": "StringArray",
"SupplierGet" : "get",
"IsArray" : true
"IsArray" : true,
"ComponentType": "String"
}
}
]
Loading

0 comments on commit 975c676

Please sign in to comment.