-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
392 additions
and
55 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
photon-core/src/main/java/org/photonvision/common/logging/KernelLogLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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.common.logging; | ||
|
||
import edu.wpi.first.util.RuntimeDetector; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import org.photonvision.common.util.TimedTaskManager; | ||
import org.photonvision.jni.QueuedFileLogger; | ||
|
||
/** | ||
* Listens for and reproduces Linux kernel logs, from /var/log/kern.log, into the Photon logger | ||
* ecosystem | ||
*/ | ||
public class KernelLogLogger { | ||
private static KernelLogLogger INSTANCE; | ||
|
||
public static KernelLogLogger getInstance() { | ||
if (INSTANCE == null) { | ||
INSTANCE = new KernelLogLogger(); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
QueuedFileLogger listener = null; | ||
Logger logger = new Logger(KernelLogLogger.class, LogGroup.General); | ||
|
||
public KernelLogLogger() { | ||
if (RuntimeDetector.isLinux()) { | ||
logger.info("Listening for klogs on /var/log/dmesg ! Boot logs:"); | ||
|
||
try { | ||
var bootlog = Files.readAllLines(Path.of("/var/log/dmesg")); | ||
for (var line : bootlog) { | ||
logger.log(line, LogLevel.DEBUG); | ||
} | ||
} catch (IOException e) { | ||
logger.error("Couldn't read /var/log/dmesg - not printing boot logs"); | ||
} | ||
|
||
listener = new QueuedFileLogger("/var/log/kern.log"); | ||
} else { | ||
System.out.println("NOT for klogs"); | ||
} | ||
|
||
// arbitrary frequency to grab logs. The underlying native buffer will grow unbounded without | ||
// this, lol | ||
TimedTaskManager.getInstance().addTask("outputPrintk", this::outputNewPrintks, 1000); | ||
} | ||
|
||
public void outputNewPrintks() { | ||
for (var msg : listener.getNewlines()) { | ||
// We currently set all logs to debug regardless of their actual level | ||
logger.log(msg, LogLevel.DEBUG); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,5 @@ public enum LogGroup { | |
Config, | ||
CSCore, | ||
NetworkTables, | ||
System, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
photon-targeting/src/main/java/org/photonvision/jni/QueuedFileLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Oops, something went wrong.