[Freeswitch-svn] [commit] r8997 - in freeswitch/trunk: conf/autoload_configs src src/include src/include/private src/mod/event_handlers/mod_event_socket src/mod/loggers/mod_console src/mod/loggers/mod_logfile src/mod/loggers/mod_syslog

Freeswitch SVN anthm at freeswitch.org
Fri Jul 11 10:35:09 EDT 2008


Author: anthm
Date: Fri Jul 11 10:35:08 2008
New Revision: 8997

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

Log:
fix FSCORE-156

Modified: freeswitch/trunk/conf/autoload_configs/switch.conf.xml
==============================================================================
--- freeswitch/trunk/conf/autoload_configs/switch.conf.xml	(original)
+++ freeswitch/trunk/conf/autoload_configs/switch.conf.xml	Fri Jul 11 10:35:08 2008
@@ -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 -->

Modified: freeswitch/trunk/src/include/private/switch_core_pvt.h
==============================================================================
--- freeswitch/trunk/src/include/private/switch_core_pvt.h	(original)
+++ freeswitch/trunk/src/include/private/switch_core_pvt.h	Fri Jul 11 10:35:08 2008
@@ -195,6 +195,7 @@
 	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;

Modified: freeswitch/trunk/src/include/switch_log.h
==============================================================================
--- freeswitch/trunk/src/include/switch_log.h	(original)
+++ freeswitch/trunk/src/include/switch_log.h	Fri Jul 11 10:35:08 2008
@@ -74,7 +74,7 @@
   \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 @@
   \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

Modified: freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c
==============================================================================
--- freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c	(original)
+++ freeswitch/trunk/src/mod/event_handlers/mod_event_socket/mod_event_socket.c	Fri Jul 11 10:35:08 2008
@@ -323,6 +323,8 @@
 
 	prefs.done = 1;
 
+	switch_log_unbind_logger(socket_logger);
+
 	close_socket(&listen_list.sock);
 
 	while (prefs.threads) {
@@ -1368,7 +1370,7 @@
 		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 (;;) {

Modified: freeswitch/trunk/src/mod/loggers/mod_console/mod_console.c
==============================================================================
--- freeswitch/trunk/src/mod/loggers/mod_console/mod_console.c	(original)
+++ freeswitch/trunk/src/mod/loggers/mod_console/mod_console.c	Fri Jul 11 10:35:08 2008
@@ -42,7 +42,7 @@
 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 @@
 	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 @@
 
 
 	/* 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_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:

Modified: freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c
==============================================================================
--- freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c	(original)
+++ freeswitch/trunk/src/mod/loggers/mod_logfile/mod_logfile.c	Fri Jul 11 10:35:08 2008
@@ -374,9 +374,9 @@
 		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)

Modified: freeswitch/trunk/src/mod/loggers/mod_syslog/mod_syslog.c
==============================================================================
--- freeswitch/trunk/src/mod/loggers/mod_syslog/mod_syslog.c	(original)
+++ freeswitch/trunk/src/mod/loggers/mod_syslog/mod_syslog.c	Fri Jul 11 10:35:08 2008
@@ -152,7 +152,7 @@
 
 	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_SHUTDOWN_FUNCTION(mod_syslog_shutdown)
 {
 	closelog();
+	switch_log_unbind_logger(mod_syslog_logger);
 
 	return SWITCH_STATUS_SUCCESS;
 }

Modified: freeswitch/trunk/src/switch_core.c
==============================================================================
--- freeswitch/trunk/src/switch_core.c	(original)
+++ freeswitch/trunk/src/switch_core.c	Fri Jul 11 10:35:08 2008
@@ -896,7 +896,7 @@
 	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 @@
 					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_xml_free(xml);
 	}
 
+	switch_log_init(runtime.memory_pool, runtime.colorize_console);
 	switch_core_state_machine_init(runtime.memory_pool);
 
 	*err = NULL;

Modified: freeswitch/trunk/src/switch_log.c
==============================================================================
--- freeswitch/trunk/src/switch_log.c	(original)
+++ freeswitch/trunk/src/switch_log.c	Fri Jul 11 10:35:08 2008
@@ -48,6 +48,7 @@
 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_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 @@
 	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 @@
 
 	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 @@
 	} else {
 		BINDINGS = binding;
 	}
+	if (is_console) {
+		mods_loaded++;
+	}
 	switch_mutex_unlock(BINDLOCK);
 
 	return SWITCH_STATUS_SUCCESS;
@@ -255,7 +309,7 @@
 		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 @@
 			}
 #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(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 @@
 	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;
 }
 



More information about the Freeswitch-svn mailing list