foreword
In the previous blog, FFmpeg has been introduced in detail, including how to compile the FFmpeg source code under windows. Friends in need can read the blog in the previous chapter.
This chapter mainly introduces how to properly output FFmpeg logs.
| Version statement: Mr. Shanhe, without the permission of the blogger, reprinting is prohibited
1. Introduction of av_log
1. Why redirect log output
When using ffmpeg normally, the default log output is to sterr, that is to say, the log can only be seen on the console screen, which is not easy to save and view.
2. API brief introduction
API | paraphrase |
---|---|
void av_log_set_level(int level); | Set the log level, and the logs before this level will be output |
void av_log(void *avcl, int level, const char *fmt, …) av_printf_format(3, 4); | Use ffmpegAPI to output logs |
void av_vlog(void *avcl, int level, const char *fmt, va_list vl); | Use ffmpegAPI to output logs, the difference from av_log is that va_list is used to pass parameters |
void av_log_set_callback(void (* callback)(void * , int, const char*, va_list)); | redirect log output |
The log level is divided into the following types. In general, the larger the value set, the more detailed the output log information, because the logs before this level will also be output. For example, if the level is set to , then the AV_LOG_FATAL
output AV_LOG_FATAL
andAV_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
2. Use steps
1. Import library
Here is a general introduction to the header files and libraries required by the project. For details on how to compile, please refer to the previous article. Of course, in actual use, you can choose library files to link according to your needs. Header file: library
file
:
2. Specific use
- Normal use of
av_log_set_level globally only needs to be set once
av_log_set_level(AV_LOG_TRACE); //设置日志级别
av_log(NULL, AV_LOG_DEBUG, "the debug line:%d, string:%s", __LINE__, "hello");
output
- Redirection is written to the log
head File
static FILE* m_pLogFile; //文件路径
static void LogCallback(void* ptr, int level, const char* fmt, va_list vl); //回调
Source file
First set the log level and write time
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());
}
write to file
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);
}
}
Summarize
The log is a great tool for development. The so-called good work must first sharpen the tool. Here is a brief introduction on how to use ffmpeg to output logs.
If it is helpful to you, please help to like it!