[wpiutil, wpilib] Add FileLogger and log console output (#6977)

This commit is contained in:
Gold856
2024-09-13 01:13:06 -04:00
committed by GitHub
parent 32252f7d6a
commit 3bbbf86632
12 changed files with 428 additions and 0 deletions

View File

@@ -5,6 +5,7 @@
package edu.wpi.first.wpilibj;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.util.FileLogger;
import edu.wpi.first.util.WPIUtilJNI;
import edu.wpi.first.util.concurrent.Event;
import edu.wpi.first.util.datalog.DataLog;
@@ -52,6 +53,8 @@ public final class DataLogManager {
private static boolean m_ntLoggerEnabled = true;
private static int m_ntEntryLogger;
private static int m_ntConnLogger;
private static boolean m_consoleLoggerEnabled = true;
private static FileLogger m_consoleLogger;
private static StringLogEntry m_messageLog;
// if less than this much free space, delete log files until there is this much free space
@@ -121,6 +124,10 @@ public final class DataLogManager {
if (m_ntLoggerEnabled) {
startNtLog();
}
// Log console output
if (m_consoleLoggerEnabled) {
startConsoleLog();
}
} else if (m_stopped) {
m_log.setFilename(makeLogFilename(filename));
m_log.resume();
@@ -205,6 +212,25 @@ public final class DataLogManager {
}
}
/**
* Enable or disable logging of the console output. Defaults to enabled.
*
* @param enabled true to enable, false to disable
*/
public static synchronized void logConsoleOutput(boolean enabled) {
boolean wasEnabled = m_consoleLoggerEnabled;
m_consoleLoggerEnabled = enabled;
if (m_log == null) {
start();
return;
}
if (enabled && !wasEnabled) {
startConsoleLog();
} else if (!enabled && wasEnabled) {
stopConsoleLog();
}
}
private static String makeLogDir(String dir) {
if (!dir.isEmpty()) {
return dir;
@@ -266,6 +292,18 @@ public final class DataLogManager {
NetworkTableInstance.stopConnectionDataLog(m_ntConnLogger);
}
private static void startConsoleLog() {
if (RobotBase.isReal()) {
m_consoleLogger = new FileLogger("/home/lvuser/FRC_UserProgram.log", m_log, "console");
}
}
private static void stopConsoleLog() {
if (RobotBase.isReal()) {
m_consoleLogger.close();
}
}
private static void logMain() {
// based on free disk space, scan for "old" FRC_*.wpilog files and remove
{