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;
}

View File

@@ -9,6 +9,9 @@
#include <wp/wp.h>
#include <wplua/wplua.h>
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting
WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting)
/* API */
static int

View File

@@ -11,6 +11,9 @@
#include <spa/utils/type.h>
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting
WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting)
#define MAX_LUA_TYPES 9
/* Builder */

View File

@@ -9,6 +9,9 @@
#include <wp/wp.h>
#include <wplua/wplua.h>
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting
WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting)
struct _WpRequireApiTransition
{
WpTransition parent;

View File

@@ -12,6 +12,9 @@
#include "script.h"
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting
WP_LOG_TOPIC (log_topic_lua_scripting, "m-lua-scripting")
void wp_lua_scripting_api_init (lua_State *L);
struct _WpLuaScriptingPlugin

View File

@@ -9,6 +9,9 @@
#include "script.h"
#include <pipewire/keys.h>
#define WP_LOCAL_LOG_TOPIC log_topic_lua_scripting
WP_LOG_TOPIC_EXTERN (log_topic_lua_scripting)
/*
* This is a WpPlugin subclass that wraps a single lua script and acts like
* a handle for that script. When enabled, through the WpObject activation

View File

@@ -19,7 +19,6 @@ wplua_lib = static_library('wplua-' + wireplumber_api_version,
c_args : [
'-D_GNU_SOURCE',
'-DG_LOG_USE_STRUCTURED',
'-DG_LOG_DOMAIN="wplua"',
],
install: false,
include_directories: wplua_include_dir,

View File

@@ -13,6 +13,9 @@
G_BEGIN_DECLS
#define WP_LOCAL_LOG_TOPIC log_topic_wplua
WP_LOG_TOPIC_EXTERN (log_topic_wplua)
/* boxed.c */
void _wplua_init_gboxed (lua_State *L);

View File

@@ -10,6 +10,8 @@
#include "private.h"
#include <wp/wp.h>
WP_LOG_TOPIC (log_topic_wplua, "wplua")
#define URI_SANDBOX "resource:///org/freedesktop/pipewire/wireplumber/wplua/sandbox.lua"
extern void _wplua_register_resource (void);