From e02631c0a8e105d93aa7132a4d2d7c0055d26912 Mon Sep 17 00:00:00 2001
From: Dragos Oancea <dragos.oancea@nexmo.com>
Date: Thu, 23 Mar 2017 13:01:45 +0000
Subject: [PATCH] FS-10172: mod_event_socket: handle return codes from
 switch_queue_trypush() , more verbose logging when we cannot enqueue

---
 .../mod_event_socket/mod_event_socket.c       | 31 ++++++++++++++-----
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c
index 415e30131a..82bd7b9f79 100644
--- a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c
+++ b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c
@@ -168,22 +168,29 @@ static void launch_listener_thread(listener_t *listener);
 static switch_status_t socket_logger(const switch_log_node_t *node, switch_log_level_t level)
 {
 	listener_t *l;
-
+	switch_status_t qstatus;
 	switch_mutex_lock(globals.listener_mutex);
 	for (l = listen_list.listeners; l; l = l->next) {
 		if (switch_test_flag(l, LFLAG_LOG) && l->level >= node->level) {
 			switch_log_node_t *dnode = switch_log_node_dup(node);
-
-			if (switch_queue_trypush(l->log_queue, dnode) == SWITCH_STATUS_SUCCESS) {
+			qstatus = switch_queue_trypush(l->log_queue, dnode); 
+			if (qstatus == SWITCH_STATUS_SUCCESS) {
 				if (l->lost_logs) {
 					int ll = l->lost_logs;
 					l->lost_logs = 0;
-					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Lost %d log lines!\n", ll);
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Lost [%d] log lines! Log Queue size: [%u/%u]\n", ll, switch_queue_size(l->log_queue), MAX_QUEUE_LEN);
 				}
 			} else {
+				char errbuf[512] = {0};
+				unsigned int qsize = switch_queue_size(l->log_queue);
+				switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, 
+						"Log enqueue ERROR [%d] | [%s] Queue size: [%u/%u] %s\n", 
+						(int)qstatus, switch_strerror(qstatus, errbuf, sizeof(errbuf)), qsize, MAX_QUEUE_LEN, (qsize == MAX_QUEUE_LEN)?"Max queue size reached":"");
 				switch_log_node_free(&dnode);
 				if (++l->lost_logs > MAX_MISSED) {
-					kill_listener(l, NULL);
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, 
+							"Killing listener because of too many lost log lines. Lost [%d] Queue size [%u/%u]!\n", l->lost_logs, qsize, MAX_QUEUE_LEN);
+					kill_listener(l, "killed listener because of lost log lines\n");
 				}
 			}
 		}
@@ -264,6 +271,7 @@ static void event_handler(switch_event_t *event)
 	switch_event_t *clone = NULL;
 	listener_t *l, *lp, *last = NULL;
 	time_t now = switch_epoch_time_now(NULL);
+	switch_status_t qstatus;
 
 	switch_assert(event != NULL);
 
@@ -371,15 +379,22 @@ static void event_handler(switch_event_t *event)
 
 		if (send) {
 			if (switch_event_dup(&clone, event) == SWITCH_STATUS_SUCCESS) {
-				if (switch_queue_trypush(l->event_queue, clone) == SWITCH_STATUS_SUCCESS) {
+				qstatus = switch_queue_trypush(l->event_queue, clone); 
+				if (qstatus == SWITCH_STATUS_SUCCESS) {
 					if (l->lost_events) {
 						int le = l->lost_events;
 						l->lost_events = 0;
-						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(l->session), SWITCH_LOG_CRIT, "Lost %d events!\n", le);
+						switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(l->session), SWITCH_LOG_CRIT, "Lost [%d] events! Event Queue size: [%u/%u]\n", le, switch_queue_size(l->event_queue), MAX_QUEUE_LEN);
 					}
 				} else {
+					char errbuf[512] = {0};
+					unsigned int qsize = switch_queue_size(l->event_queue);
+					switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, 
+							"Event enqueue ERROR [%d] | [%s] | Queue size: [%u/%u] %s\n", 
+							(int)qstatus, switch_strerror(qstatus, errbuf, sizeof(errbuf)), qsize, MAX_QUEUE_LEN, (qsize == MAX_QUEUE_LEN)?"Max queue size reached":"");
 					if (++l->lost_events > MAX_MISSED) {
-						kill_listener(l, NULL);
+						switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Killing listener because of too many lost events. Lost [%d] Queue size[%u/%u]\n", l->lost_events, qsize, MAX_QUEUE_LEN);
+						kill_listener(l, "killed listener because of lost events\n");
 					}
 					switch_event_destroy(&clone);
 				}