【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权)
qt/ffmpeg 调试方法
1. qt打印文件名, 函数名, 行号
使用内置宏:
/**
* __FILE__, 代表当前文件名;
* __FUNCTION__, 代表当前函数名;
* __LINE__, 代表的当前行号;
*/
#include <QDebug>
#define MYDEBUG qDebug() <<"[FILE:" <<__FILE__ <<",FUNC:" <<__FUNCTION__ <<",LINE:" <<__LINE__ <<"] "
MYDEBUG <<"还可以在后面添加打印哦";
2. ffmpeg 获取调试信息
在ffmpeg的许多函数中, 都会提供返回值供我们确认函数是否成功调用. 当调用失败时将会返回错误值供我们查看出错信息. 如下面的函数声明:/**
* 由于篇幅关系, 我们只看其中的一行.
* @return 0 on success, a negative AVERROR on failure.
*/
int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);
由上可以见, 当函数调用成功时, 返回0; 调用失败时, 返回一个负数的AVERROR.
这个负数的AVERROR怎么使用呢? ffmpeg给我们提供了一个方法: av_strerror(), 用法如下:
/**
* Put a description of the AVERROR code errnum in errbuf.
* In case of failure the global variable errno is set to indicate the
* error. Even in case of failure av_strerror() will print a generic
* error message indicating the errnum provided to errbuf.
*
* @param errnum error code to describe
* @param errbuf buffer to which description is written
* @param errbuf_size the size in bytes of errbuf
* @return 0 on success, a negative value if a description for errnum
* cannot be found
*/
int av_strerror(int errnum, char *errbuf, size_t errbuf_size);
//========================================================================
#define ERR_STR_LEN 1024
char err_buf[ERR_STR_LEN];
int ret = -1;
ret = avformat_open_input(&pFormatCtx, file_path, NULL, NULL);
if(ret< 0 && !av_strerror(ret, err_buf, ERR_STR_LEN)){
MYDEBUG <<err_buf;
return -1;
}
3. ffmpeg通过打印不同颜色的字体来表示调试信息等级的不同
/**
* Print no output.
*/
#define AV_LOG_QUIET -8
/**
* Something went really wrong and we will crash now.
*/
#define AV_LOG_PANIC 0
/**
* Something went wrong and recovery is not possible.
* For example, no header was found for a format which depends
* on headers or an illegal combination of parameters is used.
*/
#define AV_LOG_FATAL 8
/**
* Something went wrong and cannot losslessly be recovered.
* However, not all future data is affected.
*/
#define AV_LOG_ERROR 16
/**
* Something somehow does not look correct. This may or may not
* lead to problems. An example would be the use of '-vstrict -2'.
*/
#define AV_LOG_WARNING 24
/**
* Standard information.
*/
#define AV_LOG_INFO 32
/**
* Detailed information.
*/
#define AV_LOG_VERBOSE 40
/**
* Stuff which is only useful for libav* developers.
*/
#define AV_LOG_DEBUG 48
/**
* Extremely verbose debugging, useful for libav* development.
*/
#define AV_LOG_TRACE 56
/**
* Set the log level
*
* @see lavu_log_constants
*
* @param level Logging level
*/
void av_log_set_level(int level);
该函数用于设置log的等级, 若低于该等级则不会被显示出来.( 数值越小, 等级越高 )
示例如下:
#include <stdio.h>
#include <libavutil/log.h>
int main(int argc, char *argv[])
{
av_log_set_level(AV_LOG_VERBOSE);
av_log(NULL, AV_LOG_INFO, "I am AV_LOG_INFO, %d\n", AV_LOG_INFO);
av_log(NULL, AV_LOG_VERBOSE, "I am AV_LOG_VERBOSE, %d\n", AV_LOG_VERBOSE);
av_log(NULL, AV_LOG_DEBUG, "I am AV_LOG_DEBUG, %d\n", AV_LOG_DEBUG);
return 0;
}
结果如下:
如果你是Linux终端的话, 那么不同的log等级会显示不同的颜色, 因为我这里是QT所以没有显示.