Skip to content

Commit 5733b25

Browse files
committed
Merge pull request #73 from rhauch/issue-72-data-logging
Issue 72: Corrected the data logging and decoding
2 parents 94b3ebd + 9b81275 commit 5733b25

File tree

3 files changed

+88
-20
lines changed

3 files changed

+88
-20
lines changed

strongback-tools/src/org/strongback/tools/logdecoder/LogDecoder.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
import java.io.FileNotFoundException;
2525
import java.io.FileWriter;
2626
import java.io.IOException;
27+
import java.nio.charset.StandardCharsets;
2728
import java.security.InvalidParameterException;
28-
import java.util.Arrays;
2929
import java.util.Map;
30+
import java.util.StringJoiner;
3031

3132
import org.strongback.tools.utils.FileUtils;
3233
import org.strongback.tools.utils.Parser;
@@ -180,33 +181,41 @@ public static final void main(String[] args) {
180181
}
181182
}
182183

184+
private static String readString(DataInputStream in) throws EOFException, IOException{
185+
int len = in.readInt();
186+
byte[] value = new byte[len];
187+
in.read(value);
188+
return new String(value,StandardCharsets.UTF_8);
189+
}
190+
183191
/* Actually converts the log */
184192
private static final void decode(DataInputStream in, BufferedWriter writer)
185193
throws BadFileFormatException, EOFException, IOException {
186194
// Verify Header
187195
printer.print(Strings.CHECK_LOG, Printer.Verbosity.VERBOSE);
188-
byte[] header = new byte[3];
189-
in.read(header);
190-
if(!Arrays.equals(header, "log".getBytes())) throw new BadFileFormatException();
196+
String header = readString(in);
197+
printer.print("Found header = " + header, Printer.Verbosity.VERBOSE);
198+
if(!"data-record".equals(header)) throw new BadFileFormatException();
191199
printer.print(Strings.SUCCESS, Printer.Verbosity.VERBOSE);
192200

193-
// Get the number of elements
194-
int numElements = in.read();
201+
// Get the number of channels
202+
int numElements = in.readInt();
195203
printer.print(Strings.ELEMENT_COUNT + numElements, Printer.Verbosity.VERBOSE);
196204

197-
// Get the size of each element
205+
// Get the size of each channel sample
198206
int[] elementSizes = new int[numElements];
199207
for(int i = 0; i< elementSizes.length; i++) {
200-
elementSizes[i] = in.read();
208+
elementSizes[i] = in.readInt();
209+
printer.print("read channel " + i + " size = " + elementSizes[i], Printer.Verbosity.VERBOSE);
201210
}
202211

203-
// Write the name of each element
212+
// Read and write the name of each channel
213+
StringJoiner joiner = new StringJoiner(",");
204214
for(int i = 0; i< numElements; i++) {
205-
int nameSize = in.read();
206-
byte[] b = new byte[nameSize];
207-
in.read(b);
208-
writer.write(new String(b) + ", ");
215+
joiner.add(readString(in));
209216
}
217+
writer.write(joiner.toString());
218+
printer.print("channel names = " + joiner, Printer.Verbosity.VERBOSE);
210219
writer.newLine();
211220

212221
printer.print(Strings.READING_LOG, Printer.Verbosity.VERBOSE);
@@ -218,11 +227,16 @@ private static final void decode(DataInputStream in, BufferedWriter writer)
218227
printer.print(Strings.READ_LINE + lineCount, Printer.Verbosity.VERBOSE);
219228
in.reset();
220229
for(int i = 0; i < numElements; i++) {
230+
if (i!=0) writer.write(",");
221231
if(elementSizes[i]==4){
222-
writer.write(in.readInt() + ", ");
232+
int value = in.readInt();
233+
writer.write(Integer.toString(value));
234+
} else if(elementSizes[i]==2) {
235+
short value = in.readShort();
236+
writer.write(Short.toString(value));
237+
} else {
238+
throw new IOException("Unexpected size of data: " + elementSizes[i]);
223239
}
224-
else if(elementSizes[i]==2)
225-
writer.write(in.readShort() + ", ");
226240
}
227241
writer.newLine();
228242
lineCount++;

strongback/src/org/strongback/FileDataWriter.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.concurrent.atomic.AtomicInteger;
2223
import java.util.function.IntSupplier;
2324
import java.util.function.Supplier;
2425

@@ -33,6 +34,7 @@ final class FileDataWriter implements DataWriter {
3334
private MappedFileDataWriter writer;
3435
private long recordLength;
3536
private final long fileSize;
37+
private final int channelCount;
3638

3739
public FileDataWriter(Iterable<DataRecorderChannel> channels, Supplier<String> filenameGenerator, int writesPerSecond,
3840
int runningTimeInSeconds) {
@@ -47,6 +49,10 @@ public FileDataWriter(Iterable<DataRecorderChannel> channels, Supplier<String> f
4749
recordLength += (Short.BYTES * suppliers.size());
4850
fileSize = numWrites * recordLength + 1024; // add extra room for header and miscellaneous
4951

52+
AtomicInteger count = new AtomicInteger();
53+
channels.forEach(ch->count.incrementAndGet());
54+
channelCount = count.get() + 1; // adding the time sequence
55+
5056
openIfNeeded();
5157
}
5258

@@ -57,16 +63,16 @@ protected void openIfNeeded() {
5763
} catch (IOException e) {
5864
throw new RuntimeException(e);
5965
}
66+
suppliers.clear();
6067

6168
// Write the header
6269
writer.write("data-record");
6370

6471
// Write the number of elements
65-
writer.write(suppliers.size() + 1);
66-
67-
// Write the size of each element (Infrastructure for variable length element)
68-
writer.write(Integer.BYTES);
72+
writer.write(channelCount);
6973

74+
// Write the size of each channel as an integer
75+
writer.write(Integer.BYTES); // size of the time channel
7076
channels.forEach(channel -> {
7177
IntSupplier supplier = channel.getSupplier();
7278
assert supplier != null;
@@ -101,6 +107,7 @@ public void close() {
101107
writer.close();
102108
} finally {
103109
writer = null;
110+
suppliers.clear();
104111
}
105112
}
106113

strongback/src/org/strongback/Strongback.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.concurrent.atomic.AtomicLong;
2323
import java.util.concurrent.locks.LockSupport;
2424
import java.util.function.BooleanSupplier;
25+
import java.util.function.Consumer;
2526
import java.util.function.Function;
2627
import java.util.function.LongConsumer;
2728
import java.util.function.Supplier;
@@ -358,6 +359,52 @@ public Configurator reportExcessiveExecutionPeriods(LongConsumer handler) {
358359
return this;
359360
}
360361

362+
/**
363+
* When the supplied condition is {@code true}, call the supplied function with this Configurator.
364+
* @param condition the condition that determines whether the supplied function should be called; may not be null
365+
* @param configure the function that will perform additional configuration
366+
* @return this configurator so that methods can be chained together; never null
367+
*/
368+
public Configurator when( boolean condition, Runnable configure ) {
369+
return when(()->condition,configure);
370+
}
371+
372+
/**
373+
* When the supplied condition is {@code true}, call the supplied function with this Configurator.
374+
* @param condition the function that determines whether the supplied function should be called; may not be null
375+
* @param configure the function that will perform additional configuration
376+
* @return this configurator so that methods can be chained together; never null
377+
*/
378+
public Configurator when( BooleanSupplier condition, Runnable configure ) {
379+
if ( condition != null && configure != null && condition.getAsBoolean() ) {
380+
configure.run();
381+
}
382+
return this;
383+
}
384+
385+
/**
386+
* When the supplied condition is {@code true}, call the supplied function with this Configurator.
387+
* @param condition the condition that determines whether the supplied function should be called; may not be null
388+
* @param configure the function that will perform additional configuration
389+
* @return this configurator so that methods can be chained together; never null
390+
*/
391+
public Configurator when( boolean condition, Consumer<Configurator> configure ) {
392+
return when(()->condition,configure);
393+
}
394+
395+
/**
396+
* When the supplied condition is {@code true}, call the supplied function with this Configurator.
397+
* @param condition the function that determines whether the supplied function should be called; may not be null
398+
* @param configure the function that will perform additional configuration
399+
* @return this configurator so that methods can be chained together; never null
400+
*/
401+
public Configurator when( BooleanSupplier condition, Consumer<Configurator> configure ) {
402+
if ( condition != null && configure != null && condition.getAsBoolean() ) {
403+
configure.accept(this);
404+
}
405+
return this;
406+
}
407+
361408
/**
362409
* Complete the Strongback configuration and initialize Strongback so that it can be used.
363410
*/

0 commit comments

Comments
 (0)