static void spool_cdr(const char *path, const char *log_line)
{
    cdr_fd_t *fd = NULL;
    char *log_line_lf = NULL;
    unsigned int bytes_in, bytes_out;
    int loops = 0;

    if (!(fd = switch_core_hash_find(globals.fd_hash, path))) {
        fd = switch_core_alloc(globals.pool, sizeof(*fd));
        switch_assert(fd);
        memset(fd, 0, sizeof(*fd));
        fd->fd = -1;
        switch_mutex_init(&fd->mutex, SWITCH_MUTEX_NESTED, globals.pool);
        fd->path = switch_core_strdup(globals.pool, path);
        switch_core_hash_insert(globals.fd_hash, path, fd);
    }

    if (end_of(log_line) != '\n') {
        log_line_lf = switch_mprintf("%s\n", log_line);
    } else {
        switch_strdup(log_line_lf, log_line);
    }
    assert(log_line_lf);

    switch_mutex_lock(fd->mutex);
    bytes_out = (unsigned) strlen(log_line_lf);

    if (fd->fd < 0) {
        do_reopen(fd);
        if (fd->fd < 0) {
            switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error opening %s\n", path);
            goto end;
        }
    }

    if (fd->bytes + bytes_out > UINT_MAX) {
        do_rotate(fd);
    }

    while ((bytes_in = write(fd->fd, log_line_lf, bytes_out)) != bytes_out && ++loops < 10) {
        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Write error to file %s %d/%d\n", path, (int) bytes_in, (int) bytes_out);
        do_rotate(fd);
        switch_yield(250000);
    }

    if (bytes_in > 0) {
        fd->bytes += bytes_in;
    }

end:

    switch_mutex_unlock(fd->mutex);
    switch_safe_free(log_line_lf);
}
Exemple #2
0
SWITCH_DECLARE(char *) switch_pgsql_handle_get_error(switch_pgsql_handle_t *handle)
{
#ifdef SWITCH_HAVE_PGSQL
    char * err_str;
    if (!handle) {
        return NULL;
    };
    switch_strdup(err_str, PQerrorMessage(handle->con));
    return err_str;
#else
    return NULL;
#endif
}
static switch_status_t cdr_mongo_authenticate() {
	switch_status_t status = SWITCH_STATUS_SUCCESS;
	mongo_error_t db_status;
	char *ns_tmp, *ns_split[2];

	/* Split namespace db.collection into separate vars */
	switch_strdup(ns_tmp, globals.mongo_namespace);
	switch_separate_string(ns_tmp, '.', ns_split, 2);

	db_status = mongo_cmd_authenticate(globals.mongo_conn, ns_split[0], globals.mongo_username, globals.mongo_password);

	if (db_status != MONGO_OK) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mongo_cmd_authenticate: authentication failed\n");
		status = SWITCH_STATUS_FALSE;
	} else {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Successfully authenticated %s@%s\n", globals.mongo_username, ns_split[0]);
	}

	switch_safe_free(ns_tmp);
	return status;
}