From 0f873e692b7e8171158e374dfdeaadf5207287b6 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Fri, 11 Jul 2008 14:35:08 +0000 Subject: [PATCH] fix FSCORE-156 git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8997 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- conf/autoload_configs/switch.conf.xml | 2 + src/include/private/switch_core_pvt.h | 1 + src/include/switch_log.h | 5 +- .../mod_event_socket/mod_event_socket.c | 4 +- src/mod/loggers/mod_console/mod_console.c | 14 ++-- src/mod/loggers/mod_logfile/mod_logfile.c | 4 +- src/mod/loggers/mod_syslog/mod_syslog.c | 3 +- src/switch_core.c | 5 +- src/switch_log.c | 82 +++++++++++++++++-- 9 files changed, 102 insertions(+), 18 deletions(-) diff --git a/conf/autoload_configs/switch.conf.xml b/conf/autoload_configs/switch.conf.xml index c4ec287af3..7a0fbde30a 100644 --- a/conf/autoload_configs/switch.conf.xml +++ b/conf/autoload_configs/switch.conf.xml @@ -16,6 +16,8 @@ + + diff --git a/src/include/private/switch_core_pvt.h b/src/include/private/switch_core_pvt.h index d623ac0cfa..49d21a6d64 100644 --- a/src/include/private/switch_core_pvt.h +++ b/src/include/private/switch_core_pvt.h @@ -195,6 +195,7 @@ struct switch_runtime { uint32_t default_dtmf_duration; switch_frame_t dummy_cng_frame; char dummy_data[5]; + switch_bool_t colorize_console; }; extern struct switch_runtime runtime; diff --git a/src/include/switch_log.h b/src/include/switch_log.h index 52ee71afb7..01efc164a3 100644 --- a/src/include/switch_log.h +++ b/src/include/switch_log.h @@ -74,7 +74,7 @@ typedef switch_status_t (*switch_log_function_t) (const switch_log_node_t *node, \param pool the memory pool to use \note to be called at application startup by the core */ -SWITCH_DECLARE(switch_status_t) switch_log_init(_In_ switch_memory_pool_t *pool); +SWITCH_DECLARE(switch_status_t) switch_log_init(_In_ switch_memory_pool_t *pool, _In_ switch_bool_t colorize); /*! \brief Shut down the logging engine @@ -104,7 +104,8 @@ SWITCH_DECLARE(void) switch_log_printf(_In_ switch_text_channel_t channel, _In_z \brief Shut down the logging engine \note to be called at application termination by the core */ -SWITCH_DECLARE(switch_status_t) switch_log_bind_logger(_In_ switch_log_function_t function, _In_ switch_log_level_t level); +SWITCH_DECLARE(switch_status_t) switch_log_bind_logger(_In_ switch_log_function_t function, _In_ switch_log_level_t level, _In_ switch_bool_t is_console); +SWITCH_DECLARE(switch_status_t) switch_log_unbind_logger(_In_ switch_log_function_t function); /*! \brief Return the name of the specified log level diff --git a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c index fbc0b9381a..3bcb39ef4c 100644 --- a/src/mod/event_handlers/mod_event_socket/mod_event_socket.c +++ b/src/mod/event_handlers/mod_event_socket/mod_event_socket.c @@ -323,6 +323,8 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_event_socket_shutdown) prefs.done = 1; + switch_log_unbind_logger(socket_logger); + close_socket(&listen_list.sock); while (prefs.threads) { @@ -1368,7 +1370,7 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_event_socket_runtime) return SWITCH_STATUS_GENERR; } - switch_log_bind_logger(socket_logger, SWITCH_LOG_DEBUG); + switch_log_bind_logger(socket_logger, SWITCH_LOG_DEBUG, SWITCH_FALSE); for (;;) { diff --git a/src/mod/loggers/mod_console/mod_console.c b/src/mod/loggers/mod_console/mod_console.c index b4503554f6..c3b5868eb6 100644 --- a/src/mod/loggers/mod_console/mod_console.c +++ b/src/mod/loggers/mod_console/mod_console.c @@ -42,7 +42,7 @@ static int COLORIZE = 0; static HANDLE hStdout; static WORD wOldColorAttrs; static CONSOLE_SCREEN_BUFFER_INFO csbiInfo; -static WORD COLORS[] = { FOREGROUND_RED | FOREGROUND_INTENSITY, +static WORD COLORS[] = { FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, FOREGROUND_RED | FOREGROUND_INTENSITY, FOREGROUND_RED | FOREGROUND_INTENSITY, FOREGROUND_RED | FOREGROUND_INTENSITY, @@ -53,7 +53,7 @@ static WORD COLORS[] = { FOREGROUND_RED | FOREGROUND_INTENSITY, FOREGROUND_GREEN | FOREGROUND_INTENSITY }; #else -static const char *COLORS[] = { SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FMAGEN, SWITCH_SEQ_FCYAN, +static const char *COLORS[] = { SWITCH_SEQ_FWHITE, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FMAGEN, SWITCH_SEQ_FCYAN, SWITCH_SEQ_FGREEN, SWITCH_SEQ_FYELLOW, "" }; #endif @@ -338,7 +338,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_console_load) /* setup my logger function */ - switch_log_bind_logger(switch_console_logger, SWITCH_LOG_DEBUG); + switch_log_bind_logger(switch_console_logger, SWITCH_LOG_DEBUG, SWITCH_TRUE); config_logger(); RUNNING = 1; @@ -348,10 +348,12 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_console_load) SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_console_shutdown) { - //switch_core_hash_destroy(&log_hash); - //switch_core_hash_destroy(&name_hash); + + switch_log_unbind_logger(switch_console_logger); + switch_core_hash_destroy(&log_hash); + RUNNING = 0; - return SWITCH_STATUS_SUCCESS; + return SWITCH_STATUS_UNLOAD; } /* For Emacs: diff --git a/src/mod/loggers/mod_logfile/mod_logfile.c b/src/mod/loggers/mod_logfile/mod_logfile.c index 599e825718..c0417e5dde 100644 --- a/src/mod/loggers/mod_logfile/mod_logfile.c +++ b/src/mod/loggers/mod_logfile/mod_logfile.c @@ -374,9 +374,9 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_logfile_load) switch_xml_free(xml); } - switch_log_bind_logger(mod_logfile_logger, SWITCH_LOG_DEBUG); + switch_log_bind_logger(mod_logfile_logger, SWITCH_LOG_DEBUG, SWITCH_FALSE); - return SWITCH_STATUS_SUCCESS; + return SWITCH_STATUS_NOUNLOAD; } SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_logfile_shutdown) diff --git a/src/mod/loggers/mod_syslog/mod_syslog.c b/src/mod/loggers/mod_syslog/mod_syslog.c index bc225b933b..e467d5fcf6 100644 --- a/src/mod/loggers/mod_syslog/mod_syslog.c +++ b/src/mod/loggers/mod_syslog/mod_syslog.c @@ -152,7 +152,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_syslog_load) openlog(globals.ident, LOG_PID, LOG_USER); - switch_log_bind_logger(mod_syslog_logger, SWITCH_LOG_DEBUG); + switch_log_bind_logger(mod_syslog_logger, SWITCH_LOG_DEBUG, SWITCH_FALSE); return SWITCH_STATUS_SUCCESS; } @@ -160,6 +160,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_syslog_load) SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_syslog_shutdown) { closelog(); + switch_log_unbind_logger(mod_syslog_logger); return SWITCH_STATUS_SUCCESS; } diff --git a/src/switch_core.c b/src/switch_core.c index 0553289b86..5a0badc709 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -896,7 +896,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc switch_core_set_variable("local_ip_v6", guess_ip); switch_core_set_variable("base_dir", SWITCH_GLOBAL_dirs.base_dir); - switch_log_init(runtime.memory_pool); + switch_event_init(runtime.memory_pool); if (switch_xml_init(runtime.memory_pool, err) != SWITCH_STATUS_SUCCESS) { @@ -935,6 +935,8 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc rlp.rlim_max = RLIM_INFINITY; setrlimit(RLIMIT_CORE, &rlp); #endif + } else if (!strcasecmp(var, "colorize-console") && switch_true(val)) { + runtime.colorize_console = SWITCH_TRUE; } else if (!strcasecmp(var, "mailer-app")) { runtime.mailer_app = switch_core_strdup(runtime.memory_pool, val); } else if (!strcasecmp(var, "mailer-app-args")) { @@ -978,6 +980,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc switch_xml_free(xml); } + switch_log_init(runtime.memory_pool, runtime.colorize_console); switch_core_state_machine_init(runtime.memory_pool); *err = NULL; diff --git a/src/switch_log.c b/src/switch_log.c index f752a9da72..4edb5d27b4 100644 --- a/src/switch_log.c +++ b/src/switch_log.c @@ -48,6 +48,7 @@ static const char *LEVELS[] = { struct switch_log_binding { switch_log_function_t function; switch_log_level_t level; + int is_console; struct switch_log_binding *next; }; @@ -60,6 +61,29 @@ static switch_queue_t *LOG_QUEUE = NULL; static switch_queue_t *LOG_RECYCLE_QUEUE = NULL; static int8_t THREAD_RUNNING = 0; static uint8_t MAX_LEVEL = 0; +static int mods_loaded = 0; +static switch_bool_t COLORIZE = SWITCH_FALSE; + +#ifdef WIN32 +static HANDLE hStdout; +static WORD wOldColorAttrs; +static CONSOLE_SCREEN_BUFFER_INFO csbiInfo; +static WORD COLORS[] = { FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, + FOREGROUND_RED | FOREGROUND_INTENSITY, + FOREGROUND_RED | FOREGROUND_INTENSITY, + FOREGROUND_RED | FOREGROUND_INTENSITY, + FOREGROUND_BLUE | FOREGROUND_INTENSITY, + FOREGROUND_BLUE | FOREGROUND_INTENSITY, + FOREGROUND_GREEN | FOREGROUND_INTENSITY, + FOREGROUND_GREEN | FOREGROUND_INTENSITY, + FOREGROUND_GREEN | FOREGROUND_INTENSITY +}; +#else +static const char *COLORS[] = { SWITCH_SEQ_FWHITE, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FMAGEN, SWITCH_SEQ_FCYAN, + SWITCH_SEQ_FGREEN, SWITCH_SEQ_FYELLOW, "" +}; +#endif + SWITCH_DECLARE(const char *) switch_log_level2str(switch_log_level_t level) { @@ -116,7 +140,33 @@ SWITCH_DECLARE(switch_log_level_t) switch_log_str2level(const char *str) return level; } -SWITCH_DECLARE(switch_status_t) switch_log_bind_logger(switch_log_function_t function, switch_log_level_t level) +SWITCH_DECLARE(switch_status_t) switch_log_unbind_logger(switch_log_function_t function) +{ + switch_log_binding_t *ptr = NULL, *last = NULL; + switch_status_t status = SWITCH_STATUS_FALSE; + + switch_mutex_lock(BINDLOCK); + for (ptr = BINDINGS; ptr; ptr = ptr->next) { + if (ptr->function == function) { + if (last) { + last->next = ptr->next; + } else { + BINDINGS = ptr->next; + } + status = SWITCH_STATUS_SUCCESS; + if (ptr->is_console) { + mods_loaded--; + } + break; + } + last = ptr; + } + switch_mutex_unlock(BINDLOCK); + + return status; +} + +SWITCH_DECLARE(switch_status_t) switch_log_bind_logger(switch_log_function_t function, switch_log_level_t level, switch_bool_t is_console) { switch_log_binding_t *binding = NULL, *ptr = NULL; switch_assert(function != NULL); @@ -131,6 +181,7 @@ SWITCH_DECLARE(switch_status_t) switch_log_bind_logger(switch_log_function_t fun binding->function = function; binding->level = level; + binding->is_console = is_console; switch_mutex_lock(BINDLOCK); for (ptr = BINDINGS; ptr && ptr->next; ptr = ptr->next); @@ -140,6 +191,9 @@ SWITCH_DECLARE(switch_status_t) switch_log_bind_logger(switch_log_function_t fun } else { BINDINGS = binding; } + if (is_console) { + mods_loaded++; + } switch_mutex_unlock(BINDLOCK); return SWITCH_STATUS_SUCCESS; @@ -255,7 +309,7 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char goto end; } - if (level == SWITCH_LOG_CONSOLE || !LOG_QUEUE || !THREAD_RUNNING) { + if (level == SWITCH_LOG_CONSOLE || mods_loaded == 0 || !LOG_QUEUE || !THREAD_RUNNING) { if (handle) { int aok = 1; #ifndef WIN32 @@ -276,7 +330,11 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char } #endif if (aok) { - fprintf(handle, "%s", data); + if (COLORIZE) { + fprintf(handle, "%s%s%s", COLORS[level], data, SWITCH_SEQ_DEFAULT_COLOR); + } else { + fprintf(handle, "%s", data); + } } } } else if (level <= MAX_LEVEL) { @@ -318,11 +376,11 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char } } -SWITCH_DECLARE(switch_status_t) switch_log_init(switch_memory_pool_t *pool) +SWITCH_DECLARE(switch_status_t) switch_log_init(switch_memory_pool_t *pool, switch_bool_t colorize) { switch_thread_t *thread; switch_threadattr_t *thd_attr;; - + switch_assert(pool != NULL); LOG_POOL = pool; @@ -340,6 +398,20 @@ SWITCH_DECLARE(switch_status_t) switch_log_init(switch_memory_pool_t *pool) while (!THREAD_RUNNING) { switch_yield(1000); } + + if (colorize) { +#ifdef WIN32 + hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + if (switch_core_get_console() == stdout && hStdout != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) { + wOldColorAttrs = csbiInfo.wAttributes; + COLORIZE = SWITCH_TRUE; + } +#else + COLORIZE = SWITCH_TRUE; +#endif + } + + return SWITCH_STATUS_SUCCESS; }