From 2fb055f43d9365a186004a63dfceeec1d002d1f9 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Sun, 28 Apr 2024 15:59:56 +0300 Subject: [PATCH] log: add wp_logt_checked taking WpLogTopic, to decide on debug messages Make decision on whether to show code location in journal messages based on whether the WpLogTopic would enable DEBUG level messages. Add wp_logt_checked API to take WpLogTopic as input to make this possible, and deprecate wp_log_checked. --- lib/wp/log.c | 41 ++++++++++++++++++++++++++ lib/wp/log.h | 8 ++++- modules/module-lua-scripting/api/api.c | 2 +- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/wp/log.c b/lib/wp/log.c index 131e6d52..9f418024 100644 --- a/lib/wp/log.c +++ b/lib/wp/log.c @@ -892,6 +892,8 @@ wp_log_writer_default (GLogLevelFlags log_level_flags, /*! * \brief Used internally by the debug logging macros. Avoid using it directly. * + * \deprecated Use \ref wp_logt_checked instead. + * * This assumes that the arguments are correct and that the log_topic is * enabled for the given log_level. No additional checks are performed. * \ingroup wplog @@ -922,6 +924,45 @@ wp_log_checked ( wp_log_fields_log (&lf); } +/*! + * \brief Used internally by the debug logging macros. Avoid using it directly. + * + * This assumes that the arguments are correct and that the log_topic is + * enabled for the given log_level. No additional checks are performed. + * \ingroup wplog + */ +void +wp_logt_checked ( + const WpLogTopic *topic, + GLogLevelFlags log_level_flags, + const gchar *file, + const gchar *line, + const gchar *func, + GType object_type, + gconstpointer object, + const gchar *message_format, + ...) +{ + WpLogFields lf = {0}; + g_autofree gchar *message = NULL; + va_list args; + const gchar *log_topic = topic ? topic->topic_name : NULL; + gboolean debug; + + if (topic) + debug = (topic->flags & WP_LOG_TOPIC_LEVEL_MASK & G_LOG_LEVEL_DEBUG); + else + debug = wp_want_debug_log (NULL); + + va_start (args, message_format); + message = g_strdup_vprintf (message_format, args); + va_end (args); + + wp_log_fields_init (&lf, log_topic, level_index_from_flags (log_level_flags), debug, + file, line, func, object_type, object, message); + wp_log_fields_log (&lf); +} + static G_GNUC_PRINTF (7, 0) void wp_spa_log_logtv (void *object, enum spa_log_level level, diff --git a/lib/wp/log.h b/lib/wp/log.h index 731bcebd..ad09ba29 100644 --- a/lib/wp/log.h +++ b/lib/wp/log.h @@ -121,6 +121,12 @@ GLogWriterOutput wp_log_writer_default (GLogLevelFlags log_level, WP_API void wp_log_checked (const gchar *log_topic, GLogLevelFlags log_level, + const gchar *file, const gchar *line, const gchar *func, + GType object_type, gconstpointer object, + const gchar *message_format, ...) G_GNUC_PRINTF (8, 9) __attribute__ ((deprecated)); + +WP_API +void wp_logt_checked (const WpLogTopic *topic, GLogLevelFlags log_level, const gchar *file, const gchar *line, const gchar *func, GType object_type, gconstpointer object, const gchar *message_format, ...) G_GNUC_PRINTF (8, 9); @@ -128,7 +134,7 @@ void wp_log_checked (const gchar *log_topic, GLogLevelFlags log_level, #define wp_log(topic, level, type, object, ...) \ ({ \ if (G_UNLIKELY (wp_log_topic_is_enabled (topic, level))) \ - wp_log_checked (topic->topic_name, level, __FILE__, G_STRINGIFY (__LINE__), \ + wp_logt_checked (topic, level, __FILE__, G_STRINGIFY (__LINE__), \ G_STRFUNC, type, object, __VA_ARGS__); \ }) diff --git a/modules/module-lua-scripting/api/api.c b/modules/module-lua-scripting/api/api.c index b991e930..9d5f7d87 100644 --- a/modules/module-lua-scripting/api/api.c +++ b/modules/module-lua-scripting/api/api.c @@ -384,7 +384,7 @@ log_log (lua_State *L, GLogLevelFlags lvl) snprintf (line_str, 11, "%d", ar.currentline); ar.name = ar.name ? ar.name : "chunk"; - wp_log_checked (topic->topic_name, lvl, + wp_logt_checked (topic, lvl, ar.source, line_str, ar.name, type, instance, "%s", message); return 0; }