增加将log信息存入/data/中的功能
1源码在system/extras/test/rec-kmsg/main.c
2修改system/extras/test/rec-kmsg/Android.mk
将LOCAL_MODULE_TAGS := optional
修改为LOCAL_MODULE_TAGS := optional eng
因为前者生成的文件rec-kmsg只存于out/target/product/ msm8660_surf/symbols/system中,加了eng之后,还存于out/target/product/ msm8660_surf/system中。
LOCAL_MODULE_TAGS用于指定编译android时形成的版本风格
3修改system/core/rootdir/init.rc
加入:
service rec-kmsg /system/xbin/rec-kmsg
class main
user root
则在系统启动时,自动运行rec-kmsg。
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <fcntl.h> 4 #include <linux/ioctl.h> 5 #include <time.h> 6 #include <linux/msm_audio.h> 7 #include <pthread.h> 8 #include <sys/ioctl.h> 9 #include <string.h> 10 #include <stdlib.h> 11 #include <unistd.h> 12 #include <stdint.h> 13 14 #define CAT_BUFSIZ (4096) 15 16 static void raw_cat(void) 17 { 18 time_t now_t = time(NULL); 19 struct tm now = *localtime(&now_t); 20 char date[32]; 21 char logcat[32]; 22 char command[256]; 23 static char *buf; 24 static char fb_buf[CAT_BUFSIZ]; 25 static size_t bsize; 26 ssize_t nr, nw, off; 27 int wfd, rfd; 28 29 /* Get day time as file name */ 30 strftime(date, 32, "/data/%Y%m%d-%H-%M-%S-kmsg", &now); 31 strftime(logcat, 32, "/data/%Y%m%d-%H-%M-%S-logcat", &now); 32 date[31] = 0; 33 logcat[31] = 0; 34 printf("%s\n", date); 35 snprintf(command, 256, "/system/bin/logcat -b main -b system -b events -b radio -v time *:v > %s&", logcat); 36 37 system(command); 38 39 /* Read fd */ 40 rfd = open("/proc/kmsg", O_RDONLY); 41 //printf("rfd = %d\n", rfd); 42 43 /* Write fd */ 44 wfd = open(date, O_RDWR|O_CREAT); 45 //wfd = open("/data/kmsg", O_RDWR|O_CREAT); 46 //printf("wfd = %d\n", wfd); 47 48 if (buf == NULL) { 49 buf = fb_buf; 50 bsize = CAT_BUFSIZ; 51 } 52 53 while ((nr = read(rfd, buf, bsize)) > 0) 54 for (off = 0; nr; nr -= nw, off += nw) 55 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0) 56 { 57 perror("write"); 58 exit(EXIT_FAILURE); 59 } 60 } 61 62 int main(int argc, char **argv) 63 { 64 raw_cat(); 65 return 0; 66 }
system/extras/tests/rec-kmsg/Android.mk
1 ifneq ($(TARGET_SIMULATOR),true) 2 ifeq ($(TARGET_PRODUCT),msm8660_surf) 3 LOCAL_PATH:= $(call my-dir) 4 include $(CLEAR_VARS) 5 LOCAL_SRC_FILES := \ 6 main.c 7 LOCAL_MODULE := rec-kmsg 8 LOCAL_MODULE_TAGS := optional 9 LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES) 10 include $(BUILD_EXECUTABLE) 11 endif # TARGET_PRODUCT == msm8660_surf 12 endif # TARGET_SIMULATOR != true ~