enhance logger stuff

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@1123 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Brian West 2006-04-12 14:41:35 +00:00
parent cf2bcc233a
commit 1bb893efae
4 changed files with 42 additions and 14 deletions

View File

@ -47,6 +47,18 @@ extern "C" {
#include <switch.h> #include <switch.h>
typedef struct {
char *data;
char *file;
char *func;
char *content;
uint32_t line;
switch_log_level level;
switch_time_t timestamp;
} switch_log_node;
typedef switch_status (*switch_log_function)(const switch_log_node *node, switch_log_level level);
/*! /*!
\brief A method akin to printf that allows you to redirect output to a specific log \brief A method akin to printf that allows you to redirect output to a specific log
*/ */

View File

@ -233,7 +233,6 @@ typedef enum {
SWITCH_LOG_ALERT = 1, SWITCH_LOG_ALERT = 1,
SWITCH_LOG_EMERG = 0 SWITCH_LOG_EMERG = 0
} switch_log_level; } switch_log_level;
typedef switch_status (*switch_log_function)(const char *data, switch_log_level level);
/*! /*!

View File

@ -46,12 +46,12 @@ static switch_loadable_module_interface console_module_interface = {
/*.directory_interface */ NULL /*.directory_interface */ NULL
}; };
static switch_status switch_console_logger(const char *data, switch_log_level level) static switch_status switch_console_logger(const switch_log_node *node, switch_log_level level)
{ {
FILE *handle; FILE *handle;
if ((handle = switch_core_data_channel(SWITCH_CHANNEL_ID_LOG))) { if ((handle = switch_core_data_channel(SWITCH_CHANNEL_ID_LOG))) {
fprintf(handle, data); fprintf(handle, node->data);
} }
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;

View File

@ -49,12 +49,8 @@ struct switch_log_binding {
switch_log_level level; switch_log_level level;
struct switch_log_binding *next; struct switch_log_binding *next;
}; };
typedef struct switch_log_binding switch_log_binding;
typedef struct { typedef struct switch_log_binding switch_log_binding;
char *data;
switch_log_level level;
} switch_log_node;
static switch_memory_pool *LOG_POOL = NULL; static switch_memory_pool *LOG_POOL = NULL;
static switch_log_binding *BINDINGS = NULL; static switch_log_binding *BINDINGS = NULL;
@ -117,7 +113,7 @@ static void *SWITCH_THREAD_FUNC log_thread(switch_thread *thread, void *obj)
switch_mutex_lock(BINDLOCK); switch_mutex_lock(BINDLOCK);
for(binding = BINDINGS; binding; binding = binding->next) { for(binding = BINDINGS; binding; binding = binding->next) {
if (binding->level >= node->level) { if (binding->level >= node->level) {
binding->function(node->data, node->level); binding->function(node, node->level);
} }
} }
switch_mutex_unlock(BINDLOCK); switch_mutex_unlock(BINDLOCK);
@ -125,9 +121,17 @@ static void *SWITCH_THREAD_FUNC log_thread(switch_thread *thread, void *obj)
if (node->data) { if (node->data) {
free(node->data); free(node->data);
} }
if (node->file) {
free(node->file);
}
if (node->func) {
free(node->func);
}
free(node); free(node);
} }
} }
THREAD_RUNNING = 0; THREAD_RUNNING = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Logger Ended.\n"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Logger Ended.\n");
@ -142,9 +146,10 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel channel, char *file,
va_list ap; va_list ap;
FILE *handle; FILE *handle;
char *filep = switch_cut_path(file); char *filep = switch_cut_path(file);
char *content = NULL;
switch_time_t now = switch_time_now();
uint32_t len; uint32_t len;
const char *extra_fmt = "%s [%s] %s:%d %s() %s"; const char *extra_fmt = "%s [%s] %s:%d %s()%c%s";
va_start(ap, fmt); va_start(ap, fmt);
handle = switch_core_data_channel(channel); handle = switch_core_data_channel(channel);
@ -154,12 +159,12 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel channel, char *file,
switch_size_t retsize; switch_size_t retsize;
switch_time_exp_t tm; switch_time_exp_t tm;
switch_time_exp_lt(&tm, switch_time_now()); switch_time_exp_lt(&tm, now);
switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm); switch_strftime(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
len = (uint32_t)(strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(func) + strlen(fmt)); len = (uint32_t)(strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(func) + strlen(fmt));
new_fmt = malloc(len+1); new_fmt = malloc(len+1);
snprintf(new_fmt, len, extra_fmt, date, LEVELS[level], filep, line, func, fmt); snprintf(new_fmt, len, extra_fmt, date, LEVELS[level], filep, line, func, (char) 128, fmt);
fmt = new_fmt; fmt = new_fmt;
} }
@ -173,6 +178,13 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel channel, char *file,
if (ret == -1) { if (ret == -1) {
fprintf(stderr, "Memory Error\n"); fprintf(stderr, "Memory Error\n");
} else { } else {
if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) {
content = data;
} else {
content = strchr(data, (char)128);
}
if (channel == SWITCH_CHANNEL_ID_EVENT) { if (channel == SWITCH_CHANNEL_ID_EVENT) {
switch_event *event; switch_event *event;
if (switch_event_running() == SWITCH_STATUS_SUCCESS && switch_event_create(&event, SWITCH_EVENT_LOG) == SWITCH_STATUS_SUCCESS) { if (switch_event_running() == SWITCH_STATUS_SUCCESS && switch_event_create(&event, SWITCH_EVENT_LOG) == SWITCH_STATUS_SUCCESS) {
@ -190,7 +202,12 @@ SWITCH_DECLARE(void) switch_log_printf(switch_text_channel channel, char *file,
if (level >= MAX_LEVEL) { if (level >= MAX_LEVEL) {
switch_log_node *node = malloc(sizeof(*node)); switch_log_node *node = malloc(sizeof(*node));
node->data = data; node->data = data;
node->file = strdup(filep);
node->func = strdup(func);
node->line = line;
node->level = level; node->level = level;
node->content = content;
node->timestamp = now;
switch_queue_push(LOG_QUEUE, node); switch_queue_push(LOG_QUEUE, node);
} else { } else {
free(data); free(data);