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 8abf8068c3
commit 93a195f0e8
+27 -20
View File
@@ -43,7 +43,6 @@ struct UCLogDestination {
//this is the base file name (e.g. witout a date or count)
size_t size; //keeps track of no of bytes written
struct UCLogRotateSettings rotate;
unsigned rotate_count; //current num_file
} file;
} type;
};
@@ -249,7 +248,6 @@ struct UCLogDestination *uc_log_new_file(const char *filename, int log_level, in
dest->log_location = log_location;
dest->type.file.rotate.policy = UC_LOG_ROTATE_NONE;
dest->type.file.rotate_count = 0;
return dest;
}
@@ -426,7 +424,9 @@ int uc_log_reopen_files(void)
if (dest->dest_type == UC_LDEST_FILE) {
rc = uc_log_reopen_file(dest);
dest->type.file.size = 0;
if (rc == 0) {
dest->type.file.size = 0;
}
}
}
@@ -599,31 +599,38 @@ void uc_log_destination_set_mask(
//assumes the destination is a UC_LDEST_FILE
//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 old_filename[_POSIX_PATH_MAX * 2];
unsigned int i;
int rc;
int rotate_count;
if (dest->type.file.file_name == NULL) {
return 0;
}
rotate_count = ++dest->type.file.rotate_count;
rotate_count %= dest->type.file.rotate.size_settings.num_files;
snprintf(new_filename, sizeof new_filename, "%s.%d",
dest->type.file.file_name,
rotate_count);
rc = rename(dest->type.file.file_name, new_filename);
if (rc != -1) {
dest->type.file.rotate_count = rotate_count;
return;
}
uc_log_reopen_file(dest); //try to reopen the log file regardless if renaming failed
//rotate existing .num files
i = dest->type.file.rotate.size_settings.num_files -1;
while (i > 0) {
sprintf(new_filename, "%s.%d", dest->type.file.file_name, i);
sprintf(old_filename, "%s.%d", dest->type.file.file_name, i - 1);
rename(old_filename, new_filename);
i--;
}
return rc;
sprintf(new_filename, "%s.0", dest->type.file.file_name);
//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