Skip to content

Commit 6eac644

Browse files
committed
command: add msg command
This allows C plugins, IPC clients, and libmpv to write log messages. Closes: #14551 Closes: #16707
1 parent 2f7543f commit 6eac644

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

DOCS/interface-changes/msg.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add `msg` command

DOCS/man/input.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,16 @@ Text Manipulation
814814

815815
This line of Lua prints "foo \\{bar}" on the OSD.
816816

817+
``msg <level> <message> [...]``
818+
Write a log message. ``level`` must be one of the log levels accepted by the
819+
``mp.msg.log`` Lua function. The ``message`` arguments are separated from
820+
each other with a space. A newline is added to the end of the message.
821+
822+
This command has a variable number of arguments, and cannot be used with
823+
named arguments.
824+
825+
.. note:: Lua and JS code should use the provided ``mp.msg`` modules.
826+
817827
Configuration Commands
818828
~~~~~~~~~~~~~~~~~~~~~~
819829

player/command.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7104,6 +7104,25 @@ static void cmd_notify_property(void *p)
71047104
mp_notify_property(mpctx, cmd->args[0].v.s);
71057105
}
71067106

7107+
static void cmd_msg(void *p)
7108+
{
7109+
struct mp_cmd_ctx *cmd = p;
7110+
struct MPContext *mpctx = cmd->mpctx;
7111+
7112+
if (cmd->num_args < 2)
7113+
return;
7114+
7115+
int level = mp_msg_find_level(cmd->args[0].v.s);
7116+
if (level < 0)
7117+
return;
7118+
7119+
struct mp_log *log = mp_log_new(NULL, mpctx->log, cmd->cmd->sender);
7120+
for (int i = 1; i < cmd->num_args; i++)
7121+
mp_msg(log, level, (i == 1 ? "%s" : " %s"), cmd->args[i].v.s);
7122+
mp_msg(log, level, "\n");
7123+
talloc_free(log);
7124+
}
7125+
71077126
/* This array defines all known commands.
71087127
* The first field the command name used in libmpv and input.conf.
71097128
* The second field is the handler function (see mp_cmd_def.handler and
@@ -7623,6 +7642,9 @@ const struct mp_cmd_def mp_cmds[] = {
76237642

76247643
{ "notify-property", cmd_notify_property, { {"property", OPT_STRING(v.s)} } },
76257644

7645+
{ "msg", cmd_msg, { {"level", OPT_STRING(v.s)}, {"message", OPT_STRING(v.s)} },
7646+
.is_noisy = true, .vararg = true },
7647+
76267648
{0}
76277649
};
76287650

0 commit comments

Comments
 (0)