下面是源码
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- static void mp4api_dbprint_ttxgz(char* format,...)
- {
- va_list args;
- int fd;
- char string[1000];
- va_start(args,format);
- vsprintf(string,format,args);
- va_end(args);
- fd = open("/dev/kmsg",O_RDWR);
- if(fd == -1)
- {
- return;
- }
- write(fd,string,strlen(string)+1);
- close(fd);
- }
要注意的是,linux起来以后,需要在终端修改/dev/kmsg的权限为777
这样做的好处是,开发的时候当kernel连不上adb的时候,没办法通过ddms看Logcat打印,在终端看logcat打印又太麻烦,就可以用这种方式直接在串口添加打印。
但是这里的write可能是阻塞式,有可能让程序变慢。