项目中分析log的小工具

快速摘出log中的关键信息

项目中出现问题后,需要通过log分析参数是否正确,往往log文件会非常大,并且各个模块的log信息混杂在一起,需要一点点的找出自己需要的关键信息,身为阿猿,当然要发挥工作的长处,就写了一个分析log的C程序,当然啦,非常的简陋,只是写了一个demon版本,要把这个想法做成一个可以动态指定参数和动态分析log的应用程序才行…

#include<stdio.h>
#include<string.h>

int main(int argc,char *argv[])
{
    FILE *fpold = NULL;
    FILE *fpnew = NULL;
    char buf[500];
    size_t ret;
    char *pret = NULL;
    int i = 0,count = 0;
    int nret ;
    int goon = 0;

    fpold = fopen(argv[1],"r");
    if(NULL == fpold)
    {
        printf("open %s fail,please check it!\n",argv[1]);
        return -1;
    }
    fpnew = fopen("./new.log","w+");
    if(NULL == fpnew)
    {
        printf("create new log file fail!\n");
        return -1;
    }
    memset(buf,0,sizeof(buf));

    while((pret = fgets(buf,500,fpold)) != NULL)
    {
        //    printf("%d\n",strlen(buf));
            for(i = 0; i < strlen(buf);i++)
            {
                if(buf[i] == '&')
                {
                    count++;
                    if(count == 11)
                    {
                        if(0 == goon)
                        {
                            goon = 1;
                            continue;
                        }
                    }
                    else
                    {
                        goon = 0;
                    }
                }
                else
                {
                 //   printf("%d\n",count);
                    if(count == 11)
                    {
                        nret = fputs(buf,fpnew); 
                        if(nret == 0)
                        {
                            printf("write new file fail\n");
                            return -1;
                        }
                        goon = 0;
                        count = 0;
                        break;
                    }
                    count = 0;
                }
            }
         //   printf("%s\n",buf);
    }
    printf("file read end!\n");
    
    fclose(fpold);
    fclose(fpnew);

    return 0;
}
发布了53 篇原创文章 · 获赞 16 · 访问量 2213

猜你喜欢

转载自blog.csdn.net/m0_37757533/article/details/105490598