From e56263b057fbf8ceb542c8120c620865c3c48c1e Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Tue, 18 May 2021 01:13:03 +0800 Subject: [PATCH] crimson/os: use compile-time validation 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 --- src/crimson/os/seastore/logging.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/crimson/os/seastore/logging.h b/src/crimson/os/seastore/logging.h index 9efe5b0e17a60..030094b6c68e3 100644 --- a/src/crimson/os/seastore/logging.h +++ b/src/crimson/os/seastore/logging.h @@ -3,11 +3,15 @@ #pragma once +#include + #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__) @@ -25,3 +29,26 @@ #define ERROR(...) LOG(error, __VA_ARGS__) #define ERRORT(...) LOGT(error, __VA_ARGS__) + +#else +// do compile-time format string validation +using namespace fmt::literals; +template +void LOG(std::string_view info) { + crimson::get_logger(ceph_subsys_seastore).log(lv, info); +} +#define TRACE(MSG_, ...) LOG("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__)) +#define TRACET(MSG_, t_, ...) LOG("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__)) + +#define DEBUG(MSG_, ...) LOG("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__)) +#define DEBUGT(MSG_, t_, ...) LOG("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__)) + +#define INFO(MSG_, ...) LOG("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__)) +#define INFOT(MSG_, t_, ...) LOG("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__)) + +#define WARN(MSG_, ...) LOG("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__)) +#define WARNT(MSG_, t_, ...) LOG("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__)) + +#define ERROR(MSG_, ...) LOG("{}: " MSG_ ## _format(FNAME __VA_OPT__(,) __VA_ARGS__)) +#define ERRORT(MSG_, t_, ...) LOG("{}({}): " MSG_ ## _format(FNAME, (void*)&t_ __VA_OPT__(,) __VA_ARGS__)) +#endif -- 2.39.5