]> git.ipfire.org Git - location/libloc.git/commitdiff
lua: Implement setting a log callback function
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 May 2024 15:35:28 +0000 (15:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 May 2024 15:35:28 +0000 (15:35 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/lua/location.c
tests/lua/main.lua

index b7d2c0e869f14859952f60c673e29630caa9201f..1c9488971fea0582c2f415d5df7d07efa256285d 100644 (file)
@@ -15,6 +15,7 @@
 */
 
 #include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include <lua.h>
 
 struct loc_ctx* ctx = NULL;
 
+static int log_callback_ref = 0;
+
+static void log_callback(struct loc_ctx* _ctx, void* data, int priority, const char* file,
+               int line, const char* fn, const char* format, va_list args) {
+       char* message = NULL;
+       int r;
+
+       lua_State* L = data;
+
+       // Format the log message
+       r = vasprintf(&message, format, args);
+       if (r < 0)
+               return;
+
+       // Fetch the Lua callback function
+       lua_rawgeti(L, LUA_REGISTRYINDEX, log_callback_ref);
+
+       // Pass the priority as first argument
+       lua_pushnumber(L, priority);
+
+       // Pass the message as second argument
+       lua_pushstring(L, message);
+
+       // Call the function
+       lua_call(L, 2, 0);
+
+       free(message);
+}
+
+static int set_log_callback(lua_State* L) {
+       // Check if we have received a function
+       luaL_checktype(L, 1, LUA_TFUNCTION);
+
+       // Store a reference to the callback function
+       log_callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
+
+       // Register our callback helper
+       if (ctx)
+               loc_set_log_callback(ctx, log_callback, L);
+
+       return 0;
+}
+
+static int set_log_level(lua_State* L) {
+       const int level = luaL_checknumber(L, 1);
+
+       // Store the new log level
+       if (ctx)
+               loc_set_log_priority(ctx, level);
+
+       return 0;
+}
+
 static int version(lua_State* L) {
        lua_pushstring(L, PACKAGE_VERSION);
        return 1;
 }
 
 static const struct luaL_Reg location_functions[] = {
+       { "set_log_callback", set_log_callback },
+       { "set_log_level", set_log_level },
        { "version", version },
        { NULL, NULL },
 };
index e139b2dc2171ee65bb52972dec99b1df15244c29..0133dc5e113826709e8754640532be5845cd89a8 100755 (executable)
@@ -30,6 +30,28 @@ function test_load()
        print(location.version())
 end
 
+log_callback_called = 0
+
+function log_callback(level, message)
+       log_callback_called = 1
+       print("LOG " .. message)
+end
+
+function test_log_callback()
+       location = require("location")
+
+       -- Set the callback
+       location.set_log_callback(log_callback)
+
+       -- Enable debugging
+       location.set_log_level(7)
+
+       -- Perform some random operation
+       local db = location.Database.open(ENV_TEST_DATABASE)
+
+       luaunit.assertIsTrue(log_callback_called)
+end
+
 function test_open_database()
        location = require("location")