CougarProgramming623 / InfiniteRecharge

First Robotics Challenge 2020
https://cougarrobotics.org/
BSD 3-Clause "New" or "Revised" License
2 stars 0 forks source link

Need to standardize debug printing #17

Closed Mee42 closed 4 years ago

Mee42 commented 4 years ago

We need to standardize how we print to the driver station. We currently have several different ways to do it.

There's two different ways to report data: as an warning or as a printout statement. The first can be done using DriverStation::reportWarning and the second a PrintCommand.

Right now we mix DebugOutF, DriverStation::reportWarning (with or without the formatter) and so on. We need to go through our code and fix up how we do debugging and standardize it.

We should first look into getting printStatement output on the driver station, because using warnings doesn't seem like the best way to do things - we aren't warning, we are printing debug info.

I'm proposing 2 different methods of printing that we can use. The first is a function that takes a function pointer, which takes in a formatter and returns nothing (void, or formater, or whatever works). Code that uses this method would look like this:

ohs623::printOut([&form] {
    form << "motor output " << motor.output();
});

which would replace the more cumbersome

ohs623::DefaultFormatter formatter;
formatter << "motor output " << motor.output();
frc::DriverStation::ReportError(formatter.c_str());

but does the same thing

The second debug method should be a macro GOT_HERE which expands to a debug printout that prints the line and file that was just ran.

Mee42 commented 4 years ago

Once we have this method we could replace every single print statement in our program.

TroyNeubauer commented 4 years ago

The final API is condensed into a set of macros (OHS_DEBUG, OHS_INFO, OHS_WARN, OHS_ERROR). Each of these take in a lambda that takes in a Formatter reference. These macros display information at various levels of severity. Warnings and errors are displayed in red and yellow respectively on the driver station and any level can be selected as the minimum level, causing all levels below to not be displayed (Eg if the log level is warning then only warnings and errors will be printed). This is desirable in competitions where we don't want the driver station cluttered with the target setpoint etc.

https://github.com/CougarProgramming623/InfiniteRecharge/blob/8e06b94a14362b69d8cf80ec64f763908a384a65/src/main/cpp/ohs/Log.cpp#L13

Here's an example of the api: OHS_DEBUG([](auto& f){ f << "Setpoint at " << m_Setpoint << " ticks"; }); OHS_DEBUG([](auto& f){ f.Write( "Motor ID is 0b").Base(m_Setpoint, 2).Write(" ticks"; }); Make sure to note that the formatter is passed by reference.

Any API exposed by Formatter can be used inside the lambda too. https://github.com/CougarProgramming623/InfiniteRecharge/blob/8e06b94a14362b69d8cf80ec64f763908a384a65/src/main/include/ohs/Formatter.h#L36