How to get named list of arguments from fmt::format_args

Viewed 132

I'm trying to build C++ logging integration for Logz.IO which uses named parameters for string interpolation and I'd like to leverage fmt for argument parsing and formatting. Essentially I'm trying to build function that will allow me to pass named arguments e.g.

log_fmt("Length: {Length:.2f}", fmt::arg("Length", 125.5f));

I followed documentation to build templated function and my own logging function:

  template <typename S, typename... Args>
  void log_fmt(const S& format, Args&&... args) {
    vlog(level, format,
         fmt::make_args_checked<Args...>(format, args...));
  }

  void vlog(fmt::string_view format, fmt::format_args args) {}

Inside my implementation of vlog I would like to convert args to JSON object that will contain all arguments (ideally without formatting part):

{
    "Length": 125.5
}

However it looks like format_args exposes only very basic public interface (only allowing to get. How can I iterate over args and get each argument name and value?

1 Answers

{fmt} doesn't provide an API to iterate over named arguments but you can do it yourself in log_fmt. Each named argument will have the following type:

template <typename Char, typename T> struct named_arg : view {
  const Char* name;
  const T& value;
  named_arg(const Char* n, const T& v) : name(n), value(v) {}
};
Related