From 0fe19af6b6be20c7d7f31c5d36a59df721678f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Sat, 19 Apr 2014 00:20:11 +0200 Subject: [PATCH] Use TailQ instead of sys/queue.h in the logging module --- src/logging.c | 46 +++++++++++++++++++++++++++------------------ test/test_logging.c | 9 +++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/logging.c b/src/logging.c index aad0c34..b0120e8 100644 --- a/src/logging.c +++ b/src/logging.c @@ -6,6 +6,7 @@ #include #include #include +#include "ucore/tailq.h" #include "ucore/logging.h" #include "ucore/utils.h" @@ -16,7 +17,7 @@ struct UCLogModule_setting { }; struct UCLogDestination { - SLIST_ENTRY(UCLogDestination) entry; + struct TailQ entry; enum UC_LOG_DESTINATION dest_type; //log level for this destination 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 //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); //Uses enum UC_LOG_LEVEL as index 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 -static int uc_log_init_settings(struct UCLogDestination *dest) +static int uc_log_init_common(struct UCLogDestination *dest) { int i; int rc = -1; - + + //copy over the log_level_settings pthread_mutex_lock(&g_log_lock); 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); + uc_tailq_init(&dest->entry); + return rc; } @@ -150,11 +154,11 @@ struct UCLogDestination *uc_log_new_stderr(int log_level, int log_location) if(dest == NULL) return NULL; - if (uc_log_init_settings(dest) != 0) { + if (uc_log_init_common(dest) != 0) { free(dest); return NULL; } - + dest->dest_type = UC_LDEST_STDERR; dest->type.file.file = stderr; dest->log_level = log_level;; @@ -171,7 +175,7 @@ struct UCLogDestination *uc_log_new_syslog(const char *ident, int facility, int if (dest == NULL) return NULL; - if (uc_log_init_settings(dest) != 0) { + if (uc_log_init_common(dest) != 0) { free(dest); return NULL; } @@ -204,7 +208,7 @@ struct UCLogDestination *uc_log_new_file(const char *filename, int log_level, in if (dest == NULL) return NULL; - if (uc_log_init_settings(dest) != 0) { + if (uc_log_init_common(dest) != 0) { free(dest); 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) { struct UCLogDestination *it; + int exists = 0; if (dest == NULL) return; @@ -245,14 +250,16 @@ void uc_log_add_destination(struct UCLogDestination *dest) pthread_mutex_lock(&g_log_lock); //make sure the destination isn't added twice. - SLIST_FOREACH(it, &g_destinations, entry) { - if (dest == it) + UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) { + if (dest == it) { + exists = 1; break; + } } //add if it isn't already in the list - if (it == NULL) { - SLIST_INSERT_HEAD(&g_destinations, dest, entry); + if (!exists) { + uc_tailq_insert_head(&g_destinations, &dest->entry); } 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) { struct UCLogDestination *it; + int exists = 0; //check that it's not already removed - SLIST_FOREACH(it, &g_destinations, entry) { - if (it == dest) + UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) { + if (it == dest) { + exists = 1; break; + } } - if (it != NULL) { - SLIST_REMOVE(&g_destinations, dest, UCLogDestination, entry); + if (exists) { + uc_tailq_remove(&dest->entry); } } @@ -373,7 +383,7 @@ int uc_log_reopen_files(void) 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) { 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); //do logging for each destination - SLIST_FOREACH(dest, &g_destinations, entry) { + UC_TAILQ_FOREACH_CONTAINER(dest, entry, &g_destinations) { const struct UCLogModule *log_module; va_list apc; int module_log_level; diff --git a/test/test_logging.c b/test/test_logging.c index 783357d..f91150a 100644 --- a/test/test_logging.c +++ b/test/test_logging.c @@ -292,6 +292,15 @@ START_TEST (test_logfile_remove_dest) 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 + //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 START_TEST (test_logfile_full_filesystem)