Skip to content

Commit

Permalink
Merge branch 'main' into rlog-r2
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Mar 13, 2024
2 parents 5768b56 + d2db89e commit 29d4230
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class Logger {
private static List<LoggedDashboardInput> dashboardInputs = new ArrayList<>();
private static Supplier<ByteBuffer[]> urclSupplier = null;
private static boolean deterministicTimestamps = true;
private static boolean enableConsole = false;
private static boolean enableConsole = true;

private static LogReplaySource replaySource;
private static final BlockingQueue<LogTable> receiverQueue = new ArrayBlockingQueue<LogTable>(receiverQueueCapcity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
package org.littletonrobotics.junction.console;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.BufferOverflowException;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import edu.wpi.first.wpilibj.DriverStation;

Expand All @@ -29,60 +36,92 @@
*/
public class RIOConsoleSource implements ConsoleSource {
private static final String filePath = "/home/lvuser/FRC_UserProgram.log";
private BufferedReader reader = null;

private CharBuffer buffer = CharBuffer.allocate(10240);
private final Thread thread;
private final BlockingQueue<String> queue = new ArrayBlockingQueue<>(100);
private final List<String> lines = new ArrayList<>();

public RIOConsoleSource() {
thread = new Thread(this::run, "RIOConsoleSource");
thread.setDaemon(true);
thread.start();
}

public String getNewData() {
lines.clear();
queue.drainTo(lines);
return String.join("\n", lines);
}

public void close() throws Exception {
thread.interrupt();
}

private void run() {
// Initialize reader
CharBuffer buffer = CharBuffer.allocate(10240);
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
DriverStation.reportError("Failed to open console file \"" + filePath + "\", disabling console capture.", true);
}
}

public String getNewData() {
if (reader == null) {
return "";
return;
}

// Read new data from console
while (true) {
int nextChar = -1;
try {
nextChar = reader.read();
} catch (IOException e) {
DriverStation.reportError("Failed to read console file \"" + filePath + "\", disabling console capture.", true);
reader = null;
return "";
// Read new data from console
while (true) {
int nextChar = -1;
try {
nextChar = reader.read();
} catch (IOException e) {
DriverStation.reportError("Failed to read console file \"" + filePath + "\", disabling console capture.", true);
try {
reader.close();
} catch (IOException io) {}
return;
}
if (nextChar != -1) {
try {
buffer.put((char) nextChar);
} catch (BufferOverflowException e) {}
} else {
// Break read loop, send complete lines to queue
break;
}
}
if (nextChar != -1) {

// Read all complete lines
String output = null;
for (int i = buffer.position(); i > 0; i--) {
if (i < buffer.position() && buffer.get(i) == '\n') {
int originalPosition = buffer.position();
output = new String(buffer.array(), 0, i);
buffer.rewind();
buffer.put(buffer.array(), i + 1, buffer.limit() - i - 1);
buffer.position(originalPosition - i - 1);
break;
}
}
if (output != null) {
try {
buffer.put((char) nextChar);
} catch (BufferOverflowException e) {}
} else {
break;
queue.put(output);
} catch (InterruptedException e) {
try {
reader.close();
} catch (IOException io) {}
return;
}
}
}

// Read all complete lines
String output = null;
for (int i = buffer.position(); i > 0; i--) {
if (i < buffer.position() && buffer.get(i) == '\n') {
int originalPosition = buffer.position();
output = new String(buffer.array(), 0, i);
buffer.rewind();
buffer.put(buffer.array(), i + 1, buffer.limit() - i - 1);
buffer.position(originalPosition - i - 1);
break;
// Sleep to avoid spinning needlessly
try {
Thread.sleep(20);
} catch (InterruptedException e) {
try {
reader.close();
} catch (IOException io) {}
return;
}
}
if (output == null) output = "";
return output;
}

public void close() throws Exception {
reader.close();
}

}

0 comments on commit 29d4230

Please sign in to comment.