默认情况下,libavformat会将错误消息写入stderr
,例如:
从比特率估计持续时间,这可能不准确
我该如何关闭它?或更好的方法是将其导入到我的日志函数中?
编辑:重定向stderr到其他位置不可接受,因为我需要它作为其他日志记录目的,我只想让libavformat不写入它。
默认情况下,libavformat会将错误消息写入stderr
,例如:
从比特率估计持续时间,这可能不准确
我该如何关闭它?或更好的方法是将其导入到我的日志函数中?
编辑:重定向stderr到其他位置不可接受,因为我需要它作为其他日志记录目的,我只想让libavformat不写入它。
从代码来看,您可以通过为av_log
函数编写自己的回调函数来更改其行为。
根据libavutil/log.h中的描述:
如果级别小于或等于当前的av_log_level,则将指定的消息发送到日志。默认情况下,所有日志消息都会发送到stderr。通过设置不同的av_vlog回调函数,可以更改此行为。
该API提供了一个函数,允许您定义自己的回调函数:
void av_log_set_callback(void (*)(void*, int, const char*, va_list));
针对您的情况,您可以编写一个简单的回调函数,将消息完全丢弃(或重定向到专用日志等),而不会破坏您的 stderr
流。
av_log_set_level(AV_LOG_QUIET)
来静音所有日志记录。 - Go Dan尝试使用av_log_set_level(level)
!
include this header file
#include <libavutil/log.h>
add this code will disable the log
av_log_set_level(AV_LOG_QUIET);
您可以将它们重定向到自定义文件,它将重定向所有cerr条目:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream file("file.txt");
streambuf *old_cerr = cerr.rdbuf();
cerr.rdbuf (file.rdbuf());
cerr << "test test test" << endl; // writes to file.txt
// ...
cerr.rdbuf (old_cerr); // restore orginal cerr
return 0;
}
编辑:在修改问题后,我警告以上代码将把所有cerr
的流输入重定向到file.txt
我不熟悉libavformat,但如果它的代码无法更改,您可以在调用库的api之前将cerr
暂时重定向到文件中,并将其重定向回原始的cerr
。(然而这是丑陋的方法)
cerr
消息。我认为那不是OP想要的。 - BЈовић