From b46b5df16aac6d9b32e2ac09e64ae3697bfa2fcb Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 5 Apr 2020 23:09:21 -0700 Subject: [PATCH] [wpilibc] Output Tracer to DriverStation by default (#2469) This matches the Java behavior. Also optimizes Java to only create a StringBuffer and call DriverStation.reportWarning if there is data to output. --- wpilibc/src/main/native/cpp/Tracer.cpp | 21 +++++++++++++------ wpilibc/src/main/native/include/frc/Tracer.h | 13 +++++++++++- .../java/edu/wpi/first/wpilibj/Tracer.java | 8 ++++--- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/wpilibc/src/main/native/cpp/Tracer.cpp b/wpilibc/src/main/native/cpp/Tracer.cpp index 9cb7910fbc..af5177a4d6 100644 --- a/wpilibc/src/main/native/cpp/Tracer.cpp +++ b/wpilibc/src/main/native/cpp/Tracer.cpp @@ -8,8 +8,11 @@ #include "frc/Tracer.h" #include +#include #include +#include "frc/DriverStation.h" + using namespace frc; Tracer::Tracer() { ResetTimer(); } @@ -28,6 +31,13 @@ void Tracer::AddEpoch(wpi::StringRef epochName) { } void Tracer::PrintEpochs() { + wpi::SmallString<128> buf; + wpi::raw_svector_ostream os(buf); + PrintEpochs(os); + if (!buf.empty()) DriverStation::ReportWarning(buf); +} + +void Tracer::PrintEpochs(wpi::raw_ostream& os) { using std::chrono::duration_cast; using std::chrono::microseconds; @@ -35,12 +45,11 @@ void Tracer::PrintEpochs() { if (now - m_lastEpochsPrintTime > kMinPrintPeriod) { m_lastEpochsPrintTime = now; for (const auto& epoch : m_epochs) { - wpi::outs() << '\t' << epoch.getKey() << ": " - << wpi::format( - "%.6f", - duration_cast(epoch.getValue()).count() / - 1.0e6) - << "s\n"; + os << '\t' << epoch.getKey() << ": " + << wpi::format( + "%.6f", + duration_cast(epoch.getValue()).count() / 1.0e6) + << "s\n"; } } } diff --git a/wpilibc/src/main/native/include/frc/Tracer.h b/wpilibc/src/main/native/include/frc/Tracer.h index 8b495713f9..924783369c 100644 --- a/wpilibc/src/main/native/include/frc/Tracer.h +++ b/wpilibc/src/main/native/include/frc/Tracer.h @@ -14,6 +14,10 @@ #include #include +namespace wpi { +class raw_ostream; +} // namespace wpi + namespace frc { /** * A class for keeping track of how much time it takes for different parts of @@ -51,10 +55,17 @@ class Tracer { void AddEpoch(wpi::StringRef epochName); /** - * Prints list of epochs added so far and their times. + * Prints list of epochs added so far and their times to the DriverStation. */ void PrintEpochs(); + /** + * Prints list of epochs added so far and their times to a stream. + * + * @param os output stream + */ + void PrintEpochs(wpi::raw_ostream& os); + private: static constexpr std::chrono::milliseconds kMinPrintPeriod{1000}; diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Tracer.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Tracer.java index 3518023479..3026e90c94 100644 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/Tracer.java +++ b/wpilibj/src/main/java/edu/wpi/first/wpilibj/Tracer.java @@ -83,13 +83,15 @@ public class Tracer { */ public void printEpochs(Consumer output) { long now = RobotController.getFPGATime(); - StringBuilder sb = new StringBuilder(); - if (now - m_lastEpochsPrintTime > kMinPrintPeriod) { + if (now - m_lastEpochsPrintTime > kMinPrintPeriod) { + StringBuilder sb = new StringBuilder(); m_lastEpochsPrintTime = now; m_epochs.forEach((key, value) -> { sb.append(String.format("\t%s: %.6fs\n", key, value / 1.0e6)); }); + if (sb.length() > 0) { + output.accept(sb.toString()); + } } - output.accept(sb.toString()); } }