]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crimson/os: use compile-time validation
authorKefu Chai <kchai@redhat.com>
Mon, 17 May 2021 17:13:03 +0000 (01:13 +0800)
committerKefu Chai <kchai@redhat.com>
Mon, 17 May 2021 17:38:55 +0000 (01:38 +0800)
libfmt does compile-time format argument validation of the format string
and the argument when the the user-defined literal is used. but the
downside is that the formatter materialize the whole formatted string
into a std::string, before printing them argument into seastar's log buffer
inserter. presumably, the inserter would be more efficient in
comparision to the pre-format approach. so this validation is only
enabled for non NDEBUG build. so it is able to help us to identify
errors like

DEBUG("{} {}", 1, 2, 3)

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/crimson/os/seastore/logging.h

index 9efe5b0e17a60840e69489b4705d2a4eb8a2776f..030094b6c68e366cc788dacfb3689ccabbb03ced 100644 (file)
@@ -3,11 +3,15 @@
 
 #pragma once
 
+#include <fmt/format.h>
+
 #include "crimson/common/log.h"
 
 #define LOGGER crimson::get_logger(ceph_subsys_seastore)
 #define LOG_PREFIX(x) constexpr auto FNAME = #x
 
+#ifdef NDEBUG
+
 #define LOG(LEVEL, MSG, ...) LOGGER . LEVEL("{}: " MSG, FNAME __VA_OPT__(,) __VA_ARGS__)
 #define LOGT(LEVEL, MSG, t, ...) LOGGER . LEVEL("{}({}): " MSG, FNAME, (void*)&t __VA_OPT__(,) __VA_ARGS__)
 
 
 #define ERROR(...) LOG(error, __VA_ARGS__)
 #define ERRORT(...) LOGT(error, __VA_ARGS__)
+
+#else
+// do compile-time format string validation
+using namespace fmt::literals;
+template<seastar::log_level lv>
+void LOG(std::string_view info) {
+  crimson::get_logger(ceph_subsys_seastore).log(lv, info);
+}
+#define TRACE(MSG_, ...) LOG<seastar::log_level::trace>("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__))
+#define TRACET(MSG_, t_, ...) LOG<seastar::log_level::trace>("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__))
+
+#define DEBUG(MSG_, ...) LOG<seastar::log_level::debug>("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__))
+#define DEBUGT(MSG_, t_, ...) LOG<seastar::log_level::debug>("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__))
+
+#define INFO(MSG_, ...) LOG<seastar::log_level::info>("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__))
+#define INFOT(MSG_, t_, ...) LOG<seastar::log_level::info>("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__))
+
+#define WARN(MSG_, ...) LOG<seastar::log_level::warn>("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__))
+#define WARNT(MSG_, t_, ...) LOG<seastar::log_level::warn>("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__))
+
+#define ERROR(MSG_, ...) LOG<seastar::log_level::error>("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__))
+#define ERRORT(MSG_, t_, ...) LOG<seastar::log_level::error>("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__))
+#endif