dmesg调试i信息常常作为判断系统异常退出的重要信息,但是当个系统异常退出或重启时,信息又难以保存下来,于是才有了将dmesg调试信息保存到文件中的想法:
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- //#include <linux/autoconf.h>//内核编译的配置信息
- #include <sys/klog.h>
- #include <sys/stat.h>
- //#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)//在2.6.28内核中为默认1<<17,这才是真正dmesg buffer的大小,网上其他都扯淡。
- #define __LOG_BUF_LEN (1 << 17)//在2.6.28内核中为默认1<<17,这才是真正dmesg buffer的大小,网上其他都扯淡。
- #define __LOG_PATH "/home/default/dmesg.log"
- #define LOG_SLEEP(x) (sleep(x))
- #define __LOG_SIZE 10485760//大于10M时删除文件
- #define BUF_SIZE 256
- long check_log_size(void)
- {
- struct stat f_stat;
- if( stat( __LOG_PATH, &f_stat ) == -1 )
- {
- return -1;
- }
- return (long)f_stat.st_size;
- }
- int main(int argc, char *argv[])
- {
- char buf[__LOG_BUF_LEN]={0,};
- char tmpbuf[BUF_SIZE]={0,};
- int ret = 0;
- FILE *fp =NULL;
- struct tm *ptr;
- time_t lt;
- //daemon(0,0);//进入守护模式
- while(1)
- {
- LOG_SLEEP(120);//sleep 10 秒
- fp = fopen(__LOG_PATH,"a+");//追加打开
- if(NULL == fp)
- {
- printf("creat file faild !\n");
- continue;
- }
- ret = klogctl(4,buf,__LOG_BUF_LEN);//获得dmesg信息,该函数需要超级用户权限运行
- if(0 >= ret){
- perror("klogctl ");
- fclose(fp);
- continue;
- }
- lt = time(NULL);//获得时间
- ptr = (struct tm *)localtime(<);
- sprintf(tmpbuf," [LOG TIME:] %s",asctime(ptr));//记录时间
- printf("tmpbuf = %s\n",tmpbuf);
- fwrite(tmpbuf,strlen(tmpbuf),1,fp);
- fwrite(buf,strlen(buf),1,fp);
- fflush(fp);
- fclose(fp);
- if(__LOG_SIZE < check_log_size())
- {
- unlink(__LOG_PATH);//删除该文件
- }
- memset(tmpbuf,0,BUF_SIZE);
- memset(buf,0,__LOG_BUF_LEN);
- }
- return 0;
- }
以下是Makefil
- CC := arm-none-linux-gnueabi-gcc
- all:dmesg_test
- dmesg_test:dmesg_test.c
- $(CC) dmesg_test.c -o dmesg_test -Wall
- install:
- cp dmesg_test /nfsroot/update
- clean:
- rm dmesg_test
运行:dmesg_test &
程序就进入后台执行了,每隔120秒会读取一次dmesg信息。
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- //#include <linux/autoconf.h>//内核编译的配置信息
- #include <sys/klog.h>
- #include <sys/stat.h>
- //#define __LOG_BUF_LEN (1 << CONFIG_LOG_BUF_SHIFT)//在2.6.28内核中为默认1<<17,这才是真正dmesg buffer的大小,网上其他都扯淡。
- #define __LOG_BUF_LEN (1 << 17)//在2.6.28内核中为默认1<<17,这才是真正dmesg buffer的大小,网上其他都扯淡。
- #define __LOG_PATH "/home/default/dmesg.log"
- #define LOG_SLEEP(x) (sleep(x))
- #define __LOG_SIZE 10485760//大于10M时删除文件
- #define BUF_SIZE 256
- long check_log_size(void)
- {
- struct stat f_stat;
- if( stat( __LOG_PATH, &f_stat ) == -1 )
- {
- return -1;
- }
- return (long)f_stat.st_size;
- }
- int main(int argc, char *argv[])
- {
- char buf[__LOG_BUF_LEN]={0,};
- char tmpbuf[BUF_SIZE]={0,};