From a9a23aed22702adddf6c48a545f5334ff234d93e Mon Sep 17 00:00:00 2001 From: Brian West Date: Wed, 12 Apr 2006 17:51:47 +0000 Subject: [PATCH] file/func specific logging git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@1128 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- conf/freeswitch.conf | 10 ++++ src/include/switch_log.h | 7 +++ src/mod/loggers/mod_console/mod_console.c | 63 ++++++++++++++++++++++- src/switch_log.c | 25 ++++++++- 4 files changed, 103 insertions(+), 2 deletions(-) diff --git a/conf/freeswitch.conf b/conf/freeswitch.conf index a90985986d..8768f918e5 100644 --- a/conf/freeswitch.conf +++ b/conf/freeswitch.conf @@ -1,6 +1,16 @@ ; Unified Config file ; each section denoted with a + could also be in it's own file +;---- CONSOLE LOGGER +;-------------------------------------------------------------------------------- +[+console.conf] +[mappings] +; pick a file name, a function name or 'all' +; map as many as you need for specific debugging +;mod_exosip.c => DEBUG +;log_event => DEBUG +;all => DEBUG + ;---- MODULES ;-------------------------------------------------------------------------------- [+modules.conf] diff --git a/src/include/switch_log.h b/src/include/switch_log.h index 50ccec1225..9aeb487093 100644 --- a/src/include/switch_log.h +++ b/src/include/switch_log.h @@ -114,6 +114,13 @@ SWITCH_DECLARE(switch_status) switch_log_bind_logger(switch_log_function functio */ SWITCH_DECLARE(const char *) switch_log_level2str(switch_log_level level); +/*! + \brief Return the level number of the specified log level name + \param str the name of the level + \return the log level +*/ +SWITCH_DECLARE(switch_log_level) switch_log_str2level(const char *str); + ///\} #ifdef __cplusplus } diff --git a/src/mod/loggers/mod_console/mod_console.c b/src/mod/loggers/mod_console/mod_console.c index b9eecdb50b..051dde3f3c 100644 --- a/src/mod/loggers/mod_console/mod_console.c +++ b/src/mod/loggers/mod_console/mod_console.c @@ -46,12 +46,65 @@ static switch_loadable_module_interface console_module_interface = { /*.directory_interface */ NULL }; +static switch_memory_pool *module_pool = NULL; +static switch_hash *log_hash = NULL; +static int8_t all_level = -1; + +static switch_status config_logger(void) +{ + switch_config cfg; + char *var, *val; + char *cf = "console.conf"; + + if (!switch_config_open_file(&cfg, cf)) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "open of %s failed\n", cf); + return SWITCH_STATUS_FALSE; + } + + switch_core_hash_init(&log_hash, module_pool); + + while (switch_config_next_pair(&cfg, &var, &val)) { + if (!strcasecmp(cfg.category, "mappings")) { + switch_core_hash_insert(log_hash, switch_core_strdup(module_pool, var), switch_core_strdup(module_pool, val)); + } + } + + return SWITCH_STATUS_SUCCESS; +} + static switch_status switch_console_logger(const switch_log_node *node, switch_log_level level) { FILE *handle; if ((handle = switch_core_data_channel(SWITCH_CHANNEL_ID_LOG))) { - fprintf(handle, node->data); + char *lookup = NULL; + switch_log_level level = SWITCH_LOG_DEBUG; + + if (all_level > -1) { + level = (switch_log_level) all_level; + } else if (log_hash) { + lookup = switch_core_hash_find(log_hash, node->file); + if (!lookup) { + lookup = switch_core_hash_find(log_hash, node->func); + + if (!lookup && all_level == -1) { + if ((lookup = switch_core_hash_find(log_hash, "all"))) { + all_level = (int) switch_log_str2level(lookup); + } else { + all_level = -2; + } + } + + } + } + + if (lookup) { + level = switch_log_str2level(lookup); + } + + if (!log_hash || (((all_level > - 1) || lookup) && level >= node->level)) { + fprintf(handle, node->data); + } } return SWITCH_STATUS_SUCCESS; @@ -59,12 +112,20 @@ static switch_status switch_console_logger(const switch_log_node *node, switch_l SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) { + if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "OH OH no pool\n"); + return SWITCH_STATUS_TERM; + } + + /* connect my internal structure to the blank pointer passed to me */ *interface = &console_module_interface; /* setup my logger function */ switch_log_bind_logger(switch_console_logger, SWITCH_LOG_DEBUG); + config_logger(); + /* indicate that the module should continue to be loaded */ return SWITCH_STATUS_SUCCESS; } diff --git a/src/switch_log.c b/src/switch_log.c index 93bad6c36b..0cfdece4d4 100644 --- a/src/switch_log.c +++ b/src/switch_log.c @@ -41,7 +41,8 @@ static const char *LEVELS[] = { "NOTICE" , "INFO" , "DEBUG" , - "CONSOLE" + "CONSOLE", + NULL }; struct switch_log_binding { @@ -59,6 +60,28 @@ static switch_queue_t *LOG_QUEUE = NULL; static int8_t THREAD_RUNNING = 0; static uint8_t MAX_LEVEL = 0; +SWITCH_DECLARE(const char *) switch_log_level2str(switch_log_level level) +{ + return LEVELS[level]; +} + +SWITCH_DECLARE(switch_log_level) switch_log_str2level(const char *str) +{ + int x = 0; + switch_log_level level = SWITCH_LOG_DEBUG; + for(x = 0;;x++) { + if (!LEVELS[x]) { + break; + } + if (!strcasecmp(LEVELS[x], str)) { + level = (switch_log_level) x; + break; + } + } + + return level; +} + SWITCH_DECLARE(switch_status) switch_log_bind_logger(switch_log_function function, switch_log_level level) { switch_log_binding *binding = NULL, *ptr = NULL;