osmo_ortp: Log domain and fix strings without endl char

Change-Id: Ib6df0df04cd6ba342753c84398274d04917ace40
diff --git a/src/trau/osmo_ortp.c b/src/trau/osmo_ortp.c
index d8f4abf..0eb1661 100644
--- a/src/trau/osmo_ortp.c
+++ b/src/trau/osmo_ortp.c
@@ -102,8 +102,35 @@
 #endif
 	OrtpLogLevel lev, const char *fmt, va_list args)
 {
+	/* Some strings coming from ortp are not endline terminated and mangle
+	 * the output. Make sure all strings are endl terminated before
+	 * printing.
+	 */
+	int needs_endl;
+	const char *domain_str;
+	char *str;
+	size_t fmt_len = strlen(fmt);
+#if HAVE_ORTP_LOG_DOMAIN
+	/* domain can be NULL, found experimentally */
+	domain_str = domain ? : "";
+#else
+	domain_str = "";
+#endif
+	size_t  domain_len = strlen(domain_str);
+
+	if (fmt_len == 0)
+		return;
+
+	needs_endl = fmt[fmt_len - 1] != '\n' ? 1 : 0;
+
+	str = osmo_ortp_malloc(domain_len + 2 /*": "*/ + fmt_len + needs_endl + 1);
+	sprintf(str, "%s%s%s%s", domain_str, domain_len ? ": " : "", fmt, needs_endl ? "\n" : "");
+
 	osmo_vlogp(DLMIB, ortp_to_osmo_lvl(lev), __FILE__, 0,
-		   0, fmt, args);
+		   0, str, args);
+
+	osmo_ortp_free(str);
+
 }
 
 /* ORTP signal callbacks */