Skip to content

Commit 8dcf0b3

Browse files
authored
Create FileLogger JNI (#1517)
1 parent a99a8e7 commit 8dcf0b3

File tree

14 files changed

+392
-55
lines changed

14 files changed

+392
-55
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) Photon Vision.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.photonvision.common.logging;
19+
20+
import edu.wpi.first.util.RuntimeDetector;
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import org.photonvision.common.util.TimedTaskManager;
25+
import org.photonvision.jni.QueuedFileLogger;
26+
27+
/**
28+
* Listens for and reproduces Linux kernel logs, from /var/log/kern.log, into the Photon logger
29+
* ecosystem
30+
*/
31+
public class KernelLogLogger {
32+
private static KernelLogLogger INSTANCE;
33+
34+
public static KernelLogLogger getInstance() {
35+
if (INSTANCE == null) {
36+
INSTANCE = new KernelLogLogger();
37+
}
38+
return INSTANCE;
39+
}
40+
41+
QueuedFileLogger listener = null;
42+
Logger logger = new Logger(KernelLogLogger.class, LogGroup.General);
43+
44+
public KernelLogLogger() {
45+
if (RuntimeDetector.isLinux()) {
46+
logger.info("Listening for klogs on /var/log/dmesg ! Boot logs:");
47+
48+
try {
49+
var bootlog = Files.readAllLines(Path.of("/var/log/dmesg"));
50+
for (var line : bootlog) {
51+
logger.log(line, LogLevel.DEBUG);
52+
}
53+
} catch (IOException e) {
54+
logger.error("Couldn't read /var/log/dmesg - not printing boot logs");
55+
}
56+
57+
listener = new QueuedFileLogger("/var/log/kern.log");
58+
} else {
59+
System.out.println("NOT for klogs");
60+
}
61+
62+
// arbitrary frequency to grab logs. The underlying native buffer will grow unbounded without
63+
// this, lol
64+
TimedTaskManager.getInstance().addTask("outputPrintk", this::outputNewPrintks, 1000);
65+
}
66+
67+
public void outputNewPrintks() {
68+
for (var msg : listener.getNewlines()) {
69+
// We currently set all logs to debug regardless of their actual level
70+
logger.log(msg, LogLevel.DEBUG);
71+
}
72+
}
73+
}

photon-core/src/main/java/org/photonvision/common/logging/LogGroup.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ public enum LogGroup {
2626
Config,
2727
CSCore,
2828
NetworkTables,
29+
System,
2930
}

photon-core/src/main/java/org/photonvision/common/logging/Logger.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,34 @@
3030
import org.photonvision.common.dataflow.events.OutgoingUIEvent;
3131
import org.photonvision.common.util.TimedTaskManager;
3232

