Skip to content

Commit 1d83424

Browse files
committed
command: add msg-prefix command
This may be useful for IPC clients and libmpv users, which do not have descriptive client names (`ipc_%d`, `main`), or may want to log using a different prefixes for different components.
1 parent 6eac644 commit 1d83424

File tree

3 files changed

+28
-0
lines changed

3 files changed

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

DOCS/man/input.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,10 @@ Text Manipulation
824824

825825
.. note:: Lua and JS code should use the provided ``mp.msg`` modules.
826826

827+
``msg-prefix <level> <prefix> <message> [...]``
828+
Same as ``msg``, but allows writing messages using ``prefix`` as the prefix
829+
instead of the name of the client that sent the command.
830+
827831
Configuration Commands
828832
~~~~~~~~~~~~~~~~~~~~~~
829833

player/command.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7123,6 +7123,25 @@ static void cmd_msg(void *p)
71237123
talloc_free(log);
71247124
}
71257125

7126+
static void cmd_msg_prefix(void *p)
7127+
{
7128+
struct mp_cmd_ctx *cmd = p;
7129+
struct MPContext *mpctx = cmd->mpctx;
7130+
7131+
if (cmd->num_args < 3)
7132+
return;
7133+
7134+
int level = mp_msg_find_level(cmd->args[0].v.s);
7135+
if (level < 0)
7136+
return;
7137+
7138+
struct mp_log *log = mp_log_new(NULL, mpctx->log, cmd->args[1].v.s);
7139+
for (int i = 2; i < cmd->num_args; i++)
7140+
mp_msg(log, level, (i == 2 ? "%s" : " %s"), cmd->args[i].v.s);
7141+
mp_msg(log, level, "\n");
7142+
talloc_free(log);
7143+
}
7144+
71267145
/* This array defines all known commands.
71277146
* The first field the command name used in libmpv and input.conf.
71287147
* The second field is the handler function (see mp_cmd_def.handler and
@@ -7644,6 +7663,10 @@ const struct mp_cmd_def mp_cmds[] = {
76447663

76457664
{ "msg", cmd_msg, { {"level", OPT_STRING(v.s)}, {"message", OPT_STRING(v.s)} },
76467665
.is_noisy = true, .vararg = true },
7666+
{ "msg-prefix", cmd_msg_prefix, { {"level", OPT_STRING(v.s)},
7667+
{"prefix", OPT_STRING(v.s)},
7668+
{"message", OPT_STRING(v.s)} },
7669+
.is_noisy = true, .vararg = true },
76477670

76487671
{0}
76497672
};

0 commit comments

Comments
 (0)