Skip to content
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

Create NTLogAppender #1297

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.photonvision.common.logging;

import edu.wpi.first.cscore.CameraServerJNI;
import edu.wpi.first.networktables.PubSubOption;
import edu.wpi.first.networktables.StringPublisher;
import java.io.*;
import java.nio.file.Path;
import java.text.ParseException;
Expand All @@ -28,6 +31,7 @@
import org.photonvision.common.configuration.PathManager;
import org.photonvision.common.dataflow.DataChangeService;
import org.photonvision.common.dataflow.events.OutgoingUIEvent;
import org.photonvision.common.dataflow.networktables.NetworkTablesManager;
import org.photonvision.common.util.TimedTaskManager;

@SuppressWarnings("unused")
Expand Down Expand Up @@ -123,6 +127,11 @@ public static void addFileAppender(Path logFilePath) {
currentAppenders.add(new FileLogAppender(logFilePath));
}

/** Publish log stringst to NT. This must be done AFTER loading ntcore. */
public static void addNtAppender() {
currentAppenders.add(new NTLogAppender());
}

public static void cleanLogs(Path folderToClean) {
File[] logs = folderToClean.toFile().listFiles();
if (logs == null) return;
Expand Down Expand Up @@ -305,6 +314,20 @@ public void log(String message, LogLevel level) {
}
}

private static class NTLogAppender implements LogAppender {
private StringPublisher pub =
NetworkTablesManager.getInstance()
.kRootTable
.getSubTable("log_outputs")
.getStringTopic(CameraServerJNI.getHostname())
.publish(PubSubOption.sendAll(true));

@Override
public void log(String message, LogLevel level) {
pub.accept(message);
}
}

private static class FileLogAppender implements LogAppender {
private OutputStream out;
private boolean wantsFlush;
Expand Down
48 changes: 36 additions & 12 deletions photon-lib/src/main/java/org/photonvision/PhotonCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
import edu.wpi.first.networktables.StringSubscriber;
import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.Timer;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.photonvision.common.hardware.VisionLEDMode;
import org.photonvision.common.networktables.PacketSubscriber;
import org.photonvision.targeting.PhotonPipelineResult;
Expand Down Expand Up @@ -74,6 +75,8 @@ public class PhotonCamera implements AutoCloseable {
IntegerSubscriber heartbeatEntry;
DoubleArraySubscriber cameraIntrinsicsSubscriber;
DoubleArraySubscriber cameraDistortionSubscriber;
MultiSubscriber m_topicNameSubscriber;
NetworkTable rootPhotonTable;

@Override
public void close() {
Expand All @@ -97,6 +100,7 @@ public void close() {
pipelineIndexRequest.close();
cameraIntrinsicsSubscriber.close();
cameraDistortionSubscriber.close();
m_topicNameSubscriber.close();
}

private final String path;
Expand Down Expand Up @@ -124,8 +128,8 @@ public static void setVersionCheckEnabled(boolean enabled) {
*/
public PhotonCamera(NetworkTableInstance instance, String cameraName) {
name = cameraName;
var photonvision_root_table = instance.getTable(kTableName);
this.cameraTable = photonvision_root_table.getSubTable(cameraName);
rootPhotonTable = instance.getTable(kTableName);
this.cameraTable = rootPhotonTable.getSubTable(cameraName);
path = cameraTable.getPath();
var rawBytesEntry =
cameraTable
Expand All @@ -147,14 +151,17 @@ public PhotonCamera(NetworkTableInstance instance, String cameraName) {
cameraDistortionSubscriber =
cameraTable.getDoubleArrayTopic("cameraDistortion").subscribe(null);

ledModeRequest = photonvision_root_table.getIntegerTopic("ledModeRequest").publish();
ledModeState = photonvision_root_table.getIntegerTopic("ledModeState").subscribe(-1);
versionEntry = photonvision_root_table.getStringTopic("version").subscribe("");
ledModeRequest = rootPhotonTable.getIntegerTopic("ledModeRequest").publish();
ledModeState = rootPhotonTable.getIntegerTopic("ledModeState").subscribe(-1);
versionEntry = rootPhotonTable.getStringTopic("version").subscribe("");

// Existing is enough to make this multisubscriber do its thing
MultiSubscriber m_topicNameSubscriber =
m_topicNameSubscriber =
new MultiSubscriber(
instance, new String[] {"/photonvision/"}, PubSubOption.topicsOnly(true));
instance,
new String[] {"/photonvision/"},
PubSubOption.topicsOnly(true),
PubSubOption.disableLocal(true));

HAL.report(tResourceType.kResourceType_PhotonCamera, InstanceCount);
InstanceCount++;
Expand Down Expand Up @@ -346,20 +353,28 @@ private void verifyVersion() {
// Heartbeat entry is assumed to always be present. If it's not present, we
// assume that a camera with that name was never connected in the first place.
if (!heartbeatEntry.exists()) {
Set<String> cameraNames = cameraTable.getInstance().getTable(kTableName).getSubTables();
var cameraNames = getTablesThatLookLikePhotonCameras();
if (cameraNames.isEmpty()) {
DriverStation.reportError(
"Could not find any PhotonVision coprocessors on NetworkTables. Double check that PhotonVision is running, and that your camera is connected!",
"Could not find **any** PhotonVision coprocessors on NetworkTables. Double check that PhotonVision is running, and that your camera is connected!",
false);
} else {
DriverStation.reportError(
"PhotonVision coprocessor at path "
+ path
+ " not found on NetworkTables. Double check that your camera names match!",
true);

var cameraNameStr = new StringBuilder();
for (var c : cameraNames) {
cameraNameStr.append(" ==> ");
cameraNameStr.append(c);
cameraNameStr.append("\n");
}

DriverStation.reportError(
"Found the following PhotonVision cameras on NetworkTables:\n"
+ String.join("\n", cameraNames),
"Found the following PhotonVision cameras active on NetworkTables:\n"
+ String.join("\n", cameraNameStr),
false);
}
}
Expand Down Expand Up @@ -404,4 +419,13 @@ else if (!isConnected()) {
throw new UnsupportedOperationException(versionMismatchMessage);
}
}

private List<String> getTablesThatLookLikePhotonCameras() {
return rootPhotonTable.getSubTables().stream()
.filter(
it -> {
return rootPhotonTable.getSubTable(it).getEntry("rawBytes").exists();
})
.collect(Collectors.toList());
}
}
3 changes: 3 additions & 0 deletions photon-server/src/main/java/org/photonvision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ public static void main(String[] args) {
logger.error("Failed to load native libraries!", e);
System.exit(1);
}

Logger.addNtAppender();

logger.info("Native libraries loaded.");

try {
Expand Down
Loading