Esempio n. 1
0
static axis2_status_t
axutil_log_impl_rotate(
    axutil_log_t *log)
{
    long size = -1;
    FILE *old_log_fd = NULL;
    axis2_char_t old_log_file_name[AXUTIL_LOG_FILE_NAME_SIZE];
    axutil_log_impl_t *log_impl = AXUTIL_INTF_TO_IMPL(log);

    /*If the log stream is a file*/
    if(log_impl->stream_type == AXUTIL_LOG_FILE)
    {
        if(log_impl->file_name)
            size = axutil_file_handler_size(log_impl->file_name);

        if(size >= log->size)
        {
            AXIS2_SNPRINTF(old_log_file_name, AXUTIL_LOG_FILE_NAME_SIZE, "%s%s", log_impl->file_name,
                ".old");
            axutil_file_handler_close(log_impl->stream);
            old_log_fd = axutil_file_handler_open(old_log_file_name, "w+");
            log_impl->stream = axutil_file_handler_open(log_impl->file_name, "r");
            if(old_log_fd && log_impl->stream)
            {
                axutil_file_handler_copy(log_impl->stream, old_log_fd);
                axutil_file_handler_close(old_log_fd);
                axutil_file_handler_close(log_impl->stream);
                old_log_fd = NULL;
                log_impl->stream = NULL;
            }
            if(old_log_fd)
            {
                axutil_file_handler_close(old_log_fd);
            }
            if(log_impl->stream)
            {
                axutil_file_handler_close(log_impl->stream);
            }
            log_impl->stream = axutil_file_handler_open(log_impl->file_name, "w+");
        }
    }
    return AXIS2_SUCCESS;
}
Esempio n. 2
0
AXIS2_EXTERN axutil_log_t *AXIS2_CALL
axutil_log_create(
    axutil_allocator_t *allocator,
    axutil_log_ops_t *ops,
    const axis2_char_t *stream_name)
{
    axutil_log_impl_t *log_impl;
    axis2_char_t *path_home;
    axis2_char_t log_file_name[AXUTIL_LOG_FILE_NAME_SIZE];
    axis2_char_t log_dir[AXUTIL_LOG_FILE_NAME_SIZE];
    axis2_char_t tmp_filename[AXUTIL_LOG_FILE_NAME_SIZE];

    if (!allocator)
        return NULL;

    log_impl = (axutil_log_impl_t *) AXIS2_MALLOC(allocator,
                   sizeof(axutil_log_impl_t));

    if (!log_impl)
        return NULL;

    log_impl->mutex =
        axutil_thread_mutex_create(allocator, AXIS2_THREAD_MUTEX_DEFAULT);

    if (!log_impl->mutex)
    {
        fprintf(stderr, "cannot create log mutex \n");
        return NULL;
    }

#ifndef WIN32
    signal(SIGXFSZ, SIG_IGN);
#endif

    /* default log file is axis2.log */
    if (stream_name)
        AXIS2_SNPRINTF(tmp_filename, AXUTIL_LOG_FILE_NAME_SIZE, "%s", stream_name);
    else
        AXIS2_SNPRINTF(tmp_filename, AXUTIL_LOG_FILE_NAME_SIZE, "%s", "axis2.log");

    /* we write all logs to AXIS2C_HOME/logs if it is set otherwise
     * to the working dir
     */
    if (stream_name && !(axutil_rindex(stream_name, AXIS2_PATH_SEP_CHAR)))
    {
        path_home = AXIS2_GETENV("AXIS2C_HOME");
        if (path_home)
        {
            AXIS2_SNPRINTF(log_dir, AXUTIL_LOG_FILE_NAME_SIZE, "%s%c%s", 
                path_home, AXIS2_PATH_SEP_CHAR, "logs");
            if (AXIS2_SUCCESS ==
                axutil_file_handler_access(log_dir, AXIS2_F_OK))
            {
                AXIS2_SNPRINTF(log_file_name, AXUTIL_LOG_FILE_NAME_SIZE, 
                    "%s%c%s", log_dir, AXIS2_PATH_SEP_CHAR, tmp_filename);
            }
            else
            {
                fprintf(stderr, "log folder %s does not exist - log file %s "\
                    "is written to . dir\n", log_dir, tmp_filename);
                AXIS2_SNPRINTF(log_file_name, AXUTIL_LOG_FILE_NAME_SIZE, "%s", 
                    tmp_filename);
            }
        }
        else
        {
            fprintf(stderr,
                "AXIS2C_HOME is not set - log is written to . dir\n");
            AXIS2_SNPRINTF(log_file_name, AXUTIL_LOG_FILE_NAME_SIZE, "%s", 
                tmp_filename);
        }
    }
    else
    {
        AXIS2_SNPRINTF(log_file_name, AXUTIL_LOG_FILE_NAME_SIZE, "%s", 
            tmp_filename);
    }
    log_impl->file_name = AXIS2_MALLOC(allocator, AXUTIL_LOG_FILE_NAME_SIZE);
    log_impl->log.size = AXUTIL_LOG_FILE_SIZE;
    sprintf(log_impl->file_name, "%s", log_file_name);

    axutil_thread_mutex_lock(log_impl->mutex);

    log_impl->stream = axutil_file_handler_open(log_file_name, "a+");
    axutil_log_impl_rotate((axutil_log_t *) log_impl);

    axutil_thread_mutex_unlock(log_impl->mutex);

    if (!log_impl->stream)
        log_impl->stream = stderr;

    /* by default, log is enabled */
    log_impl->log.enabled = 1;
    log_impl->log.level = AXIS2_LOG_LEVEL_DEBUG;

    if (ops)
    {
        log_impl->log.ops = ops;
    }
    else
    {
        log_impl->log.ops = &axutil_log_ops_var;
    }

    return &(log_impl->log);
}