From 5daf2d13b94719f8a4cdd165033853454b6ef029 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 31 Dec 2008 22:44:23 +0000 Subject: [PATCH] wash behind your ears git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@11040 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- .../mod_event_socket/mod_event_socket.c | 170 ++++++++++++++---- src/switch_event.c | 27 ++- 2 files changed, 151 insertions(+), 46 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 cd7538a571..fb55cae4c8 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 @@ -628,15 +628,67 @@ SWITCH_STANDARD_API(event_sink_function) switch_mutex_unlock(listener->filter_mutex); + } else if (!strcasecmp(wcmd, "stop-logging")) { + char *id = switch_event_get_header(stream->param_event, "listen-id"); + uint32_t idl = 0; + + if (id) { + idl = (uint32_t) atol(id); + } + + if (!(listener = find_listener(idl))) { + stream->write_function(stream, "Invalid Listen-ID\n"); + goto end; + } + + if (switch_test_flag(listener, LFLAG_LOG)) { + switch_clear_flag_locked(listener, LFLAG_LOG); + stream->write_function(stream, "Not Logging\n"); + } else { + stream->write_function(stream, "Not Logging\n"); + } + + goto end; + + } else if (!strcasecmp(wcmd, "set-loglevel")) { + char *loglevel = switch_event_get_header(stream->param_event, "loglevel"); + char *id = switch_event_get_header(stream->param_event, "listen-id"); + uint32_t idl = 0; + + if (id) { + idl = (uint32_t) atol(id); + } + + if (!(listener = find_listener(idl))) { + stream->write_function(stream, "Invalid Listen-ID\n"); + goto end; + } + + if (loglevel) { + switch_log_level_t ltype = switch_log_str2level(loglevel); + if (ltype != SWITCH_LOG_INVALID) { + listener->level = ltype; + switch_set_flag(listener, LFLAG_LOG); + stream->write_function(stream, "Log Level %s\n", loglevel); + } else { + stream->write_function(stream, "Invalid Level\n"); + } + } else { + stream->write_function(stream, "Invalid Syntax\n"); + } + + goto end; + } else if (!strcasecmp(wcmd, "create-listener")) { char *events = switch_event_get_header(stream->param_event, "events"); + char *loglevel = switch_event_get_header(stream->param_event, "loglevel"); switch_memory_pool_t *pool; char *next, *cur; uint32_t count = 0, key_count = 0; uint8_t custom = 0; char *edup; - if (switch_strlen_zero(events)) { + if (switch_strlen_zero(events) && switch_strlen_zero(loglevel)) { stream->write_function(stream, "Missing parameter!\n"); goto end; } @@ -652,58 +704,69 @@ SWITCH_STANDARD_API(event_sink_function) switch_set_flag(listener, LFLAG_AUTHED); switch_set_flag(listener, LFLAG_STATEFUL); switch_queue_create(&listener->event_queue, SWITCH_CORE_QUEUE_LEN, listener->pool); + switch_queue_create(&listener->log_queue, SWITCH_CORE_QUEUE_LEN, listener->pool); + + if (loglevel) { + switch_log_level_t ltype = switch_log_str2level(loglevel); + if (ltype != SWITCH_LOG_INVALID) { + listener->level = ltype; + switch_set_flag(listener, LFLAG_LOG); + } + } switch_thread_rwlock_create(&listener->rwlock, listener->pool); listener->id = next_id(); listener->timeout = 60; listener->last_flush = switch_timestamp(NULL); - if (switch_stristr("xml", format)) { - listener->format = EVENT_FORMAT_XML; - } else { - listener->format = EVENT_FORMAT_PLAIN; - } + if (events) { - edup = strdup(events); - - for (cur = edup; cur; count++) { - switch_event_types_t type; - - if ((next = strchr(cur, ' '))) { - *next++ = '\0'; + if (switch_stristr("xml", format)) { + listener->format = EVENT_FORMAT_XML; + } else { + listener->format = EVENT_FORMAT_PLAIN; } + + edup = strdup(events); + + for (cur = edup; cur; count++) { + switch_event_types_t type; + + if ((next = strchr(cur, ' '))) { + *next++ = '\0'; + } - if (custom) { - switch_core_hash_insert(listener->event_hash, cur, MARKER); - } else if (switch_name_event(cur, &type) == SWITCH_STATUS_SUCCESS) { - key_count++; - if (type == SWITCH_EVENT_ALL) { - uint32_t x = 0; - for (x = 0; x < SWITCH_EVENT_ALL; x++) { - listener->event_list[x] = 1; + if (custom) { + switch_core_hash_insert(listener->event_hash, cur, MARKER); + } else if (switch_name_event(cur, &type) == SWITCH_STATUS_SUCCESS) { + key_count++; + if (type == SWITCH_EVENT_ALL) { + uint32_t x = 0; + for (x = 0; x < SWITCH_EVENT_ALL; x++) { + listener->event_list[x] = 1; + } + } + if (type <= SWITCH_EVENT_ALL) { + listener->event_list[type] = 1; + } + if (type == SWITCH_EVENT_CUSTOM) { + custom++; } } - if (type <= SWITCH_EVENT_ALL) { - listener->event_list[type] = 1; - } - if (type == SWITCH_EVENT_CUSTOM) { - custom++; - } + + cur = next; } - - cur = next; - } - switch_safe_free(edup); + switch_safe_free(edup); - if (!key_count) { - switch_core_hash_destroy(&listener->event_hash); - switch_core_destroy_memory_pool(&listener->pool); - stream->write_function(stream, "No keywords supplied\n"); - goto end; + if (!key_count) { + switch_core_hash_destroy(&listener->event_hash); + switch_core_destroy_memory_pool(&listener->pool); + stream->write_function(stream, "No keywords supplied\n"); + goto end; + } } - switch_set_flag_locked(listener, LFLAG_EVENTS); add_listener(listener); stream->write_function(stream, "\n"); @@ -750,7 +813,38 @@ SWITCH_STANDARD_API(event_sink_function) listener->last_flush = switch_timestamp(NULL); stream->write_function(stream, "\n Current Events Follow\n"); xmlize_listener(listener, stream); - stream->write_function(stream, "\n"); + + if (switch_test_flag(listener, LFLAG_LOG)) { + stream->write_function(stream, "\n"); + + while (switch_queue_trypop(listener->log_queue, &pop) == SWITCH_STATUS_SUCCESS) { + switch_log_node_t *dnode = (switch_log_node_t *) pop; + int encode_len = (strlen(dnode->data) * 3) + 1; + char *encode_buf = malloc(encode_len); + + switch_assert(encode_buf); + + memset(encode_buf, 0, encode_len); + switch_url_encode((char *) dnode->data, encode_buf, encode_len); + + + stream->write_function(stream, "%s\n", + dnode->level, + dnode->channel, + dnode->file, + dnode->func, + dnode->line, + encode_buf + ); + free(encode_buf); + free(dnode->data); + free(dnode); + } + + stream->write_function(stream, "\n"); + } + + stream->write_function(stream, "\n"); while (switch_queue_trypop(listener->event_queue, &pop) == SWITCH_STATUS_SUCCESS) { char *etype; diff --git a/src/switch_event.c b/src/switch_event.c index 05c5cd2dd1..f7403db4cc 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -930,13 +930,20 @@ SWITCH_DECLARE(switch_status_t) switch_event_serialize(switch_event_t *event, ch static switch_xml_t add_xml_header(switch_xml_t xml, char *name, char *value, int offset) { - switch_xml_t header = switch_xml_add_child_d(xml, "header", offset); - + switch_xml_t header = switch_xml_add_child_d(xml, name, offset); + if (header) { - switch_xml_set_attr_d(header, "name", name); - switch_xml_set_attr_d(header, "value", value); + int encode_len = (strlen(value) * 3) + 1; + char *encode_buf = malloc(encode_len); + + switch_assert(encode_buf); + + memset(encode_buf, 0, encode_len); + switch_url_encode((char *) value, encode_buf, encode_len); + switch_xml_set_txt_d(header, encode_buf); + free(encode_buf); } - + return header; } @@ -948,6 +955,7 @@ SWITCH_DECLARE(switch_xml_t) switch_event_xmlize(switch_event_t *event, const ch switch_xml_t xml = NULL; uint32_t off = 0; va_list ap; + switch_xml_t xheaders = NULL; if (!(xml = switch_xml_new("event"))) { return xml; @@ -972,8 +980,11 @@ SWITCH_DECLARE(switch_xml_t) switch_event_xmlize(switch_event_t *event, const ch } } - for (hp = event->headers; hp; hp = hp->next) { - add_xml_header(xml, hp->name, hp->value, off++); + if ((xheaders = switch_xml_add_child_d(xml, "headers", off++))) { + int hoff = 0; + for (hp = event->headers; hp; hp = hp->next) { + add_xml_header(xheaders, hp->name, hp->value, hoff++); + } } if (!switch_strlen_zero(data)) { @@ -1032,7 +1043,7 @@ SWITCH_DECLARE(switch_status_t) switch_event_fire_detailed(const char *file, con switch_event_add_header_string(*event, SWITCH_STACK_BOTTOM, "Event-Date-Local", date); switch_rfc822_date(date, ts); switch_event_add_header_string(*event, SWITCH_STACK_BOTTOM, "Event-Date-GMT", date); - switch_event_add_header(*event, SWITCH_STACK_BOTTOM, "Event-Date-timestamp", "%" SWITCH_UINT64_T_FMT, (uint64_t) ts); + switch_event_add_header(*event, SWITCH_STACK_BOTTOM, "Event-Date-Timestamp", "%" SWITCH_UINT64_T_FMT, (uint64_t) ts); switch_event_add_header_string(*event, SWITCH_STACK_BOTTOM, "Event-Calling-File", switch_cut_path(file)); 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);