Skip to content

Commit de9b9ef

Browse files
committed
lua: reimplement mp.log using msg command
1 parent 1d83424 commit de9b9ef

File tree

2 files changed

+8
-35
lines changed

2 files changed

+8
-35
lines changed

player/lua.c

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include "common/common.h"
3333
#include "options/m_property.h"
3434
#include "common/msg.h"
35-
#include "common/msg_control.h"
3635
#include "common/stats.h"
3736
#include "options/m_option.h"
3837
#include "input/input.h"
@@ -499,39 +498,6 @@ static int load_lua(struct mp_script_args *args)
499498
return r;
500499
}
501500

502-
static int check_loglevel(lua_State *L, int arg)
503-
{
504-
const char *level = luaL_checkstring(L, arg);
505-
int n = mp_msg_find_level(level);
506-
if (n >= 0)
507-
return n;
508-
luaL_error(L, "Invalid log level '%s'", level);
509-
abort();
510-
}
511-
512-
static int script_log(lua_State *L)
513-
{
514-
struct script_ctx *ctx = get_ctx(L);
515-
516-
int msgl = check_loglevel(L, 1);
517-
518-
int last = lua_gettop(L);
519-
lua_getglobal(L, "tostring"); // args... tostring
520-
for (int i = 2; i <= last; i++) {
521-
lua_pushvalue(L, -1); // args... tostring tostring
522-
lua_pushvalue(L, i); // args... tostring tostring args[i]
523-
lua_call(L, 1, 1); // args... tostring str
524-
const char *s = lua_tostring(L, -1);
525-
if (s == NULL)
526-
return luaL_error(L, "Invalid argument");
527-
mp_msg(ctx->log, msgl, (i == 2 ? "%s" : " %s"), s);
528-
lua_pop(L, 1); // args... tostring
529-
}
530-
mp_msg(ctx->log, msgl, "\n");
531-
532-
return 0;
533-
}
534-
535501
static int script_find_config_file(lua_State *L)
536502
{
537503
struct MPContext *mpctx = get_mpctx(L);
@@ -1226,7 +1192,6 @@ struct fn_entry {
12261192
};
12271193

12281194
static const struct fn_entry main_fns[] = {
1229-
FN_ENTRY(log),
12301195
AF_ENTRY(raw_wait_event),
12311196
FN_ENTRY(request_event),
12321197
FN_ENTRY(find_config_file),

player/lua/defaults.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,14 @@ end
486486
-- sent by "script-binding"
487487
mp.register_script_message("key-binding", dispatch_key_binding)
488488

489+
function mp.log(level, ...)
490+
local cmd = {"msg", level}
491+
for i = 1, select("#", ...) do
492+
cmd[#cmd + 1] = tostring(select(i, ...))
493+
end
494+
mp.command_native(cmd)
495+
end
496+
489497
mp.msg = {
490498
log = mp.log,
491499
fatal = function(...) return mp.log("fatal", ...) end,

0 commit comments

Comments
 (0)