make debug-logging code event driven with back history

This commit is contained in:
Nick Kelsey 2017-06-02 12:25:16 -07:00
commit e1076a2e97
2 changed files with 30 additions and 20 deletions

View file

@ -42,7 +42,6 @@
struct hdhomerun_debug_message_t struct hdhomerun_debug_message_t
{ {
struct hdhomerun_debug_message_t *next; struct hdhomerun_debug_message_t *next;
struct hdhomerun_debug_message_t *prev;
char buffer[2048]; char buffer[2048];
}; };
@ -57,6 +56,7 @@ struct hdhomerun_debug_t
pthread_mutex_t queue_lock; pthread_mutex_t queue_lock;
pthread_mutex_t send_lock; pthread_mutex_t send_lock;
thread_cond_t queue_cond;
struct hdhomerun_debug_message_t *queue_head; struct hdhomerun_debug_message_t *queue_head;
struct hdhomerun_debug_message_t *queue_tail; struct hdhomerun_debug_message_t *queue_tail;
uint32_t queue_depth; uint32_t queue_depth;
@ -80,6 +80,7 @@ struct hdhomerun_debug_t *hdhomerun_debug_create(void)
pthread_mutex_init(&dbg->print_lock, NULL); pthread_mutex_init(&dbg->print_lock, NULL);
pthread_mutex_init(&dbg->queue_lock, NULL); pthread_mutex_init(&dbg->queue_lock, NULL);
pthread_mutex_init(&dbg->send_lock, NULL); pthread_mutex_init(&dbg->send_lock, NULL);
thread_cond_init(&dbg->queue_cond);
if (pthread_create(&dbg->thread, NULL, &hdhomerun_debug_thread_execute, dbg) != 0) { if (pthread_create(&dbg->thread, NULL, &hdhomerun_debug_thread_execute, dbg) != 0) {
free(dbg); free(dbg);
@ -111,6 +112,7 @@ void hdhomerun_debug_destroy(struct hdhomerun_debug_t *dbg)
hdhomerun_sock_destroy(dbg->sock); hdhomerun_sock_destroy(dbg->sock);
} }
thread_cond_dispose(&dbg->queue_cond);
pthread_mutex_dispose(&dbg->print_lock); pthread_mutex_dispose(&dbg->print_lock);
pthread_mutex_dispose(&dbg->queue_lock); pthread_mutex_dispose(&dbg->queue_lock);
pthread_mutex_dispose(&dbg->send_lock); pthread_mutex_dispose(&dbg->send_lock);
@ -205,8 +207,12 @@ void hdhomerun_debug_enable(struct hdhomerun_debug_t *dbg)
if (!dbg) { if (!dbg) {
return; return;
} }
if (dbg->enabled) {
return;
}
dbg->enabled = true; dbg->enabled = true;
thread_cond_signal(&dbg->queue_cond);
} }
void hdhomerun_debug_disable(struct hdhomerun_debug_t *dbg) void hdhomerun_debug_disable(struct hdhomerun_debug_t *dbg)
@ -237,7 +243,7 @@ void hdhomerun_debug_flush(struct hdhomerun_debug_t *dbg, uint64_t timeout)
while (getcurrenttime() < timeout) { while (getcurrenttime() < timeout) {
pthread_mutex_lock(&dbg->queue_lock); pthread_mutex_lock(&dbg->queue_lock);
struct hdhomerun_debug_message_t *message = dbg->queue_tail; struct hdhomerun_debug_message_t *message = dbg->queue_head;
pthread_mutex_unlock(&dbg->queue_lock); pthread_mutex_unlock(&dbg->queue_lock);
if (!message) { if (!message) {
@ -261,9 +267,6 @@ void hdhomerun_debug_vprintf(struct hdhomerun_debug_t *dbg, const char *fmt, va_
if (!dbg) { if (!dbg) {
return; return;
} }
if (!dbg->enabled) {
return;
}
struct hdhomerun_debug_message_t *message = (struct hdhomerun_debug_message_t *)malloc(sizeof(struct hdhomerun_debug_message_t)); struct hdhomerun_debug_message_t *message = (struct hdhomerun_debug_message_t *)malloc(sizeof(struct hdhomerun_debug_message_t));
if (!message) { if (!message) {
@ -313,17 +316,21 @@ void hdhomerun_debug_vprintf(struct hdhomerun_debug_t *dbg, const char *fmt, va_
*/ */
pthread_mutex_lock(&dbg->queue_lock); pthread_mutex_lock(&dbg->queue_lock);
message->prev = NULL; message->next = NULL;
message->next = dbg->queue_head; if (dbg->queue_tail) {
dbg->queue_head = message; dbg->queue_tail->next = message;
if (message->next) { dbg->queue_tail = message;
message->next->prev = message;
} else { } else {
dbg->queue_head = message;
dbg->queue_tail = message; dbg->queue_tail = message;
} }
dbg->queue_depth++; dbg->queue_depth++;
pthread_mutex_unlock(&dbg->queue_lock); pthread_mutex_unlock(&dbg->queue_lock);
if (dbg->enabled) {
thread_cond_signal(&dbg->queue_cond);
}
} }
/* Send lock held by caller */ /* Send lock held by caller */
@ -403,12 +410,10 @@ static void hdhomerun_debug_pop_and_free_message(struct hdhomerun_debug_t *dbg)
{ {
pthread_mutex_lock(&dbg->queue_lock); pthread_mutex_lock(&dbg->queue_lock);
struct hdhomerun_debug_message_t *message = dbg->queue_tail; struct hdhomerun_debug_message_t *message = dbg->queue_head;
dbg->queue_tail = message->prev; dbg->queue_head = message->next;
if (message->prev) { if (!dbg->queue_head) {
message->prev->next = NULL; dbg->queue_tail = NULL;
} else {
dbg->queue_head = NULL;
} }
dbg->queue_depth--; dbg->queue_depth--;
@ -422,14 +427,13 @@ static THREAD_FUNC_PREFIX hdhomerun_debug_thread_execute(void *arg)
struct hdhomerun_debug_t *dbg = (struct hdhomerun_debug_t *)arg; struct hdhomerun_debug_t *dbg = (struct hdhomerun_debug_t *)arg;
while (!dbg->terminate) { while (!dbg->terminate) {
pthread_mutex_lock(&dbg->queue_lock); pthread_mutex_lock(&dbg->queue_lock);
struct hdhomerun_debug_message_t *message = dbg->queue_tail; struct hdhomerun_debug_message_t *message = dbg->queue_head;
uint32_t queue_depth = dbg->queue_depth; uint32_t queue_depth = dbg->queue_depth;
pthread_mutex_unlock(&dbg->queue_lock); pthread_mutex_unlock(&dbg->queue_lock);
if (!message) { if (!message) {
msleep_approx(250); thread_cond_wait(&dbg->queue_cond);
continue; continue;
} }
@ -438,8 +442,13 @@ static THREAD_FUNC_PREFIX hdhomerun_debug_thread_execute(void *arg)
continue; continue;
} }
if (!dbg->enabled) {
thread_cond_wait(&dbg->queue_cond);
continue;
}
if (!hdhomerun_debug_output_message(dbg, message)) { if (!hdhomerun_debug_output_message(dbg, message)) {
msleep_approx(250); msleep_approx(1000);
continue; continue;
} }

View file

@ -31,6 +31,7 @@
#include <signal.h> #include <signal.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <netinet/in.h> #include <netinet/in.h>