Skip to content

Commit 2496a12

Browse files
committed
libmpv: add mpv_msg() and mpv_msg_va() functions
This gives C plugins (and libmpv users) the ability to write log messages prefixed with their client name like Lua/JS scripts. Fixes: #14551 Fixes: #16707
1 parent 7037ff4 commit 2496a12

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

DOCS/client-api-changes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ API changes
3232

3333
::
3434

35+
--- mpv 0.41.0 ---
36+
2.6 - add functions to write log messages using the log instance of the
37+
mpv_handle: mpv_msg(), mpv_msg_va(), mpv_msg_err(), ...
3538
--- mpv 0.40.0 ---
3639
2.5 - Deprecate MPV_RENDER_PARAM_AMBIENT_LIGHT. no replacement.
3740
--- mpv 0.39.0 ---

include/mpv/client.h

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#ifndef MPV_CLIENT_API_H_
2424
#define MPV_CLIENT_API_H_
2525

26+
#include <stdarg.h>
2627
#include <stddef.h>
2728
#include <stdint.h>
2829

@@ -43,6 +44,19 @@
4344
#define MPV_DECLTYPE __typeof__
4445
#endif
4546

47+
// Stolen from osdep/compiler.h
48+
#if defined(__GNUC__) || defined(__clang__)
49+
#define MPV_PRINTF_ATTRIBUTE(a1, a2) __attribute__((format(printf, a1, a2)))
50+
#else
51+
#define MPV_PRINTF_ATTRIBUTE(a1, a2)
52+
#endif
53+
54+
// Broken crap with __USE_MINGW_ANSI_STDIO
55+
#if defined(__MINGW32__) && defined(__GNUC__) && !defined(__clang__)
56+
#undef MPV_PRINTF_ATTRIBUTE
57+
#define MPV_PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (gnu_printf, a1, a2)))
58+
#endif
59+
4660
#ifdef __cplusplus
4761
extern "C" {
4862
#endif
@@ -248,7 +262,7 @@ extern "C" {
248262
* relational operators (<, >, <=, >=).
249263
*/
250264
#define MPV_MAKE_VERSION(major, minor) (((major) << 16) | (minor) | 0UL)
251-
#define MPV_CLIENT_API_VERSION MPV_MAKE_VERSION(2, 5)
265+
#define MPV_CLIENT_API_VERSION MPV_MAKE_VERSION(2, 6)
252266

253267
/**
254268
* The API user is allowed to "#define MPV_ENABLE_DEPRECATED 0" before
@@ -1682,6 +1696,41 @@ MPV_EXPORT int mpv_request_event(mpv_handle *ctx, mpv_event_id event, int enable
16821696
*/
16831697
MPV_EXPORT int mpv_request_log_messages(mpv_handle *ctx, const char *min_level);
16841698

1699+
/**
1700+
* Write a log message using the log instance of the mpv_handle.
1701+
*
1702+
* Safe to be called from mpv render API threads.
1703+
*
1704+
* @param lev See enum mpv_log_level.
1705+
* @param format printf-style format string
1706+
*/
1707+
MPV_EXPORT void mpv_msg(mpv_handle *ctx, mpv_log_level lev, const char *format, ...)
1708+
MPV_PRINTF_ATTRIBUTE(3, 4);
1709+
1710+
/**
1711+
* Same as mpv_msg but takes a va_list.
1712+
*/
1713+
MPV_EXPORT void mpv_msg_va(mpv_handle *ctx, mpv_log_level lev, const char *format, va_list va)
1714+
MPV_PRINTF_ATTRIBUTE(3, 0);
1715+
1716+
/**
1717+
* @defgroup msg_macros Convenience macros for mpv_msg
1718+
*
1719+
* @{
1720+
*/
1721+
1722+
#define mpv_msg_fatal(ctx, ...) mpv_msg(ctx, MPV_LOG_LEVEL_FATAL, __VA_ARGS__)
1723+
#define mpv_msg_err(ctx, ...) mpv_msg(ctx, MPV_LOG_LEVEL_ERROR, __VA_ARGS__)
1724+
#define mpv_msg_warn(ctx, ...) mpv_msg(ctx, MPV_LOG_LEVEL_WARN, __VA_ARGS__)
1725+
#define mpv_msg_info(ctx, ...) mpv_msg(ctx, MPV_LOG_LEVEL_INFO, __VA_ARGS__)
1726+
#define mpv_msg_verbose(ctx, ...) mpv_msg(ctx, MPV_LOG_LEVEL_V, __VA_ARGS__)
1727+
#define mpv_msg_dbg(ctx, ...) mpv_msg(ctx, MPV_LOG_LEVEL_DEBUG, __VA_ARGS__)
1728+
#define mpv_msg_trace(ctx, ...) mpv_msg(ctx, MPV_LOG_LEVEL_TRACE, __VA_ARGS__)
1729+
1730+
/**
1731+
* @}
1732+
*/
1733+
16851734
/**
16861735
* Wait for the next event, or until the timeout expires, or if another thread
16871736
* makes a call to mpv_wakeup(). Passing 0 as timeout will never wait, and
@@ -2008,6 +2057,10 @@ MPV_DEFINE_SYM_PTR(mpv_request_event)
20082057
#define mpv_request_event pfn_mpv_request_event
20092058
MPV_DEFINE_SYM_PTR(mpv_request_log_messages)
20102059
#define mpv_request_log_messages pfn_mpv_request_log_messages
2060+
MPV_DEFINE_SYM_PTR(mpv_msg)
2061+
#define mpv_msg pfn_mpv_msg
2062+
MPV_DEFINE_SYM_PTR(mpv_msg_va)
2063+
#define mpv_msg_va pfn_mpv_msg_va
20112064
MPV_DEFINE_SYM_PTR(mpv_wait_event)
20122065
#define mpv_wait_event pfn_mpv_wait_event
20132066
MPV_DEFINE_SYM_PTR(mpv_wakeup)

player/client.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <fcntl.h>
1919
#include <math.h>
2020
#include <stdatomic.h>
21+
#include <stdarg.h>
2122
#include <stddef.h>
2223
#include <stdint.h>
2324
#include <stdlib.h>
@@ -1929,6 +1930,43 @@ static bool gen_log_message_event(struct mpv_handle *ctx)
19291930
return false;
19301931
}
19311932

1933+
static int mp_msg_level_from_mpv_level(mpv_log_level lev)
1934+
{
1935+
switch (lev) {
1936+
case MPV_LOG_LEVEL_FATAL:
1937+
return MSGL_FATAL;
1938+
case MPV_LOG_LEVEL_ERROR:
1939+
return MSGL_ERR;
1940+
case MPV_LOG_LEVEL_WARN:
1941+
return MSGL_WARN;
1942+
case MPV_LOG_LEVEL_INFO:
1943+
return MSGL_INFO;
1944+
case MPV_LOG_LEVEL_V:
1945+
return MSGL_V;
1946+
case MPV_LOG_LEVEL_DEBUG:
1947+
return MSGL_DEBUG;
1948+
case MPV_LOG_LEVEL_TRACE:
1949+
return MSGL_TRACE;
1950+
}
1951+
return -1;
1952+
}
1953+
1954+
void mpv_msg_va(mpv_handle *ctx, mpv_log_level lev, const char *format, va_list va)
1955+
{
1956+
int msgl = mp_msg_level_from_mpv_level(lev);
1957+
if (msgl < 0)
1958+
return;
1959+
mp_msg_va(ctx->log, msgl, format, va);
1960+
}
1961+
1962+
void mpv_msg(mpv_handle *ctx, mpv_log_level lev, const char *format, ...)
1963+
{
1964+
va_list va;
1965+
va_start(va, format);
1966+
mpv_msg_va(ctx, lev, format, va);
1967+
va_end(va);
1968+
}
1969+
19321970
int mpv_get_wakeup_pipe(mpv_handle *ctx)
19331971
{
19341972
mp_mutex_lock(&ctx->wakeup_lock);

player/scripting.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ static void init_sym_table(struct mp_script_args *args, void *lib) {
356356
INIT_SYM(mpv_event_to_node);
357357
INIT_SYM(mpv_request_event);
358358
INIT_SYM(mpv_request_log_messages);
359+
INIT_SYM(mpv_msg);
360+
INIT_SYM(mpv_msg_va);
359361
INIT_SYM(mpv_wait_event);
360362
INIT_SYM(mpv_wakeup);
361363
INIT_SYM(mpv_set_wakeup_callback);

0 commit comments

Comments
 (0)