Fix size rotating, so filenames are in the order of newest to oldest
This commit is contained in:
+24
-17
@@ -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,9 +424,11 @@ int uc_log_reopen_files(void)
|
||||
|
||||
if (dest->dest_type == UC_LDEST_FILE) {
|
||||
rc = uc_log_reopen_file(dest);
|
||||
if (rc == 0) {
|
||||
dest->type.file.size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&g_log_lock);
|
||||
|
||||
@@ -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;
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
//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--;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user