Skip to content

Commit

Permalink
Fix init ordering lol
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Nov 4, 2024
1 parent 1c80fb7 commit 8556b61
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ public enum LogGroup {
Config,
CSCore,
NetworkTables,
System,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.photonvision.common.logging;

import edu.wpi.first.util.RuntimeDetector;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -29,9 +31,40 @@
import org.photonvision.common.dataflow.DataChangeService;
import org.photonvision.common.dataflow.events.OutgoingUIEvent;
import org.photonvision.common.util.TimedTaskManager;
import org.photonvision.jni.QueuedFileLogger;

@SuppressWarnings("unused")
/** TODO: get rid of static {} blocks and refactor to singleton pattern */
public class Logger {
private static final HashMap<LogGroup, LogLevel> levelMap = new HashMap<>();
private static final List<LogAppender> currentAppenders = new ArrayList<>();

private static final UILogAppender uiLogAppender = new UILogAppender();

// TODO why's the logger care about this? split it out
private static KernelLogListener klogListener = null;

static {
levelMap.put(LogGroup.Camera, LogLevel.INFO);
levelMap.put(LogGroup.General, LogLevel.INFO);
levelMap.put(LogGroup.WebServer, LogLevel.INFO);
levelMap.put(LogGroup.Data, LogLevel.INFO);
levelMap.put(LogGroup.VisionModule, LogLevel.INFO);
levelMap.put(LogGroup.Config, LogLevel.INFO);
levelMap.put(LogGroup.CSCore, LogLevel.TRACE);
levelMap.put(LogGroup.NetworkTables, LogLevel.DEBUG);
levelMap.put(LogGroup.System, LogLevel.DEBUG);

currentAppenders.add(new ConsoleLogAppender());
currentAppenders.add(uiLogAppender);
addFileAppender(PathManager.getInstance().getLogPath());

cleanLogs(PathManager.getInstance().getLogsDir());
}

public static void addKlongListener() {
klogListener = new KernelLogListener();
}

public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
Expand All @@ -50,8 +83,6 @@ public class Logger {
private static final List<Pair<String, LogLevel>> uiBacklog = new ArrayList<>();
private static boolean connected = false;

private static final UILogAppender uiLogAppender = new UILogAppender();

private final String className;
private final LogGroup group;

Expand Down Expand Up @@ -89,27 +120,6 @@ public static String format(
return builder.toString();
}

private static final HashMap<LogGroup, LogLevel> levelMap = new HashMap<>();
private static final List<LogAppender> currentAppenders = new ArrayList<>();

static {
levelMap.put(LogGroup.Camera, LogLevel.INFO);
levelMap.put(LogGroup.General, LogLevel.INFO);
levelMap.put(LogGroup.WebServer, LogLevel.INFO);
levelMap.put(LogGroup.Data, LogLevel.INFO);
levelMap.put(LogGroup.VisionModule, LogLevel.INFO);
levelMap.put(LogGroup.Config, LogLevel.INFO);
levelMap.put(LogGroup.CSCore, LogLevel.TRACE);
levelMap.put(LogGroup.NetworkTables, LogLevel.DEBUG);
}

static {
currentAppenders.add(new ConsoleLogAppender());
currentAppenders.add(uiLogAppender);
addFileAppender(PathManager.getInstance().getLogPath());
cleanLogs(PathManager.getInstance().getLogsDir());
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void addFileAppender(Path logFilePath) {
var file = logFilePath.toFile();
Expand Down Expand Up @@ -288,6 +298,38 @@ private static String convertStackTraceToString(Throwable throwable) {
}
}

private static class KernelLogListener {
QueuedFileLogger listener = null;
Logger logger = new Logger(KernelLogListener.class, LogGroup.General);

public KernelLogListener() {
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");
}

TimedTaskManager.getInstance().addTask("outputPrintk", this::outputNewPrintks, 1000);
}

public void outputNewPrintks() {
for (var msg : listener.getNewlines()) {
logger.log(msg, LogLevel.DEBUG);
}
}
}

private interface LogAppender {
void log(String message, LogLevel level);

Expand Down
1 change: 0 additions & 1 deletion photon-lib/pybind11
Submodule pybind11 deleted from a5b0cd
4 changes: 4 additions & 0 deletions photon-server/src/main/java/org/photonvision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ public static void main(String[] args) {
Logger.setLevel(LogGroup.General, logLevel);
logger.info("Logging initialized in debug mode.");

// after native libraries are loaded :(
Logger.addKlongListener();

// Add CSCore->Photon logger
PvCSCoreLogger.getInstance();

logger.debug("Loading ConfigManager...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public QueuedFileLogger(String path) {
public String[] getNewlines() {
String newBuffer = null;

synchronized(this) {
synchronized (this) {
if (m_handle == 0) {
System.err.println("QueuedFileLogger use after free");
return new String[0];
Expand All @@ -44,7 +44,7 @@ public String[] getNewlines() {
}

public void stop() {
synchronized(this) {
synchronized (this) {
if (m_handle != 0) {
QueuedFileLogger.destroy(m_handle);
m_handle = 0;
Expand All @@ -53,6 +53,8 @@ public void stop() {
}

private static native long create(String path);

private static native void destroy(long handle);

private static native String getNewLines(long handle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@

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;

import edu.wpi.first.hal.HAL;

public class FileLoggerTest {
@BeforeAll
public static void load_wpilib() throws UnsatisfiedLinkError, IOException {
Expand All @@ -50,8 +48,8 @@ public static void teardown() {

@Test
public void smoketest() throws InterruptedException {
var logger = new QueuedFileLogger("/home/matt/github/photonvision/foobar.txt");
for(int i = 0; i < 100; i++) {
var logger = new QueuedFileLogger("/var/log/kern.log");
for (int i = 0; i < 100; i++) {
Thread.sleep(1000);

for (var line : logger.getNewlines()) {
Expand Down

0 comments on commit 8556b61

Please sign in to comment.