【X264系列】之不同强度的printf

  在工程中我们常需要打印,但打印也可分多种类型,某些类型可以打印给用户看;而某些类型又不适合打印给用户看。由此可设定一个打印强度阈值,低于这个强度阈值不打印,高于这个强度阈值可打印。

1、实现代码

c文件中

static int cli_log_level;

#if !HAVE_WINRT
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 );
}
#endif

void x264_cli_printf( int i_level, const char *fmt, ... )
{
    if( i_level > cli_log_level )//cli_log_level
        return;
    va_list arg;
    va_start( arg, fmt );
    x264_vfprintf( stderr, fmt, arg );
    va_end( arg );
}

h头文件中

#if defined(_WIN32) && !HAVE_WINRT
int x264_vfprintf( FILE *stream, const char *format, va_list arg );
int x264_is_pipe( const char *path );
#else
#define x264_vfprintf vfprintf
#define x264_is_pipe(x) 0
#endif

你可能感兴趣的:(【X264系列】之不同强度的printf)