[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;