Tidied logging, added debug

This commit is contained in:
James Booth
2015-06-08 21:26:44 +01:00
parent d25f8c9acb
commit ad6728e74b
3 changed files with 25 additions and 3 deletions

View File

@@ -25,6 +25,7 @@
#include <string.h>
#include <errno.h>
#include <sys/prctl.h>
#include <pthread.h>
#include <glib.h>
#include <glib/gstdio.h>
@@ -32,7 +33,9 @@
#include "stabber.h"
static FILE *logp;
static gboolean logready = FALSE;
static stbbr_log_t minlevel;
pthread_mutex_t loglock;
gchar *
_xdg_get_data_home(void)
@@ -107,6 +110,7 @@ _mkdir_recursive(const char *dir)
void
log_init(stbbr_log_t loglevel)
{
pthread_mutex_lock(&loglock);
minlevel = loglevel;
gchar *xdg_data = _xdg_get_data_home();
GString *log_dir = g_string_new(xdg_data);
@@ -119,6 +123,8 @@ log_init(stbbr_log_t loglevel)
logp = fopen(log_file, "a");
g_chmod(log_file, S_IRUSR | S_IWUSR);
free(log_file);
logready = TRUE;
pthread_mutex_unlock(&loglock);
}
static char*
@@ -136,7 +142,8 @@ _levelstr(stbbr_log_t loglevel)
void
log_println(stbbr_log_t loglevel, const char * const msg, ...)
{
if (loglevel >= minlevel) {
if (logready && loglevel >= minlevel) {
pthread_mutex_lock(&loglock);
va_list arg;
va_start(arg, msg);
GString *fmt_msg = g_string_new(NULL);
@@ -154,13 +161,17 @@ log_println(stbbr_log_t loglevel, const char * const msg, ...)
g_free(date_fmt);
g_string_free(fmt_msg, TRUE);
va_end(arg);
pthread_mutex_unlock(&loglock);
}
}
void
log_close(void)
{
if (logp) {
pthread_mutex_lock(&loglock);
if (logready && logp) {
fclose(logp);
}
logready = FALSE;
pthread_mutex_unlock(&loglock);
}

View File

@@ -255,7 +255,7 @@ server_wait_for(char *id)
void*
_start_server_cb(void* userdata)
{
prctl(PR_SET_NAME, "stbbr");
prctl(PR_SET_NAME, "stbr");
struct sockaddr_in client_addr;
@@ -299,6 +299,7 @@ _start_server_cb(void* userdata)
int
server_run(stbbr_log_t loglevel, int port, int httpport)
{
prctl(PR_SET_NAME, "main");
pthread_mutex_lock(&send_queue_lock);
send_queue = NULL;
pthread_mutex_unlock(&send_queue_lock);