M lua_mod.c => lua_mod.c +44 -0
@@ 206,6 206,46 @@ int pustule_suspend_device(lua_State* L) {
return 0;
}
+/* Lua log functions */
+static void _lua_log(lua_State* L, int level) {
+ /* Format string */
+ int n = lua_gettop(L);
+
+ lua_getglobal(L, "string");
+ lua_getfield(L, -1, "format");
+ lua_remove(L, -2);
+ lua_rotate(L, 1, 1);
+
+ int res = lua_pcall(L, n, 1, 0);
+ if (check_pcall_error(L, res))
+ return;
+
+ /* Log the message */
+ const char* msg = luaL_checkstring(L, 1);
+
+ lua_Debug ar;
+ lua_getstack(L, 1, &ar);
+ lua_getinfo(L, "lS", &ar);
+ log_log(level, ar.short_src, ar.currentline, msg);
+}
+
+int lua_log_debug(lua_State* L) {
+ _lua_log(L, LOG_DEBUG);
+ return 0;
+}
+int lua_log_info(lua_State* L) {
+ _lua_log(L, LOG_INFO);
+ return 0;
+}
+int lua_log_warn(lua_State* L) {
+ _lua_log(L, LOG_WARN);
+ return 0;
+}
+int lua_log_error(lua_State* L) {
+ _lua_log(L, LOG_ERROR);
+ return 0;
+}
+
/* Helpers */
static void _table_add_str(lua_State* L, const char* name, const char* value) {
lua_pushstring(L, name);
@@ 331,6 371,10 @@ static const struct luaL_Reg pustule_lib[] = {
{"get_default_device", pustule_get_default_device},
{"set_default_device", pustule_set_default_device},
{"suspend_device", pustule_suspend_device},
+ {"log_debug", lua_log_debug},
+ {"log_info", lua_log_info},
+ {"log_warn", lua_log_warn},
+ {"log_error", lua_log_error},
{NULL, NULL},
};
M pustule.h => pustule.h +6 -0
@@ 114,6 114,12 @@ int pustule_get_default_device(lua_State* L);
int pustule_set_default_device(lua_State* L);
int pustule_suspend_device(lua_State* L);
+/* Lua log functions */
+int lua_log_debug(lua_State* L);
+int lua_log_info(lua_State* L);
+int lua_log_warn(lua_State* L);
+int lua_log_error(lua_State* L);
+
/* Lua helpers */
void pustule_push_input_event(lua_State* L, pustule_input_event_t* event);
void pustule_push_device_event(lua_State* L, pustule_device_event_t* event);
M pustule.lua => pustule.lua +9 -11
@@ 52,20 52,18 @@
local io, string = io, string
local pustule = require("pustule")
+local log_d, log_i = pustule.log_debug, pustule.log_info
------------------------------------------------------------------------
-- Helpers
------------------------------------------------------------------------
-function printf(s, ...)
- return io.write(s:format(...))
-end
-- Table of all the active inputs and devices
active_inputs = {}
active_devices = {}
function set_input_volume(idx, vol)
- printf("[%d] Setting volume to %.2f.\n", idx, vol)
+ log_i("[%d] Setting volume to %.2f", idx, vol)
pustule.set_input_volume(idx, vol)
active_inputs[idx] = true
end
@@ 76,21 74,21 @@ function is_input_volume_set(idx)
end
function set_device_volume(idx)
- printf("{%d} Setting volume to %.2f.\n", idx, vol)
+ log_i("{%d} Setting volume to %.2f", idx, vol)
pustule.set_device_volume(idx, vol)
end
function show_input(idx, input)
local k, v
for k, v in pairs(input) do
- printf(" [%d] %s: %s\n", idx, k, v)
+ log_d(" [%d] %s: %s", idx, k, v)
end
end
function show_device(idx, device)
local k, v
for k, v in pairs(device) do
- printf(" {%d} %s: %s\n", idx, k, v)
+ log_d(" {%d} %s: %s", idx, k, v)
end
end
@@ 149,13 147,13 @@ pustule.connect_signal("input_removed", input_removed__media_role)
-- Default rules: inputs
------------------------------------------------------------------------
function input_added__all(idx, input)
- printf("[%d] Input added.\n", idx)
+ log_i("[%d] Input added: %s (%s)", idx, input["application.name"], input.name)
show_input(idx, input)
end
pustule.connect_signal("input_added", input_added__all)
function input_removed__all(idx, input)
- printf("[%d] Input removed.\n", idx)
+ log_i("[%d] Input removed", idx)
active_inputs[idx] = nil
end
pustule.connect_signal("input_removed", input_removed__all)
@@ 173,14 171,14 @@ pustule.connect_signal("input_added", input_added__default_volume)
-- Default rules: devices
------------------------------------------------------------------------
function device_added__all(idx, device)
- printf("{%d} Device added.\n", idx)
+ log_i("{%d} Device added: %s", idx, device.description)
show_device(idx, device)
active_devices[idx] = device
end
pustule.connect_signal("device_added", device_added__all)
function device_removed__all(idx)
- printf("{%d} Device removed.\n", idx)
+ log_i("{%d} Device removed", idx)
active_devices[idx] = nil
end
pustule.connect_signal("device_removed", device_removed__all)