[Freeswitch-svn] [commit] r9876 - freeswitch/trunk/src

Freeswitch SVN brian at freeswitch.org
Tue Oct 7 13:09:32 EDT 2008


Author: brian
Date: Tue Oct  7 13:09:31 2008
New Revision: 9876

Modified:
   freeswitch/trunk/src/switch_cpp.cpp
   freeswitch/trunk/src/switch_event.c

Log:
add error handling and error messages for incorrect event api usage

Modified: freeswitch/trunk/src/switch_cpp.cpp
==============================================================================
--- freeswitch/trunk/src/switch_cpp.cpp	(original)
+++ freeswitch/trunk/src/switch_cpp.cpp	Tue Oct  7 13:09:31 2008
@@ -208,7 +208,16 @@
 		event_id = SWITCH_EVENT_MESSAGE;
 	}
 
-	switch_event_create_subclass(&event, event_id, subclass_name);
+	if (!switch_strlen_zero(subclass_name) && event_id != SWITCH_EVENT_CUSTOM) {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_WARNING, "Changing event type to custom because you specified a subclass name!\n");
+		event_id = SWITCH_EVENT_CUSTOM;
+	}
+
+	if (switch_event_create_subclass(&event, event_id, subclass_name) != SWITCH_STATUS_SUCCESS) {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Failed to create event!\n");
+		event = NULL;
+	}
+
 	serialized_string = NULL;
 	mine = 1;
 }
@@ -283,9 +292,17 @@
 	if (event) {
 		switch_event_t *new_event;
 		if (switch_event_dup(&new_event, event) == SWITCH_STATUS_SUCCESS) {
-			switch_event_fire(&new_event);
+			if (switch_event_fire(&new_event) != SWITCH_STATUS_SUCCESS) {
+				switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Failed to fire the event!\n");
+				switch_event_destroy(&new_event);
+				return false;
+			}
 			return true;
+		} else {
+			switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Failed to dup the event!\n");
 		}
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to fire an event that does not exist!\n");
 	}
 	return false;
 }
@@ -297,6 +314,8 @@
 	if (event) {
         switch_event_set_priority(event, priority);
 		return true;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to setPriority an event that does not exist!\n");
     }
 	return false;
 }
@@ -307,6 +326,8 @@
 
 	if (event) {
 		return switch_event_get_header(event, header_name);
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to getHeader an event that does not exist!\n");
 	}
 	return NULL;
 }
@@ -317,6 +338,8 @@
 
 	if (event) {
 		return switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, header_name, value) == SWITCH_STATUS_SUCCESS ? true : false;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to addHeader an event that does not exist!\n");
 	}
 
 	return false;
@@ -328,6 +351,8 @@
 
 	if (event) {
 		return switch_event_del_header(event, header_name) == SWITCH_STATUS_SUCCESS ? true : false;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to delHeader an event that does not exist!\n");
 	}
 
 	return false;
@@ -340,6 +365,8 @@
 
 	if (event) {
 		return switch_event_add_body(event, "%s", value) == SWITCH_STATUS_SUCCESS ? true : false;
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to addBody an event that does not exist!\n");
 	}
 	
 	return false;
@@ -352,6 +379,8 @@
 
 	if (event) {
 		return switch_event_get_body(event);
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to getBody an event that does not exist!\n");
 	}
 	
 	return NULL;
@@ -363,6 +392,8 @@
 
 	if (event) {
 		return switch_event_name(event->event_id);
+	} else {
+		switch_log_printf(SWITCH_CHANNEL_LOG,SWITCH_LOG_ERROR, "Trying to getType an event that does not exist!\n");
 	}
 	
 	return (char *) "invalid";

Modified: freeswitch/trunk/src/switch_event.c
==============================================================================
--- freeswitch/trunk/src/switch_event.c	(original)
+++ freeswitch/trunk/src/switch_event.c	Tue Oct  7 13:09:31 2008
@@ -548,11 +548,13 @@
 {
 	void *pop;
 
+	*event = NULL;
+
 	if (event_id != SWITCH_EVENT_CUSTOM && subclass_name) {
 		return SWITCH_STATUS_GENERR;
 	}
 
-	if (switch_queue_trypop(EVENT_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
+	if (switch_queue_trypop(EVENT_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS && pop) {
 		*event = (switch_event_t *) pop;
 	} else {
 		*event = ALLOC(sizeof(switch_event_t));
@@ -1014,10 +1016,6 @@
 	switch_event_add_header_string(*event, SWITCH_STACK_BOTTOM, "Event-Calling-Function", func);
 	switch_event_add_header(*event, SWITCH_STACK_BOTTOM, "Event-Calling-Line-Number", "%d", line);
 
-	if ((*event)->subclass_name) {
-		switch_event_add_header_string(*event, SWITCH_STACK_BOTTOM, "Event-Subclass", (*event)->subclass_name);
-	}
-
 	if (user_data) {
 		(*event)->event_user_data = user_data;
 	}



More information about the Freeswitch-svn mailing list