From 7f5f4a49f94e8b0cbb5228d464d3b24a5164f70c Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Wed, 28 Nov 2018 11:48:28 +0100 Subject: [PATCH] tvhlog: add tvhdbg() and send realtime mutex log lines to the UDP socket (if requested) --- src/tvh_thread.c | 4 +++- src/tvhlog.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++- src/tvhlog.h | 6 +++++ 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/tvh_thread.c b/src/tvh_thread.c index 139b61090..11ca776da 100644 --- a/src/tvh_thread.c +++ b/src/tvh_thread.c @@ -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 diff --git a/src/tvhlog.c b/src/tvhlog.c index ef41aaf82..f895289aa 100644 --- a/src/tvhlog.c +++ b/src/tvhlog.c @@ -21,13 +21,19 @@ #include #include #include +#include +#include +#include +#include #if ENABLE_EXECINFO #include #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(); } diff --git a/src/tvhlog.h b/src/tvhlog.h index 54ba771dd..7bef63aaf 100644 --- a/src/tvhlog.h +++ b/src/tvhlog.h @@ -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__ */ -- 2.47.2