]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
fix FSCORE-156
authorAnthony Minessale <anthony.minessale@gmail.com>
Fri, 11 Jul 2008 14:35:08 +0000 (14:35 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Fri, 11 Jul 2008 14:35:08 +0000 (14:35 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8997 d0543943-73ff-0310-b7d9-9358b9ac24b2

conf/autoload_configs/switch.conf.xml
src/include/private/switch_core_pvt.h
src/include/switch_log.h
src/mod/event_handlers/mod_event_socket/mod_event_socket.c
src/mod/loggers/mod_console/mod_console.c
src/mod/loggers/mod_logfile/mod_logfile.c
src/mod/loggers/mod_syslog/mod_syslog.c
src/switch_core.c
src/switch_log.c

index c4ec287af3c4fdffa4fbd331d947bf37a69f4ec9..7a0fbde30aa241983cdb4fa20d5e0e505c4c12da 100644 (file)
@@ -16,6 +16,8 @@
   </cli-keybindings> 
   
   <settings>
+    <!--Colorize the Console -->
+    <param name="colorize-console" value="true"/>
     <!--Most channels to allow at once -->
     <param name="max-sessions" value="1000"/>
     <!--Most channels to create per second -->
index d623ac0cfa315b376e0a7aa74adfa5440445e9cd..49d21a6d641435f19ce5abe3f415b22a075184cc 100644 (file)
@@ -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;
index 52ee71afb719a61f63cadefcb98baa12ff219bfb..01efc164a310f9eed2c375e4d7b24d7d9f394dee 100644 (file)
@@ -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
index fbc0b9381aa6b6f5cacfd0d34bbcc2fc54f850e4..3bcb39ef4ca3e6ef2420e27750e818d0b9f90cf7 100644 (file)
@@ -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 (;;) {
index b4503554f6296d4cfef5fb073abef45325cbdd86..c3b5868eb6964b8caf0b0bfd763ef74d164355a7 100644 (file)
@@ -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:
index 599e82571816e5ffacea817bb628754d5c43b62b..c0417e5dde436d091ca8f4894117fe4dd4e68ba4 100644 (file)
@@ -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)
index bc225b933b7b26ea1155772b4593e03b87be11a4..e467d5fcf66bfec6a4f8b7e2d1e8b460c51e7470 100644 (file)
@@ -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;
 }
index 0553289b86f3005076c4788cefde5a05568f9c82..5a0badc70989f4578d3a7b409f65e18a2e5734cb 100644 (file)
@@ -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;
index f752a9da721a0fb60d27ba7b3571119487c3fb58..4edb5d27b489fa9e96b89c3552fcddaa36577d06 100644 (file)
@@ -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;
 }