mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpiutil, wpilib] Add FileLogger and log console output (#6977)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user