[87711] Resolve the issue of log loss after reboot device
Change-Id: I9690d2a2c5e844e67ffa825e9febbd0c22388756
diff --git a/mbtk/mbtk_logd/common.c b/mbtk/mbtk_logd/common.c
index 5b2de92..56df064 100755
--- a/mbtk/mbtk_logd/common.c
+++ b/mbtk/mbtk_logd/common.c
@@ -190,70 +190,46 @@
*/
int get_rotate_file(int fd, char *base_file, struct file_list_t *_file_list)
{
- char _time[16] = {0};
- int num = 0;
- char tmp_buf[48] = {0};
- char *current_file = _file_list->file[_file_list->current];
- int len = strlen(base_file);
- char *old = malloc(len + 20);
- int rotate_file_count_max;
+ char old_name[32] = {0};
+ char new_name[32] = {0};
+
+ int rotate_file_count_max = 0;
+
if(_file_list->total == -1 || _file_list->total == 0 || _file_list->total > ROTATE_FILE_COUNT_MAX)
+ {
rotate_file_count_max = ROTATE_FILE_COUNT_MAX;
+ }
else
+ {
rotate_file_count_max = _file_list->total;
-
-
- if(0 == _file_list->current && NULL == current_file)
- _file_list->current = rotate_file_count_max - 1;
- if(NULL == old)
- {
- fprintf(stderr, "failed to malloc %s\n", strerror(errno));
- exit(-1);
}
- close(fd);
- sprintf(tmp_buf, "/tmp/log%s", strstr_tail(base_file, "/"));
- get_time(_time);
- sprintf(old, "%s.%s", base_file, _time);
- if (current_file && strstr(current_file, old)) {
- int tmp_len = strlen(current_file);
- // 以时间命名的文件是否已经存在
- if(current_file[tmp_len - 3] == '.'){
- num = atoi(¤t_file[tmp_len - 2]);
- num++;
- sprintf(old, "%s.%02d", old, num);
- }else{
- sprintf(old, "%s.%02d", old, num);
+
+
+ snprintf(new_name, sizeof(new_name), "%s.%d", base_file, rotate_file_count_max - 1);
+ remove(new_name);
+
+ for (int i = rotate_file_count_max - 2; i >= 0; i--)
+ {
+ if (i == 0)
+ {
+ snprintf(old_name, sizeof(old_name), "%s", base_file);
+ }
+ else
+ {
+ snprintf(old_name, sizeof(old_name), "%s.%d", base_file, i - 1);
}
- }
- printf("MBTK_LOGD: tmp_buf is %s, old is %s\n", tmp_buf, old);
- if (movefile(tmp_buf, old) != 0){
- printf("%s %d: Renamed error ,%s to %s.\n", __FUNCTION__, __LINE__, tmp_buf, old);
- }
- _file_list->current++;
- // ringbuf
- if(_file_list->current > (rotate_file_count_max - 1))
- {
- _file_list->current = 0;
- }
- // 如果之前已经存在,先释放之前存放的资源
- if(_file_list->file[_file_list->current])
- {
- // printf("%s %d: remove %s\n", __FUNCTION__, __LINE__, _file_list->file[_file_list->current]);
- // 如果 不限制文件数目,则不删除文件
- if(_file_list->total != -1)
- unlink(_file_list->file[_file_list->current]);
- free(_file_list->file[_file_list->current]);
- }
- // 将文件名保存下来
- _file_list->file[_file_list->current] = strdup(old);
- free(old);
- int fd_new = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
- if (fd_new < 0) {
- fprintf(stderr, "failed to open %s: %s\n", tmp_buf, strerror(errno));
- exit(-1);
+ snprintf(new_name, sizeof(new_name), "%s.%d", base_file, i);
+ rename(old_name, new_name);
}
+ close(fd);
+ int fd_new = open(base_file, O_CREAT | O_WRONLY | O_APPEND, 0600);
+ if (fd_new < 0)
+ {
+ fprintf(stderr, "failed to open %s: %s\n", base_file, strerror(errno));
+ exit(-1);
+ }
return fd_new;
}