avlogbuf.c 1.01 KB
Newer Older
Julian Rother's avatar
Julian Rother committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <pthread.h>
#include <libavutil/log.h>

#include "../util.h"

static char *logbuffer = 0;
static size_t logoffset = 0, logsize = 0;
static pthread_mutex_t loglock = PTHREAD_MUTEX_INITIALIZER;;

void avlogbuf_callback(void *class, int level, const char *fmt, va_list ap)
{
	int len, print_prefix;
	if (level >= AV_LOG_VERBOSE)
		return;
	av_log_default_callback(class, level, fmt, ap);
	if (pthread_mutex_lock(&loglock))
		return;
	if (logsize-logoffset < 1024)
		logbuffer = realloc(logbuffer, (logsize += 1024));
	if (!logbuffer)
		return;
	print_prefix = 1;
	len = av_log_format_line2(class, level, fmt, ap, logbuffer+logoffset,
			logsize-logoffset, &print_prefix);
	if (len < logsize-logoffset)
		logoffset += len;
	else
		logoffset = logsize-1;
	pthread_mutex_unlock(&loglock);
}

char *get_avlogbuf(void)
{
	char *ret, *p;
	if (!logbuffer || pthread_mutex_lock(&loglock))
		return strdup("");
	ret = strdup(logbuffer);
	pthread_mutex_unlock(&loglock);
	for (p = ret; *p; p ++)
		if (*p == '"')
			*p = '\'';
	return ret;
}