Skip to content

Commit

Permalink
new parser can write SD log (#307)
Browse files Browse the repository at this point in the history
* stub sd logs

* framing for SD log output

* make old and new match

* only grab one struct, prefix

* sd log seems to just work?

* wiring

* wiring

* wiring

* array-of-struct handling

* handle array of struct

* array of structs doesn't really work right

* sd log end file
  • Loading branch information
mck1117 authored Nov 25, 2023
1 parent baa9bc3 commit 6ac689f
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ private int handleYaml(Map<String, Object> data) throws IOException {
// tsOutputsDestination + File.separator + "/data_logs.ini"
// );

// SdLogWriter sdLogWriter = new SdLogWriter("console/binary_log/log_fields_generated.h");

EntryHandler handler = new EntryHandler() {
@Override
public void onEntry(String name, String javaName, String folder, String prepend, boolean withCDefines, String[] outputNames, String constexpr, String conditional, Boolean isPtr) throws IOException {
Expand Down Expand Up @@ -163,6 +165,10 @@ public void onEntry(String name, String javaName, String folder, String prepend,
// outputChannelWriter.writeOutputChannels(parseState, outputNames[i]);
// }
// }

// if (constexpr != null) {
// sdLogWriter.writeSdLogs(parseState, constexpr + (isPtr ? "->" : "."));
// }
}

state.doJob();
Expand Down Expand Up @@ -241,6 +247,7 @@ public void onEntry(String name, String javaName, String folder, String prepend,
lazyFile.close();

outputValueConsumer.endFile();
// sdLogWriter.endFile();

// return outputChannelWriter.getSize();
return outputsSections.sensorTsPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ public void writeCOffsetCheck(PrintStream ps, String parentTypeName) {
protected void writeOutputChannelLayout(PrintStream ps, PrintStream psDatalog, StructNamePrefixer prefixer, int offsetAdd) {
this.prototypeLayout.writeOutputChannelLayout(ps, psDatalog, prefixer, offsetAdd, this.length);
}

@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) {
this.prototypeLayout.writeSdLogLayout(ps, prefixer, sourceName, this.length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public String toString() {
}

public final void writeTunerstudioLayout(PrintStream ps, TsMetadata meta) {
writeTunerstudioLayout(ps, meta, new StructNamePrefixer(), 0);
writeTunerstudioLayout(ps, meta, new StructNamePrefixer('_'), 0);
}

protected void writeTunerstudioLayout(PrintStream ps, TsMetadata meta, StructNamePrefixer prefixer, int offsetAdd) {}
Expand Down Expand Up @@ -68,7 +68,7 @@ public void writeCLayout(PrintStream ps, int[] arrayLength) {
public void writeCOffsetCheck(PrintStream ps, String parentTypeName) { }

public void writeOutputChannelLayout(PrintStream ps, PrintStream psDatalog, String prefix, int offsetAdd) {
StructNamePrefixer prefixer = new StructNamePrefixer();
StructNamePrefixer prefixer = new StructNamePrefixer('_');

if (prefix != null) {
prefixer.push(prefix);
Expand All @@ -91,4 +91,23 @@ protected static void writeDatalogName(PrintStream ps, String name, String comme

ps.print(text);
}

public void writeSdLogLayout(PrintStream ps, String sourceName) {
// TODO
final String prefix = null;

StructNamePrefixer prefixer = new StructNamePrefixer('.');

if (prefix != null) {
prefixer.push(prefix);
}

writeSdLogLayout(ps, prefixer, sourceName);
}

protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) { }

protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, int[] arrayLength) {
throw new IllegalStateException("This type can't be in an array!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,51 @@ protected void writeOutputChannelLayout(PrintStream ps, PrintStream psDatalog, S
elementOffset += type.size;
}
}

@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) {
writeSdLogLayout(ps, prefixer, "", "", sourceName);
}

@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, int[] arrayLength) {
if (arrayLength.length != 1) {
throw new IllegalStateException("Output channels don't support multi dimension arrays");
}

for (int i = 0; i < arrayLength[0]; i++) {
writeSdLogLayout(ps, prefixer, "[" + i + "]", " " + (i + 1), sourceName);
}
}

private void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String arraySub, String commentSuffix, String sourceName) {
if (this.name.startsWith("unused")) {
return;
}

// {engine->outputChannels.mafMeasured, "MAF", "kg/h", 1},

final String prefixedName = prefixer.get(this.name);

ps.print("\t{");
ps.print(sourceName);
ps.print(prefixedName);
ps.print(arraySub);
ps.print(", \"");

String comment = this.options.comment;

// default to name in case of no comment
if (comment == null || comment.length() == 0) {
comment = prefixedName;
}

ps.print(comment);
ps.print(commentSuffix);
ps.print("\", ");
ps.print(this.options.units);
ps.print(", ");
ps.print(this.options.digits);
ps.println("},");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,33 @@ protected void writeOutputChannelLayout(PrintStream ps, PrintStream psDatalog, S
elementOffset += this.size;
}
}

private void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, String name) {
if (!this.noPrefix) {
prefixer.push(name);
}

this.children.forEach(c -> c.writeSdLogLayout(ps, prefixer, sourceName));

if (!this.noPrefix) {
prefixer.pop();
}
}

@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName) {
writeSdLogLayout(ps, prefixer, sourceName, this.name);
}

@Override
protected void writeSdLogLayout(PrintStream ps, StructNamePrefixer prefixer, String sourceName, int[] arrayLength) {
if (arrayLength.length != 1) {
throw new IllegalStateException("Output channels don't support multi dimension arrays");
}

// TODO: This doesn't quite work, as it's unclear how to make automatic naming work properly
// for (int i = 0; i < arrayLength[0]; i++) {
// writeSdLogLayout(ps, prefixer, sourceName, this.name + "[" + i + "]");
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
public class StructNamePrefixer {
private final Stack<String> stack = new Stack<>();
private int idx = -1;
private final char prefixChar;

public StructNamePrefixer(char prefixChar) {
this.prefixChar = prefixChar;
}

public void pop() {
stack.pop();
Expand All @@ -18,7 +23,7 @@ public void push(String name) {
this.idx = -1;
}

stack.push(name + "_");
stack.push(name + this.prefixChar);
}

void setIndex(int idx) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.rusefi.newparse.outputs;

import com.rusefi.newparse.ParseState;
import com.rusefi.newparse.layout.StructLayout;
import com.rusefi.newparse.parsing.Struct;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class SdLogWriter {
private final PrintStream ps;

public SdLogWriter(String outputFile) throws FileNotFoundException {
this(new PrintStreamAlwaysUnix(new FileOutputStream(outputFile)));
}

public SdLogWriter(PrintStream ps) {
this.ps = ps;

ps.println("static constexpr LogField fields[] = {");
ps.println("\t{packedTime, GAUGE_NAME_TIME, \"sec\", 0},");
}

public void endFile() {
ps.println("};");
}

public void writeSdLogs(ParseState parser, String sourceName) {
// Assume the last struct is the one we want...
Struct s = parser.getStructs().get(parser.getStructs().size() - 1);

StructLayout sl = new StructLayout(0, "root", s);
sl.writeSdLogLayout(ps, sourceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void endFile() throws IOException {

public static void wrapContent(LazyFile output, String content) {
output.write("static constexpr LogField fields[] = {\n" +
"{packedTime, GAUGE_NAME_TIME, \"sec\", 0},\n");
"\t{packedTime, GAUGE_NAME_TIME, \"sec\", 0},\n");
output.write(content);
output.write("};\n");
}
Expand Down

0 comments on commit 6ac689f

Please sign in to comment.