Use TailQ instead of sys/queue.h in the logging module

This commit is contained in:
Nils O. Selåsdal
2014-04-19 00:20:11 +02:00
parent 7a8731b44d
commit 0fe19af6b6
2 changed files with 37 additions and 18 deletions
+26 -16
View File
@@ -6,6 +6,7 @@
#include <errno.h> #include <errno.h>
#include <syslog.h> #include <syslog.h>
#include <sys/queue.h> #include <sys/queue.h>
#include "ucore/tailq.h"
#include "ucore/logging.h" #include "ucore/logging.h"
#include "ucore/utils.h" #include "ucore/utils.h"
@@ -16,7 +17,7 @@ struct UCLogModule_setting {
}; };
struct UCLogDestination { struct UCLogDestination {
SLIST_ENTRY(UCLogDestination) entry; struct TailQ entry;
enum UC_LOG_DESTINATION dest_type; enum UC_LOG_DESTINATION dest_type;
//log level for this destination //log level for this destination
int log_level; int log_level;
@@ -63,7 +64,7 @@ static const struct UCLogModule g_unknown_module = {
//All destinations. The g_log_lock must be held while accessing //All destinations. The g_log_lock must be held while accessing
//this list an anything contained within it. //this list an anything contained within it.
static SLIST_HEAD(, UCLogDestination) g_destinations; static UC_TAILQ_HEAD(g_destinations);
static int uc_log_reopen_file(struct UCLogDestination *dest); static int uc_log_reopen_file(struct UCLogDestination *dest);
//Uses enum UC_LOG_LEVEL as index //Uses enum UC_LOG_LEVEL as index
static const char *const g_log_levels[] = { static const char *const g_log_levels[] = {
@@ -122,11 +123,12 @@ static inline int uc_ll_2_syslog(enum UC_LOG_LEVEL l)
} }
//Note, this grabs the global lock //Note, this grabs the global lock
static int uc_log_init_settings(struct UCLogDestination *dest) static int uc_log_init_common(struct UCLogDestination *dest)
{ {
int i; int i;
int rc = -1; int rc = -1;
//copy over the log_level_settings
pthread_mutex_lock(&g_log_lock); pthread_mutex_lock(&g_log_lock);
dest->module_settings = calloc(g_modules.cnt, sizeof *dest->module_settings); dest->module_settings = calloc(g_modules.cnt, sizeof *dest->module_settings);
@@ -140,6 +142,8 @@ static int uc_log_init_settings(struct UCLogDestination *dest)
pthread_mutex_unlock(&g_log_lock); pthread_mutex_unlock(&g_log_lock);
uc_tailq_init(&dest->entry);
return rc; return rc;
} }
@@ -150,7 +154,7 @@ struct UCLogDestination *uc_log_new_stderr(int log_level, int log_location)
if(dest == NULL) if(dest == NULL)
return NULL; return NULL;
if (uc_log_init_settings(dest) != 0) { if (uc_log_init_common(dest) != 0) {
free(dest); free(dest);
return NULL; return NULL;
} }
@@ -171,7 +175,7 @@ struct UCLogDestination *uc_log_new_syslog(const char *ident, int facility, int
if (dest == NULL) if (dest == NULL)
return NULL; return NULL;
if (uc_log_init_settings(dest) != 0) { if (uc_log_init_common(dest) != 0) {
free(dest); free(dest);
return NULL; return NULL;
} }
@@ -204,7 +208,7 @@ struct UCLogDestination *uc_log_new_file(const char *filename, int log_level, in
if (dest == NULL) if (dest == NULL)
return NULL; return NULL;
if (uc_log_init_settings(dest) != 0) { if (uc_log_init_common(dest) != 0) {
free(dest); free(dest);
return NULL; return NULL;
} }
@@ -238,6 +242,7 @@ struct UCLogDestination *uc_log_new_file(const char *filename, int log_level, in
void uc_log_add_destination(struct UCLogDestination *dest) void uc_log_add_destination(struct UCLogDestination *dest)
{ {
struct UCLogDestination *it; struct UCLogDestination *it;
int exists = 0;
if (dest == NULL) if (dest == NULL)
return; return;
@@ -245,14 +250,16 @@ void uc_log_add_destination(struct UCLogDestination *dest)
pthread_mutex_lock(&g_log_lock); pthread_mutex_lock(&g_log_lock);
//make sure the destination isn't added twice. //make sure the destination isn't added twice.
SLIST_FOREACH(it, &g_destinations, entry) { UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) {
if (dest == it) if (dest == it) {
exists = 1;
break; break;
} }
}
//add if it isn't already in the list //add if it isn't already in the list
if (it == NULL) { if (!exists) {
SLIST_INSERT_HEAD(&g_destinations, dest, entry); uc_tailq_insert_head(&g_destinations, &dest->entry);
} }
pthread_mutex_unlock(&g_log_lock); pthread_mutex_unlock(&g_log_lock);
@@ -262,15 +269,18 @@ void uc_log_add_destination(struct UCLogDestination *dest)
static inline void uc_log_remove_dest_unlocked(struct UCLogDestination *dest) static inline void uc_log_remove_dest_unlocked(struct UCLogDestination *dest)
{ {
struct UCLogDestination *it; struct UCLogDestination *it;
int exists = 0;
//check that it's not already removed //check that it's not already removed
SLIST_FOREACH(it, &g_destinations, entry) { UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) {
if (it == dest) if (it == dest) {
exists = 1;
break; break;
} }
}
if (it != NULL) { if (exists) {
SLIST_REMOVE(&g_destinations, dest, UCLogDestination, entry); uc_tailq_remove(&dest->entry);
} }
} }
@@ -373,7 +383,7 @@ int uc_log_reopen_files(void)
pthread_mutex_lock(&g_log_lock); pthread_mutex_lock(&g_log_lock);
SLIST_FOREACH(dest, &g_destinations, entry) { UC_TAILQ_FOREACH_CONTAINER(dest, entry, &g_destinations) {
if (dest->dest_type == UC_LDEST_FILE) { if (dest->dest_type == UC_LDEST_FILE) {
rc = uc_log_reopen_file(dest); rc = uc_log_reopen_file(dest);
@@ -553,7 +563,7 @@ void uc_logf(int log_level, int module, int raw,
pthread_mutex_lock(&g_log_lock); pthread_mutex_lock(&g_log_lock);
//do logging for each destination //do logging for each destination
SLIST_FOREACH(dest, &g_destinations, entry) { UC_TAILQ_FOREACH_CONTAINER(dest, entry, &g_destinations) {
const struct UCLogModule *log_module; const struct UCLogModule *log_module;
va_list apc; va_list apc;
int module_log_level; int module_log_level;
+9
View File
@@ -292,6 +292,15 @@ START_TEST (test_logfile_remove_dest)
fail_if(rc != 0, "2. stat failed: %s\n", strerror(errno)); fail_if(rc != 0, "2. stat failed: %s\n", strerror(errno));
fail_if(st.st_size != 77*2, "was %ld\n", (long)st.st_size);//should be same size fail_if(st.st_size != 77*2, "was %ld\n", (long)st.st_size);//should be same size
//removing the last destination should be fine too
uc_log_remove_destination(dest2);
UC_LOGF(UC_LL_DEBUG, 0, "Lorum ipson %d\n", 1);
rc = stat(FILE_NAME, &st);
fail_if(rc != 0, "2. stat failed: %s\n", strerror(errno));
fail_if(st.st_size != 77*2, "was %ld\n", (long)st.st_size);//should be same size
END_TEST END_TEST
START_TEST (test_logfile_full_filesystem) START_TEST (test_logfile_full_filesystem)