From 3e60547958b311ebc15383caeeec3174ed053e53 Mon Sep 17 00:00:00 2001
From: Julian Rother <julianr@fsmpi.rwth-aachen.de>
Date: Mon, 27 Nov 2017 00:53:26 +0100
Subject: [PATCH] avlogbuf: Collapse repeating messages

---
 util/avlogbuf.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/util/avlogbuf.c b/util/avlogbuf.c
index 6ccaa13..2d34f99 100644
--- a/util/avlogbuf.c
+++ b/util/avlogbuf.c
@@ -6,6 +6,9 @@
 static char *logbuffer;
 static size_t logsize;
 static FILE *logstream;
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+static char prevbuf[200] = "";
+static int repeats = 0;
 
 void init_avlogbuf(void)
 {
@@ -20,21 +23,39 @@ void avlogbuf_callback(void *class, int level, const char *fmt, va_list ap)
 {
 	int print_prefix;
 	char buf[200];
-	if (level >= AV_LOG_VERBOSE)
+	if (level > AV_LOG_WARNING)
 		return;
 	print_prefix = 1;
 	av_log_format_line(class, level, fmt, ap, BL(buf), &print_prefix);
+	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);
 	fputs(buf, logstream);
+	pthread_mutex_unlock(&mutex);
 }
 
 char *get_avlogbuf(void)
 {
-	char *p;
+	char *p, *tmp;
 	if (!logstream)
 		return "";
 	fclose(logstream);
 	p = logbuffer;
 	init_avlogbuf();
 	fputs(p, logstream);
+	if (repeats)
+	{
+		tmp = mprintf("%sLast message repeated %d times\n", p, repeats);
+		free(p); p = tmp;
+	}
 	return p;
 }
-- 
GitLab