Fix size rotating, so filenames are in the order of newest to oldest

This commit is contained in:
Nils O. Selåsdal
2014-04-23 22:38:04 +02:00
parent dc9e38b231
commit 10c0e37253
+24 -17
View File
@@ -43,7 +43,6 @@ struct UCLogDestination {
//this is the base file name (e.g. witout a date or count) //this is the base file name (e.g. witout a date or count)
size_t size; //keeps track of no of bytes written size_t size; //keeps track of no of bytes written
struct UCLogRotateSettings rotate; struct UCLogRotateSettings rotate;
unsigned rotate_count; //current num_file
} file; } file;
} type; } type;
}; };
@@ -249,7 +248,6 @@ struct UCLogDestination *uc_log_new_file(const char *filename, int log_level, in
dest->log_location = log_location; dest->log_location = log_location;
dest->type.file.rotate.policy = UC_LOG_ROTATE_NONE; dest->type.file.rotate.policy = UC_LOG_ROTATE_NONE;
dest->type.file.rotate_count = 0;
return dest; return dest;
} }
@@ -426,9 +424,11 @@ int uc_log_reopen_files(void)
if (dest->dest_type == UC_LDEST_FILE) { if (dest->dest_type == UC_LDEST_FILE) {
rc = uc_log_reopen_file(dest); rc = uc_log_reopen_file(dest);
if (rc == 0) {
dest->type.file.size = 0; dest->type.file.size = 0;
} }
} }
}
pthread_mutex_unlock(&g_log_lock); pthread_mutex_unlock(&g_log_lock);
@@ -599,31 +599,38 @@ void uc_log_destination_set_mask(
//assumes the destination is a UC_LDEST_FILE //assumes the destination is a UC_LDEST_FILE
//hold g_log_lock while calling this //hold g_log_lock while calling this
static int uc_log_file_rotate_size(struct UCLogDestination *dest) static void uc_log_file_rotate_size(struct UCLogDestination *dest)
{ {
char new_filename[_POSIX_PATH_MAX * 2]; char new_filename[_POSIX_PATH_MAX * 2];
char old_filename[_POSIX_PATH_MAX * 2];
unsigned int i;
int rc; int rc;
int rotate_count;
if (dest->type.file.file_name == NULL) { if (dest->type.file.file_name == NULL) {
return 0; return;
} }
rotate_count = ++dest->type.file.rotate_count; //rotate existing .num files
rotate_count %= dest->type.file.rotate.size_settings.num_files; i = dest->type.file.rotate.size_settings.num_files -1;
while (i > 0) {
snprintf(new_filename, sizeof new_filename, "%s.%d", sprintf(new_filename, "%s.%d", dest->type.file.file_name, i);
dest->type.file.file_name, sprintf(old_filename, "%s.%d", dest->type.file.file_name, i - 1);
rotate_count); rename(old_filename, new_filename);
i--;
rc = rename(dest->type.file.file_name, new_filename);
if (rc != -1) {
dest->type.file.rotate_count = rotate_count;
} }
uc_log_reopen_file(dest); //try to reopen the log file regardless if renaming failed sprintf(new_filename, "%s.0", dest->type.file.file_name);
return rc; //rename current file
rename(dest->type.file.file_name, new_filename);
//re-open current file
rc = uc_log_reopen_file(dest);
if (rc == 0) {
dest->type.file.size = 0;
}
return;
} }
//assumes the destination is a UC_LDEST_FILE //assumes the destination is a UC_LDEST_FILE