log: implement a log topics system, like pipewire

The intention is to make checks for enabled log topics faster.

Every topic has its own structure that is statically defined in the file
where the logs are printed from. The structure is initialized transparently
when it is first used and it contains all the log level flags for the levels
that this topic should print messages. It is then checked on the wp_log()
macro before printing the message.

Topics from SPA/PipeWire are also handled natively, so messages are printed
directly without checking if the topic is enabled, since the PipeWire and SPA
macros do the checking themselves.

Messages coming from GLib are checked inside the handler.

An internal WpLogFields object is used to manage the state of each log
message, populating all the fields appropriately from the place they
are coming from (wp_log, spa_log, glib log), formatting the message and
then printing it. For printing to the journald, we still use the glib
message handler, converting all the needed fields to GLogField on demand.
That message handler does not do any checks for the topic or the level, so
we can just call it to send the message.
This commit is contained in:
George Kiagiadakis
2023-05-16 11:51:29 +03:00
parent e908b93f3b
commit 4736d56557
75 changed files with 464 additions and 348 deletions

View File

@@ -12,6 +12,9 @@
#include <wplua/wplua.h>
#include <libintl.h>
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting
WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting)
#define URI_API "resource:///org/freedesktop/pipewire/wireplumber/m-lua-scripting/api.lua"
void wp_lua_scripting_pod_init (lua_State *L);
@@ -284,14 +287,13 @@ static int
log_log (lua_State *L, GLogLevelFlags lvl)
{
lua_Debug ar = {0};
const gchar *message, *tmp;
gchar domain[25];
const gchar *message;
gchar line_str[11];
gconstpointer instance = NULL;
GType type = G_TYPE_INVALID;
int index = 1;
if (!wp_log_level_is_enabled (lvl))
if (!wp_log_topic_is_enabled (log_topic_lua_scripting, lvl))
return 0;
g_warn_if_fail (lua_getstack (L, 1, &ar) == 1);
@@ -309,14 +311,10 @@ log_log (lua_State *L, GLogLevelFlags lvl)
}
message = luaL_checkstring (L, index);
tmp = ar.source ? g_strrstr (ar.source, ".lua") : NULL;
snprintf (domain, 25, "script/%.*s",
tmp ? MIN((gint)(tmp - ar.source), 17) : 17,
ar.source);
snprintf (line_str, 11, "%d", ar.currentline);
ar.name = ar.name ? ar.name : "chunk";
wp_log_structured_standard (domain, lvl,
wp_log_checked (log_topic_lua_scripting->topic_name, lvl,
ar.source, line_str, ar.name, type, instance, "%s", message);
return 0;
}