Skip to content

How to convert a VSFileSystem::vs_dprintf() call to the Boost logging way

Stephen G Tuggy edited this page Jul 27, 2020 · 3 revisions

To convert a VSFileSystem::vs_dprintf() call to the Boost logging way:

  1. Replace the text VSFileSystem::vs_dprintf with BOOST_LOG_TRIVIAL.
  2. Change the following parentheses from wrapping the entire rest of what follows on the line, to wrapping only the log level. Said log level should be one of: info (for debug level 1); debug (for debug level 2); or trace (for debug level 3). Other, non-debug log levels include warning, error, and fatal.
  3. After the log level and its parentheses, put <<, as if you were writing to cout.
  4. If a printf-style format string with args was used, replace it with boost::format(format_str) % arg1 % arg2 ... (https://www.boost.org/doc/libs/1_73_0/libs/format/doc/format.html). Otherwise, just do a straight string literal.
  5. If there was a \n at the end, be sure to remove it. The newline is implied with BOOST_LOG_TRIVIAL.
  6. Finally, format the code nicely, being sure to indent it properly and wrap it to the next line if it gets too long.

Taking this all together:

        VSFileSystem::vs_dprintf(1, "Pause key pressed\n");

becomes

        BOOST_LOG_TRIVIAL(info) << "Pause key pressed";

while

        VSFileSystem::vs_dprintf(3, "Player ship needs transport from %s to %s across %d systems",
            locationBaseName.c_str(),
            destinationSystemName.c_str(),
            jumps.size());

becomes

        BOOST_LOG_TRIVIAL(trace) << boost::format("Player ship needs transport from %1% to %2% across %3% systems") % locationBaseName %
                                       destinationSystemName % jumps.size();

Note that in the last example, the .c_str() was also removed in a couple of cases, because boost::format is aware of C++-style strings. Note also the change from %s to %1% and so forth, boost::format-style.

Hopefully this is enough to give you the idea. Feel free to contact me (@stephengtuggy) with any questions or feedback.