Create FileLogger JNI (#1517)

This commit is contained in:
Matt
2024-11-06 20:16:36 -05:00
committed by GitHub
parent a99a8e750b
commit 8dcf0b31a2
14 changed files with 392 additions and 55 deletions

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.photonvision.jni;
public class QueuedFileLogger {
long m_handle = 0;
public QueuedFileLogger(String path) {
m_handle = QueuedFileLogger.create(path);
}
public String[] getNewlines() {
String newBuffer = null;
synchronized (this) {
if (m_handle == 0) {
System.err.println("QueuedFileLogger use after free");
return new String[0];
}
newBuffer = QueuedFileLogger.getNewLines(m_handle);
}
if (newBuffer == null) {
return new String[0];
}
return newBuffer.split("\n");
}
public void stop() {
synchronized (this) {
if (m_handle != 0) {
QueuedFileLogger.destroy(m_handle);
m_handle = 0;
}
}
}
private static native long create(String path);
private static native void destroy(long handle);
private static native String getNewLines(long handle);
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright (C) Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <functional>
#include <string>
#include <vector>
#include <wpi/FileLogger.h>
#include "jni_utils.h"
#include "org_photonvision_jni_QueuedFileLogger.h"
struct QueuedFileLogger {
// ew ew ew ew ew ew ew ew
std::vector<char> m_data{};
std::mutex m_mutex;
wpi::FileLogger logger;
explicit QueuedFileLogger(std::string_view file)
: logger{file, std::bind(&QueuedFileLogger::callback, this,
std::placeholders::_1)} {
// fmt::println("Watching {}", file);
}
void callback(std::string_view newline) {
std::lock_guard lock{m_mutex};
// fmt::println("FileLogger got: {}", newline);
m_data.insert(m_data.end(), newline.begin(), newline.end());
}
std::vector<char> SwapData() {
std::vector<char> ret;
{
std::lock_guard lock{m_mutex};
ret.swap(m_data);
}
return ret;
}
};
extern "C" {
/*
* Class: org_photonvision_jni_QueuedFileLogger
* Method: create
* Signature: (Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL
Java_org_photonvision_jni_QueuedFileLogger_create
(JNIEnv* env, jclass, jstring name)
{
const char* c_name{env->GetStringUTFChars(name, 0)};
std::string cpp_name{c_name};
jlong ret{reinterpret_cast<jlong>(new QueuedFileLogger(cpp_name))};
env->ReleaseStringUTFChars(name, c_name);
return ret;
}
/*
* Class: org_photonvision_jni_QueuedFileLogger
* Method: destroy
* Signature: (J)V
*/
JNIEXPORT void JNICALL
Java_org_photonvision_jni_QueuedFileLogger_destroy
(JNIEnv*, jclass, jlong handle)
{
CHECK_PTR(handle);
delete reinterpret_cast<QueuedFileLogger*>(handle);
}
/*
* Class: org_photonvision_jni_QueuedFileLogger
* Method: getNewLines
* Signature: (J)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_org_photonvision_jni_QueuedFileLogger_getNewLines
(JNIEnv* env, jclass, jlong handle)
{
CHECK_PTR_RETURN(handle, nullptr);
QueuedFileLogger* logger = reinterpret_cast<QueuedFileLogger*>(handle);
return env->NewStringUTF(logger->SwapData().data());
}
} // extern "C"

View File

@@ -20,21 +20,11 @@
#include <cstdio>
#include <string>
#include "jni_utils.h"
#include "net/TimeSyncClient.h"
using namespace wpi::tsp;
#define CHECK_PTR(ptr) \
if (!ptr) { \
fmt::println("Got invalid pointer?? {}:{}", __FILE__, __LINE__); \
return; \
}
#define CHECK_PTR_RETURN(ptr, default) \
if (!ptr) { \
fmt::println("Got invalid pointer?? {}:{}", __FILE__, __LINE__); \
return default; \
}
/**
* Finds a class and keeps it as a global reference.
*

View File

@@ -20,21 +20,11 @@
#include <cstdio>
#include "jni_utils.h"
#include "net/TimeSyncServer.h"
using namespace wpi::tsp;
#define CHECK_PTR(ptr) \
if (!ptr) { \
fmt::println("Got invalid pointer?? {}:{}", __FILE__, __LINE__); \
return; \
}
#define CHECK_PTR_RETURN(ptr, default) \
if (!ptr) { \
fmt::println("Got invalid pointer?? {}:{}", __FILE__, __LINE__); \
return default; \
}
extern "C" {
/*

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#define CHECK_PTR(ptr) \
if (!ptr) { \
fmt::println("Got invalid pointer?? {}:{}", __FILE__, __LINE__); \
return; \
}
#define CHECK_PTR_RETURN(ptr, default) \
if (!ptr) { \
fmt::println("Got invalid pointer?? {}:{}", __FILE__, __LINE__); \
return default; \
}

View File

@@ -36,6 +36,8 @@ public class TimeSyncTest {
if (!PhotonTargetingJniLoader.load()) {
fail();
}
HAL.initialize(1000, 0);
}
@AfterAll
@@ -45,8 +47,6 @@ public class TimeSyncTest {
@Test
public void smoketest() throws InterruptedException {
HAL.initialize(1000, 0);
// NetworkTableInstance.getDefault().stopClient();
// NetworkTableInstance.getDefault().startServer();

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package wpiutil_extras;
import static org.junit.jupiter.api.Assertions.fail;
import edu.wpi.first.hal.HAL;
import java.io.IOException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.photonvision.jni.PhotonTargetingJniLoader;
import org.photonvision.jni.QueuedFileLogger;
import org.photonvision.jni.WpilibLoader;
public class FileLoggerTest {
@BeforeAll
public static void load_wpilib() throws UnsatisfiedLinkError, IOException {
if (!WpilibLoader.loadLibraries()) {
fail();
}
if (!PhotonTargetingJniLoader.load()) {
fail();
}
HAL.initialize(1000, 0);
}
@AfterAll
public static void teardown() {
HAL.shutdown();
}
@Test
public void smoketest() throws InterruptedException {
var logger = new QueuedFileLogger("/var/log/kern.log");
for (int i = 0; i < 100; i++) {
Thread.sleep(1000);
for (var line : logger.getNewlines()) {
System.out.println(" ->:" + line);
}
}
logger.stop();
}
}