]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
tvhlog: add tvhdbg() and send realtime mutex log lines to the UDP socket (if requested)
authorJaroslav Kysela <perex@perex.cz>
Wed, 28 Nov 2018 10:48:28 +0000 (11:48 +0100)
committerJaroslav Kysela <perex@perex.cz>
Wed, 28 Nov 2018 11:33:41 +0000 (12:33 +0100)
src/tvh_thread.c
src/tvhlog.c
src/tvhlog.h

index 139b61090b5dee495b68ff076dd6ce9f6515be32..11ca776daeeb45e86076e35d7a3d4c32438e47e1 100644 (file)
@@ -178,7 +178,9 @@ static void tvh_mutex_check_interval(tvh_mutex_t *mutex)
     int64_t ms = (tvh_thread_debug - 10000) * 1000;
     int64_t diff = getfastmonoclock() - mutex->tstamp;
     if (diff > ms)
-      printf("mutex %p at %s:%d took %lldms\n", mutex, mutex->filename, mutex->lineno, diff / (MONOCLOCK_RESOLUTION / 1000));
+      tvhdbg(LS_THREAD, "mutex %p at %s:%d took %lldms",
+             mutex, mutex->filename, mutex->lineno,
+             diff / (MONOCLOCK_RESOLUTION / 1000));
   }
 }
 #endif
index ef41aaf82c2a9f2981f2b44116aebbd2f0d6f43c..f895289aa04117b391bd2ca9bedb9793cf7abc6c 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/time.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/ip.h>
 
 #if ENABLE_EXECINFO
 #include <execinfo.h>
 #endif
 
 #include "bitops.h"
+#include "settings.h"
 #include "libav.h"
+#include "tcp.h"
 #include "webui/webui.h"
 
 #define TVHLOG_BITARRAY ((LS_LAST + (BITS_PER_LONG - 1)) / BITS_PER_LONG)
@@ -44,6 +50,10 @@ tvh_cond_t               tvhlog_cond;
 TAILQ_HEAD(,tvhlog_msg)  tvhlog_queue;
 int                      tvhlog_queue_size;
 int                      tvhlog_queue_full;
+#if ENABLE_TRACE
+int                      tvhlog_rtfd = STDOUT_FILENO;
+struct sockaddr_storage  tvhlog_rtss;
+#endif
 
 #define TVHLOG_QUEUE_MAXSIZE 10000
 #define TVHLOG_THREAD 1
@@ -522,6 +532,37 @@ tvhlog_backtrace_printf(const char *fmt, ...)
 #endif
 }
 
+/*
+ *
+ */
+#if ENABLE_TRACE
+static void tvhdbgv(int subsys, const char *fmt, va_list *args)
+{
+  char buf[2048];
+  size_t l = 0;
+
+  if (tvhlog_rtfd < 0) return;
+  tvh_strlcatf(buf, sizeof(buf), l, "%s: ", tvhlog_subsystems[subsys].name);
+  l += vsnprintf(buf + l, sizeof(buf) - l, fmt, *args);
+  if (l + 1 < sizeof(buf))
+    buf[l++] = '\n';
+  sendto(tvhlog_rtfd, buf, l, 0, (struct sockaddr *)&tvhlog_rtss, sizeof(struct sockaddr_in));
+}
+#endif
+
+/*
+ *
+ */
+#if ENABLE_TRACE
+void tvhdbg(int subsys, const char *fmt, ...)
+{
+  va_list args;
+  va_start(args, fmt);
+  tvhdbgv(subsys, fmt, &args);
+  va_end(args);
+}
+#endif
+
 /*
  * Initialise
  */
@@ -538,6 +579,17 @@ tvhlog_init ( int level, int options, const char *path )
   tvh_mutex_init(&tvhlog_mutex, NULL);
   tvh_cond_init(&tvhlog_cond, 1);
   TAILQ_INIT(&tvhlog_queue);
+#if ENABLE_TRACE
+  {
+    const char *rtport0 = getenv("TVHEADEND_RTLOG_UDP_PORT");
+    int rtport = atoi(rtport0);
+    if (rtport > 0) {
+      tvhlog_rtfd = tvh_socket(AF_INET, SOCK_DGRAM, 0);
+      tcp_get_ip_from_str("127.0.0.1", &tvhlog_rtss);
+      IP_AS_V4(tvhlog_rtss, port) = htons(rtport);
+    }
+  }
+#endif
 }
 
 void
@@ -552,7 +604,6 @@ tvhlog_end ( void )
 {
   FILE *fp = NULL;
   tvhlog_msg_t *msg;
-  tvh_mutex_lock(&tvhlog_mutex);
   tvhlog_run = 0;
   tvh_cond_signal(&tvhlog_cond, 0);
   tvh_mutex_unlock(&tvhlog_mutex);
@@ -567,6 +618,12 @@ tvhlog_end ( void )
   if (fp)
     fclose(fp);
   free(tvhlog_path);
+#if ENABLE_TRACE
+  if (tvhlog_rtfd >= 0) {
+    close(tvhlog_rtfd);
+    tvhlog_rtfd = 1;
+  }
+#endif
   closelog();
 }
 
index 54ba771dd323ee161a4c75e7997397ebc15a172c..7bef63aaf5449edf68892b9a3bb217e1fd4c10fe 100644 (file)
@@ -245,4 +245,10 @@ static inline void tvhtrace_no_warnings(const char *fmt, ...) { (void)fmt; }
 
 void tvhlog_backtrace_printf(const char *fmt, ...);
 
+#if ENABLE_TRACE
+void tvhdbg(int subsys, const char *fmt, ...);
+#else
+static void tvhdbg(int subsys, const char *fmt, ...) {};
+#endif
+
 #endif /* __TVH_LOGGING_H__ */