From bb1b152fd003108903821a41640d95adf4414cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Sat, 19 Apr 2014 00:36:24 +0200 Subject: [PATCH] Slight refactor of checking if a destination exists --- src/logging.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/logging.c b/src/logging.c index b0120e8..f0d44ab 100644 --- a/src/logging.c +++ b/src/logging.c @@ -239,24 +239,30 @@ struct UCLogDestination *uc_log_new_file(const char *filename, int log_level, in return dest; } -void uc_log_add_destination(struct UCLogDestination *dest) +//Check if a destination already exists (hold g_log_lock when calling this) +static int uc_log_dest_exists_unlocked(struct UCLogDestination *dest) { struct UCLogDestination *it; - int exists = 0; + + UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) { + if (dest == it) { + return 1; + } + } + + return 0; +} + +void uc_log_add_destination(struct UCLogDestination *dest) +{ + int exists; if (dest == NULL) return; pthread_mutex_lock(&g_log_lock); - //make sure the destination isn't added twice. - UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) { - if (dest == it) { - exists = 1; - break; - } - } - + exists = uc_log_dest_exists_unlocked(dest); //add if it isn't already in the list if (!exists) { uc_tailq_insert_head(&g_destinations, &dest->entry); @@ -268,16 +274,10 @@ void uc_log_add_destination(struct UCLogDestination *dest) //hold g_log_lock while calling this. static inline void uc_log_remove_dest_unlocked(struct UCLogDestination *dest) { - struct UCLogDestination *it; - int exists = 0; + int exists; //check that it's not already removed - UC_TAILQ_FOREACH_CONTAINER(it, entry, &g_destinations) { - if (it == dest) { - exists = 1; - break; - } - } + exists = uc_log_dest_exists_unlocked(dest); if (exists) { uc_tailq_remove(&dest->entry);