保存dmesg 信息到文件源码

#include #include #include #include #include //#include //内核编译的配置信息 #include #include //#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; }

你可能感兴趣的:(linux开发技术)