现在的位置: 首页 > 综合 > 正文

dmesg打印信息如何后台保存到文件中

2013年11月22日 ⁄ 综合 ⁄ 共 3064字 ⁄ 字号 评论关闭
         dmesg调试i信息常常作为判断系统异常退出的重要信息,但是当个系统异常退出或重启时,信息又难以保存下来,于是才有了将dmesg调试信息保存到文件中的想法:
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <time.h>  
  6. //#include <linux/autoconf.h>//内核编译的配置信息  
  7. #include <sys/klog.h>  
  8. #include <sys/stat.h>  
  9. //#define __LOG_BUF_LEN   (1 << CONFIG_LOG_BUF_SHIFT)//在2.6.28内核中为默认1<<17,这才是真正dmesg buffer的大小,网上其他都扯淡。  
  10. #define __LOG_BUF_LEN   (1 << 17)//在2.6.28内核中为默认1<<17,这才是真正dmesg buffer的大小,网上其他都扯淡。  
  11. #define __LOG_PATH      "/home/default/dmesg.log"  
  12. #define LOG_SLEEP(x)    (sleep(x))  
  13. #define __LOG_SIZE      10485760//大于10M时删除文件  
  14. #define BUF_SIZE        256  
  15.   
  16.   
  17. long check_log_size(void)  
  18. {  
  19.     struct stat f_stat;  
  20.     if( stat( __LOG_PATH, &f_stat ) == -1 )  
  21.     {  
  22.         return -1;  
  23.     }  
  24.     return (long)f_stat.st_size;  
  25. }  
  26.   
  27. int main(int argc, char *argv[])  
  28. {  
  29.     char buf[__LOG_BUF_LEN]={0,};  
  30.     char tmpbuf[BUF_SIZE]={0,};  
  31.     int ret = 0;  
  32.     FILE *fp =NULL;  
  33.     struct tm *ptr;  
  34.     time_t lt;  
  35.     //daemon(0,0);//进入守护模式  
  36.     while(1)  
  37.     {  
  38.         LOG_SLEEP(120);//sleep 10 秒  
  39.         fp = fopen(__LOG_PATH,"a+");//追加打开  
  40.         if(NULL == fp)  
  41.         {  
  42.             printf("creat file faild !\n");  
  43.             continue;  
  44.         }  
  45.         ret = klogctl(4,buf,__LOG_BUF_LEN);//获得dmesg信息,该函数需要超级用户权限运行  
  46.         if(0 >= ret){  
  47.             perror("klogctl ");  
  48.             fclose(fp);  
  49.             continue;  
  50.         }  
  51.         lt = time(NULL);//获得时间  
  52.         ptr = (struct tm *)localtime(<);  
  53.         sprintf(tmpbuf,"       [LOG TIME:]       %s",asctime(ptr));//记录时间  
  54.         printf("tmpbuf = %s\n",tmpbuf);  
  55.         fwrite(tmpbuf,strlen(tmpbuf),1,fp);  
  56.         fwrite(buf,strlen(buf),1,fp);  
  57.         fflush(fp);  
  58.         fclose(fp);  
  59.         if(__LOG_SIZE < check_log_size())  
  60.         {  
  61.             unlink(__LOG_PATH);//删除该文件  
  62.         }  
  63.         memset(tmpbuf,0,BUF_SIZE);  
  64.         memset(buf,0,__LOG_BUF_LEN);  
  65.     }  
  66.     return 0;  
  67. }  

以下是Makefil

  1. CC := arm-none-linux-gnueabi-gcc  
  2. all:dmesg_test  
  3.   
  4. dmesg_test:dmesg_test.c  
  5.     $(CC) dmesg_test.c -o dmesg_test -Wall  
  6.   
  7. install:  
  8.     cp dmesg_test /nfsroot/update  
  9.   
  10. clean:  
  11.     rm dmesg_test  

运行:dmesg_test &

程序就进入后台执行了,每隔120秒会读取一次dmesg信息。

  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <stdlib.h>  
  4. #include <string.h>  
  5. #include <time.h>  
  6. //#include <linux/autoconf.h>//内核编译的配置信息  
  7. #include <sys/klog.h>  
  8. #include <sys/stat.h>  
  9. //#define __LOG_BUF_LEN   (1 << CONFIG_LOG_BUF_SHIFT)//在2.6.28内核中为默认1<<17,这才是真正dmesg buffer的大小,网上其他都扯淡。  
  10. #define __LOG_BUF_LEN   (1 << 17)//在2.6.28内核中为默认1<<17,这才是真正dmesg buffer的大小,网上其他都扯淡。  
  11. #define __LOG_PATH      "/home/default/dmesg.log"  
  12. #define LOG_SLEEP(x)    (sleep(x))  
  13. #define __LOG_SIZE      10485760//大于10M时删除文件  
  14. #define BUF_SIZE        256  
  15.   
  16.   
  17. long check_log_size(void)  
  18. {  
  19.     struct stat f_stat;  
  20.     if( stat( __LOG_PATH, &f_stat ) == -1 )  
  21.     {  
  22.         return -1;  
  23.     }  
  24.     return (long)f_stat.st_size;  
  25. }  
  26.   
  27. int main(int argc, char *argv[])  
  28. {  
  29.     char buf[__LOG_BUF_LEN]={0,};  
  30.     char tmpbuf[BUF_SIZE]={0,};  

抱歉!评论已关闭.