From e4c0abf28655a0873522df83434bccedbe6387aa Mon Sep 17 00:00:00 2001 From: Chris Rienzo Date: Thu, 7 Mar 2019 15:26:34 +0000 Subject: [PATCH] FS-11693 [core] Added switch_channel_set_log_tag() and switch_channel_get_log_tags() This allows you to add log tags to an active call channel. If using SWITCH_CHANNEL_SESSION_LOG, with switch_log_printf(), the tags will be added to the the log node so a logging module can send additional contextual data to a log aggregator. --- src/include/switch_channel.h | 3 +++ src/include/switch_log.h | 1 + src/switch_channel.c | 35 +++++++++++++++++++++++++++++++++++ src/switch_log.c | 11 +++++++++++ 4 files changed, 50 insertions(+) diff --git a/src/include/switch_channel.h b/src/include/switch_channel.h index 8e24893fdc..ec066852c6 100644 --- a/src/include/switch_channel.h +++ b/src/include/switch_channel.h @@ -270,6 +270,9 @@ SWITCH_DECLARE(char *) switch_channel_get_uuid(switch_channel_t *channel); SWITCH_DECLARE(switch_status_t) switch_channel_set_profile_var(switch_channel_t *channel, const char *name, const char *val); +SWITCH_DECLARE(switch_status_t) switch_channel_set_log_tag(switch_channel_t *channel, const char *tagname, const char *tagvalue); +SWITCH_DECLARE(switch_status_t) switch_channel_get_log_tags(switch_channel_t *channel, switch_event_t **log_tags); + SWITCH_DECLARE(switch_status_t) switch_channel_set_variable_var_check(switch_channel_t *channel, const char *varname, const char *value, switch_bool_t var_check); SWITCH_DECLARE(switch_status_t) switch_channel_add_variable_var_check(switch_channel_t *channel, diff --git a/src/include/switch_log.h b/src/include/switch_log.h index f0df9d8315..357f20f324 100644 --- a/src/include/switch_log.h +++ b/src/include/switch_log.h @@ -65,6 +65,7 @@ SWITCH_BEGIN_EXTERN_C /* 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_level_t slevel; + switch_event_t *tags; } switch_log_node_t; typedef switch_status_t (*switch_log_function_t) (const switch_log_node_t *node, switch_log_level_t level); diff --git a/src/switch_channel.c b/src/switch_channel.c index 684c4014ec..b282e2385a 100644 --- a/src/switch_channel.c +++ b/src/switch_channel.c @@ -176,6 +176,7 @@ struct switch_channel { switch_hold_record_t *hold_record; switch_device_node_t *device_node; char *device_id; + switch_event_t *log_tags; }; static void process_device_hup(switch_channel_t *channel); @@ -741,6 +742,9 @@ SWITCH_DECLARE(void) switch_channel_uninit(switch_channel_t *channel) switch_event_destroy(&channel->api_list); switch_event_destroy(&channel->var_list); switch_event_destroy(&channel->app_list); + if (channel->log_tags) { + switch_event_destroy(&channel->log_tags); + } switch_mutex_unlock(channel->profile_mutex); } @@ -1412,6 +1416,37 @@ SWITCH_DECLARE(void) switch_channel_set_presence_data_vals(switch_channel_t *cha switch_safe_free(data_copy); } +SWITCH_DECLARE(switch_status_t) switch_channel_set_log_tag(switch_channel_t *channel, const char *tagname, const char *tagvalue) +{ + switch_status_t status = SWITCH_STATUS_FALSE; + switch_assert(channel != NULL); + switch_mutex_lock(channel->profile_mutex); + if (!zstr(tagname)) { + if (!channel->log_tags) { + switch_event_create_plain(&channel->log_tags, SWITCH_EVENT_CHANNEL_DATA); + } + if (zstr(tagvalue)) { + switch_event_del_header(channel->log_tags, tagname); + } else { + switch_event_add_header_string(channel->log_tags, SWITCH_STACK_BOTTOM, tagname, tagvalue); + } + status = SWITCH_STATUS_SUCCESS; + } + switch_mutex_unlock(channel->profile_mutex); + return status; +} + +SWITCH_DECLARE(switch_status_t) switch_channel_get_log_tags(switch_channel_t *channel, switch_event_t **log_tags) +{ + switch_status_t status = SWITCH_STATUS_FALSE; + switch_assert(channel != NULL); + switch_mutex_lock(channel->profile_mutex); + if (channel->log_tags && log_tags) { + status = switch_event_dup(log_tags, channel->log_tags); + } + switch_mutex_unlock(channel->profile_mutex); + return status; +} SWITCH_DECLARE(switch_status_t) switch_channel_set_variable_var_check(switch_channel_t *channel, const char *varname, const char *value, switch_bool_t var_check) diff --git a/src/switch_log.c b/src/switch_log.c index bed7b48968..e33b0421f3 100644 --- a/src/switch_log.c +++ b/src/switch_log.c @@ -125,6 +125,10 @@ SWITCH_DECLARE(switch_log_node_t *) switch_log_node_dup(const switch_log_node_t switch_assert(newnode->userdata); } + if (node->tags) { + switch_event_dup(&newnode->tags, node->tags); + } + return newnode; } @@ -141,6 +145,9 @@ SWITCH_DECLARE(void) switch_log_node_free(switch_log_node_t **pnode) if (node) { switch_safe_free(node->userdata); switch_safe_free(node->data); + if (node->tags) { + switch_event_destroy(&node->tags); + } #ifdef SWITCH_LOG_RECYCLE if (switch_queue_trypush(LOG_RECYCLE_QUEUE, node) != SWITCH_STATUS_SUCCESS) { free(node); @@ -490,9 +497,13 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha node->content = content; node->timestamp = now; node->channel = channel; + node->tags = NULL; if (channel == SWITCH_CHANNEL_ID_SESSION) { switch_core_session_t *session = (switch_core_session_t *) userdata; node->userdata = userdata ? strdup(switch_core_session_get_uuid(session)) : NULL; + if (session) { + switch_channel_get_log_tags(switch_core_session_get_channel(session), &node->tags); + } } else { node->userdata = !zstr(userdata) ? strdup(userdata) : NULL; }