x264 设置日志级别

目录

  1. x264命令行和libx264设置日志级别

参考

  • [1] 雷霄骅/x264源代码简单分析:编码器主干部分-1

1. x264命令行和libx264设置日志级别

(1) 命令行
x264 --fullhelp可以看到日志等级的设置方式:

      --log-level     Specify the maximum level of logging ["info"]
                                  - none, error, warning, info, debug
  • 日志等级有none, error, warning, info, debug5个等级,debug等级最低,默认是info。

(2) libx264
libx264中使用x264_param_t.i_log_level参数设置日志级别。

//x264.h
/* Log level */
#define X264_LOG_NONE          (-1)
#define X264_LOG_ERROR          0
#define X264_LOG_WARNING        1
#define X264_LOG_INFO           2
#define X264_LOG_DEBUG          3

2. 源码学习

输出日志使用的函数为x264_log(),函数的定义位于common\common.h。

void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... )
{
    if( !h || i_level <= h->param.i_log_level )
    {
        va_list arg;
        va_start( arg, psz_fmt );
        if( !h )
            x264_log_default( NULL, i_level, psz_fmt, arg );
        else
            h->param.pf_log( h->param.p_log_private, i_level, psz_fmt, arg );
        va_end( arg );
    }
}
//x264\common\base.c
void x264_log_default( void *p_unused, int i_level, const char *psz_fmt, va_list arg )
{
    char *psz_prefix;
    switch( i_level )
    {
        case X264_LOG_ERROR:
            psz_prefix = "error";
            break;
        case X264_LOG_WARNING:
            psz_prefix = "warning";
            break;
        case X264_LOG_INFO:
            psz_prefix = "info";
            break;
        case X264_LOG_DEBUG:
            psz_prefix = "debug";
            break;
        default:
            psz_prefix = "unknown";
            break;
    }
    fprintf( stderr, "x264 [%s]: ", psz_prefix );
    x264_vfprintf( stderr, psz_fmt, arg );
}
//x264\common\osdep.h
#if defined(_WIN32) && !HAVE_WINRT
static inline int x264_vfprintf( FILE *stream, const char *format, va_list arg )
{
    HANDLE console = NULL;
    DWORD mode;

    if( stream == stdout )
        console = GetStdHandle( STD_OUTPUT_HANDLE );
    else if( stream == stderr )
        console = GetStdHandle( STD_ERROR_HANDLE );

    /* Only attempt to convert to UTF-16 when writing to a non-redirected console screen buffer. */
    if( GetConsoleMode( console, &mode ) )
    {
        char buf[4096];
        wchar_t buf_utf16[4096];
        va_list arg2;

        va_copy( arg2, arg );
        int length = vsnprintf( buf, sizeof(buf), format, arg2 );
        va_end( arg2 );

        if( length > 0 && length < sizeof(buf) )
        {
            /* WriteConsoleW is the most reliable way to output Unicode to a console. */
            int length_utf16 = MultiByteToWideChar( CP_UTF8, 0, buf, length, buf_utf16, sizeof(buf_utf16)/sizeof(wchar_t) );
            DWORD written;
            WriteConsoleW( console, buf_utf16, length_utf16, &written, NULL );
            return length;
        }
    }
    return vfprintf( stream, format, arg );
}

static inline int x264_is_pipe( const char *path )
{
    wchar_t path_utf16[MAX_PATH];
    if( utf8_to_utf16( path, path_utf16 ) )
        return WaitNamedPipeW( path_utf16, 0 );
    return 0;
}
#else
#define x264_vfprintf vfprintf
#define x264_is_pipe(x) 0
#endif

使用x264_param_t.pf_log参数设置自定义日志打印的实现,默认使用的x264_log_default实现。

//x264\common\base.c
REALIGN_STACK void x264_param_default( x264_param_t *param )
{
    /* */
    memset( param, 0, sizeof( x264_param_t ) );
...
    /* Log */
    param->pf_log = x264_log_default;
...
}

你可能感兴趣的:(x264 设置日志级别)