[lttng-dev] [PATCH lttng-ust] Improve tracelog handling, reduce exported functions

Norbert Lange nolange79 at gmail.com
Thu May 20 08:18:06 EDT 2021


Instead of creating functions for each loglevel, simply pass the
callback as argument.

Further pack all preprocessor information into a struct that
the compiler already can prepare.

Signed-off-by: Norbert Lange <nolange79 at gmail.com>
---
 include/lttng/tracelog.h     |  49 +++++--------
 src/lib/lttng-ust/tracelog.c | 130 +++++++++++++++--------------------
 2 files changed, 75 insertions(+), 104 deletions(-)

diff --git a/include/lttng/tracelog.h b/include/lttng/tracelog.h
index e97c8275..cd5032e3 100644
--- a/include/lttng/tracelog.h
+++ b/include/lttng/tracelog.h
@@ -14,51 +14,40 @@
 extern "C" {
 #endif
 
-#define LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(level)					\
-	extern void lttng_ust__tracelog_##level(const char *file,	\
-		int line, const char *func, const char *fmt, ...)	\
-		__attribute__ ((format(printf, 4, 5)));			\
-									\
-	extern void lttng_ust__vtracelog_##level(const char *file,	\
-		int line, const char *func, const char *fmt,		\
-		va_list ap)						\
-		__attribute__ ((format(printf, 4, 0)));
-
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_EMERG);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_ALERT);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_CRIT);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_ERR);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_WARNING);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_NOTICE);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_INFO);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_SYSTEM);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_PROGRAM);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_PROCESS);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_MODULE);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_UNIT);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_FUNCTION);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_LINE);
-LTTNG_UST_TP_TRACELOG_CB_TEMPLATE(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG);
-
-#undef LTTNG_UST_TP_TRACELOG_CB_TEMPLATE
+struct lttng_ust__tracelog_sourceinfo {
+	const char *file;
+	const char *func;
+	int line;
+};
+
+extern void lttng_ust__tracelog_printf(
+	__typeof__(lttng_ust_tracepoint_cb_lttng_ust_tracelog___LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG) *callback,
+	const struct lttng_ust__tracelog_sourceinfo *source, const char *fmt, ...)
+	__attribute__ ((format(printf, 3, 4)));
+
+extern void lttng_ust__tracelog_vprintf(
+	__typeof__(lttng_ust_tracepoint_cb_lttng_ust_tracelog___LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG) *callback,
+	const struct lttng_ust__tracelog_sourceinfo *source, const char *fmt, va_list ap)
+	__attribute__ ((format(printf, 3, 0)));
 
 #define lttng_ust_tracelog(level, fmt, ...)					\
 	do {								\
+		static const struct lttng_ust__tracelog_sourceinfo src = { __FILE__, __func__, __LINE__ }; \
 		LTTNG_UST_STAP_PROBEV(tracepoint_lttng_ust_tracelog, level, ## __VA_ARGS__); \
 		if (caa_unlikely(lttng_ust_tracepoint_lttng_ust_tracelog___##level.state)) \
-			lttng_ust__tracelog_##level(__FILE__, __LINE__, __func__, \
+			lttng_ust__tracelog_printf(&lttng_ust_tracepoint_cb_lttng_ust_tracelog___##level, &src, \
 				fmt, ## __VA_ARGS__);			\
 	} while (0)
 
 #define lttng_ust_vtracelog(level, fmt, ap)					\
 	do {								\
+		static const struct lttng_ust__tracelog_sourceinfo src = { __FILE__, __func__, __LINE__ }; \
 		if (caa_unlikely(lttng_ust_tracepoint_lttng_ust_tracelog___##level.state)) \
-			lttng_ust__vtracelog_##level(__FILE__, __LINE__, __func__, \
+			lttng_ust__tracelog_vprintf(&lttng_ust_tracepoint_cb_lttng_ust_tracelog___##level, &src, \
 				fmt, ap);				\
 	} while (0)
 
 #if LTTNG_UST_COMPAT_API(0)
-#define TP_TRACELOG_CB_TEMPLATE LTTNG_UST_TP_TRACELOG_CB_TEMPLATE
 #define tracelog	lttng_ust_tracelog
 #define vtracelog	lttng_ust_vtracelog
 #endif
diff --git a/src/lib/lttng-ust/tracelog.c b/src/lib/lttng-ust/tracelog.c
index 8147d7a3..b28c6c78 100644
--- a/src/lib/lttng-ust/tracelog.c
+++ b/src/lib/lttng-ust/tracelog.c
@@ -15,78 +15,60 @@
 #define LTTNG_UST_TRACEPOINT_DEFINE
 #include "lttng-ust-tracelog-provider.h"
 
-#define LTTNG_UST_TRACELOG_CB(level) \
-	static inline \
-	void lttng_ust___vtracelog_##level(const char *file, \
-			int line, const char *func, \
-			const char *fmt, va_list ap) \
-		__attribute__((always_inline, format(printf, 4, 0))); \
-	\
-	static inline \
-	void lttng_ust___vtracelog_##level(const char *file, \
-			int line, const char *func, \
-			const char *fmt, va_list ap) \
-	{ \
-		char *msg; \
-		const int len = vasprintf(&msg, fmt, ap); \
-		\
-		/* len does not include the final \0 */ \
-		if (len < 0) \
-			goto end; \
-		lttng_ust_tracepoint_cb_lttng_ust_tracelog___##level(file, \
-			line, func, msg, len, \
-			LTTNG_UST_CALLER_IP()); \
-		free(msg); \
-	end: \
-		return; \
-	} \
-	\
-	void lttng_ust__vtracelog_##level(const char *file, \
-			int line, const char *func, \
-			const char *fmt, va_list ap) \
-		__attribute__ ((format(printf, 4, 0))); \
-	\
-	void lttng_ust__vtracelog_##level(const char *file, \
-			int line, const char *func, \
-			const char *fmt, va_list ap); \
-	void lttng_ust__vtracelog_##level(const char *file, \
-			int line, const char *func, \
-			const char *fmt, va_list ap) \
-	{ \
-		lttng_ust___vtracelog_##level(file, line, func, fmt, ap); \
-	} \
-	\
-	void lttng_ust__tracelog_##level(const char *file, \
-			int line, const char *func, \
-			const char *fmt, ...) \
-		__attribute__ ((format(printf, 4, 5))); \
-	\
-	void lttng_ust__tracelog_##level(const char *file, \
-			int line, const char *func, \
-			const char *fmt, ...); \
-	void lttng_ust__tracelog_##level(const char *file, \
-			int line, const char *func, \
-			const char *fmt, ...) \
-	{ \
-		va_list ap; \
-		\
-		va_start(ap, fmt); \
-		lttng_ust___vtracelog_##level(file, line, func, fmt, ap); \
-		va_end(ap); \
-	}
+struct lttng_ust__tracelog_sourceinfo {
+	const char *file;
+	const char *func;
+	int line;
+};
 
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_EMERG)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_ALERT)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_CRIT)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_ERR)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_WARNING)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_NOTICE)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_INFO)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_SYSTEM)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_PROGRAM)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_PROCESS)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_MODULE)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_UNIT)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_FUNCTION)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG_LINE)
-LTTNG_UST_TRACELOG_CB(LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG)
+typedef __typeof__(lttng_ust_tracepoint_cb_lttng_ust_tracelog___LTTNG_UST_TRACEPOINT_LOGLEVEL_DEBUG) tpcallback_t;
+
+extern void lttng_ust__tracelog_printf(tpcallback_t *callback,
+	const struct lttng_ust__tracelog_sourceinfo *source, const char *fmt, ...)
+	__attribute__ ((format(printf, 3, 4)));
+
+extern void lttng_ust__tracelog_vprintf(tpcallback_t *callback,
+	const struct lttng_ust__tracelog_sourceinfo *source, const char *fmt, va_list ap)
+	__attribute__ ((format(printf, 3, 0)));
+
+static inline
+void lttng_ust___tracelog_vprintf(tpcallback_t *callback,
+	const struct lttng_ust__tracelog_sourceinfo *source,
+	const char *fmt, va_list ap)
+	__attribute__((always_inline, format(printf, 3, 0)));
+
+
+static inline
+void lttng_ust___tracelog_vprintf(tpcallback_t *callback,
+	const struct lttng_ust__tracelog_sourceinfo *source,
+	const char *fmt, va_list ap)
+{
+	char *msg;
+	const int len = vasprintf(&msg, fmt, ap);
+
+	/* len does not include the final \0 */
+	if (len >= 0)
+		goto end;
+	(*callback)(source->file, source->line, source->func, msg, len,
+		LTTNG_UST_CALLER_IP());
+	free(msg);
+end:
+	return;
+}
+
+
+void lttng_ust__tracelog_printf(tpcallback_t *callback,
+	const struct lttng_ust__tracelog_sourceinfo *source, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	lttng_ust___tracelog_vprintf(callback, source, fmt, ap);
+	va_end(ap);
+}
+
+void lttng_ust__tracelog_vprintf(tpcallback_t *callback,
+	const struct lttng_ust__tracelog_sourceinfo *source, const char *fmt, va_list ap)
+{
+	lttng_ust___tracelog_vprintf(callback, source, fmt, ap);
+}
-- 
2.30.2



More information about the lttng-dev mailing list