2006-04-11 21:13:44 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005/2006, Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthmct@yahoo.com>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* mod_console.c -- Console Logger
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <switch.h>
|
|
|
|
|
|
|
|
static const char modname[] = "mod_console";
|
2006-04-12 18:53:42 +00:00
|
|
|
static const uint8_t STATIC_LEVELS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
|
2007-02-28 18:28:07 +00:00
|
|
|
static int COLORIZE = 0;
|
2007-03-04 22:19:39 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
static HANDLE hStdout;
|
2007-03-24 03:32:24 +00:00
|
|
|
static WORD wOldColorAttrs;
|
|
|
|
static CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
|
|
|
static WORD COLORS[] =
|
|
|
|
{ FOREGROUND_RED | 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
|
2007-02-28 18:28:07 +00:00
|
|
|
static const char *COLORS[] =
|
|
|
|
{ SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FMAGEN, SWITCH_SEQ_FCYAN, SWITCH_SEQ_FGREEN, SWITCH_SEQ_FYELLOW, "" };
|
2007-03-04 22:19:39 +00:00
|
|
|
#endif
|
2006-04-11 21:13:44 +00:00
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_loadable_module_interface_t console_module_interface = {
|
2006-04-11 21:13:44 +00:00
|
|
|
/*.module_name */ modname,
|
|
|
|
/*.endpoint_interface */ NULL,
|
|
|
|
/*.timer_interface */ NULL,
|
|
|
|
/*.dialplan_interface */ NULL,
|
|
|
|
/*.codec_interface */ NULL,
|
|
|
|
/*.application_interface */ NULL,
|
|
|
|
/*.api_interface */ NULL,
|
|
|
|
/*.file_interface */ NULL,
|
|
|
|
/*.speech_interface */ NULL,
|
|
|
|
/*.directory_interface */ NULL
|
|
|
|
};
|
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
static switch_memory_pool_t *module_pool = NULL;
|
|
|
|
static switch_hash_t *log_hash = NULL;
|
|
|
|
static switch_hash_t *name_hash = NULL;
|
2006-04-12 17:51:47 +00:00
|
|
|
static int8_t all_level = -1;
|
|
|
|
|
2006-04-12 18:53:42 +00:00
|
|
|
static void del_mapping(char *var) {
|
|
|
|
if (!strcasecmp(var, "all")) {
|
|
|
|
all_level = -1;
|
2006-04-12 19:10:04 +00:00
|
|
|
return;
|
2006-04-12 18:53:42 +00:00
|
|
|
}
|
|
|
|
switch_core_hash_insert(log_hash, var, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_mapping(char *var, char *val)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
|
2006-04-12 19:10:04 +00:00
|
|
|
if (!strcasecmp(var, "all")) {
|
|
|
|
all_level = (int8_t) switch_log_str2level(val);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-04-12 18:53:42 +00:00
|
|
|
if (!(name = switch_core_hash_find(name_hash, var))) {
|
|
|
|
name = switch_core_strdup(module_pool, var);
|
|
|
|
switch_core_hash_insert(name_hash, name, name);
|
|
|
|
}
|
2006-04-14 15:59:22 +00:00
|
|
|
|
2006-04-12 18:53:42 +00:00
|
|
|
del_mapping(name);
|
|
|
|
switch_core_hash_insert(log_hash, name, (void *) &STATIC_LEVELS[(uint8_t)switch_log_str2level(val)]);
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_status_t config_logger(void)
|
2006-04-12 17:51:47 +00:00
|
|
|
{
|
|
|
|
char *cf = "console.conf";
|
2006-05-10 03:23:05 +00:00
|
|
|
switch_xml_t cfg, xml, settings, param;
|
2006-04-12 17:51:47 +00:00
|
|
|
|
2006-05-10 15:47:54 +00:00
|
|
|
if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "open of %s failed\n", cf);
|
2006-05-10 03:23:05 +00:00
|
|
|
return SWITCH_STATUS_TERM;
|
2006-04-12 17:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch_core_hash_init(&log_hash, module_pool);
|
2006-04-12 18:53:42 +00:00
|
|
|
switch_core_hash_init(&name_hash, module_pool);
|
2006-05-10 03:23:05 +00:00
|
|
|
|
|
|
|
if ((settings = switch_xml_child(cfg, "mappings"))) {
|
|
|
|
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
|
2006-05-17 00:58:21 +00:00
|
|
|
char *var = (char *) switch_xml_attr_soft(param, "name");
|
|
|
|
char *val = (char *) switch_xml_attr_soft(param, "value");
|
2006-04-12 17:51:47 +00:00
|
|
|
|
2006-04-12 18:53:42 +00:00
|
|
|
add_mapping(var, val);
|
2006-04-12 17:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
2006-05-10 03:23:05 +00:00
|
|
|
|
2007-02-28 18:28:07 +00:00
|
|
|
if ((settings = switch_xml_child(cfg, "settings"))) {
|
|
|
|
for (param = switch_xml_child(settings, "param"); param; param = param->next) {
|
|
|
|
char *var = (char *) switch_xml_attr_soft(param, "name");
|
|
|
|
char *val = (char *) switch_xml_attr_soft(param, "value");
|
|
|
|
|
|
|
|
if (!strcasecmp(var, "colorize") && switch_true(val)) {
|
2007-03-04 22:19:39 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
if (switch_core_get_console() == stdout && hStdout != INVALID_HANDLE_VALUE
|
|
|
|
&& GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
|
|
|
|
wOldColorAttrs = csbiInfo.wAttributes;
|
|
|
|
COLORIZE = 1;
|
|
|
|
}
|
|
|
|
#else
|
2007-02-28 18:28:07 +00:00
|
|
|
COLORIZE = 1;
|
2007-03-04 22:19:39 +00:00
|
|
|
#endif
|
2007-02-28 18:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-10 03:23:05 +00:00
|
|
|
switch_xml_free(xml);
|
|
|
|
|
2006-04-12 17:51:47 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
static switch_status_t switch_console_logger(const switch_log_node_t *node, switch_log_level_t level)
|
2006-04-11 21:13:44 +00:00
|
|
|
{
|
|
|
|
FILE *handle;
|
|
|
|
|
|
|
|
if ((handle = switch_core_data_channel(SWITCH_CHANNEL_ID_LOG))) {
|
2006-04-12 18:53:42 +00:00
|
|
|
uint8_t *lookup = NULL;
|
2006-04-29 23:43:28 +00:00
|
|
|
switch_log_level_t level = SWITCH_LOG_DEBUG;
|
2006-04-26 18:37:53 +00:00
|
|
|
|
2006-04-14 15:59:22 +00:00
|
|
|
if (log_hash) {
|
2006-04-12 17:51:47 +00:00
|
|
|
lookup = switch_core_hash_find(log_hash, node->file);
|
2006-04-12 18:53:42 +00:00
|
|
|
|
2006-04-12 17:51:47 +00:00
|
|
|
if (!lookup) {
|
|
|
|
lookup = switch_core_hash_find(log_hash, node->func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lookup) {
|
2006-04-29 23:43:28 +00:00
|
|
|
level = (switch_log_level_t) *lookup;
|
2006-04-14 15:59:22 +00:00
|
|
|
} else if (all_level > -1) {
|
2006-04-29 23:43:28 +00:00
|
|
|
level = (switch_log_level_t) all_level;
|
2006-04-14 15:59:22 +00:00
|
|
|
}
|
|
|
|
|
2006-04-12 17:51:47 +00:00
|
|
|
if (!log_hash || (((all_level > - 1) || lookup) && level >= node->level)) {
|
2007-02-28 18:28:07 +00:00
|
|
|
if (COLORIZE) {
|
2007-03-04 22:19:39 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
SetConsoleTextAttribute(hStdout, COLORS[node->level]);
|
2007-03-24 03:32:24 +00:00
|
|
|
WriteFile(hStdout, node->data, (DWORD)strlen(node->data), NULL, NULL);
|
2007-03-04 22:19:39 +00:00
|
|
|
SetConsoleTextAttribute(hStdout, wOldColorAttrs);
|
|
|
|
#else
|
2007-02-28 18:28:07 +00:00
|
|
|
fprintf(handle, "%s%s%s", COLORS[node->level], node->data, SWITCH_SEQ_DEFAULT_COLOR);
|
2007-03-04 22:19:39 +00:00
|
|
|
#endif
|
2007-02-28 18:28:07 +00:00
|
|
|
} else {
|
|
|
|
fprintf(handle, "%s", node->data);
|
|
|
|
}
|
2006-04-12 17:51:47 +00:00
|
|
|
}
|
2006-04-26 18:37:53 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "HELP I HAVE NO CONSOLE TO LOG TO!\n");
|
|
|
|
fflush(stderr);
|
2006-04-11 21:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-04-30 18:24:24 +00:00
|
|
|
SWITCH_MOD_DECLARE(switch_status_t) switch_module_load(const switch_loadable_module_interface_t **module_interface, char *filename)
|
2006-04-11 21:13:44 +00:00
|
|
|
{
|
2006-04-12 17:51:47 +00:00
|
|
|
if (switch_core_new_memory_pool(&module_pool) != SWITCH_STATUS_SUCCESS) {
|
2006-04-16 06:05:53 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "OH OH no pool\n");
|
2006-04-12 17:51:47 +00:00
|
|
|
return SWITCH_STATUS_TERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
2006-04-30 18:24:24 +00:00
|
|
|
*module_interface = &console_module_interface;
|
2006-04-11 21:13:44 +00:00
|
|
|
|
|
|
|
/* setup my logger function */
|
|
|
|
switch_log_bind_logger(switch_console_logger, SWITCH_LOG_DEBUG);
|
|
|
|
|
2006-04-12 17:51:47 +00:00
|
|
|
config_logger();
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
/* indicate that the module should continue to be loaded */
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-11-27 22:30:48 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2007-02-09 02:36:03 +00:00
|
|
|
* indent-tabs-mode:t
|
2006-11-27 22:30:48 +00:00
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
|
|
|
|
*/
|