ros-perception / openslam_gmapping

218 stars 206 forks source link

Generalize ostreams used for console output in GridSlamProcessor #45

Open kevin-robb opened 4 months ago

kevin-robb commented 4 months ago

Motivation

The GridSlamProcessor class already had a member variable std::ostream& m_infoStream; which is used in some places to write console output for logs and general info. This stream can be set through the class' constructor, so users have some control over the output from the class. However, many functions in this class were writing directly to std::cerr instead, which cannot be controlled without modifying the code itself.

Description

This PR generalizes uses of cerr in gridslamprocessor.cpp to use m_infoStream or a new member variable m_errStream. Most of the cerr statements seemed more informative than error-based, so most have been changed to use the former.

The class constructor now accepts two optional arguments, infoStr and errStr, which default to std::cout and std::cerr respectively. I have combined the existing two constructors into this new one by using optional arguments.

Finally, I have introduced a new utility file nullstream.h, containing a class definition and a global/static variable g_null_stream. When constructing the class, a user can pass this argument for infoStr and/or errStr to effectively suppress console output from GridSlamProcessor. (I would be fine reverting the nullstream addition if y'all would prefer to merge the previously mentioned ostream generalizations on their own. This was just kind of annoying to get working, so I figured I'd add it for others to use too.)

Testing

This change should have no effect on the performance itself, and other than some things printing to cout instead of cerr by default, the console output will not change by default. New behavior is only achieved by a runner class modifying its constructor call.

I have tested this on our usage of the GridSlamProcessor in our own code, and see all the usual console outputs when constructing the class with

std::unique_ptr<GMapping::GridSlamProcessor> m_gsp = std::make_unique<GMapping::GridSlamProcessor>();

or

std::unique_ptr<GMapping::GridSlamProcessor> m_gsp = std::make_unique<GMapping::GridSlamProcessor>(std::cout, std::cerr);

and am able to run it just the same, but without any console outputs, with

std::unique_ptr<GMapping::GridSlamProcessor> m_gsp = std::make_unique<GMapping::GridSlamProcessor>(GMapping::g_null_stream, GMapping::g_null_stream);