[Freeswitch-dev] [POST-1.0.2][RFC][PATCH 1/2] Per-session logging core support
stkn at freeswitch.org
stkn at freeswitch.org
Mon Dec 15 06:26:54 PST 2008
diff --git a/src/include/private/switch_core_pvt.h b/src/include/private/switch_core_pvt.h
index 040a6ec..c3b9fc2 100644
--- a/src/include/private/switch_core_pvt.h
+++ b/src/include/private/switch_core_pvt.h
@@ -98,6 +98,7 @@ struct switch_core_session {
switch_size_t id;
switch_session_flag_t flags;
int thread_running;
+ switch_log_level_t log_level;
switch_channel_t *channel;
switch_io_event_hooks_t event_hooks;
diff --git a/src/include/switch_core.h b/src/include/switch_core.h
index 7670435..89ffbf3 100644
--- a/src/include/switch_core.h
+++ b/src/include/switch_core.h
@@ -729,6 +729,21 @@ SWITCH_DECLARE(void *) switch_core_session_get_private(_In_ switch_core_session_
SWITCH_DECLARE(switch_status_t) switch_core_session_set_private(_In_ switch_core_session_t *session, _In_ void *private_info);
/*!
+ \brief Set session loglevel
+ \param session the session
+ \param loglevel the desired loglevel
+ \return SWITCH_STATUS_SUCCESS on success
+*/
+SWITCH_DECLARE(switch_status_t) switch_core_session_set_loglevel(_In_ switch_core_session_t *session, _In_ switch_log_level_t loglevel);
+
+/*!
+ \brief Get session loglevel
+ \param session the session
+ \return the loglevel
+*/
+SWITCH_DECLARE(switch_log_level_t) switch_core_session_get_loglevel(_In_ switch_core_session_t *session);
+
+/*!
\brief Add a logical stream to a session
\param session the session to add the stream to
\param private_info an optional pointer to private data for the new stream
diff --git a/src/include/switch_log.h b/src/include/switch_log.h
index 01efc16..c7ec263 100644
--- a/src/include/switch_log.h
+++ b/src/include/switch_log.h
@@ -99,6 +99,24 @@ SWITCH_DECLARE(void) switch_log_printf(_In_ switch_text_channel_t channel, _In_z
_In_z_ const char *func, _In_ int line,
_In_opt_z_ const char *userdata, _In_ switch_log_level_t level,
_In_z_ _Printf_format_string_ const char *fmt, ...) PRINTF_FUNCTION(7, 8);
+
+/*!
+ \brief Write session log data to the logging engine
+ \param session the session to log for
+ \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 fmt desired format
+ \param ... variable args
+ \note there are channel macros to supply the 4 parameters after session
+*/
+SWITCH_DECLARE(void) switch_core_session_log_printf(_In_ switch_core_session_t *session, _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,
+ _In_z_ _Printf_format_string_ const char *fmt, ...) PRINTF_FUNCTION(8, 9);
#endif
/*!
\brief Shut down the logging engine
diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c
index 677c5fa..52fd2e2 100644
--- a/src/mod/applications/mod_commands/mod_commands.c
+++ b/src/mod/applications/mod_commands/mod_commands.c
@@ -1296,6 +1296,41 @@ SWITCH_STANDARD_API(uuid_deflect)
return SWITCH_STATUS_SUCCESS;
}
+#define UUID_LOGLEVEL_SYNTAX "<uuid> <level>"
+SWITCH_STANDARD_API(uuid_loglevel)
+{
+ switch_core_session_t *tsession = NULL;
+ char *uuid = NULL, *text = NULL;
+
+ if (!switch_strlen_zero(cmd) && (uuid = strdup(cmd))) {
+ if ((text = strchr(uuid, ' '))) {
+ *text++ = '\0';
+ }
+ }
+
+ if (switch_strlen_zero(uuid) || switch_strlen_zero(text)) {
+ stream->write_function(stream, "-USAGE: %s\n", UUID_LOGLEVEL_SYNTAX);
+ } else {
+ switch_log_level_t level = switch_log_str2level(text);
+
+ if (level == SWITCH_LOG_INVALID) {
+ stream->write_function(stream, "-ERR Invalid log level!\n");
+ }
+ else if ((tsession = switch_core_session_locate(uuid))) {
+
+ switch_core_session_set_loglevel(tsession, level);
+ stream->write_function(stream, "+OK\n");
+ switch_core_session_rwunlock(tsession);
+ }
+ else {
+ stream->write_function(stream, "-ERR No Such Channel %s!\n", uuid);
+ }
+ }
+
+ switch_safe_free(uuid);
+ return SWITCH_STATUS_SUCCESS;
+}
+
#define SCHED_TRANSFER_SYNTAX "[+]<time> <uuid> <extension> [<dialplan>] [<context>]"
SWITCH_STANDARD_API(sched_transfer_function)
{
@@ -3148,6 +3183,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load)
SWITCH_ADD_API(commands_api_interface, "system", "Execute a system command", system_function, SYSTEM_SYNTAX);
SWITCH_ADD_API(commands_api_interface, "time_test", "time_test", time_test_function, "<mss>");
+ SWITCH_ADD_API(commands_api_interface, "uuid_loglevel", "set loglevel on session", uuid_loglevel, UUID_LOGLEVEL_SYNTAX);
+
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_NOUNLOAD;
}
diff --git a/src/mod/applications/mod_dptools/mod_dptools.c b/src/mod/applications/mod_dptools/mod_dptools.c
index 52fc3e9..70d3ebd 100644
--- a/src/mod/applications/mod_dptools/mod_dptools.c
+++ b/src/mod/applications/mod_dptools/mod_dptools.c
@@ -1648,6 +1648,23 @@ SWITCH_STANDARD_APP(phrase_function)
}
}
+#define SESSION_LOGLEVEL_SYNTAX "<level>"
+SWITCH_STANDARD_APP(session_loglevel_function)
+{
+ if (!switch_strlen_zero(data)) {
+ switch_log_level_t level = switch_log_str2level(data);
+
+ if (level == SWITCH_LOG_INVALID) {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid log level: %s\n", data);
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Setting log level \"%s\" on session\n", switch_log_level2str(level));
+ switch_core_session_set_loglevel(session, level);
+ }
+ } else {
+ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No log level specified\n");
+ }
+}
+
SWITCH_STANDARD_APP(playback_function)
{
@@ -2428,6 +2445,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_dptools_load)
SWITCH_ADD_APP(app_interface, "wait_for_silence", "wait_for_silence", "wait_for_silence", wait_for_silence_function, WAIT_FOR_SILENCE_SYNTAX, SAF_NONE);
+ SWITCH_ADD_APP(app_interface, "session_loglevel", "session_loglevel", "session_loglevel", session_loglevel_function, SESSION_LOGLEVEL_SYNTAX, SAF_NONE);
+
SWITCH_ADD_DIALPLAN(dp_interface, "inline", inline_dialplan_hunt);
/* indicate that the module should continue to be loaded */
diff --git a/src/switch_core_session.c b/src/switch_core_session.c
index 58f3853..63e80cf 100644
--- a/src/switch_core_session.c
+++ b/src/switch_core_session.c
@@ -221,6 +221,19 @@ SWITCH_DECLARE(int) switch_core_session_get_stream_count(switch_core_session_t *
return session->stream_count;
}
+SWITCH_DECLARE(switch_status_t) switch_core_session_set_loglevel(switch_core_session_t *session, switch_log_level_t log_level)
+{
+ switch_assert(session != NULL);
+ session->log_level = log_level;
+ return SWITCH_STATUS_SUCCESS;
+}
+
+SWITCH_DECLARE(switch_log_level_t) switch_core_session_get_loglevel(switch_core_session_t *session)
+{
+ switch_assert(session != NULL);
+ return session->log_level;
+}
+
SWITCH_DECLARE(switch_call_cause_t) switch_core_session_resurrect_channel(const char *endpoint_name,
switch_core_session_t **new_session, switch_memory_pool_t **pool, void *data)
{
@@ -1090,6 +1103,8 @@ SWITCH_DECLARE(switch_core_session_t *) switch_core_session_request_uuid(switch_
switch_uuid_format(session->uuid_str, &uuid);
}
+ session->log_level = runtime.hard_log_level;
+
session->endpoint_interface = endpoint_interface;
session->raw_write_frame.data = session->raw_write_buf;
session->raw_write_frame.buflen = sizeof(session->raw_write_buf);
diff --git a/src/switch_log.c b/src/switch_log.c
index 4804f6d..31b860e 100644
--- a/src/switch_log.c
+++ b/src/switch_log.c
@@ -234,13 +234,12 @@ static void *SWITCH_THREAD_FUNC log_thread(switch_thread_t *thread, void *obj)
}
#define do_mods (LOG_QUEUE && THREAD_RUNNING)
-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, ...)
+static 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)
{
char *data = NULL;
char *new_fmt = NULL;
int ret = 0;
- va_list ap;
FILE *handle;
const char *filep = (file ? switch_cut_path(file) : "");
const char *funcp = (func ? func : "");
@@ -255,8 +254,6 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char
switch_assert(level < SWITCH_LOG_INVALID);
- va_start(ap, fmt);
-
handle = switch_core_data_channel(channel);
if (channel != SWITCH_CHANNEL_ID_LOG_CLEAN) {
@@ -275,7 +272,6 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char
}
ret = switch_vasprintf(&data, fmt, ap);
- va_end(ap);
if (ret == -1) {
fprintf(stderr, "Memory Error\n");
@@ -381,6 +377,32 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char
}
}
+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_printf(channel, file, func, line, userdata, level, fmt, ap);
+ va_end(ap);
+}
+
+SWITCH_DECLARE(void) switch_core_session_log_printf(switch_core_session_t *session, 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_assert(session != NULL);
+
+ if (level > session->log_level) {
+ return;
+ }
+
+ va_start(ap, fmt);
+ __switch_log_printf(channel, file, func, line, userdata, level, fmt, ap);
+ va_end(ap);
+}
+
SWITCH_DECLARE(switch_status_t) switch_log_init(switch_memory_pool_t *pool, switch_bool_t colorize)
{
switch_thread_t *thread;
More information about the Freeswitch-dev
mailing list