33-
@SuppressWarnings("unused")
33+
/** TODO: get rid of static {} blocks and refactor to singleton pattern */
3434
public class Logger {
35+
private static final HashMap<LogGroup, LogLevel> levelMap = new HashMap<>();
36+
private static final List<LogAppender> currentAppenders = new ArrayList<>();
37+
38+
private static final UILogAppender uiLogAppender = new UILogAppender();
39+
40+
// // TODO why's the logger care about this? split it out
41+
// private static KernelLogLogger klogListener = null;
42+
43+
static {
44+
levelMap.put(LogGroup.Camera, LogLevel.INFO);
45+
levelMap.put(LogGroup.General, LogLevel.INFO);
46+
levelMap.put(LogGroup.WebServer, LogLevel.INFO);
47+
levelMap.put(LogGroup.Data, LogLevel.INFO);
48+
levelMap.put(LogGroup.VisionModule, LogLevel.INFO);
49+
levelMap.put(LogGroup.Config, LogLevel.INFO);
50+
levelMap.put(LogGroup.CSCore, LogLevel.TRACE);
51+
levelMap.put(LogGroup.NetworkTables, LogLevel.DEBUG);
52+
levelMap.put(LogGroup.System, LogLevel.DEBUG);
53+
54+
currentAppenders.add(new ConsoleLogAppender());
55+
currentAppenders.add(uiLogAppender);
56+
addFileAppender(PathManager.getInstance().getLogPath());
57+
58+
cleanLogs(PathManager.getInstance().getLogsDir());
59+
}
60+
3561
public static final String ANSI_RESET = "\u001B[0m";
3662
public static final String ANSI_BLACK = "\u001B[30m";
3763
public static final String ANSI_RED = "\u001B[31m";
@@ -50,8 +76,6 @@ public class Logger {
5076
private static final List<Pair<String, LogLevel>> uiBacklog = new ArrayList<>();
5177
private static boolean connected = false;
5278

53-
private static final UILogAppender uiLogAppender = new UILogAppender();
54-
5579
private final String className;
5680
private final LogGroup group;
5781

@@ -89,27 +113,6 @@ public static String format(
89113
return builder.toString();
90114
}
91115

92-
private static final HashMap<LogGroup, LogLevel> levelMap = new HashMap<>();
93-
private static final List<LogAppender> currentAppenders = new ArrayList<>();
94-
95-
static {
96-
levelMap.put(LogGroup.Camera, LogLevel.INFO);
97-
levelMap.put(LogGroup.General, LogLevel.INFO);
98-
levelMap.put(LogGroup.WebServer, LogLevel.INFO);
99-
levelMap.put(LogGroup.Data, LogLevel.INFO);
100-
levelMap.put(LogGroup.VisionModule, LogLevel.INFO);
101-
levelMap.put(LogGroup.Config, LogLevel.INFO);
102-
levelMap.put(LogGroup.CSCore, LogLevel.TRACE);
103-
levelMap.put(LogGroup.NetworkTables, LogLevel.DEBUG);
104-
}
105-
106-
static {
107-
currentAppenders.add(new ConsoleLogAppender());
108-
currentAppenders.add(uiLogAppender);
109-
addFileAppender(PathManager.getInstance().getLogPath());
110-
cleanLogs(PathManager.getInstance().getLogsDir());
111-
}
112-
113116
@SuppressWarnings("ResultOfMethodCallIgnored")
114117
public static void addFileAppender(Path logFilePath) {
115118
var file = logFilePath.toFile();

photon-server/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ apply from: "${rootDir}/shared/common.gradle"
1111
dependencies {
1212
implementation project(':photon-core')
1313

14+
// Zip
15+
implementation 'org.zeroturnaround:zt-zip:1.14'
16+
1417
// Needed for Javalin Runtime Logging
1518
implementation "org.slf4j:slf4j-simple:2.0.7"
1619
}

photon-server/src/main/java/org/photonvision/Main.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.photonvision.common.hardware.HardwareManager;
3434
import org.photonvision.common.hardware.PiVersion;
3535
import org.photonvision.common.hardware.Platform;
36+
import org.photonvision.common.logging.KernelLogLogger;
3637
import org.photonvision.common.logging.LogGroup;
3738
import org.photonvision.common.logging.LogLevel;
3839
import org.photonvision.common.logging.Logger;
@@ -437,6 +438,10 @@ public static void main(String[] args) {
437438
Logger.setLevel(LogGroup.General, logLevel);
438439
logger.info("Logging initialized in debug mode.");
439440

441+
// Add Linux kernel log->Photon logger
442+
KernelLogLogger.getInstance();
443+
444+
// Add CSCore->Photon logger
440445
PvCSCoreLogger.getInstance();
441446

442447
logger.debug("Loading ConfigManager...");

photon-server/src/main/java/org/photonvision/server/RequestHandler.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import org.photonvision.vision.calibration.CameraCalibrationCoefficients;
5454
import org.photonvision.vision.camera.CameraQuirk;
5555
import org.photonvision.vision.processes.VisionModuleManager;
56+
import org.zeroturnaround.zip.ZipUtil;
5657

5758
public class RequestHandler {
5859
// Treat all 2XX calls as "INFO"
@@ -422,20 +423,34 @@ public static void onLogExportRequest(Context ctx) {
422423
try {
423424
ShellExec shell = new ShellExec();
424425
var tempPath = Files.createTempFile("photonvision-journalctl", ".txt");
425-
shell.executeBashCommand("journalctl -u photonvision.service > " + tempPath.toAbsolutePath());
426+
var tempPath2 = Files.createTempFile("photonvision-kernelogs", ".txt");
427+
shell.executeBashCommand(
428+
"journalctl -u photonvision.service > "
429+
+ tempPath.toAbsolutePath()
430+
+ " && journalctl -k > "
431+
+ tempPath2.toAbsolutePath());
426432

427433
while (!shell.isOutputCompleted()) {
428434
// TODO: add timeout
429435
}
430436

431437
if (shell.getExitCode() == 0) {
432-
// Wrote to the temp file! Add it to the ctx
433-
var stream = new FileInputStream(tempPath.toFile());
434-
ctx.contentType("text/plain");
435-
ctx.header("Content-Disposition", "attachment; filename=\"photonvision-journalctl.txt\"");
436-
ctx.status(200);
438+
// Wrote to the temp file! Zip and yeet it to the client
439+
440+
var out = Files.createTempFile("photonvision-logs", "zip").toFile();
441+
442+
try {
443+
ZipUtil.packEntries(new File[] {tempPath.toFile(), tempPath2.toFile()}, out);
444+
} catch (Exception e) {
445+
e.printStackTrace();
446+
}
447+
448+
var stream = new FileInputStream(out);
449+
ctx.contentType("application/zip");
450+
ctx.header("Content-Disposition", "attachment; filename=\"photonvision-logs.zip\"");
437451
ctx.result(stream);
438-
logger.info("Uploading settings with size " + stream.available());
452+
ctx.status(200);
453+
logger.info("Outputting log ZIP with size " + stream.available());
439454
} else {
440455
ctx.status(500);
441456
ctx.result("The journalctl service was unable to export logs");

photon-targeting/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,6 @@ nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpimath")
220220
nativeConfig.dependencies.add wpilibTools.deps.wpilib("wpinet")
221221
nativeConfig.dependencies.add wpilibTools.deps.wpilib("ntcore")
222222
nativeConfig.dependencies.add wpilibTools.deps.wpilib("hal")
223+
nativeConfig.dependencies.add wpilibTools.deps.wpilib("cscore")
224+
nativeConfig.dependencies.add wpilibTools.deps.wpilibOpenCv("frc" + openCVYear, wpi.versions.opencvVersion.get())
225+
nativeConfig.dependencies.add wpilibTools.deps.wpilib("apriltag")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) Photon Vision.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.photonvision.jni;
19+
20+
public class QueuedFileLogger {
21+
long m_handle = 0;
22+
23+
public QueuedFileLogger(String path) {
24+
m_handle = QueuedFileLogger.create(path);
25+
}
26+
27+
public String[] getNewlines() {
28+
String newBuffer = null;
29+
30+
synchronized (this) {
31+
if (m_handle == 0) {
32+
System.err.println("QueuedFileLogger use after free");
33+
return new String[0];
34+
}
35+
36+
newBuffer = QueuedFileLogger.getNewLines(m_handle);
37+
}
38+
39+
if (newBuffer == null) {
40+
return new String[0];
41+
}
42+
43+
return newBuffer.split("\n");
44+
}
45+
46+
public void stop() {
47+
synchronized (this) {
48+
if (m_handle != 0) {
49+
QueuedFileLogger.destroy(m_handle);
50+
m_handle = 0;
51+
}
52+
}
53+
}
54+
55+
private static native long create(String path);
56+
57+
private static native void destroy(long handle);
58+
59+
private static native String getNewLines(long handle);
60+
}

0 commit comments

Comments
 (0)