]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
refactor node allocation and make a copy of userdata in case the session gets killed...
authorMathieu Rene <mrene@avgs.ca>
Tue, 18 Aug 2009 18:24:31 +0000 (18:24 +0000)
committerMathieu Rene <mrene@avgs.ca>
Tue, 18 Aug 2009 18:24:31 +0000 (18:24 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@14555 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_log.h
src/switch_log.c

index 51d156a73a1b1cf16f6a06eb786f706c300ed8a1..711c16a787d5f9eedf045c949d8f32668ec48d3f 100644 (file)
@@ -61,7 +61,7 @@ SWITCH_BEGIN_EXTERN_C
        switch_time_t timestamp;
        /*! A pointer to where the actual content of the message starts (skipping past the preformatted portion) */
        char *content;
-       const char *userdata;
+       char *userdata;
        /* To maintain abi, only add new elements to the end of this struct and do not delete any elements */
        switch_text_channel_t channel;
 } switch_log_node_t;
@@ -143,6 +143,9 @@ SWITCH_DECLARE(switch_log_level_t) switch_log_str2level(_In_z_ const char *str);
 SWITCH_DECLARE(uint32_t) switch_log_str2mask(_In_z_ const char *str);
 #define switch_log_check_mask(_mask, _level) (_mask & (1 << _level))
 
+SWITCH_DECLARE(switch_log_node_t*) switch_log_node_alloc();
+SWITCH_DECLARE(void) switch_log_node_free(switch_log_node_t **node);
+
 ///\}
 SWITCH_END_EXTERN_C
 #endif
index 7ed002947c56abf16fcb466c532cc68d83ca4016..cc8f3ca351ae5603c8043eb591014d0d3474c18a 100644 (file)
@@ -332,6 +332,9 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
                        switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Function", funcp);
                        switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Line", "%d", line);
                        switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Level", "%d", (int) level);
+                       if (!switch_strlen_zero(userdata)) {
+                               switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "User-Data", userdata);
+                       }
                        switch_event_fire(&event);
                        data = NULL;
                }
@@ -378,19 +381,7 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
        } 
 
        if (do_mods && level <= MAX_LEVEL) {
-               switch_log_node_t *node;
-#ifdef SWITCH_LOG_RECYCLE
-               void *pop = NULL;
-
-               if (switch_queue_trypop(LOG_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
-                       node = (switch_log_node_t *) pop;
-               } else {
-#endif
-                       node = malloc(sizeof(*node));
-                       switch_assert(node);
-#ifdef SWITCH_LOG_RECYCLE
-               }
-#endif
+               switch_log_node_t *node = switch_log_node_alloc();
                
                node->data = data;
                data = NULL;
@@ -401,18 +392,10 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
                node->content = content;
                node->timestamp = now;
                node->channel = channel;
-               node->userdata = userdata;
+               node->userdata = !switch_strlen_zero(userdata) ? strdup(userdata) : NULL;
 
                if (switch_queue_trypush(LOG_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
-                       free(node->data);
-#ifdef SWITCH_LOG_RECYCLE
-                       if (switch_queue_trypush(LOG_RECYCLE_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
-                               free(node);
-                       }
-#else
-                       free(node);
-#endif
-                       node = NULL;
+                       switch_log_node_free(&node);
                }
        }
 
@@ -471,7 +454,7 @@ SWITCH_DECLARE(void) switch_core_memory_reclaim_logger(void)
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Returning %d recycled log node(s) %d bytes\n", size,
                                          (int) sizeof(switch_log_node_t) * size);
        while (switch_queue_trypop(LOG_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
-               free(pop);
+               switch_log_node_free(&pop);
        }
 #else
        return;
@@ -496,6 +479,48 @@ SWITCH_DECLARE(switch_status_t) switch_log_shutdown(void)
        return SWITCH_STATUS_SUCCESS;
 }
 
+SWITCH_DECLARE(switch_log_node_t*) switch_log_node_alloc()
+{
+       switch_log_node_t *node = NULL;
+#ifdef SWITCH_LOG_RECYCLE
+       void *pop = NULL;
+
+       if (switch_queue_trypop(LOG_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
+               node = (switch_log_node_t *) pop;
+       } else {
+#endif
+               node = malloc(sizeof(*node));
+               switch_assert(node);
+#ifdef SWITCH_LOG_RECYCLE
+       }
+#endif
+       return node;
+}
+
+SWITCH_DECLARE(void) switch_log_node_free(switch_log_node_t **pnode)
+{
+       switch_log_node_t *node;
+       
+       if (!pnode) {
+               return;
+       }
+
+       node = *pnode;
+
+       if (node) {             
+               switch_safe_free(node->userdata);               
+               switch_safe_free(node->data);
+#ifdef SWITCH_LOG_RECYCLE
+               if (switch_queue_trypush(LOG_RECYCLE_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
+                       free(node);
+               }
+#else
+               free(node);
+#endif
+       }
+       *pnode = NULL;  
+}
+
 /* For Emacs:
  * Local Variables:
  * mode:c