Wrap calls to fmt::format to catch formatting exceptions
[lttng-tools.git] / src / common / format.hpp
index 389bc596ad85787c7f00aa7506532f56898551cf..c1ab3d54ddc33cad2fb4a2b1c91aeab7ae2338dc 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <cxxabi.h>
 #include <string>
+#include <utility>
 
 DIAGNOSTIC_PUSH
 DIAGNOSTIC_IGNORE_SUGGEST_ATTRIBUTE_FORMAT
@@ -42,4 +43,16 @@ struct formatter<std::type_info> : formatter<std::string> {
 };
 } /* namespace fmt */
 
+namespace lttng {
+template <typename... FormattingArguments>
+std::string format(FormattingArguments&&...args)
+{
+       try {
+               return fmt::format(std::forward<FormattingArguments>(args)...);
+       } catch (const fmt::format_error& ex) {
+               return std::string("Failed to format string: ") += ex.what();
+       }
+}
+} /* namespace lttng */
+
 #endif /* LTTNG_FORMAT_H */
This page took 0.023058 seconds and 4 git commands to generate.