开源之ffmpeg(二)——输出日志/重定向到文件 av_log

前言

在之前的博客中,已经详细的对于FFmpeg进行过介绍,包括如何在windows下编译FFmpeg源码,有需要的小伙伴可以看看上一章博客。

这一章主要介绍如何恰当的输出FFmpeg的日志。

|版本声明:山河君,未经博主允许,禁止转载

一、av_log的介绍

1.为什么要重定向日志输出

在正常使用ffmpeg时,默认日志输出是向sterr,也就是说只能在控制台屏幕上才能看到日志,不便于保存查看。

2.API简单介绍

API 释义
void av_log_set_level(int level); 设置日志等级,处于该等级之前的日志,都会输出
void av_log(void *avcl, int level, const char *fmt, …) av_printf_format(3, 4); 使用ffmpegAPI输出日志
void av_vlog(void *avcl, int level, const char *fmt, va_list vl); 使用ffmpegAPI输出日志,和av_log区别在于使用va_list 进行传参
void av_log_set_callback(void (* callback)(void * , int, const char*, va_list)); 重定向日志输出

日志等级分为以下几种,总的来说,设置的值越大,输出日志信息越详细,因为会把该等级之前的日志也会输出,比如如果设置等级为AV_LOG_FATAL ,那么会输出AV_LOG_FATAL AV_LOG_PANIC

/**
 * 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

二、使用步骤

1.引入库

这里大致介绍一下工程所需要的头文件和库,具体如何编译还请看上一篇文章,当然真正使用中可以根据需求选择库文件去链接
头文件:
在这里插入图片描述
库文件:
在这里插入图片描述

2.具体使用

  • 正常使用
    av_log_set_level全局只需要设置一次就行
av_log_set_level(AV_LOG_TRACE); //设置日志级别
av_log(NULL, AV_LOG_DEBUG, "the debug line:%d, string:%s", __LINE__, "hello");

输出
在这里插入图片描述

  • 重定向写到日志中

头文件

		static FILE* m_pLogFile; //文件路径
		static void LogCallback(void* ptr, int level, const char* fmt, va_list vl); //回调

源文件
首先设置日志级别以及写入时间

void setLog()
{
    
    
		if (m_pLogFile != nullptr)
		{
    
    
			fclose(m_pLogFile);
			m_pLogFile = nullptr;
		}
		time_t t = time(nullptr);
		struct tm* now = localtime(&t);

		std::stringstream time;

		time << now->tm_year + 1900 << "/";
		time << now->tm_mon + 1 << "/";
		time << now->tm_mday << "/";
		time << now->tm_hour << ":";
		time << now->tm_min << ":";
		time << now->tm_sec << std::endl;

		std::cout << time.str();
		av_log_set_level(AV_LOG_TRACE); //设置日志级别
		av_log_set_callback(LogCallback);
		av_log(NULL, AV_LOG_INFO, time.str().c_str());
}

写入文件

FILE* CAudioReadFrame::m_pLogFile = nullptr;
void CAudioReadFrame::LogCallback(void* ptr, int level, const char* fmt, va_list vl)
{
    
    
	if (m_pLogFile == nullptr)
	{
    
    
				m_pLogFile = fopen("E:\\log\\log.txt", "w+");
	}
	if (m_pLogFile)
	{
    
    
		vfprintf(m_pLogFile, fmt, vl); //写入文件
		fflush(m_pLogFile);
	}
}

总结

日志是对开发的一大利器,所谓工欲善其事必先利其器,这里简单的介绍了如何使用好ffmpeg输出日志。

如果对您有所帮助,请帮忙点个赞吧!

猜你喜欢

转载自blog.csdn.net/qq_42956179/article/details/125209578