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>
|
|
|
|
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_console_load);
|
2007-09-29 01:06:08 +00:00
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_console_shutdown);
|
|
|
|
SWITCH_MODULE_DEFINITION(mod_console, mod_console_load, mod_console_shutdown, NULL);
|
2007-06-13 17:06:10 +00:00
|
|
|
|
2007-09-29 01:06:08 +00:00
|
|
|
static int RUNNING = 0;
|
|
|
|
|
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;
|
2007-03-29 22:31:56 +00:00
|
|
|
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
|
|
|
|
};
|
2007-03-24 03:32:24 +00:00
|
|
|
#else
|
2007-03-30 00:13:31 +00:00
|
|
|
static const char *COLORS[] = { SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FMAGEN, SWITCH_SEQ_FCYAN,
|
2007-03-29 22:31:56 +00:00
|
|
|
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 01:00:52 +00:00
|
|
|
static switch_memory_pool_t *module_pool = NULL;
|
|
|
|
static switch_hash_t *log_hash = NULL;
|
2007-12-05 20:24:20 +00:00
|
|
|
static uint32_t all_level = 0;
|
|
|
|
static int32_t hard_log_level = SWITCH_LOG_DEBUG;
|
2008-02-20 20:15:45 +00:00
|
|
|
//static int32_t failed_write = 0;
|
2007-03-29 22:31:56 +00:00
|
|
|
static void del_mapping(char *var)
|
|
|
|
{
|
2006-04-12 18:53:42 +00:00
|
|
|
switch_core_hash_insert(log_hash, var, NULL);
|
|
|
|
}
|
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
static void add_mapping(char *var, char *val, int cumlative)
|
2006-04-12 18:53:42 +00:00
|
|
|
{
|
2007-12-05 20:24:20 +00:00
|
|
|
uint32_t m = 0;
|
|
|
|
|
|
|
|
if (cumlative) {
|
|
|
|
uint32_t l = switch_log_str2level(val);
|
2007-12-06 13:50:14 +00:00
|
|
|
uint32_t i;
|
2006-04-12 18:53:42 +00:00
|
|
|
|
2007-12-10 19:16:50 +00:00
|
|
|
if (l < 10) {
|
|
|
|
for (i = 0; i <= l; i++) {
|
|
|
|
m |= (1 << i);
|
|
|
|
}
|
2007-12-05 20:24:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m = switch_log_str2mask(val);
|
|
|
|
}
|
|
|
|
|
2006-04-12 19:10:04 +00:00
|
|
|
if (!strcasecmp(var, "all")) {
|
2007-12-05 20:24:20 +00:00
|
|
|
all_level |= m;
|
2006-04-12 19:10:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
del_mapping(var);
|
|
|
|
switch_core_hash_insert(log_hash, var, (void *)(intptr_t) m);
|
2006-04-12 18:53:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-09-29 01:06:08 +00:00
|
|
|
if (log_hash) {
|
|
|
|
switch_core_hash_destroy(&log_hash);
|
|
|
|
}
|
2007-12-05 20:24:20 +00:00
|
|
|
|
2006-04-12 17:51:47 +00:00
|
|
|
switch_core_hash_init(&log_hash, module_pool);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
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");
|
2007-12-05 20:24:20 +00:00
|
|
|
add_mapping(var, val, 1);
|
|
|
|
}
|
|
|
|
for (param = switch_xml_child(settings, "map"); param; param = param->next) {
|
|
|
|
char *var = (char *) switch_xml_attr_soft(param, "name");
|
|
|
|
char *val = (char *) switch_xml_attr_soft(param, "value");
|
|
|
|
add_mapping(var, val, 0);
|
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);
|
2007-03-30 00:13:31 +00:00
|
|
|
if (switch_core_get_console() == stdout && hStdout != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
|
2007-03-04 22:19:39 +00:00
|
|
|
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
|
2008-02-08 19:06:13 +00:00
|
|
|
} else if (!strcasecmp(var, "loglevel") && !switch_strlen_zero(val)) {
|
2008-02-04 18:22:29 +00:00
|
|
|
hard_log_level = switch_log_str2level(val);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2008-01-25 23:09:33 +00:00
|
|
|
static int can_write(FILE *handle, int ms)
|
|
|
|
{
|
|
|
|
#ifndef WIN32
|
|
|
|
int aok = 1;
|
|
|
|
fd_set can_write;
|
|
|
|
int fd;
|
|
|
|
struct timeval to;
|
|
|
|
int sec, usec;
|
|
|
|
|
|
|
|
sec = ms / 1000;
|
|
|
|
usec = ms % 1000;
|
|
|
|
|
|
|
|
fd = fileno(handle);
|
|
|
|
memset(&to, 0, sizeof(to));
|
|
|
|
FD_SET(fd, &can_write);
|
|
|
|
to.tv_sec = sec;
|
|
|
|
to.tv_usec = usec;
|
|
|
|
if (select(fd+1, NULL, &can_write, NULL, &to) > 0) {
|
|
|
|
aok = FD_ISSET(fd, &can_write);
|
|
|
|
} else {
|
|
|
|
aok = 0;
|
|
|
|
}
|
2008-02-20 00:30:39 +00:00
|
|
|
|
2008-01-25 23:09:33 +00:00
|
|
|
return aok;
|
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2007-12-05 20:24:20 +00:00
|
|
|
|
2007-09-29 01:06:08 +00:00
|
|
|
if (!RUNNING) {
|
2007-12-05 20:24:20 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2007-09-29 01:06:08 +00:00
|
|
|
}
|
2006-04-26 18:37:53 +00:00
|
|
|
|
2008-02-20 03:56:38 +00:00
|
|
|
#if 0
|
2008-01-25 23:09:33 +00:00
|
|
|
if (failed_write) {
|
|
|
|
if ((handle = switch_core_data_channel(SWITCH_CHANNEL_ID_LOG))) {
|
2008-02-20 00:30:39 +00:00
|
|
|
int aok = can_write(handle, 100);
|
2008-01-25 23:09:33 +00:00
|
|
|
if (aok) {
|
|
|
|
const char *msg = "Failed to write to the console! Logging disabled! RE-enable with the 'console loglevel' command\n";
|
2008-01-26 04:07:48 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%s", msg);
|
|
|
|
#else
|
2008-01-25 23:09:33 +00:00
|
|
|
if (COLORIZE) {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%s%s%s", COLORS[1], msg, SWITCH_SEQ_DEFAULT_COLOR);
|
|
|
|
} else {
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "%s", msg);
|
|
|
|
}
|
2008-01-26 04:07:48 +00:00
|
|
|
#endif
|
2008-01-25 23:09:33 +00:00
|
|
|
failed_write = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-20 03:56:38 +00:00
|
|
|
#endif
|
2008-01-25 23:09:33 +00:00
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
if (level > hard_log_level) {
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
if ((handle = switch_core_data_channel(SWITCH_CHANNEL_ID_LOG))) {
|
|
|
|
size_t mask = 0;
|
2007-12-06 13:50:14 +00:00
|
|
|
size_t ok = 0;
|
2008-01-25 23:09:33 +00:00
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
ok = switch_log_check_mask(all_level, level);
|
|
|
|
|
|
|
|
if (log_hash) {
|
|
|
|
if (!ok) {
|
|
|
|
mask = (size_t) switch_core_hash_find(log_hash, node->file);
|
|
|
|
ok = switch_log_check_mask(mask, level);
|
2006-04-12 17:51:47 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
if (!ok) {
|
|
|
|
mask = (size_t) switch_core_hash_find(log_hash, node->func);
|
|
|
|
ok = switch_log_check_mask(mask, level);
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
}
|
2006-04-14 15:59:22 +00:00
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
if (ok) {
|
2008-01-25 23:09:33 +00:00
|
|
|
#ifndef WIN32
|
2008-02-20 00:30:39 +00:00
|
|
|
int aok = can_write(handle, 10000);
|
2008-01-25 23:09:33 +00:00
|
|
|
|
|
|
|
if (!aok) {
|
2008-02-20 20:15:45 +00:00
|
|
|
//hard_log_level = 0;
|
|
|
|
//failed_write++;
|
2008-01-25 23:09:33 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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-29 22:31:56 +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-11 21:13:44 +00:00
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
SWITCH_STANDARD_API(console_api_function)
|
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
char *mydata = NULL, *argv[3];
|
|
|
|
const char *err = NULL;
|
|
|
|
|
|
|
|
if (!cmd) {
|
|
|
|
err = "bad args";
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
mydata = strdup(cmd);
|
|
|
|
assert(mydata);
|
|
|
|
|
|
|
|
argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
|
|
|
|
|
|
|
|
if (argc > 0) {
|
|
|
|
|
2008-01-25 23:09:33 +00:00
|
|
|
if (argc < 1) {
|
2007-12-05 20:24:20 +00:00
|
|
|
err = "missing arg";
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(argv[0], "loglevel")) {
|
2008-01-25 23:09:33 +00:00
|
|
|
int level = hard_log_level;
|
2007-12-05 20:24:20 +00:00
|
|
|
|
2008-01-25 23:09:33 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
if (*argv[1] > 47 && *argv[1] < 58) {
|
|
|
|
level = atoi(argv[1]);
|
|
|
|
} else {
|
|
|
|
level = switch_log_str2level(argv[1]);
|
|
|
|
}
|
2007-12-05 20:24:20 +00:00
|
|
|
}
|
|
|
|
|
2007-12-10 19:16:50 +00:00
|
|
|
if (level == SWITCH_LOG_INVALID) {
|
|
|
|
stream->write_function(stream, "-ERR syntax error, console log level not set!\n");
|
|
|
|
} else {
|
|
|
|
hard_log_level = level;
|
|
|
|
stream->write_function(stream, "+OK console log level set to %s\n", switch_log_level2str(hard_log_level));
|
|
|
|
}
|
2007-12-05 20:24:20 +00:00
|
|
|
goto end;
|
|
|
|
} else if (!strcasecmp(argv[0], "colorize")) {
|
2008-01-25 23:09:33 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
COLORIZE = switch_true(argv[1]);
|
|
|
|
}
|
2007-12-05 20:24:20 +00:00
|
|
|
stream->write_function(stream, "+OK console color %s\n", COLORIZE ? "enabled" : "disabled");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = "invalid command";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
stream->write_function(stream, "-Error %s\n", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
free(mydata);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-06-13 17:06:10 +00:00
|
|
|
SWITCH_MODULE_LOAD_FUNCTION(mod_console_load)
|
2006-04-11 21:13:44 +00:00
|
|
|
{
|
2007-12-05 20:24:20 +00:00
|
|
|
switch_api_interface_t *api_interface;
|
|
|
|
|
2006-04-12 17:51:47 +00:00
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
module_pool = pool;
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
/* connect my internal structure to the blank pointer passed to me */
|
2007-06-20 07:53:33 +00:00
|
|
|
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
2006-04-11 21:13:44 +00:00
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
SWITCH_ADD_API(api_interface, "console", "Console", console_api_function, "");
|
|
|
|
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
/* setup my logger function */
|
|
|
|
switch_log_bind_logger(switch_console_logger, SWITCH_LOG_DEBUG);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-12 17:51:47 +00:00
|
|
|
config_logger();
|
2007-09-29 01:06:08 +00:00
|
|
|
RUNNING = 1;
|
2006-04-11 21:13:44 +00:00
|
|
|
/* indicate that the module should continue to be loaded */
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-09-29 01:06:08 +00:00
|
|
|
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_console_shutdown)
|
|
|
|
{
|
|
|
|
//switch_core_hash_destroy(&log_hash);
|
|
|
|
//switch_core_hash_destroy(&name_hash);
|
|
|
|
RUNNING = 0;
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2006-11-27 22:30:48 +00:00
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
2008-02-03 22:14:57 +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:
|
|
|
|
*/
|