static void do_rotate(cdr_fd_t *fd)
{
	switch_time_exp_t tm;
	char date[80] = "";
	switch_size_t retsize;
	char *p;

	close(fd->fd);
	fd->fd = -1;

	if (globals.rotate) {
		switch_time_exp_lt(&tm, switch_micro_time_now());
		switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm);


		p = switch_mprintf("%s.%s", fd->path, date);
		assert(p);
		switch_file_rename(fd->path, p, globals.pool);
		free(p);
	}

	do_reopen(fd);

	if (fd->fd < 0) {
		switch_event_t *event;
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error opening %s\n", fd->path);
		if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
			switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Critical-Error", "Error opening cdr file %s\n", fd->path);
			switch_event_fire(&event);
		}
	} else {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s CDR logfile %s\n", globals.rotate ? "Rotated" : "Re-opened", fd->path);
	}

}
Ejemplo n.º 2
0
static void write_cdr(const char *path, const char *log_line)
{
	cdr_fd_t *fd = NULL;
	//unsigned int bytes_in, bytes_out;
	switch_size_t 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);
	}

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

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

	if (fd->bytes + bytes_out > UINT_MAX) {
		if(do_rotate(fd) != SWITCH_STATUS_SUCCESS ){
			goto end;
		}

	}

	/*	while ((bytes_in = write(fd->fd, log_line, 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;
	}
*/
	if(switch_file_write(fd->fd,log_line,&bytes_in) != SWITCH_STATUS_SUCCESS){
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Write error to file %s %d/%d\n", path, (int) bytes_in, (int) bytes_out);
	}
	

	fd->bytes += bytes_in;

  end:

	switch_mutex_unlock(fd->mutex);
}
Ejemplo n.º 3
0
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);
}
Ejemplo n.º 4
0
static void write_cdr(const char *path, const char *log_line)
{
	cdr_fd_t *fd = NULL;
	unsigned int bytes_in, bytes_out;

	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);
	}

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

	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);
	}

	if ((bytes_in = write(fd->fd, log_line, bytes_out)) != bytes_out) {
		switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Write error to file %s %d/%d\n", path, (int) bytes_in, (int) bytes_out);
	}

	fd->bytes += bytes_in;

  end:

	switch_mutex_unlock(fd->mutex);
}