[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 <fmt/format.h>
#include <networktables/NetworkTableInstance.h>
#include <wpi/DataLogBackgroundWriter.h>
#include <wpi/FileLogger.h>
#include <wpi/SafeThread.h>
#include <wpi/StringExtras.h>
#include <wpi/fs.h>
@@ -191,6 +192,8 @@ struct Thread final : public wpi::SafeThread {
void StartNTLog();
void StopNTLog();
void StartConsoleLog();
void StopConsoleLog();
std::string m_logDir;
bool m_filenameOverride;
@@ -198,6 +201,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;
};
@@ -452,6 +457,20 @@ void Thread::StopNTLog() {
}
}
void Thread::StartConsoleLog() {
if (!m_consoleLoggerEnabled) {
m_consoleLoggerEnabled = true;
m_consoleLogger = {"/home/lvuser/FRC_UserProgram.log", m_log, "output"};
}
}
void Thread::StopConsoleLog() {
if (m_consoleLoggerEnabled) {
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
@@ -516,6 +535,16 @@ 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();
}
}
}
void DataLogManager::SignalNewDSDataOccur() {
wpi::SetSignalObject(DriverStation::gNewDataEvent);
}
@@ -546,6 +575,10 @@ void DLM_LogNetworkTables(int enabled) {
DataLogManager::LogNetworkTables(enabled);
}
void DLM_LogConsoleOutput(int enabled) {
DataLogManager::LogConsoleOutput(enabled);
}
void DLM_SignalNewDSDataOccur(void) {
DataLogManager::SignalNewDSDataOccur();
}

View File

@@ -89,6 +89,11 @@ class DataLogManager final {
*/
static void LogNetworkTables(bool enabled);
/**
* Enable or disable logging of the console output. Defaults to enabled.
* @param enabled true to enable, false to disable
*/
static void LogConsoleOutput(bool enabled);
/**
* Signal new DS data is available.
*/
@@ -152,6 +157,13 @@ const char* DLM_GetLogDir(void);
*/
void DLM_LogNetworkTables(int enabled);
/**
* Enable or disable logging of the console output. Defaults to enabled.
* @param enabled true to enable, false to disable
*/
void DLM_LogConsoleOutput(int enabled);
/**
* Signal new DS data is available.
*/

View File

@@ -1,6 +1,7 @@
DLM_GetLog
DLM_GetLogDir
DLM_Log
DLM_LogConsoleOutput
DLM_LogNetworkTables
DLM_SignalNewDSDataOccur
DLM_Start