static switch_status_t http_stream_write(switch_stream_handle_t *handle, const char *fmt, ...) { TSession *r = (TSession *) handle->data; int ret = 1; char *data; switch_event_t *evnt = handle->param_event; va_list ap; va_start(ap, fmt); ret = switch_vasprintf(&data, fmt, ap); va_end(ap); if (data) { /* Stream Content-Type (http header) to the xmlrpc (web) client, if fs api command did not do it yet. */ /* If (Content-Type in event) then the header was already replied. */ /* If fs api command is not "web aware", this will set the Content-Type to "text/plain". */ const char *http_refresh = NULL; const char *ct = NULL; const char *refresh = NULL; if (evnt && !(ct = switch_event_get_header(evnt, "Content-Type"))){ const char *val = switch_stristr("Content-Type", data); if (!val) { val = "Content-Type: text/plain\r\n\r\n"; ret = HTTPWrite(r, val, (uint32_t) strlen(val)); } /* flag to prevent running this more than once per http reply */ switch_event_add_header_string(evnt, SWITCH_STACK_BOTTOM, "Content-Type", strstr(val,":")+2); ct = switch_event_get_header(evnt, "Content-Type"); } if (ret) { ret = HTTPWrite(r, data, (uint32_t) strlen(data)); } switch_safe_free(data); /* e.g. "http://www.cluecon.fs/api/show?calls &refresh=5" */ /* fs api command can set event header "HTTP-REFRESH" so that the web page will automagically refresh, if */ /* "refresh=xxx" was part of the http query kv pairs */ if (ret && ct && *ct && (http_refresh = switch_event_get_header(evnt, "HTTP-REFRESH")) && (refresh = switch_event_get_header(evnt, "refresh")) && !strstr("text/html", ct) && (atoi(refresh) > 0 )) { const char *query = switch_event_get_header(evnt, "HTTP-QUERY"); const char *uri = switch_event_get_header(evnt, "HTTP-URI"); if (uri && query && *uri && *query) { char *buf = switch_mprintf("<META HTTP-EQUIV=REFRESH CONTENT=\"%s; URL=%s?%s\">\n", refresh, uri, query); ret = HTTPWrite(r, buf, (uint32_t) strlen(buf)); switch_safe_free(buf); } } /* only one refresh meta header per reply */ if (http_refresh) { switch_event_del_header(evnt, "HTTP-REFRESH"); } } return ret ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS; }
SWITCH_DECLARE(void) switch_console_printf(switch_text_channel_t channel, const char *file, const char *func, int line, const char *fmt, ...) { char *data = NULL; int ret = 0; va_list ap; FILE *handle = switch_core_data_channel(channel); const char *filep = switch_cut_path(file); char date[80] = ""; switch_size_t retsize; switch_time_exp_t tm; switch_event_t *event; va_start(ap, fmt); ret = switch_vasprintf(&data, fmt, ap); va_end(ap); if (ret == -1) { fprintf(stderr, "Memory Error\n"); goto done; } if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) { fprintf(handle, "%s", data); goto done; } switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm); if (channel == SWITCH_CHANNEL_ID_LOG) { fprintf(handle, "[%d] %s %s:%d %s() %s", (int) getpid(), date, filep, line, func, data); goto done; } if (channel == SWITCH_CHANNEL_ID_EVENT && switch_event_running() == SWITCH_STATUS_SUCCESS && switch_event_create(&event, SWITCH_EVENT_LOG) == SWITCH_STATUS_SUCCESS) { 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", func); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Line", "%d", line); switch_event_fire(&event); } done: if (data) { free(data); } fflush(handle); }
SWITCH_DECLARE(int) switch_file_printf(switch_file_t *thefile, const char *format, ...) { va_list ap; int ret; char *data; va_start(ap, format); if ((ret = switch_vasprintf(&data, format, ap)) != -1) { switch_size_t bytes = strlen(data); switch_file_write(thefile, data, &bytes); free(data); } va_end(ap); return ret; }
static switch_status_t http_stream_write(switch_stream_handle_t *handle, const char *fmt, ...) { va_list ap; TSession *r = handle->data; int ret = 0; char *data; va_start(ap, fmt); ret = switch_vasprintf(&data, fmt, ap); va_end(ap); if (data) { ret = 0; HTTPWrite(r, data, (uint32_t) strlen(data)); free(data); } return ret ? SWITCH_STATUS_FALSE : SWITCH_STATUS_SUCCESS; }
static int sangoma_logger(int level, char *fmt, ...) { char *data; int ret = 0; va_list ap; va_start(ap, fmt); ret = switch_vasprintf(&data, fmt, ap); if (ret == -1) { return -1; } va_end(ap); switch (level) { case SNGTC_LOGLEVEL_DEBUG: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "%s\n", data); break; case SNGTC_LOGLEVEL_WARN: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "%s\n", data); break; case SNGTC_LOGLEVEL_INFO: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s\n", data); break; case SNGTC_LOGLEVEL_STATS: break; case SNGTC_LOGLEVEL_ERROR: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s\n", data); break; case SNGTC_LOGLEVEL_CRIT: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "%s\n", data); break; default: switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unexpected msg with loglevel %d: %s\n", level, data); } free(data); return 0; }
SWITCH_DECLARE(void) switch_caller_extension_add_application_printf(switch_core_session_t *session, switch_caller_extension_t *caller_extension, const char *application_name, const char *fmt, ...) { va_list ap; char *data = NULL; va_start(ap, fmt); switch_vasprintf(&data, fmt, ap); va_end(ap); if (data) { char *p; if ((p = strstr(data, "\\'"))) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "App not added, Invalid character sequence in data string [%s]\n", data); free(data); return; } switch_caller_extension_add_application(session, caller_extension, application_name, data); free(data); } }
SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, const char *fmt, va_list ap) { char *data = NULL; char *new_fmt = NULL; int ret = 0; FILE *handle; const char *filep = (file ? switch_cut_path(file) : ""); const char *funcp = (func ? func : ""); char *content = NULL; switch_time_t now = switch_micro_time_now(); uint32_t len; #ifdef SWITCH_FUNC_IN_LOG const char *extra_fmt = "%s [%s] %s:%d %s()%c%s"; #else const char *extra_fmt = "%s [%s] %s:%d%c%s"; #endif switch_log_level_t limit_level = runtime.hard_log_level; if (channel == SWITCH_CHANNEL_ID_SESSION && userdata) { switch_core_session_t *session = (switch_core_session_t *) userdata; if (limit_level < session->loglevel) { limit_level = session->loglevel; } } if (level > 100) { if ((uint32_t) (level - 100) > runtime.debug_level) { return; } level = 7; } if (level > limit_level) { return; } switch_assert(level < SWITCH_LOG_INVALID); 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; switch_time_exp_lt(&tm, now); switch_snprintf(date, sizeof(date), "%0.4d-%0.2d-%0.2d %0.2d:%0.2d:%0.2d.%0.6d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec); //switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm); #ifdef SWITCH_FUNC_IN_LOG len = (uint32_t) (strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(funcp) + strlen(fmt)); #else len = (uint32_t) (strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(fmt)); #endif new_fmt = malloc(len + 1); switch_assert(new_fmt); #ifdef SWITCH_FUNC_IN_LOG switch_snprintf(new_fmt, len, extra_fmt, date, switch_log_level2str(level), filep, line, funcp, 128, fmt); #else switch_snprintf(new_fmt, len, extra_fmt, date, switch_log_level2str(level), filep, line, 128, fmt); #endif fmt = new_fmt; } ret = switch_vasprintf(&data, fmt, ap); if (ret == -1) { fprintf(stderr, "Memory Error\n"); goto end; } if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) { content = data; } else { if ((content = strchr(data, 128))) { *content = ' '; } } 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) { 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); 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); if (!zstr(userdata)) { switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "User-Data", userdata); } switch_event_fire(&event); data = NULL; } goto end; } if (console_mods_loaded == 0 || !do_mods) { if (handle) { int aok = 1; #ifndef WIN32 fd_set can_write; int fd; struct timeval to; fd = fileno(handle); memset(&to, 0, sizeof(to)); FD_ZERO(&can_write); FD_SET(fd, &can_write); to.tv_sec = 0; to.tv_usec = 100000; if (select(fd + 1, NULL, &can_write, NULL, &to) > 0) { aok = FD_ISSET(fd, &can_write); } else { aok = 0; } #endif if (aok) { if (COLORIZE) { #ifdef WIN32 SetConsoleTextAttribute(hStdout, COLORS[level]); WriteFile(hStdout, data, (DWORD) strlen(data), NULL, NULL); SetConsoleTextAttribute(hStdout, wOldColorAttrs); #else fprintf(handle, "%s%s%s", COLORS[level], data, SWITCH_SEQ_DEFAULT_COLOR); #endif } else { fprintf(handle, "%s", data); } } } } if (do_mods && level <= MAX_LEVEL) { switch_log_node_t *node = switch_log_node_alloc(); 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; node->channel = channel; if (channel == SWITCH_CHANNEL_ID_SESSION) { node->userdata = userdata ? strdup(switch_core_session_get_uuid((switch_core_session_t *) userdata)) : NULL; } else { node->userdata = !zstr(userdata) ? strdup(userdata) : NULL; } if (switch_queue_trypush(LOG_QUEUE, node) != SWITCH_STATUS_SUCCESS) { switch_log_node_free(&node); } } end: switch_safe_free(data); switch_safe_free(new_fmt); }