diff --git a/src/main.c b/src/main.c index 01278878..ddc31cb3 100644 --- a/src/main.c +++ b/src/main.c @@ -663,6 +663,9 @@ main(int argc, char **argv) umask(0); } + /* Start log thread (must be done post fork) */ + tvhlog_start(); + /* Alter logging */ if (opt_fork) tvhlog_options &= ~TVHLOG_OPT_STDERR; diff --git a/src/tvhlog.c b/src/tvhlog.c index 10f25a47..e85c35a3 100644 --- a/src/tvhlog.c +++ b/src/tvhlog.c @@ -25,7 +25,7 @@ #include "webui/webui.h" -int tvhlog_exit; +int tvhlog_run; int tvhlog_level; int tvhlog_options; char *tvhlog_path; @@ -215,7 +215,7 @@ static void * tvhlog_thread ( void *p ) { int options; - char *path, buf[512]; + char *path = NULL, buf[512]; FILE *fp = NULL; tvhlog_msg_t *msg; @@ -224,7 +224,7 @@ tvhlog_thread ( void *p ) /* Wait */ if (!(msg = TAILQ_FIRST(&tvhlog_queue))) { - if (tvhlog_exit) break; + if (tvhlog_run != 1) break; if (fp) { fclose(fp); // only issue here is we close with mutex! // but overall performance will be higher @@ -324,13 +324,17 @@ void tvhlogv ( const char *file, int line, msg->severity = severity; msg->notify = notify; #if TVHLOG_THREAD - TAILQ_INSERT_TAIL(&tvhlog_queue, msg, link); - tvhlog_queue_size++; - pthread_cond_signal(&tvhlog_cond); -#else - FILE *fp = NULL; - tvhlog_process(msg, tvhlog_options, &fp, tvhlog_path); - if (fp) fclose(fp); + if (tvhlog_run) { + TAILQ_INSERT_TAIL(&tvhlog_queue, msg, link); + tvhlog_queue_size++; + pthread_cond_signal(&tvhlog_cond); + } else { +#endif + FILE *fp = NULL; + tvhlog_process(msg, tvhlog_options, &fp, tvhlog_path); + if (fp) fclose(fp); +#if TVHLOG_THREAD + } #endif pthread_mutex_unlock(&tvhlog_mutex); } @@ -400,16 +404,22 @@ _tvhlog_hexdump(const char *file, int line, void tvhlog_init ( int level, int options, const char *path ) { - tvhlog_exit = 0; tvhlog_level = level; tvhlog_options = options; tvhlog_path = path ? strdup(path) : NULL; tvhlog_trace = NULL; tvhlog_debug = NULL; + tvhlog_run = 0; openlog("tvheadend", LOG_PID, LOG_DAEMON); pthread_mutex_init(&tvhlog_mutex, NULL); pthread_cond_init(&tvhlog_cond, NULL); TAILQ_INIT(&tvhlog_queue); +} + +void +tvhlog_start ( void ) +{ + tvhlog_run = 1; tvhthread_create(&tvhlog_tid, NULL, tvhlog_thread, NULL, 1); } @@ -417,7 +427,7 @@ void tvhlog_end ( void ) { pthread_mutex_lock(&tvhlog_mutex); - tvhlog_exit = 1; + tvhlog_run = 2; 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 54cb6ada..f5f5c454 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_start ( void ); void tvhlog_end ( void ); void tvhlog_set_debug ( const char *subsys ); void tvhlog_get_debug ( char *subsys, size_t len );