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

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@14555 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Mathieu Rene 2009-08-18 18:24:31 +00:00
parent eb08813dd4
commit 1c048237d7
2 changed files with 53 additions and 25 deletions

View File

@ -61,7 +61,7 @@ SWITCH_BEGIN_EXTERN_C
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(switch_log_level_t) switch_log_str2level(_In_z_ const char *str);
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

View File

@ -332,6 +332,9 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
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 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
}
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 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
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_DECLARE(void) switch_core_memory_reclaim_logger(void)
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 @@ SWITCH_DECLARE(switch_status_t) switch_log_shutdown(void)
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