[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

@@ -14,6 +14,7 @@
#include <networktables/NetworkTableInstance.h>
#include <wpi/DataLog.h>
#include <wpi/DataLogBackgroundWriter.h>
#include <wpi/FileLogger.h>
#include <wpi/SafeThread.h>
#include <wpi/StringExtras.h>
#include <wpi/fs.h>
@@ -37,6 +38,8 @@ struct Thread final : public wpi::SafeThread {
void StartNTLog();
void StopNTLog();
void StartConsoleLog();
void StopConsoleLog();
std::string m_logDir;
bool m_filenameOverride;
@@ -44,6 +47,8 @@ struct Thread final : public wpi::SafeThread {
bool m_ntLoggerEnabled = false;
NT_DataLogger m_ntEntryLogger = 0;
NT_ConnectionDataLogger m_ntConnLogger = 0;
bool m_consoleLoggerEnabled = false;
wpi::FileLogger m_consoleLogger;
wpi::log::StringLogEntry m_messageLog;
};
@@ -109,10 +114,12 @@ Thread::Thread(std::string_view dir, std::string_view filename, double period)
m_log{dir, MakeLogFilename(filename), period},
m_messageLog{m_log, "messages"} {
StartNTLog();
StartConsoleLog();
}
Thread::~Thread() {
StopNTLog();
StopConsoleLog();
}
void Thread::Main() {
@@ -297,6 +304,20 @@ void Thread::StopNTLog() {
}
}
void Thread::StartConsoleLog() {
if (!m_consoleLoggerEnabled && RobotBase::IsReal()) {
m_consoleLoggerEnabled = true;
m_consoleLogger = {"/home/lvuser/FRC_UserProgram.log", m_log, "output"};
}
}
void Thread::StopConsoleLog() {
if (m_consoleLoggerEnabled && RobotBase::IsReal()) {
m_consoleLoggerEnabled = false;
m_consoleLogger = {};
}
}
Instance::Instance(std::string_view dir, std::string_view filename,
double period) {
// Delete all previously existing FRC_TBD_*.wpilog files. These only exist
@@ -360,3 +381,13 @@ void DataLogManager::LogNetworkTables(bool enabled) {
}
}
}
void DataLogManager::LogConsoleOutput(bool enabled) {
if (auto thr = GetInstance().owner.GetThread()) {
if (enabled) {
thr->StartConsoleLog();
} else if (!enabled) {
thr->StopConsoleLog();
}
}
}