static apt_bool_t apt_log_file_dump(apt_log_file_data_t *file_data, const char *log_entry, apr_size_t size) { apr_thread_mutex_lock(file_data->mutex); file_data->cur_size += size; if(file_data->cur_size > file_data->max_size) { const char *log_file_path; /* close current log file */ fclose(file_data->file); /* roll over the next log file */ file_data->cur_file_index++; file_data->cur_file_index %= file_data->max_file_count; /* open log file */ log_file_path = apt_log_file_path_make(file_data); file_data->file = fopen(log_file_path,"wb"); if(!file_data->file) { return FALSE; } file_data->cur_size = size; } /* write to log file */ fwrite(log_entry,1,size,file_data->file); fflush(file_data->file); apr_thread_mutex_unlock(file_data->mutex); return TRUE; }
static apr_byte_t apt_log_file_exist(apt_log_file_data_t *file_data) { FILE* fp; const char *log_file_path; log_file_path = apt_log_file_path_make(file_data); fp = fopen(log_file_path,"rb"); if(!fp) return 0; fclose(fp); return 1; }
static apr_size_t apt_log_file_get_size(apt_log_file_data_t *file_data) { FILE* fp; const char *log_file_path; apr_size_t ret; log_file_path = apt_log_file_path_make(file_data); fp = fopen(log_file_path,"rb"); if(!fp) return 0; fseek(fp,0,SEEK_END); ret = ftell(fp); fclose(fp); return ret; }
APT_DECLARE(apt_bool_t) apt_log_file_open(const char *dir_path, const char *file_name, apr_size_t max_file_size, apr_size_t max_file_count, apr_pool_t *pool) { const char *log_file_path; apt_log_file_data_t *file_data; if(!apt_logger || !dir_path || !file_name) { return FALSE; } if(apt_logger->file_data) { return FALSE; } file_data = apr_palloc(pool,sizeof(apt_log_file_data_t)); file_data->log_dir_path = dir_path; file_data->log_file_name = file_name; file_data->cur_file_index = 0; file_data->cur_size = 0; file_data->max_file_count = max_file_count; file_data->max_size = max_file_size; file_data->mutex = NULL; file_data->pool = pool; if(!file_data->max_size) { file_data->max_file_count = MAX_LOG_FILE_SIZE; } if(!file_data->max_file_count) { file_data->max_file_count = MAX_LOG_FILE_COUNT; } /* create mutex */ if(apr_thread_mutex_create(&file_data->mutex,APR_THREAD_MUTEX_DEFAULT,pool) != APR_SUCCESS) { return FALSE; } /* open log file */ log_file_path = apt_log_file_path_make(file_data); file_data->file = fopen(log_file_path,"wb"); if(!file_data->file) { apr_thread_mutex_destroy(file_data->mutex); return FALSE; } apt_logger->file_data = file_data; return TRUE; }
APT_DECLARE(apt_bool_t) apt_log_file_open( const char *dir_path, const char *file_name, apr_size_t max_file_size, apr_size_t max_file_count, apt_bool_t append, apr_pool_t *pool) { const char *log_file_path; apt_log_file_data_t *file_data; if(!apt_logger || !dir_path || !file_name) { return FALSE; } if(apt_logger->file_data) { return FALSE; } file_data = apr_palloc(pool,sizeof(apt_log_file_data_t)); file_data->log_dir_path = apr_pstrdup(pool,dir_path); file_data->log_file_name = apr_pstrdup(pool,file_name); file_data->cur_file_index = 0; file_data->cur_size = 0; file_data->max_file_count = max_file_count; file_data->max_size = max_file_size; file_data->append = append; file_data->mutex = NULL; file_data->pool = pool; if(!file_data->max_size) { file_data->max_size = MAX_LOG_FILE_SIZE; } if(!file_data->max_file_count) { file_data->max_file_count = MAX_LOG_FILE_COUNT; } if(file_data->append == TRUE) { /* iteratively find the last created file */ while(file_data->cur_file_index<file_data->max_file_count) { if(apt_log_file_exist(file_data) == 0) { if(file_data->cur_file_index > 0) file_data->cur_file_index--; file_data->cur_size = apt_log_file_get_size(file_data); break; } file_data->cur_file_index++; } /* if all the files have been created start rewriting from beginning */ if(file_data->cur_file_index>=file_data->max_file_count) { file_data->cur_file_index=0; file_data->cur_size=0; log_file_path = apt_log_file_path_make(file_data); file_data->file = fopen(log_file_path,"wb"); /* truncate the first file to zero length */ fclose(file_data->file); } } /* create mutex */ if(apr_thread_mutex_create(&file_data->mutex,APR_THREAD_MUTEX_DEFAULT,pool) != APR_SUCCESS) { return FALSE; } /* open log file */ log_file_path = apt_log_file_path_make(file_data); file_data->file = fopen(log_file_path,file_data->append == TRUE ? "ab" : "wb"); if(!file_data->file) { apr_thread_mutex_destroy(file_data->mutex); return FALSE; } apt_logger->file_data = file_data; return TRUE; }