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>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* switch_log.c -- Logging
|
|
|
|
*
|
|
|
|
*/
|
2008-01-27 17:42:51 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
#include <switch.h>
|
2007-10-03 23:43:01 +00:00
|
|
|
#include "private/switch_core_pvt.h"
|
2007-10-04 21:15:08 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
static const char *LEVELS[] = {
|
2007-10-04 17:25:06 +00:00
|
|
|
"CONSOLE",
|
2007-03-29 22:31:56 +00:00
|
|
|
"ALERT",
|
|
|
|
"CRIT",
|
|
|
|
"ERR",
|
|
|
|
"WARNING",
|
|
|
|
"NOTICE",
|
|
|
|
"INFO",
|
|
|
|
"DEBUG",
|
2006-04-12 17:51:47 +00:00
|
|
|
NULL
|
2006-04-11 21:13:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct switch_log_binding {
|
2006-04-29 23:43:28 +00:00
|
|
|
switch_log_function_t function;
|
|
|
|
switch_log_level_t level;
|
2008-07-11 14:35:08 +00:00
|
|
|
int is_console;
|
2006-04-11 21:13:44 +00:00
|
|
|
struct switch_log_binding *next;
|
|
|
|
};
|
|
|
|
|
2006-05-10 03:23:05 +00:00
|
|
|
typedef struct switch_log_binding switch_log_binding_t;
|
2006-04-11 21:13:44 +00:00
|
|
|
|
2006-04-29 01:00:52 +00:00
|
|
|
static switch_memory_pool_t *LOG_POOL = NULL;
|
2006-05-10 03:23:05 +00:00
|
|
|
static switch_log_binding_t *BINDINGS = NULL;
|
2006-04-11 21:13:44 +00:00
|
|
|
static switch_mutex_t *BINDLOCK = NULL;
|
|
|
|
static switch_queue_t *LOG_QUEUE = NULL;
|
2007-09-30 20:05:18 +00:00
|
|
|
static switch_queue_t *LOG_RECYCLE_QUEUE = NULL;
|
2006-04-11 21:13:44 +00:00
|
|
|
static int8_t THREAD_RUNNING = 0;
|
|
|
|
static uint8_t MAX_LEVEL = 0;
|
2008-07-11 14:35:08 +00:00
|
|
|
static int mods_loaded = 0;
|
2008-07-15 14:56:30 +00:00
|
|
|
static int console_mods_loaded = 0;
|
2008-07-11 14:35:08 +00:00
|
|
|
static switch_bool_t COLORIZE = SWITCH_FALSE;
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
static HANDLE hStdout;
|
|
|
|
static WORD wOldColorAttrs;
|
|
|
|
static CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
2008-07-14 17:09:47 +00:00
|
|
|
|
|
|
|
static WORD
|
2008-07-11 14:35:08 +00:00
|
|
|
#else
|
2008-07-14 17:09:47 +00:00
|
|
|
static const char*
|
2008-07-11 14:35:08 +00:00
|
|
|
#endif
|
2008-07-22 15:59:06 +00:00
|
|
|
COLORS[] = { SWITCH_SEQ_DEFAULT_COLOR, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FMAGEN, SWITCH_SEQ_FCYAN, SWITCH_SEQ_FGREEN, SWITCH_SEQ_FYELLOW };
|
2006-04-11 21:13:44 +00:00
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
SWITCH_DECLARE(const char *) switch_log_level2str(switch_log_level_t level)
|
2006-04-12 17:51:47 +00:00
|
|
|
{
|
2007-10-04 17:25:06 +00:00
|
|
|
if (level > SWITCH_LOG_DEBUG) {
|
|
|
|
level = SWITCH_LOG_DEBUG;
|
|
|
|
}
|
2006-04-12 17:51:47 +00:00
|
|
|
return LEVELS[level];
|
|
|
|
}
|
|
|
|
|
2007-12-05 20:24:20 +00:00
|
|
|
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);
|
2007-12-10 19:16:50 +00:00
|
|
|
switch_log_level_t level;
|
2007-12-05 20:24:20 +00:00
|
|
|
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(p);
|
2007-12-05 20:24:20 +00:00
|
|
|
|
|
|
|
if ((argc = switch_separate_string(p, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
|
2007-12-11 21:31:57 +00:00
|
|
|
for (x = 0; x < argc && argv[x]; x++) {
|
2007-12-05 20:24:20 +00:00
|
|
|
if (!strcasecmp(argv[x], "all")) {
|
|
|
|
mask = 0xFF;
|
|
|
|
break;
|
|
|
|
} else {
|
2007-12-10 19:16:50 +00:00
|
|
|
level = switch_log_str2level(argv[x]);
|
|
|
|
if (level != SWITCH_LOG_INVALID) {
|
|
|
|
mask |= (1 << level);
|
|
|
|
}
|
2007-12-05 20:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(p);
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
SWITCH_DECLARE(switch_log_level_t) switch_log_str2level(const char *str)
|
2006-04-12 17:51:47 +00:00
|
|
|
{
|
|
|
|
int x = 0;
|
2007-12-10 19:16:50 +00:00
|
|
|
switch_log_level_t level = SWITCH_LOG_INVALID;
|
2007-10-04 17:25:06 +00:00
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
for (x = 0;; x++) {
|
2006-04-12 17:51:47 +00:00
|
|
|
if (!LEVELS[x]) {
|
|
|
|
break;
|
|
|
|
}
|
2008-12-31 19:07:20 +00:00
|
|
|
|
2006-04-12 17:51:47 +00:00
|
|
|
if (!strcasecmp(LEVELS[x], str)) {
|
2006-04-29 23:43:28 +00:00
|
|
|
level = (switch_log_level_t) x;
|
2006-04-12 17:51:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
2008-07-11 14:35:08 +00:00
|
|
|
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;
|
2008-07-15 14:56:30 +00:00
|
|
|
mods_loaded--;
|
2008-07-11 14:35:08 +00:00
|
|
|
if (ptr->is_console) {
|
2008-07-15 14:56:30 +00:00
|
|
|
console_mods_loaded--;
|
2008-07-11 14:35:08 +00:00
|
|
|
}
|
|
|
|
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)
|
2006-04-11 21:13:44 +00:00
|
|
|
{
|
2006-05-10 03:23:05 +00:00
|
|
|
switch_log_binding_t *binding = NULL, *ptr = NULL;
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(function != NULL);
|
2006-04-11 21:13:44 +00:00
|
|
|
|
|
|
|
if (!(binding = switch_core_alloc(LOG_POOL, sizeof(*binding)))) {
|
|
|
|
return SWITCH_STATUS_MEMERR;
|
|
|
|
}
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
if ((uint8_t) level > MAX_LEVEL) {
|
2006-04-11 21:13:44 +00:00
|
|
|
MAX_LEVEL = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
binding->function = function;
|
|
|
|
binding->level = level;
|
2008-07-11 14:35:08 +00:00
|
|
|
binding->is_console = is_console;
|
2006-04-11 21:13:44 +00:00
|
|
|
|
|
|
|
switch_mutex_lock(BINDLOCK);
|
|
|
|
for (ptr = BINDINGS; ptr && ptr->next; ptr = ptr->next);
|
|
|
|
|
|
|
|
if (ptr) {
|
|
|
|
ptr->next = binding;
|
|
|
|
} else {
|
|
|
|
BINDINGS = binding;
|
|
|
|
}
|
2008-07-11 14:35:08 +00:00
|
|
|
if (is_console) {
|
2008-07-15 14:56:30 +00:00
|
|
|
console_mods_loaded++;
|
2008-07-11 14:35:08 +00:00
|
|
|
}
|
2008-07-15 14:56:30 +00:00
|
|
|
mods_loaded++;
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_mutex_unlock(BINDLOCK);
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-27 04:30:03 +00:00
|
|
|
static void *SWITCH_THREAD_FUNC log_thread(switch_thread_t *thread, void *obj)
|
2006-04-11 21:13:44 +00:00
|
|
|
{
|
|
|
|
|
2007-05-25 20:39:13 +00:00
|
|
|
if (!obj) {
|
|
|
|
obj = NULL;
|
|
|
|
}
|
2006-04-11 21:13:44 +00:00
|
|
|
THREAD_RUNNING = 1;
|
|
|
|
|
2007-03-29 22:31:56 +00:00
|
|
|
while (THREAD_RUNNING == 1) {
|
2006-04-11 21:13:44 +00:00
|
|
|
void *pop = NULL;
|
2006-04-29 23:43:28 +00:00
|
|
|
switch_log_node_t *node = NULL;
|
2006-05-10 03:23:05 +00:00
|
|
|
switch_log_binding_t *binding;
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
if (switch_queue_pop(LOG_QUEUE, &pop) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
if (!pop) {
|
|
|
|
break;
|
|
|
|
}
|
2007-03-29 22:31:56 +00:00
|
|
|
|
2006-04-29 23:43:28 +00:00
|
|
|
node = (switch_log_node_t *) pop;
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_mutex_lock(BINDLOCK);
|
2007-03-29 22:31:56 +00:00
|
|
|
for (binding = BINDINGS; binding; binding = binding->next) {
|
2006-04-11 21:13:44 +00:00
|
|
|
if (binding->level >= node->level) {
|
2006-04-12 14:41:35 +00:00
|
|
|
binding->function(node, node->level);
|
2006-04-11 21:13:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
switch_mutex_unlock(BINDLOCK);
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2007-09-30 20:05:18 +00:00
|
|
|
switch_safe_free(node->data);
|
2007-10-04 17:25:06 +00:00
|
|
|
if (switch_queue_trypush(LOG_RECYCLE_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
free(node);
|
|
|
|
}
|
2006-04-11 21:13:44 +00:00
|
|
|
}
|
2006-04-26 19:11:49 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
THREAD_RUNNING = 0;
|
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Logger Ended.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-15 14:56:30 +00:00
|
|
|
#define do_mods (LOG_QUEUE && THREAD_RUNNING)
|
2007-03-29 22:31:56 +00:00
|
|
|
SWITCH_DECLARE(void) switch_log_printf(switch_text_channel_t channel, const char *file, const char *func, int line,
|
2007-06-20 09:19:45 +00:00
|
|
|
const char *userdata, switch_log_level_t level, const char *fmt, ...)
|
2006-04-11 21:13:44 +00:00
|
|
|
{
|
|
|
|
char *data = NULL;
|
|
|
|
char *new_fmt = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
va_list ap;
|
|
|
|
FILE *handle;
|
2007-03-29 22:31:56 +00:00
|
|
|
const char *filep = (file ? switch_cut_path(file) : "");
|
2006-11-10 16:30:02 +00:00
|
|
|
const char *funcp = (func ? func : "");
|
2006-04-12 14:41:35 +00:00
|
|
|
char *content = NULL;
|
2009-01-25 21:23:07 +00:00
|
|
|
switch_time_t now = switch_micro_time_now();
|
2006-04-11 21:13:44 +00:00
|
|
|
uint32_t len;
|
2006-04-12 14:41:35 +00:00
|
|
|
const char *extra_fmt = "%s [%s] %s:%d %s()%c%s";
|
2007-09-30 20:05:18 +00:00
|
|
|
|
2007-10-04 17:25:06 +00:00
|
|
|
if (level > runtime.hard_log_level) {
|
2007-10-03 23:43:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-14 14:55:03 +00:00
|
|
|
switch_assert(level < SWITCH_LOG_INVALID);
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
handle = switch_core_data_channel(channel);
|
|
|
|
|
|
|
|
if (channel != SWITCH_CHANNEL_ID_LOG_CLEAN) {
|
|
|
|
char date[80] = "";
|
|
|
|
switch_size_t retsize;
|
|
|
|
switch_time_exp_t tm;
|
|
|
|
|
2006-04-12 14:41:35 +00:00
|
|
|
switch_time_exp_lt(&tm, now);
|
2008-10-12 21:51:51 +00:00
|
|
|
switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
|
2007-03-29 22:31:56 +00:00
|
|
|
|
|
|
|
len = (uint32_t) (strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(funcp) + strlen(fmt));
|
|
|
|
new_fmt = malloc(len + 1);
|
2007-12-11 21:31:57 +00:00
|
|
|
switch_assert(new_fmt);
|
2007-12-12 21:53:32 +00:00
|
|
|
switch_snprintf(new_fmt, len, extra_fmt, date, switch_log_level2str(level), filep, line, funcp, 128, fmt);
|
2006-04-11 21:13:44 +00:00
|
|
|
fmt = new_fmt;
|
|
|
|
}
|
|
|
|
|
2007-03-09 20:44:13 +00:00
|
|
|
ret = switch_vasprintf(&data, fmt, ap);
|
2006-04-11 21:13:44 +00:00
|
|
|
va_end(ap);
|
2008-03-14 03:47:45 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "Memory Error\n");
|
2008-03-14 03:47:45 +00:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) {
|
|
|
|
content = data;
|
2006-04-11 21:13:44 +00:00
|
|
|
} else {
|
2008-03-14 03:47:45 +00:00
|
|
|
if ((content = strchr(data, 128))) {
|
|
|
|
*content = ' ';
|
|
|
|
}
|
|
|
|
}
|
2006-04-12 14:41:35 +00:00
|
|
|
|
2008-03-14 03:47:45 +00:00
|
|
|
if (channel == SWITCH_CHANNEL_ID_EVENT) {
|
|
|
|
switch_event_t *event;
|
|
|
|
if (switch_event_running() == SWITCH_STATUS_SUCCESS && switch_event_create(&event, SWITCH_EVENT_LOG) == SWITCH_STATUS_SUCCESS) {
|
2008-08-16 02:17:09 +00:00
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Data", data);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-File", filep);
|
|
|
|
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Function", funcp);
|
2008-03-14 03:47:45 +00:00
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Line", "%d", line);
|
|
|
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Level", "%d", (int) level);
|
|
|
|
switch_event_fire(&event);
|
|
|
|
data = NULL;
|
2006-04-12 14:41:35 +00:00
|
|
|
}
|
|
|
|
|
2008-03-14 03:47:45 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2008-07-15 14:56:30 +00:00
|
|
|
|
|
|
|
if (console_mods_loaded == 0 || !do_mods) {
|
2008-03-14 03:47:45 +00:00
|
|
|
if (handle) {
|
|
|
|
int aok = 1;
|
2008-01-25 23:09:33 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
|
2008-03-14 03:47:45 +00:00
|
|
|
fd_set can_write;
|
|
|
|
int fd;
|
|
|
|
struct timeval to;
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2008-03-14 03:47:45 +00:00
|
|
|
fd = fileno(handle);
|
|
|
|
memset(&to, 0, sizeof(to));
|
|
|
|
FD_SET(fd, &can_write);
|
|
|
|
to.tv_sec = 0;
|
2008-05-24 05:09:50 +00:00
|
|
|
to.tv_usec = 100000;
|
2008-05-27 04:30:03 +00:00
|
|
|
if (select(fd + 1, NULL, &can_write, NULL, &to) > 0) {
|
2008-03-14 03:47:45 +00:00
|
|
|
aok = FD_ISSET(fd, &can_write);
|
|
|
|
} else {
|
|
|
|
aok = 0;
|
|
|
|
}
|
2008-01-25 23:09:33 +00:00
|
|
|
#endif
|
2008-03-14 03:47:45 +00:00
|
|
|
if (aok) {
|
2008-07-11 14:35:08 +00:00
|
|
|
if (COLORIZE) {
|
2008-07-14 13:44:25 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2008-07-14 14:55:03 +00:00
|
|
|
SetConsoleTextAttribute(hStdout, COLORS[level]);
|
|
|
|
WriteFile(hStdout, data, (DWORD) strlen(data), NULL, NULL);
|
2008-07-14 13:44:25 +00:00
|
|
|
SetConsoleTextAttribute(hStdout, wOldColorAttrs);
|
|
|
|
#else
|
2008-07-11 14:35:08 +00:00
|
|
|
fprintf(handle, "%s%s%s", COLORS[level], data, SWITCH_SEQ_DEFAULT_COLOR);
|
2008-07-14 13:44:25 +00:00
|
|
|
#endif
|
2008-07-11 14:35:08 +00:00
|
|
|
} else {
|
|
|
|
fprintf(handle, "%s", data);
|
|
|
|
}
|
2008-03-14 03:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-15 14:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (do_mods && level <= MAX_LEVEL) {
|
2008-03-14 03:47:45 +00:00
|
|
|
switch_log_node_t *node;
|
|
|
|
void *pop = NULL;
|
2007-09-30 20:05:18 +00:00
|
|
|
|
2008-03-14 03:47:45 +00:00
|
|
|
if (switch_queue_trypop(LOG_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
node = (switch_log_node_t *) pop;
|
|
|
|
} else {
|
|
|
|
node = malloc(sizeof(*node));
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_assert(node);
|
2008-03-14 03:47:45 +00:00
|
|
|
}
|
2008-12-20 14:56:08 +00:00
|
|
|
|
2008-03-14 03:47:45 +00:00
|
|
|
node->data = data;
|
|
|
|
data = NULL;
|
|
|
|
switch_set_string(node->file, filep);
|
|
|
|
switch_set_string(node->func, funcp);
|
|
|
|
node->line = line;
|
|
|
|
node->level = level;
|
|
|
|
node->content = content;
|
|
|
|
node->timestamp = now;
|
2008-12-20 14:56:08 +00:00
|
|
|
node->channel = channel;
|
2008-05-24 05:09:50 +00:00
|
|
|
|
2008-03-14 03:47:45 +00:00
|
|
|
if (switch_queue_trypush(LOG_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
free(node->data);
|
|
|
|
if (switch_queue_trypush(LOG_RECYCLE_QUEUE, node) != SWITCH_STATUS_SUCCESS) {
|
|
|
|
free(node);
|
2007-03-29 22:31:56 +00:00
|
|
|
}
|
2008-03-14 03:47:45 +00:00
|
|
|
node = NULL;
|
2006-04-11 21:13:44 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-27 04:30:03 +00:00
|
|
|
|
|
|
|
end:
|
2006-04-11 21:13:44 +00:00
|
|
|
|
2008-03-14 03:47:45 +00:00
|
|
|
switch_safe_free(data);
|
2007-09-30 20:05:18 +00:00
|
|
|
switch_safe_free(new_fmt);
|
2008-12-19 17:12:27 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
}
|
|
|
|
|
2008-07-11 14:35:08 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_log_init(switch_memory_pool_t *pool, switch_bool_t colorize)
|
2006-04-11 21:13:44 +00:00
|
|
|
{
|
2006-04-29 01:00:52 +00:00
|
|
|
switch_thread_t *thread;
|
2006-04-11 21:41:00 +00:00
|
|
|
switch_threadattr_t *thd_attr;;
|
2008-07-11 14:35:08 +00:00
|
|
|
|
2007-12-11 19:23:57 +00:00
|
|
|
switch_assert(pool != NULL);
|
2006-04-11 21:13:44 +00:00
|
|
|
|
|
|
|
LOG_POOL = pool;
|
|
|
|
|
|
|
|
switch_threadattr_create(&thd_attr, LOG_POOL);
|
|
|
|
switch_threadattr_detach_set(thd_attr, 1);
|
|
|
|
|
|
|
|
|
2006-06-05 17:37:24 +00:00
|
|
|
switch_queue_create(&LOG_QUEUE, SWITCH_CORE_QUEUE_LEN, LOG_POOL);
|
2007-09-30 20:05:18 +00:00
|
|
|
switch_queue_create(&LOG_RECYCLE_QUEUE, SWITCH_CORE_QUEUE_LEN, LOG_POOL);
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_mutex_init(&BINDLOCK, SWITCH_MUTEX_NESTED, LOG_POOL);
|
2006-04-26 17:18:33 +00:00
|
|
|
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
|
2008-10-15 23:13:36 +00:00
|
|
|
switch_threadattr_detach_set(thd_attr, 1);
|
2006-04-11 21:13:44 +00:00
|
|
|
switch_thread_create(&thread, thd_attr, log_thread, NULL, LOG_POOL);
|
|
|
|
|
|
|
|
while (!THREAD_RUNNING) {
|
2008-11-14 23:31:21 +00:00
|
|
|
switch_cond_next();
|
2006-04-11 21:13:44 +00:00
|
|
|
}
|
2008-07-11 14:35:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2007-10-04 17:25:06 +00:00
|
|
|
SWITCH_DECLARE(void) switch_core_memory_reclaim_logger(void)
|
2006-04-11 21:13:44 +00:00
|
|
|
{
|
2007-09-30 20:05:18 +00:00
|
|
|
void *pop;
|
2007-10-04 20:22:37 +00:00
|
|
|
int size = switch_queue_size(LOG_RECYCLE_QUEUE);
|
2008-05-27 04:30:03 +00:00
|
|
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Returning %d recycled log node(s) %d bytes\n", size,
|
|
|
|
(int) sizeof(switch_log_node_t) * size);
|
2007-09-30 20:05:18 +00:00
|
|
|
while (switch_queue_trypop(LOG_RECYCLE_QUEUE, &pop) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
free(pop);
|
|
|
|
}
|
2007-10-04 17:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_log_shutdown(void)
|
|
|
|
{
|
2007-09-30 20:05:18 +00:00
|
|
|
|
2007-10-04 17:25:06 +00:00
|
|
|
THREAD_RUNNING = -1;
|
|
|
|
switch_queue_push(LOG_QUEUE, NULL);
|
|
|
|
while (THREAD_RUNNING) {
|
2008-11-14 23:31:21 +00:00
|
|
|
switch_cond_next();
|
2007-10-04 17:25:06 +00:00
|
|
|
}
|
|
|
|
switch_core_memory_reclaim_logger();
|
2008-05-27 04:30:03 +00:00
|
|
|
|
2006-04-11 21:13:44 +00:00
|
|
|
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:
|
2008-07-03 19:12:26 +00:00
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
|
2006-11-27 22:30:48 +00:00
|
|
|
*/
|