Building on clang-tidy to move from printf-style to std::print-style logging and beyond

Mike Crowe

60
intermediate
17:00-18:00, Wednesday, 3rd July 2024

Modernising your codebase sounds fun to start with, but quickly gets tedious. Can we use tools to help us speed through the tedious bit?

This is tale of how a rather old C++ codebase first moved its diagnostic logging from using non-macro wrappers for printf to libfmt's fmt::printf, then to fmt::print and hopefully on to C++23's std::print by using a small clang-tidy check based on the code behind Clang 17's modernize-use-std-print clang-tidy check.

We'll consider how the move to fmt::printf was easy and beneficial, why moving to fmt::print was rather harder and how that led me to write what became the modernize-use-std-print clang-tidy check and then adapt it for our use case. I'll also talk about how such checks could allow moving to a more-standard logging library like spdlog in the future too.

If you're thinking, "I'm no LLVM or Clang expert, so maybe this talk is not for me" - I'm not either! So I'll share some of the challenges I faced on the way to help you avoid them and get to the fun part faster.

clang-tidy
logging
std::format
std::print

Mike Crowe

I started my professional career as a C++ Windows programmer in the early 1990s but managed to escape to be an embedded Linux programmer by the end of the decade and have done that ever since. Working from low-level drivers up to JavaScript and build systems, I find it rewarding to make re-usable tools to save me from boring repetitive work.