Revision
14555
Author
mrene
Date
2009-08-18 13:24:31 -0500 (Tue, 18 Aug 2009)

Log Message

refactor node allocation and make a copy of userdata in case the session gets killed before the logger module gets the node

Modified Paths

Diff

Modified: freeswitch/trunk/src/include/switch_log.h (14554 => 14555)


--- freeswitch/trunk/src/include/switch_log.h	2009-08-18 16:58:26 UTC (rev 14554)
+++ freeswitch/trunk/src/include/switch_log.h	2009-08-18 18:24:31 UTC (rev 14555)
@@ -61,7 +61,7 @@
 	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(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

Modified: freeswitch/trunk/src/switch_log.c (14554 => 14555)


--- freeswitch/trunk/src/switch_log.c	2009-08-18 16:58:26 UTC (rev 14554)
+++ freeswitch/trunk/src/switch_log.c	2009-08-18 18:24:31 UTC (rev 14555)
@@ -332,6 +332,9 @@
 			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 @@
 	} 
 
 	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 @@
 		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_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 @@
 	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