mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-10-23 03:34:15 +00:00
[core] Add switch_log_meta_printf() and switch_log_meta_vprintf() to allow JSON metadata to tag along with the log message.
This commit is contained in:
1 parent
451900b353
commit
a26d0c39b0
5 files changed
+289
-7
No files matched your search
@@ -68,6 +68,8 @@ SWITCH_BEGIN_EXTERN_C
|
||||
switch_event_t *tags;
|
||||
/* Log sequence */
|
||||
int64_t sequence;
|
||||
/* Optional extra log metadata */
|
||||
cJSON *meta;
|
||||
} switch_log_node_t;
|
||||
|
||||
///\{
|
||||
@@ -157,7 +159,40 @@ SWITCH_DECLARE(void) switch_log_printf(_In_ switch_text_channel_t channel, _In_z
|
||||
SWITCH_DECLARE(void) switch_log_vprintf(_In_ switch_text_channel_t channel, _In_z_ const char *file,
|
||||
_In_z_ const char *func, _In_ int line,
|
||||
_In_opt_z_ const char *userdata, _In_ switch_log_level_t level, const char *fmt, va_list ap);
|
||||
/*!
|
||||
\brief Write log data to the logging engine w/ optional JSON metadata
|
||||
\param channel the log channel to write to
|
||||
\param file the current file
|
||||
\param func the current function
|
||||
\param line the current line
|
||||
\param userdata ununsed
|
||||
\param level the current log level
|
||||
\param meta log metadata - consumed by this function
|
||||
\param fmt desired format
|
||||
\param ... variable args
|
||||
\note there are channel macros to supply the first 4 parameters (SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_LOG_CLEAN, ...)
|
||||
\see switch_types.h
|
||||
*/
|
||||
SWITCH_DECLARE(void) switch_log_meta_printf(switch_text_channel_t channel, const char *file, const char *func, int line,
|
||||
const char *userdata, switch_log_level_t level, cJSON **meta, const char *fmt, ...) PRINTF_FUNCTION(8, 9);
|
||||
|
||||
/*!
|
||||
\brief Write log data to the logging engine w/ optional JSON metadata
|
||||
\param channel the log channel to write to
|
||||
\param file the current file
|
||||
\param func the current function
|
||||
\param line the current line
|
||||
\param userdata ununsed
|
||||
\param level the current log level
|
||||
\param meta log metadata - consumed by this function
|
||||
\param fmt desired format
|
||||
\param ap variable args
|
||||
\note there are channel macros to supply the first 4 parameters (SWITCH_CHANNEL_LOG, SWITCH_CHANNEL_LOG_CLEAN, ...)
|
||||
\see switch_types.h
|
||||
*/
|
||||
SWITCH_DECLARE(void) switch_log_meta_vprintf(_In_ switch_text_channel_t channel, _In_z_ const char *file,
|
||||
_In_z_ const char *func, _In_ int line,
|
||||
_In_opt_z_ const char *userdata, _In_ switch_log_level_t level, cJSON **meta, const char *fmt, va_list ap);
|
||||
#endif
|
||||
/*!
|
||||
\brief Shut down the logging engine
|
||||
|
||||
+55
-6
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
||||
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
|
||||
* Copyright (C) 2005-2021, Anthony Minessale II <anthm@freeswitch.org>
|
||||
*
|
||||
* Version: MPL 1.1
|
||||
*
|
||||
@@ -90,7 +90,7 @@ SWITCH_SEQ_FYELLOW };
|
||||
|
||||
SWITCH_DECLARE(cJSON *) switch_log_node_to_json(const switch_log_node_t *node, int log_level, switch_log_json_format_t *json_format, switch_event_t *chan_vars)
|
||||
{
|
||||
cJSON *json = cJSON_CreateObject();
|
||||
cJSON *json = NULL;
|
||||
char *hostname;
|
||||
char *full_message = node->content;
|
||||
char *parsed_full_message = NULL;
|
||||
@@ -98,6 +98,24 @@ SWITCH_DECLARE(cJSON *) switch_log_node_to_json(const switch_log_node_t *node, i
|
||||
switch_event_t *log_fields = NULL;
|
||||
switch_core_session_t *session = NULL;
|
||||
|
||||
if (node->meta && cJSON_IsObject(node->meta)) {
|
||||
if (json_format->custom_field_prefix) {
|
||||
cJSON *field = NULL;
|
||||
json = cJSON_CreateObject();
|
||||
for (field = node->meta->child; field; field = field->next) {
|
||||
if (!zstr(field->string)) {
|
||||
char *field_name = switch_mprintf("%s%s", json_format->custom_field_prefix, field->string);
|
||||
cJSON_AddItemToObject(json, field_name, cJSON_Duplicate(field, cJSON_True));
|
||||
free(field_name);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
json = cJSON_Duplicate(node->meta, cJSON_True);
|
||||
}
|
||||
} else {
|
||||
json = cJSON_CreateObject();
|
||||
}
|
||||
|
||||
if (json_format->version.name && json_format->version.value) {
|
||||
cJSON_AddItemToObject(json, json_format->version.name, cJSON_CreateString(json_format->version.value));
|
||||
}
|
||||
@@ -272,6 +290,10 @@ SWITCH_DECLARE(switch_log_node_t *) switch_log_node_dup(const switch_log_node_t
|
||||
switch_event_dup(&newnode->tags, node->tags);
|
||||
}
|
||||
|
||||
if (node->meta) {
|
||||
newnode->meta = cJSON_Duplicate(node->meta, cJSON_True);
|
||||
}
|
||||
|
||||
return newnode;
|
||||
}
|
||||
|
||||
@@ -291,6 +313,10 @@ SWITCH_DECLARE(void) switch_log_node_free(switch_log_node_t **pnode)
|
||||
if (node->tags) {
|
||||
switch_event_destroy(&node->tags);
|
||||
}
|
||||
if (node->meta) {
|
||||
cJSON_Delete(node->meta);
|
||||
node->meta = NULL;
|
||||
}
|
||||
#ifdef SWITCH_LOG_RECYCLE
|
||||
if (switch_queue_trypush(LOG_RECYCLE_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
|
||||
free(node);
|
||||
@@ -500,13 +526,22 @@ static void *SWITCH_THREAD_FUNC log_thread(switch_thread_t *t, void *obj)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(void) switch_log_meta_printf(switch_text_channel_t channel, const char *file, const char *func, int line,
|
||||
const char *userdata, switch_log_level_t level, cJSON **meta, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
switch_log_meta_vprintf(channel, file, func, line, userdata, level, meta, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char *file, const char *func, int line,
|
||||
const char *userdata, switch_log_level_t level, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
switch_log_vprintf(channel, file, func, line, userdata, level, fmt, ap);
|
||||
switch_log_meta_vprintf(channel, file, func, line, userdata, level, NULL, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
@@ -514,6 +549,12 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char
|
||||
SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const char *file, const char *func, int line,
|
||||
const char *userdata, switch_log_level_t level, const char *fmt, va_list ap)
|
||||
{
|
||||
switch_log_meta_vprintf(channel, file, func, line, userdata, level, NULL, fmt, ap);
|
||||
}
|
||||
SWITCH_DECLARE(void) switch_log_meta_vprintf(switch_text_channel_t channel, const char *file, const char *func, int line,
|
||||
const char *userdata, switch_log_level_t level, cJSON **meta, const char *fmt, va_list ap)
|
||||
{
|
||||
cJSON *log_meta = NULL;
|
||||
char *data = NULL;
|
||||
char *new_fmt = NULL;
|
||||
int ret = 0;
|
||||
@@ -531,8 +572,13 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
|
||||
switch_log_level_t limit_level = runtime.hard_log_level;
|
||||
switch_log_level_t special_level = SWITCH_LOG_UNINIT;
|
||||
|
||||
if (meta && *meta) {
|
||||
log_meta = *meta;
|
||||
*meta = NULL;
|
||||
}
|
||||
|
||||
if (limit_level == SWITCH_LOG_DISABLE) {
|
||||
return;
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (channel == SWITCH_CHANNEL_ID_SESSION && userdata) {
|
||||
@@ -545,14 +591,14 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
|
||||
|
||||
if (level > 100) {
|
||||
if ((uint32_t) (level - 100) > runtime.debug_level) {
|
||||
return;
|
||||
goto end;
|
||||
}
|
||||
|
||||
level = 1;
|
||||
}
|
||||
|
||||
if (level > limit_level) {
|
||||
return;
|
||||
goto end;
|
||||
}
|
||||
|
||||
switch_assert(level < SWITCH_LOG_INVALID);
|
||||
@@ -671,6 +717,8 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
|
||||
node->timestamp = now;
|
||||
node->channel = channel;
|
||||
node->tags = NULL;
|
||||
node->meta = log_meta;
|
||||
log_meta = 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;
|
||||
@@ -688,6 +736,7 @@ SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const cha
|
||||
|
||||
end:
|
||||
|
||||
cJSON_Delete(log_meta);
|
||||
switch_safe_free(data);
|
||||
switch_safe_free(new_fmt);
|
||||
|
||||
|
||||
Reference in new issue
Block a user