提交 b2dd6b51 authored 作者: Anthony Minessale's avatar Anthony Minessale

change logger stuff and finalize mod_logfile

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@6524 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 aa4e109a
...@@ -2,11 +2,14 @@ ...@@ -2,11 +2,14 @@
<!-- pick a file name, a function name or 'all' --> <!-- pick a file name, a function name or 'all' -->
<!-- map as many as you need for specific debugging --> <!-- map as many as you need for specific debugging -->
<mappings> <mappings>
<!-- <param name="log_event" value="DEBUG"/> --> <!--
<param name="all" value="DEBUG"/> name can be a file name, function name or 'all'
value is one or more of debug,info,notice,warning,error,crit,alert,all
-->
<map name="all" value="debug,info,notice,warning,error,crit,alert"/>
</mappings> </mappings>
<settings> <settings>
<!-- uncomment for color logging (for debugging) --> <!-- comment or set to false for no color logging -->
<!--<param name="colorize" value="true"/>--> <param name="colorize" value="true"/>
</settings> </settings>
</configuration> </configuration>
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<settings> <settings>
<!-- true to auto rotate on HUP, false to open/close --> <!-- true to auto rotate on HUP, false to open/close -->
<param name="rotate" value="true"/> <param name="rotate" value="true"/>
</settings> </settings>
<profiles> <profiles>
<profile name="default"> <profile name="default">
<settings> <settings>
...@@ -10,11 +10,14 @@ ...@@ -10,11 +10,14 @@
<!--<param name="logfile" value="/var/log/freeswitch.log"/>--> <!--<param name="logfile" value="/var/log/freeswitch.log"/>-->
<!-- At this length in bytes rotate the log file (0 for never) --> <!-- At this length in bytes rotate the log file (0 for never) -->
<!--<param name="rollover" value="10485760"/>--> <!--<param name="rollover" value="10485760"/>-->
<!-- The level of the message to log -->
<!--<param name="level" value="debug,info,warning,notice,error,crit,alert"/>-->
<param name="level" value="all"/>
</settings> </settings>
<mappings>
<!--
name can be a file name, function name or 'all'
value is one or more of debug,info,notice,warning,error,crit,alert,all
-->
<map name="all" value="debug,info,notice,warning,error,crit,alert"/>
</mappings>
</profile> </profile>
</profiles> </profiles>
</configuration> </configuration>
...@@ -117,6 +117,9 @@ SWITCH_DECLARE(const char *) switch_log_level2str(switch_log_level_t level); ...@@ -117,6 +117,9 @@ SWITCH_DECLARE(const char *) switch_log_level2str(switch_log_level_t level);
*/ */
SWITCH_DECLARE(switch_log_level_t) switch_log_str2level(const char *str); SWITCH_DECLARE(switch_log_level_t) switch_log_str2level(const char *str);
SWITCH_DECLARE(uint32_t) switch_log_str2mask(const char *str);
#define switch_log_check_mask(_mask, _level) (_mask & (1 << _level))
///\} ///\}
SWITCH_END_EXTERN_C SWITCH_END_EXTERN_C
#endif #endif
......
...@@ -35,7 +35,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_console_load); ...@@ -35,7 +35,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_console_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_console_shutdown); SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_console_shutdown);
SWITCH_MODULE_DEFINITION(mod_console, mod_console_load, mod_console_shutdown, NULL); SWITCH_MODULE_DEFINITION(mod_console, mod_console_load, mod_console_shutdown, NULL);
static const uint8_t STATIC_LEVELS[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
static int RUNNING = 0; static int RUNNING = 0;
static int COLORIZE = 0; static int COLORIZE = 0;
...@@ -61,34 +60,38 @@ static const char *COLORS[] = { SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRE ...@@ -61,34 +60,38 @@ static const char *COLORS[] = { SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRE
static switch_memory_pool_t *module_pool = NULL; static switch_memory_pool_t *module_pool = NULL;
static switch_hash_t *log_hash = NULL; static switch_hash_t *log_hash = NULL;
static switch_hash_t *name_hash = NULL; static uint32_t all_level = 0;
static int8_t all_level = -1; static int32_t hard_log_level = SWITCH_LOG_DEBUG;
static void del_mapping(char *var) static void del_mapping(char *var)
{ {
if (!strcasecmp(var, "all")) {
all_level = -1;
return;
}
switch_core_hash_insert(log_hash, var, NULL); switch_core_hash_insert(log_hash, var, NULL);
} }
static void add_mapping(char *var, char *val) static void add_mapping(char *var, char *val, int cumlative)
{ {
char *name; uint32_t m = 0;
if (!strcasecmp(var, "all")) { if (cumlative) {
all_level = (int8_t) switch_log_str2level(val); uint32_t l = switch_log_str2level(val);
return; int i;
assert(l < 10);
for (i = 0; i <= l; i++) {
m |= (1 << i);
}
} else {
m = switch_log_str2mask(val);
} }
if (!(name = switch_core_hash_find(name_hash, var))) { if (!strcasecmp(var, "all")) {
name = switch_core_strdup(module_pool, var); all_level |= m;
switch_core_hash_insert(name_hash, name, name); return;
} }
del_mapping(name); del_mapping(var);
switch_core_hash_insert(log_hash, name, (void *) &STATIC_LEVELS[(uint8_t) switch_log_str2level(val)]); switch_core_hash_insert(log_hash, var, (void *)(intptr_t) m);
} }
static switch_status_t config_logger(void) static switch_status_t config_logger(void)
...@@ -104,18 +107,19 @@ static switch_status_t config_logger(void) ...@@ -104,18 +107,19 @@ static switch_status_t config_logger(void)
if (log_hash) { if (log_hash) {
switch_core_hash_destroy(&log_hash); switch_core_hash_destroy(&log_hash);
} }
if (name_hash) {
switch_core_hash_destroy(&name_hash);
}
switch_core_hash_init(&log_hash, module_pool); switch_core_hash_init(&log_hash, module_pool);
switch_core_hash_init(&name_hash, module_pool);
if ((settings = switch_xml_child(cfg, "mappings"))) { if ((settings = switch_xml_child(cfg, "mappings"))) {
for (param = switch_xml_child(settings, "param"); param; param = param->next) { for (param = switch_xml_child(settings, "param"); param; param = param->next) {
char *var = (char *) switch_xml_attr_soft(param, "name"); char *var = (char *) switch_xml_attr_soft(param, "name");
char *val = (char *) switch_xml_attr_soft(param, "value"); char *val = (char *) switch_xml_attr_soft(param, "value");
add_mapping(var, val, 1);
add_mapping(var, val); }
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);
} }
} }
...@@ -146,30 +150,34 @@ static switch_status_t config_logger(void) ...@@ -146,30 +150,34 @@ static switch_status_t config_logger(void)
static switch_status_t switch_console_logger(const switch_log_node_t *node, switch_log_level_t level) static switch_status_t switch_console_logger(const switch_log_node_t *node, switch_log_level_t level)
{ {
FILE *handle; FILE *handle;
/*
if (!RUNNING) { if (!RUNNING) {
return SWITCH_STATUS_FALSE; return SWITCH_STATUS_SUCCESS;
} }
*/
if (level > hard_log_level) {
return SWITCH_STATUS_SUCCESS;
}
if ((handle = switch_core_data_channel(SWITCH_CHANNEL_ID_LOG))) { if ((handle = switch_core_data_channel(SWITCH_CHANNEL_ID_LOG))) {
uint8_t *lookup = NULL; size_t mask = 0;
switch_log_level_t level = SWITCH_LOG_DEBUG; int ok = 0;
if (log_hash) { ok = switch_log_check_mask(all_level, level);
lookup = switch_core_hash_find(log_hash, node->file);
if (!lookup) { if (log_hash) {
lookup = switch_core_hash_find(log_hash, node->func); if (!ok) {
} mask = (size_t) switch_core_hash_find(log_hash, node->file);
ok = switch_log_check_mask(mask, level);
} }
if (lookup) { if (!ok) {
level = (switch_log_level_t) *lookup; mask = (size_t) switch_core_hash_find(log_hash, node->func);
} else if (all_level > -1) { ok = switch_log_check_mask(mask, level);
level = (switch_log_level_t) all_level; }
} }
if (!log_hash || (((all_level > -1) || lookup) && level >= node->level)) { if (ok) {
if (COLORIZE) { if (COLORIZE) {
#ifdef WIN32 #ifdef WIN32
SetConsoleTextAttribute(hStdout, COLORS[node->level]); SetConsoleTextAttribute(hStdout, COLORS[node->level]);
...@@ -190,13 +198,78 @@ static switch_status_t switch_console_logger(const switch_log_node_t *node, swit ...@@ -190,13 +198,78 @@ static switch_status_t switch_console_logger(const switch_log_node_t *node, swit
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
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) {
if (argc < 2) {
err = "missing arg";
goto end;
}
if (!strcasecmp(argv[0], "loglevel")) {
int level;
if (*argv[1] > 47 && *argv[1] < 58) {
level = atoi(argv[1]);
} else {
level = switch_log_str2level(argv[1]);
}
hard_log_level = level;
stream->write_function(stream, "+OK console log level set to %s\n", switch_log_level2str(hard_log_level));
goto end;
} else if (!strcasecmp(argv[0], "colorize")) {
COLORIZE = switch_true(argv[1]);
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;
}
SWITCH_MODULE_LOAD_FUNCTION(mod_console_load) SWITCH_MODULE_LOAD_FUNCTION(mod_console_load)
{ {
switch_api_interface_t *api_interface;
module_pool = pool; module_pool = pool;
/* connect my internal structure to the blank pointer passed to me */ /* connect my internal structure to the blank pointer passed to me */
*module_interface = switch_loadable_module_create_module_interface(pool, modname); *module_interface = switch_loadable_module_create_module_interface(pool, modname);
SWITCH_ADD_API(api_interface, "console", "Console", console_api_function, "");
/* setup my logger function */ /* setup my logger function */
switch_log_bind_logger(switch_console_logger, SWITCH_LOG_DEBUG); switch_log_bind_logger(switch_console_logger, SWITCH_LOG_DEBUG);
......
...@@ -69,6 +69,33 @@ SWITCH_DECLARE(const char *) switch_log_level2str(switch_log_level_t level) ...@@ -69,6 +69,33 @@ SWITCH_DECLARE(const char *) switch_log_level2str(switch_log_level_t level)
return LEVELS[level]; return LEVELS[level];
} }
SWITCH_DECLARE(uint32_t) switch_log_str2mask(const char *str)
{
int argc = 0, x = 0;
char *argv[10] = { 0 };
uint32_t mask = 0;
char *p = strdup(str);
assert(p);
if ((argc = switch_separate_string(p, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
for (x = 0; x < argc; x++) {
if (!strcasecmp(argv[x], "all")) {
mask = 0xFF;
break;
} else {
mask |= (1 << switch_log_str2level(argv[x]));
}
}
}
free(p);
return mask;
}
SWITCH_DECLARE(switch_log_level_t) switch_log_str2level(const char *str) SWITCH_DECLARE(switch_log_level_t) switch_log_str2level(const char *str)
{ {
int x = 0; int x = 0;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论