avlogbuf.c 1.21 KB
Newer Older
Julian Rother's avatar
Julian Rother committed
1 2 3 4 5
#include <pthread.h>
#include <libavutil/log.h>

#include "../util.h"

6
FILE *logstream;
Julian Rother's avatar
Julian Rother committed
7 8
static char *logbuffer;
static size_t logsize;
9 10 11
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static char prevbuf[200] = "";
static int repeats = 0;
Julian Rother's avatar
Julian Rother committed
12 13 14 15 16 17 18 19 20

void init_avlogbuf(void)
{
	logbuffer = 0;
	logsize = 0;
	if (!(logstream = open_memstream(&logbuffer, &logsize)))
		exit(99);
	av_log_set_callback(avlogbuf_callback);
}
Julian Rother's avatar
Julian Rother committed
21 22 23

void avlogbuf_callback(void *class, int level, const char *fmt, va_list ap)
{
Julian Rother's avatar
Julian Rother committed
24 25
	int print_prefix;
	char buf[200];
26
	if (level > AV_LOG_WARNING)
Julian Rother's avatar
Julian Rother committed
27 28
		return;
	print_prefix = 1;
Julian Rother's avatar
Julian Rother committed
29
	av_log_format_line(class, level, fmt, ap, BL(buf), &print_prefix);
30 31 32 33 34 35 36 37 38 39 40 41
	if (pthread_mutex_lock(&mutex))
		return;
	if (!strcmp(buf, prevbuf))
	{
		repeats ++;
		pthread_mutex_unlock(&mutex);
		return;
	}
	if (repeats)
		fprintf(logstream, "Last message repeated %d times\n", repeats);
	repeats = 0;
	strcpy(prevbuf, buf);
Julian Rother's avatar
Julian Rother committed
42
	fputs(buf, logstream);
43
	pthread_mutex_unlock(&mutex);
Julian Rother's avatar
Julian Rother committed
44 45 46 47
}

char *get_avlogbuf(void)
{
48
	char *p, *tmp;
Julian Rother's avatar
Julian Rother committed
49 50 51 52 53 54
	if (!logstream)
		return "";
	fclose(logstream);
	p = logbuffer;
	init_avlogbuf();
	fputs(p, logstream);
55 56 57 58 59
	if (repeats)
	{
		tmp = mprintf("%sLast message repeated %d times\n", p, repeats);
		free(p); p = tmp;
	}
Julian Rother's avatar
Julian Rother committed
60
	return p;
Julian Rother's avatar
Julian Rother committed
61
}