diff --git a/src/main.c b/src/main.c index 7af4f473..716f4086 100644 --- a/src/main.c +++ b/src/main.c @@ -792,6 +792,7 @@ main(int argc, char **argv) pthread_mutex_unlock(&global_lock); tvhlog(LOG_NOTICE, "STOP", "Exiting HTS Tvheadend"); + tvhlog_end(); if(opt_fork) unlink(opt_pidpath); diff --git a/src/tvhlog.c b/src/tvhlog.c index e32bb240..fca5c0c7 100644 --- a/src/tvhlog.c +++ b/src/tvhlog.c @@ -25,11 +25,13 @@ #include "webui/webui.h" +int tvhlog_exit; int tvhlog_level; int tvhlog_options; char *tvhlog_path; htsmsg_t *tvhlog_debug; htsmsg_t *tvhlog_trace; +pthread_t tvhlog_tid; pthread_mutex_t tvhlog_mutex; pthread_cond_t tvhlog_cond; TAILQ_HEAD(,tvhlog_msg) tvhlog_queue; @@ -147,13 +149,13 @@ tvhlog_thread ( void *p ) size_t l; char buf[2048], t[128]; struct tm tm; - pthread_mutex_lock(&tvhlog_mutex); while (1) { /* Wait */ if (!(msg = TAILQ_FIRST(&tvhlog_queue))) { + if (tvhlog_exit) break; if (fp) { fclose(fp); // only issue here is we close with mutex! // but overall performance will be higher @@ -351,7 +353,7 @@ _tvhlog_hexdump(const char *file, int line, void tvhlog_init ( int level, int options, const char *path ) { - pthread_t tid; + tvhlog_exit = 0; tvhlog_level = level; tvhlog_options = options; tvhlog_path = path ? strdup(path) : NULL; @@ -361,6 +363,15 @@ tvhlog_init ( int level, int options, const char *path ) pthread_mutex_init(&tvhlog_mutex, NULL); pthread_cond_init(&tvhlog_cond, NULL); TAILQ_INIT(&tvhlog_queue); - pthread_create(&tid, NULL, tvhlog_thread, NULL); + pthread_create(&tvhlog_tid, NULL, tvhlog_thread, NULL); } +void +tvhlog_end ( void ) +{ + pthread_mutex_lock(&tvhlog_mutex); + tvhlog_exit = 1; + pthread_cond_signal(&tvhlog_cond); + pthread_mutex_unlock(&tvhlog_mutex); + pthread_join(tvhlog_tid, NULL); +} diff --git a/src/tvhlog.h b/src/tvhlog.h index 2003bede..54cb6ada 100644 --- a/src/tvhlog.h +++ b/src/tvhlog.h @@ -35,6 +35,7 @@ extern pthread_mutex_t tvhlog_mutex; /* Initialise */ void tvhlog_init ( int level, int options, const char *path ); +void tvhlog_end ( void ); void tvhlog_set_debug ( const char *subsys ); void tvhlog_get_debug ( char *subsys, size_t len ); void tvhlog_set_trace ( const char *subsys );