From a04d1b4f9745385b02dd9018296421cf10544fe0 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 24 May 2021 23:36:26 -0700 Subject: [PATCH] [wpilibc] DriverStation: Remove ReportError and ReportWarning Change use cases to directly call FRC_ReportError. --- cameraserver/.styleguide | 1 + .../native/cpp/cameraserver/CameraServer.cpp | 8 +-- .../cpp/cameraserver/CameraServerShared.cpp | 9 ++- .../main/native/cpp/vision/VisionRunner.cpp | 2 +- .../include/cameraserver/CameraServerShared.h | 29 +++++++-- wpilibc/src/main/native/cpp/ADXL362.cpp | 4 +- wpilibc/src/main/native/cpp/ADXRS450_Gyro.cpp | 4 +- wpilibc/src/main/native/cpp/AnalogEncoder.cpp | 5 +- wpilibc/src/main/native/cpp/DriverStation.cpp | 63 +++++++------------ .../src/main/native/cpp/DutyCycleEncoder.cpp | 5 +- wpilibc/src/main/native/cpp/Errors.cpp | 8 ++- .../main/native/cpp/IterativeRobotBase.cpp | 13 +--- wpilibc/src/main/native/cpp/Tracer.cpp | 4 +- wpilibc/src/main/native/cpp/Watchdog.cpp | 14 ++--- .../native/cpp/controller/PIDController.cpp | 11 ++-- .../cpp/shuffleboard/RecordingController.cpp | 5 +- .../simulation/DifferentialDrivetrainSim.cpp | 2 + wpilibc/src/main/native/cppcs/RobotBase.cpp | 25 ++++---- .../main/native/include/frc/DriverStation.h | 42 +++++-------- .../src/main/native/include/frc/WPIErrors.mac | 1 + .../main/native/include/frc/WPIWarnings.mac | 1 + .../src/test/native/cpp/DriverStationTest.cpp | 28 +++++---- .../cpp/subsystems/OnBoardIO.cpp | 10 +-- wpimath/.styleguide | 1 + wpimath/src/main/native/cpp/MathShared.cpp | 2 +- .../main/native/include/wpimath/MathShared.h | 18 ++++-- 26 files changed, 169 insertions(+), 146 deletions(-) diff --git a/cameraserver/.styleguide b/cameraserver/.styleguide index bea7643f55..63515d2920 100644 --- a/cameraserver/.styleguide +++ b/cameraserver/.styleguide @@ -12,6 +12,7 @@ repoRootNameOverride { } includeOtherLibs { + ^fmt/ ^hal/ ^networktables/ ^opencv2/ diff --git a/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp b/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp index fa18a8e14b..222753f991 100644 --- a/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp +++ b/cameraserver/src/main/native/cpp/cameraserver/CameraServer.cpp @@ -625,8 +625,8 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) { auto kind = it->second.GetKind(); if (kind != cs::VideoSink::kCv) { auto csShared = GetCameraServerShared(); - csShared->SetCameraServerError("expected OpenCV sink, but got " + - wpi::Twine(kind)); + csShared->SetCameraServerError("expected OpenCV sink, but got {}", + kind); return cs::CvSink{}; } return *static_cast(&it->second); @@ -648,7 +648,7 @@ cs::CvSink CameraServer::GetVideo(const wpi::Twine& name) { auto it = m_impl->m_sources.find(nameStr); if (it == m_impl->m_sources.end()) { auto csShared = GetCameraServerShared(); - csShared->SetCameraServerError("could not find camera " + nameStr); + csShared->SetCameraServerError("could not find camera {}", nameStr); return cs::CvSink{}; } source = it->second; @@ -711,7 +711,7 @@ cs::VideoSink CameraServer::GetServer(const wpi::Twine& name) { auto it = m_impl->m_sinks.find(nameStr); if (it == m_impl->m_sinks.end()) { auto csShared = GetCameraServerShared(); - csShared->SetCameraServerError("could not find server " + nameStr); + csShared->SetCameraServerError("could not find server {}", nameStr); return cs::VideoSink{}; } return it->second; diff --git a/cameraserver/src/main/native/cpp/cameraserver/CameraServerShared.cpp b/cameraserver/src/main/native/cpp/cameraserver/CameraServerShared.cpp index 3655861b5c..6d1ebc2c17 100644 --- a/cameraserver/src/main/native/cpp/cameraserver/CameraServerShared.cpp +++ b/cameraserver/src/main/native/cpp/cameraserver/CameraServerShared.cpp @@ -12,9 +12,12 @@ class DefaultCameraServerShared : public frc::CameraServerShared { void ReportUsbCamera(int id) override {} void ReportAxisCamera(int id) override {} void ReportVideoServer(int id) override {} - void SetCameraServerError(const wpi::Twine& error) override {} - void SetVisionRunnerError(const wpi::Twine& error) override {} - void ReportDriverStationError(const wpi::Twine& error) override {} + void SetCameraServerErrorV(fmt::string_view format, + fmt::format_args args) override {} + void SetVisionRunnerErrorV(fmt::string_view format, + fmt::format_args args) override {} + void ReportDriverStationErrorV(fmt::string_view format, + fmt::format_args args) override {} std::pair GetRobotMainThreadId() const override { return std::make_pair(std::thread::id(), false); } diff --git a/cameraserver/src/main/native/cpp/vision/VisionRunner.cpp b/cameraserver/src/main/native/cpp/vision/VisionRunner.cpp index e9f1785a0a..b55325a7df 100644 --- a/cameraserver/src/main/native/cpp/vision/VisionRunner.cpp +++ b/cameraserver/src/main/native/cpp/vision/VisionRunner.cpp @@ -33,7 +33,7 @@ void VisionRunnerBase::RunOnce() { auto frameTime = m_cvSink.GrabFrame(*m_image); if (frameTime == 0) { auto error = m_cvSink.GetError(); - csShared->ReportDriverStationError(error); + csShared->ReportDriverStationError(error.c_str()); } else { DoProcess(*m_image); } diff --git a/cameraserver/src/main/native/include/cameraserver/CameraServerShared.h b/cameraserver/src/main/native/include/cameraserver/CameraServerShared.h index 4ab00876c3..f6c6ae7a04 100644 --- a/cameraserver/src/main/native/include/cameraserver/CameraServerShared.h +++ b/cameraserver/src/main/native/include/cameraserver/CameraServerShared.h @@ -8,7 +8,7 @@ #include #include -#include +#include namespace frc { class CameraServerShared { @@ -17,10 +17,31 @@ class CameraServerShared { virtual void ReportUsbCamera(int id) = 0; virtual void ReportAxisCamera(int id) = 0; virtual void ReportVideoServer(int id) = 0; - virtual void SetCameraServerError(const wpi::Twine& error) = 0; - virtual void SetVisionRunnerError(const wpi::Twine& error) = 0; - virtual void ReportDriverStationError(const wpi::Twine& error) = 0; + virtual void SetCameraServerErrorV(fmt::string_view format, + fmt::format_args args) = 0; + virtual void SetVisionRunnerErrorV(fmt::string_view format, + fmt::format_args args) = 0; + virtual void ReportDriverStationErrorV(fmt::string_view format, + fmt::format_args args) = 0; virtual std::pair GetRobotMainThreadId() const = 0; + + template + inline void SetCameraServerError(const S& format, Args&&... args) { + SetCameraServerErrorV(format, + fmt::make_args_checked(format, args...)); + } + + template + inline void SetVisionRunnerError(const S& format, Args&&... args) { + SetVisionRunnerErrorV(format, + fmt::make_args_checked(format, args...)); + } + + template + inline void ReportDriverStationError(const S& format, Args&&... args) { + ReportDriverStationErrorV(format, + fmt::make_args_checked(format, args...)); + } }; CameraServerShared* GetCameraServerShared(); diff --git a/wpilibc/src/main/native/cpp/ADXL362.cpp b/wpilibc/src/main/native/cpp/ADXL362.cpp index 884ee9c4c5..3730b83a98 100644 --- a/wpilibc/src/main/native/cpp/ADXL362.cpp +++ b/wpilibc/src/main/native/cpp/ADXL362.cpp @@ -6,7 +6,7 @@ #include -#include "frc/DriverStation.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -56,7 +56,7 @@ ADXL362::ADXL362(SPI::Port port, Range range) commands[2] = 0; m_spi.Transaction(commands, commands, 3); if (commands[2] != 0xF2) { - DriverStation::ReportError("could not find ADXL362"); + FRC_ReportError(err::Error, "{}", "could not find ADXL362"); m_gsPerLSB = 0.0; return; } diff --git a/wpilibc/src/main/native/cpp/ADXRS450_Gyro.cpp b/wpilibc/src/main/native/cpp/ADXRS450_Gyro.cpp index 2c1c62b14e..68f0136630 100644 --- a/wpilibc/src/main/native/cpp/ADXRS450_Gyro.cpp +++ b/wpilibc/src/main/native/cpp/ADXRS450_Gyro.cpp @@ -6,7 +6,7 @@ #include -#include "frc/DriverStation.h" +#include "frc/Errors.h" #include "frc/Timer.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -46,7 +46,7 @@ ADXRS450_Gyro::ADXRS450_Gyro(SPI::Port port) if (!m_simDevice) { // Validate the part ID if ((ReadRegister(kPIDRegister) & 0xff00) != 0x5200) { - DriverStation::ReportError("could not find ADXRS450 gyro"); + FRC_ReportError(err::Error, "{}", "could not find ADXRS450 gyro"); return; } diff --git a/wpilibc/src/main/native/cpp/AnalogEncoder.cpp b/wpilibc/src/main/native/cpp/AnalogEncoder.cpp index f82c929b74..f1b5db14af 100644 --- a/wpilibc/src/main/native/cpp/AnalogEncoder.cpp +++ b/wpilibc/src/main/native/cpp/AnalogEncoder.cpp @@ -7,7 +7,7 @@ #include "frc/AnalogInput.h" #include "frc/Base.h" #include "frc/Counter.h" -#include "frc/DriverStation.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableBuilder.h" using namespace frc; @@ -72,7 +72,8 @@ units::turn_t AnalogEncoder::Get() const { } } - frc::DriverStation::GetInstance().ReportWarning( + FRC_ReportError( + warn::Warning, "{}", "Failed to read Analog Encoder. Potential Speed Overrun. Returning last " "value"); return m_lastPosition; diff --git a/wpilibc/src/main/native/cpp/DriverStation.cpp b/wpilibc/src/main/native/cpp/DriverStation.cpp index 0756d09ac3..babc702570 100644 --- a/wpilibc/src/main/native/cpp/DriverStation.cpp +++ b/wpilibc/src/main/native/cpp/DriverStation.cpp @@ -14,8 +14,6 @@ #include #include #include -#include -#include #include "frc/Errors.h" #include "frc/MotorSafety.h" @@ -113,31 +111,6 @@ DriverStation& DriverStation::GetInstance() { return instance; } -void DriverStation::ReportError(const wpi::Twine& error) { - wpi::SmallString<128> temp; - HAL_SendError(1, 1, 0, error.toNullTerminatedStringRef(temp).data(), "", "", - 1); -} - -void DriverStation::ReportWarning(const wpi::Twine& error) { - wpi::SmallString<128> temp; - HAL_SendError(0, 1, 0, error.toNullTerminatedStringRef(temp).data(), "", "", - 1); -} - -void DriverStation::ReportError(bool isError, int32_t code, - const wpi::Twine& error, - const wpi::Twine& location, - const wpi::Twine& stack) { - wpi::SmallString<128> errorTemp; - wpi::SmallString<128> locationTemp; - wpi::SmallString<128> stackTemp; - HAL_SendError(isError, code, 0, - error.toNullTerminatedStringRef(errorTemp).data(), - location.toNullTerminatedStringRef(locationTemp).data(), - stack.toNullTerminatedStringRef(stackTemp).data(), 1); -} - bool DriverStation::GetStickButton(int stick, int button) { if (stick < 0 || stick >= kJoystickPorts) { FRC_ReportError(warn::BadJoystickIndex, "stick {} out of range", stick); @@ -145,7 +118,7 @@ bool DriverStation::GetStickButton(int stick, int button) { } if (button <= 0) { ReportJoystickUnpluggedError( - "ERROR: Button indexes begin at 1 in WPILib for C++ and Java"); + "Joystick Button {} index out of range; indexes begin at 1", button); return false; } @@ -154,7 +127,9 @@ bool DriverStation::GetStickButton(int stick, int button) { if (button > buttons.count) { ReportJoystickUnpluggedWarning( - "Joystick Button missing, check if all controllers are plugged in"); + "Joystick Button {} missing (max {}), check if all controllers are " + "plugged in", + button, buttons.count); return false; } @@ -168,7 +143,7 @@ bool DriverStation::GetStickButtonPressed(int stick, int button) { } if (button <= 0) { ReportJoystickUnpluggedError( - "ERROR: Button indexes begin at 1 in WPILib for C++ and Java"); + "Joystick Button {} index out of range; indexes begin at 1", button); return false; } @@ -177,7 +152,9 @@ bool DriverStation::GetStickButtonPressed(int stick, int button) { if (button > buttons.count) { ReportJoystickUnpluggedWarning( - "Joystick Button missing, check if all controllers are plugged in"); + "Joystick Button {} missing (max {}), check if all controllers are " + "plugged in", + button, buttons.count); return false; } std::unique_lock lock(m_buttonEdgeMutex); @@ -197,7 +174,7 @@ bool DriverStation::GetStickButtonReleased(int stick, int button) { } if (button <= 0) { ReportJoystickUnpluggedError( - "ERROR: Button indexes begin at 1 in WPILib for C++ and Java"); + "Joystick Button {} index out of range; indexes begin at 1", button); return false; } @@ -206,7 +183,9 @@ bool DriverStation::GetStickButtonReleased(int stick, int button) { if (button > buttons.count) { ReportJoystickUnpluggedWarning( - "Joystick Button missing, check if all controllers are plugged in"); + "Joystick Button {} missing (max {}), check if all controllers are " + "plugged in", + button, buttons.count); return false; } std::unique_lock lock(m_buttonEdgeMutex); @@ -234,7 +213,9 @@ double DriverStation::GetStickAxis(int stick, int axis) { if (axis >= axes.count) { ReportJoystickUnpluggedWarning( - "Joystick Axis missing, check if all controllers are plugged in"); + "Joystick Axis {} missing (max {}), check if all controllers are " + "plugged in", + axis, axes.count); return 0.0; } @@ -256,7 +237,9 @@ int DriverStation::GetStickPOV(int stick, int pov) { if (pov >= povs.count) { ReportJoystickUnpluggedWarning( - "Joystick POV missing, check if all controllers are plugged in"); + "Joystick POV {} missing (max {}), check if all controllers are " + "plugged in", + pov, povs.count); return -1; } @@ -600,19 +583,21 @@ DriverStation::DriverStation() { m_dsThread = std::thread(&DriverStation::Run, this); } -void DriverStation::ReportJoystickUnpluggedError(const wpi::Twine& message) { +void DriverStation::ReportJoystickUnpluggedErrorV(fmt::string_view format, + fmt::format_args args) { double currentTime = Timer::GetFPGATimestamp(); if (currentTime > m_nextMessageTime) { - ReportError(message); + ReportErrorV(err::Error, "", 0, "", format, args); m_nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval; } } -void DriverStation::ReportJoystickUnpluggedWarning(const wpi::Twine& message) { +void DriverStation::ReportJoystickUnpluggedWarningV(fmt::string_view format, + fmt::format_args args) { if (IsFMSAttached() || !m_silenceJoystickWarning) { double currentTime = Timer::GetFPGATimestamp(); if (currentTime > m_nextMessageTime) { - ReportWarning(message); + ReportErrorV(warn::Warning, "", 0, "", format, args); m_nextMessageTime = currentTime + kJoystickUnpluggedMessageInterval; } } diff --git a/wpilibc/src/main/native/cpp/DutyCycleEncoder.cpp b/wpilibc/src/main/native/cpp/DutyCycleEncoder.cpp index d3dc95c337..49b3cc79a0 100644 --- a/wpilibc/src/main/native/cpp/DutyCycleEncoder.cpp +++ b/wpilibc/src/main/native/cpp/DutyCycleEncoder.cpp @@ -8,8 +8,8 @@ #include "frc/Counter.h" #include "frc/DigitalInput.h" #include "frc/DigitalSource.h" -#include "frc/DriverStation.h" #include "frc/DutyCycle.h" +#include "frc/Errors.h" #include "frc/smartdashboard/SendableBuilder.h" using namespace frc; @@ -94,7 +94,8 @@ units::turn_t DutyCycleEncoder::Get() const { } } - frc::DriverStation::GetInstance().ReportWarning( + FRC_ReportError( + warn::Warning, "{}", "Failed to read DutyCycle Encoder. Potential Speed Overrun. Returning " "last value"); return m_lastPosition; diff --git a/wpilibc/src/main/native/cpp/Errors.cpp b/wpilibc/src/main/native/cpp/Errors.cpp index 5e67961c9d..ea60b1b15b 100644 --- a/wpilibc/src/main/native/cpp/Errors.cpp +++ b/wpilibc/src/main/native/cpp/Errors.cpp @@ -35,13 +35,15 @@ void RuntimeError::Report() const { } const char* frc::GetErrorMessage(int32_t* code) { - using namespace err; - using namespace warn; switch (*code) { #define S(label, offset, message) \ - case label: \ + case err::label: \ return message; #include "frc/WPIErrors.mac" +#undef S +#define S(label, offset, message) \ + case warn::label: \ + return message; #include "frc/WPIWarnings.mac" #undef S default: diff --git a/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp b/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp index 92f2deb4ed..6202e5bff0 100644 --- a/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp +++ b/wpilibc/src/main/native/cpp/IterativeRobotBase.cpp @@ -6,11 +6,9 @@ #include #include -#include -#include #include -#include "frc/DriverStation.h" +#include "frc/Errors.h" #include "frc/livewindow/LiveWindow.h" #include "frc/shuffleboard/Shuffleboard.h" #include "frc/smartdashboard/SmartDashboard.h" @@ -197,11 +195,6 @@ void IterativeRobotBase::LoopFunc() { } void IterativeRobotBase::PrintLoopOverrunMessage() { - wpi::SmallString<128> str; - wpi::raw_svector_ostream buf(str); - - buf << "Loop time of " << wpi::format("%.6f", m_period.to()) - << "s overrun\n"; - - DriverStation::ReportWarning(str); + FRC_ReportError(err::Error, "Loop time of {:.6f}s overrun", + m_period.to()); } diff --git a/wpilibc/src/main/native/cpp/Tracer.cpp b/wpilibc/src/main/native/cpp/Tracer.cpp index a0fe5f693d..9aa0116f16 100644 --- a/wpilibc/src/main/native/cpp/Tracer.cpp +++ b/wpilibc/src/main/native/cpp/Tracer.cpp @@ -8,7 +8,7 @@ #include #include -#include "frc/DriverStation.h" +#include "frc/Errors.h" using namespace frc; @@ -36,7 +36,7 @@ void Tracer::PrintEpochs() { wpi::raw_svector_ostream os(buf); PrintEpochs(os); if (!buf.empty()) { - DriverStation::ReportWarning(buf); + FRC_ReportError(warn::Warning, "{}", buf.c_str()); } } diff --git a/wpilibc/src/main/native/cpp/Watchdog.cpp b/wpilibc/src/main/native/cpp/Watchdog.cpp index 27dc68846a..f1e10ca8e5 100644 --- a/wpilibc/src/main/native/cpp/Watchdog.cpp +++ b/wpilibc/src/main/native/cpp/Watchdog.cpp @@ -5,15 +5,14 @@ #include "frc/Watchdog.h" #include +#include #include +#include #include -#include -#include +#include #include -#include -#include "frc/DriverStation.h" #include "frc/Errors.h" #include "frc2/Timer.h" @@ -114,11 +113,8 @@ void Watchdog::Impl::Main() { if (now - watchdog->m_lastTimeoutPrintTime > kMinPrintPeriod) { watchdog->m_lastTimeoutPrintTime = now; if (!watchdog->m_suppressTimeoutMessage) { - wpi::SmallString<128> buf; - wpi::raw_svector_ostream err(buf); - err << "Watchdog not fed within " - << wpi::format("%.6f", watchdog->m_timeout.to()) << "s\n"; - frc::DriverStation::ReportWarning(err.str()); + FRC_ReportError(warn::Warning, "Watchdog not fed within {:.6f}s", + watchdog->m_timeout.to()); } } diff --git a/wpilibc/src/main/native/cpp/controller/PIDController.cpp b/wpilibc/src/main/native/cpp/controller/PIDController.cpp index aaa7461d69..054b045227 100644 --- a/wpilibc/src/main/native/cpp/controller/PIDController.cpp +++ b/wpilibc/src/main/native/cpp/controller/PIDController.cpp @@ -9,7 +9,7 @@ #include -#include "frc/DriverStation.h" +#include "frc/Errors.h" #include "frc/MathUtil.h" #include "frc/smartdashboard/SendableBuilder.h" #include "frc/smartdashboard/SendableRegistry.h" @@ -20,10 +20,13 @@ PIDController::PIDController(double Kp, double Ki, double Kd, units::second_t period) : m_Kp(Kp), m_Ki(Ki), m_Kd(Kd), m_period(period) { if (period <= 0_s) { - frc::DriverStation::ReportError( - "Controller period must be a non-zero positive number!"); + FRC_ReportError( + frc::err::Error, + "Controller period must be a non-zero positive number, got {}!", + period.to()); m_period = 20_ms; - frc::DriverStation::ReportWarning("Controller period defaulted to 20ms."); + FRC_ReportError(frc::warn::Warning, "{}", + "Controller period defaulted to 20ms."); } static int instances = 0; instances++; diff --git a/wpilibc/src/main/native/cpp/shuffleboard/RecordingController.cpp b/wpilibc/src/main/native/cpp/shuffleboard/RecordingController.cpp index 2af2b6f782..ecdf977d72 100644 --- a/wpilibc/src/main/native/cpp/shuffleboard/RecordingController.cpp +++ b/wpilibc/src/main/native/cpp/shuffleboard/RecordingController.cpp @@ -4,7 +4,7 @@ #include "frc/shuffleboard/RecordingController.h" -#include "frc/DriverStation.h" +#include "frc/Errors.h" using namespace frc; using namespace frc::detail; @@ -38,7 +38,8 @@ void RecordingController::AddEventMarker( wpi::StringRef name, wpi::StringRef description, ShuffleboardEventImportance importance) { if (name.empty()) { - DriverStation::ReportError("Shuffleboard event name was not specified"); + FRC_ReportError(err::Error, "{}", + "Shuffleboard event name was not specified"); return; } m_eventsTable->GetSubTable(name)->GetEntry("Info").SetStringArray( diff --git a/wpilibc/src/main/native/cpp/simulation/DifferentialDrivetrainSim.cpp b/wpilibc/src/main/native/cpp/simulation/DifferentialDrivetrainSim.cpp index 0e22f6d434..4628653dd4 100644 --- a/wpilibc/src/main/native/cpp/simulation/DifferentialDrivetrainSim.cpp +++ b/wpilibc/src/main/native/cpp/simulation/DifferentialDrivetrainSim.cpp @@ -8,6 +8,8 @@ #include +#include + #include "frc/RobotController.h" #include "frc/system/NumericalIntegration.h" diff --git a/wpilibc/src/main/native/cppcs/RobotBase.cpp b/wpilibc/src/main/native/cppcs/RobotBase.cpp index cfafea1e6c..f0fab4ddce 100644 --- a/wpilibc/src/main/native/cppcs/RobotBase.cpp +++ b/wpilibc/src/main/native/cppcs/RobotBase.cpp @@ -11,10 +11,10 @@ #include #include +#include #include #include #include -#include #include #include "WPILibVersion.h" @@ -53,16 +53,18 @@ class WPILibCameraServerShared : public frc::CameraServerShared { void ReportVideoServer(int id) override { HAL_Report(HALUsageReporting::kResourceType_PCVideoServer, id); } - void SetCameraServerError(const wpi::Twine& error) override { - wpi::SmallString<128> buf; - FRC_ReportError(err::CameraServerError, "{}", error.toStringRef(buf)); + void SetCameraServerErrorV(fmt::string_view format, + fmt::format_args args) override { + ReportErrorV(err::CameraServerError, __FILE__, __LINE__, __FUNCTION__, + format, args); } - void SetVisionRunnerError(const wpi::Twine& error) override { - wpi::SmallString<128> buf; - FRC_ReportError(-1, "{}", error.toStringRef(buf)); + void SetVisionRunnerErrorV(fmt::string_view format, + fmt::format_args args) override { + ReportErrorV(err::Error, __FILE__, __LINE__, __FUNCTION__, format, args); } - void ReportDriverStationError(const wpi::Twine& error) override { - DriverStation::ReportError(error); + void ReportDriverStationErrorV(fmt::string_view format, + fmt::format_args args) override { + ReportErrorV(err::Error, __FILE__, __LINE__, __FUNCTION__, format, args); } std::pair GetRobotMainThreadId() const override { return std::make_pair(RobotBase::GetThreadId(), true); @@ -70,8 +72,9 @@ class WPILibCameraServerShared : public frc::CameraServerShared { }; class WPILibMathShared : public wpi::math::MathShared { public: - void ReportError(const wpi::Twine& error) override { - DriverStation::ReportError(error); + void ReportErrorV(fmt::string_view format, fmt::format_args args) override { + frc::ReportErrorV(err::Error, __FILE__, __LINE__, __FUNCTION__, format, + args); } void ReportUsage(wpi::math::MathUsageId id, int count) override { diff --git a/wpilibc/src/main/native/include/frc/DriverStation.h b/wpilibc/src/main/native/include/frc/DriverStation.h index 91e36a6503..79ae9c2e83 100644 --- a/wpilibc/src/main/native/include/frc/DriverStation.h +++ b/wpilibc/src/main/native/include/frc/DriverStation.h @@ -10,8 +10,8 @@ #include #include +#include #include -#include #include #include @@ -40,28 +40,6 @@ class DriverStation { */ static DriverStation& GetInstance(); - /** - * Report an error to the DriverStation messages window. - * - * The error is also printed to the program console. - */ - static void ReportError(const wpi::Twine& error); - - /** - * Report a warning to the DriverStation messages window. - * - * The warning is also printed to the program console. - */ - static void ReportWarning(const wpi::Twine& error); - - /** - * Report an error to the DriverStation messages window. - * - * The error is also printed to the program console. - */ - static void ReportError(bool isError, int code, const wpi::Twine& error, - const wpi::Twine& location, const wpi::Twine& stack); - static constexpr int kJoystickPorts = 6; /** @@ -465,14 +443,28 @@ class DriverStation { * * Throttles the errors so that they don't overwhelm the DS. */ - void ReportJoystickUnpluggedError(const wpi::Twine& message); + void ReportJoystickUnpluggedErrorV(fmt::string_view format, + fmt::format_args args); + + template + inline void ReportJoystickUnpluggedError(const S& format, Args&&... args) { + ReportJoystickUnpluggedErrorV( + format, fmt::make_args_checked(format, args...)); + } /** * Reports errors related to unplugged joysticks. * * Throttles the errors so that they don't overwhelm the DS. */ - void ReportJoystickUnpluggedWarning(const wpi::Twine& message); + void ReportJoystickUnpluggedWarningV(fmt::string_view format, + fmt::format_args args); + + template + inline void ReportJoystickUnpluggedWarning(const S& format, Args&&... args) { + ReportJoystickUnpluggedWarningV( + format, fmt::make_args_checked(format, args...)); + } void Run(); diff --git a/wpilibc/src/main/native/include/frc/WPIErrors.mac b/wpilibc/src/main/native/include/frc/WPIErrors.mac index f159ccd848..c6c96e9c57 100644 --- a/wpilibc/src/main/native/include/frc/WPIErrors.mac +++ b/wpilibc/src/main/native/include/frc/WPIErrors.mac @@ -59,3 +59,4 @@ S(UnsupportedInSimulation, -80, "Unsupported in simulation") S(CameraServerError, -90, "CameraServer error") S(InvalidParameter, -100, "Invalid parameter value") S(AssertionFailure, -110, "Assertion failed") +S(Error, -111, "Error") diff --git a/wpilibc/src/main/native/include/frc/WPIWarnings.mac b/wpilibc/src/main/native/include/frc/WPIWarnings.mac index 8e39ac1d6c..fa63494155 100644 --- a/wpilibc/src/main/native/include/frc/WPIWarnings.mac +++ b/wpilibc/src/main/native/include/frc/WPIWarnings.mac @@ -21,3 +21,4 @@ S(SPIReadNoMISO, 13, "Cannot read from SPI port with no MISO input") S(SPIReadNoData, 14, "No data available to read from SPI") S(IncompatibleState, 15, "Incompatible State: The operation cannot be completed") +S(Warning, 16, "Warning") diff --git a/wpilibc/src/test/native/cpp/DriverStationTest.cpp b/wpilibc/src/test/native/cpp/DriverStationTest.cpp index 8bf96ad634..faa7aa7a96 100644 --- a/wpilibc/src/test/native/cpp/DriverStationTest.cpp +++ b/wpilibc/src/test/native/cpp/DriverStationTest.cpp @@ -54,18 +54,24 @@ TEST_P(JoystickConnectionWarningTests, JoystickConnectionWarnings) { EXPECT_EQ( frc::DriverStation::GetInstance().IsJoystickConnectionWarningSilenced(), std::get<2>(GetParam())); - EXPECT_EQ(::testing::internal::GetCapturedStderr(), std::get<3>(GetParam())); + EXPECT_EQ(::testing::internal::GetCapturedStderr().substr( + 0, std::get<3>(GetParam()).size()), + std::get<3>(GetParam())); } INSTANTIATE_TEST_SUITE_P( DriverStation, JoystickConnectionWarningTests, - ::testing::Values(std::make_tuple(false, true, true, ""), - std::make_tuple(false, false, false, - "Joystick Button missing, check if all " - "controllers are plugged in\n"), - std::make_tuple(true, true, false, - "Joystick Button missing, check if all " - "controllers are plugged in\n"), - std::make_tuple(true, false, false, - "Joystick Button missing, check if all " - "controllers are plugged in\n"))); + ::testing::Values( + std::make_tuple(false, true, true, ""), + std::make_tuple( + false, false, false, + "Warning: Joystick Button 1 missing (max 0), check if all " + "controllers are plugged in\n"), + std::make_tuple( + true, true, false, + "Warning: Joystick Button 1 missing (max 0), check if all " + "controllers are plugged in\n"), + std::make_tuple( + true, false, false, + "Warning: Joystick Button 1 missing (max 0), check if all " + "controllers are plugged in\n"))); diff --git a/wpilibcExamples/src/main/cpp/examples/RomiReference/cpp/subsystems/OnBoardIO.cpp b/wpilibcExamples/src/main/cpp/examples/RomiReference/cpp/subsystems/OnBoardIO.cpp index d7e2c42b30..ab0e9793d2 100644 --- a/wpilibcExamples/src/main/cpp/examples/RomiReference/cpp/subsystems/OnBoardIO.cpp +++ b/wpilibcExamples/src/main/cpp/examples/RomiReference/cpp/subsystems/OnBoardIO.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include OnBoardIO::OnBoardIO(OnBoardIO::ChannelMode dio1, OnBoardIO::ChannelMode dio2) { @@ -32,7 +32,7 @@ bool OnBoardIO::GetButtonBPressed() { auto currentTime = frc2::Timer::GetFPGATimestamp(); if (currentTime > m_nextMessageTime) { - frc::DriverStation::ReportError("Button B was not configured"); + FRC_ReportError(frc::err::Error, "Button {} was not configured", "B"); m_nextMessageTime = currentTime + kMessageInterval; } return false; @@ -45,7 +45,7 @@ bool OnBoardIO::GetButtonCPressed() { auto currentTime = frc2::Timer::GetFPGATimestamp(); if (currentTime > m_nextMessageTime) { - frc::DriverStation::ReportError("Button C was not configured"); + FRC_ReportError(frc::err::Error, "Button {} was not configured", "C"); m_nextMessageTime = currentTime + kMessageInterval; } return false; @@ -57,7 +57,7 @@ void OnBoardIO::SetGreenLed(bool value) { } else { auto currentTime = frc2::Timer::GetFPGATimestamp(); if (currentTime > m_nextMessageTime) { - frc::DriverStation::ReportError("Green LED was not configured"); + FRC_ReportError(frc::err::Error, "{} LED was not configured", "Green"); m_nextMessageTime = currentTime + kMessageInterval; } } @@ -69,7 +69,7 @@ void OnBoardIO::SetRedLed(bool value) { } else { auto currentTime = frc2::Timer::GetFPGATimestamp(); if (currentTime > m_nextMessageTime) { - frc::DriverStation::ReportError("Red LED was not configured"); + FRC_ReportError(frc::err::Error, "{} LED was not configured", "Red"); m_nextMessageTime = currentTime + kMessageInterval; } } diff --git a/wpimath/.styleguide b/wpimath/.styleguide index c82141bacf..8c19bcaeb9 100644 --- a/wpimath/.styleguide +++ b/wpimath/.styleguide @@ -31,6 +31,7 @@ includeGuardRoots { } includeOtherLibs { + ^fmt/ ^wpi/ } diff --git a/wpimath/src/main/native/cpp/MathShared.cpp b/wpimath/src/main/native/cpp/MathShared.cpp index 178b878e8c..500f821512 100644 --- a/wpimath/src/main/native/cpp/MathShared.cpp +++ b/wpimath/src/main/native/cpp/MathShared.cpp @@ -11,7 +11,7 @@ using namespace wpi::math; namespace { class DefaultMathShared : public MathShared { public: - void ReportError(const wpi::Twine& error) override {} + void ReportErrorV(fmt::string_view format, fmt::format_args args) override {} void ReportUsage(MathUsageId id, int count) override {} }; } // namespace diff --git a/wpimath/src/main/native/include/wpimath/MathShared.h b/wpimath/src/main/native/include/wpimath/MathShared.h index 0c873e9210..e161d1a69f 100644 --- a/wpimath/src/main/native/include/wpimath/MathShared.h +++ b/wpimath/src/main/native/include/wpimath/MathShared.h @@ -6,7 +6,7 @@ #include -#include +#include namespace wpi::math { @@ -24,8 +24,13 @@ enum class MathUsageId { class MathShared { public: virtual ~MathShared() = default; - virtual void ReportError(const wpi::Twine& error) = 0; + virtual void ReportErrorV(fmt::string_view format, fmt::format_args args) = 0; virtual void ReportUsage(MathUsageId id, int count) = 0; + + template + inline void ReportError(const S& format, Args&&... args) { + ReportErrorV(format, fmt::make_args_checked(format, args...)); + } }; class MathSharedStore { @@ -34,8 +39,13 @@ class MathSharedStore { static void SetMathShared(std::unique_ptr shared); - static void ReportError(const wpi::Twine& error) { - GetMathShared().ReportError(error); + static void ReportErrorV(fmt::string_view format, fmt::format_args args) { + GetMathShared().ReportErrorV(format, args); + } + + template + static inline void ReportError(const S& format, Args&&... args) { + ReportErrorV(format, fmt::make_args_checked(format, args...)); } static void ReportUsage(MathUsageId id, int count) {