[87711] Resolve the issue of log loss after reboot device
Change-Id: I9690d2a2c5e844e67ffa825e9febbd0c22388756
diff --git a/mbtk/mbtk_logd/alog_read.c b/mbtk/mbtk_logd/alog_read.c
index 061516d..3f13ab6 100755
--- a/mbtk/mbtk_logd/alog_read.c
+++ b/mbtk/mbtk_logd/alog_read.c
@@ -44,6 +44,8 @@
static log_config_entry *config = NULL;
static char tmp_log[48] = {0};
+static int fd_radio = 0;
+
void hex_print(char* buf, int len)
{
int i;
@@ -186,7 +188,6 @@
}
int android_log_printLogLine(
- int fd,
struct file_list_t *_file_list,
const AndroidLogEntry *entry)
{
@@ -194,17 +195,30 @@
char timeBuf[32];
char defaultBuffer[512];
size_t totalLen;
- int fd_new = fd;
- struct stat s;
+
+ struct stat s;
char * ret = NULL;
-
- if (log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) {
- fd_new = get_rotate_file(fd_new, log_file, _file_list);
- if (fd_new < 0) {
- fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
- exit(-1);
+
+ if(access(tmp_log, W_OK) != 0)
+ {
+ fd_radio = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
+ if (fd_radio < 0)
+ {
+ fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
+ exit(-1);
}
- }
+
+ }
+
+ if (log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size))
+ {
+ fd_radio = get_rotate_file(fd_radio, log_file, _file_list);
+ if (fd_radio < 0)
+ {
+ fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
+ exit(-1);
+ }
+ }
if(fileter_log(entry->priority, entry->tag, config->filter_list))
{
@@ -219,7 +233,7 @@
totalLen = snprintf(defaultBuffer, sizeof(defaultBuffer),
"%s %c/%s (%d): %s\n", timeBuf, priChar, entry->tag, entry->pid, entry->message);
- ret = write(fd_new, defaultBuffer, totalLen);
+ ret = write(fd_radio, defaultBuffer, totalLen);
return ret;
}
@@ -257,38 +271,43 @@
strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
}
- if (config->ip && config->port) {
+ if (config->ip && config->port)
+ {
int port = atoi(config->port);
printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
log_fd = tcp_connect(config->ip, port);
- } else if (strlen(log_file) > 0) {
- sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
+ }
+ else if (strlen(log_file) > 0)
+ {
+ //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
+
+ snprintf(tmp_log,sizeof(tmp_log), "%s", log_file);
// 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
- log_fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
- if (log_fd < 0) {
- fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
- exit(-1);
- }
- } else {
+
+ }
+ else
+ {
log_fd = STDOUT_FILENO;
}
if(config->rotate_file_size)
+ {
log_size = config->rotate_file_size;
-
+ }
printf("android log start...\n");
- while (1) {
+ while (1)
+ {
ret = read(dev_fd, buf, sizeof(buf));
- if (ret < 0) {
+ if (ret < 0)
+ {
printf("read error\n");
break;
}
alog_process(buf, ret, &entry);
- android_log_printLogLine(log_fd, &file_list, &entry);
+ android_log_printLogLine(&file_list, &entry);
memset(buf, 0, sizeof(buf));
}
close(dev_fd);
- close(log_fd);
-
+
printf("%s exit \n", __FUNCTION__);
pthread_exit(NULL);
return NULL;
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;
}
diff --git a/mbtk/mbtk_logd/syslog_read.c b/mbtk/mbtk_logd/syslog_read.c
index 3bca900..db30d57 100755
--- a/mbtk/mbtk_logd/syslog_read.c
+++ b/mbtk/mbtk_logd/syslog_read.c
@@ -177,7 +177,11 @@
time_t t;
char *c, *m;
- sprintf(tmp_buf, "/tmp/log%s", strstr_tail(log_file, "/"));
+ //
+ //sprintf(tmp_buf, "/tmp/log%s", strstr_tail(log_file, "/"));
+
+
+ snprintf(tmp_buf,sizeof(tmp_buf), "%s", log_file);
if(access(tmp_buf, W_OK) != 0)
{
sender.fd = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
@@ -549,7 +553,11 @@
} else if (strlen(log_file) > 0) {
log_type = LOG_FILE;
// 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
- sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
+
+ //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
+
+ //直接将log文件存储在不会掉电丢失的路径
+ snprintf(tmp_log,sizeof(tmp_log), "%s",log_file);
sender.fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
if (sender.fd < 0) {
fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));