Gadget for analyzing log in the project

Quickly extract key information in the log

After a problem occurs in the project, you need to analyze whether the parameters are correct through the log. The log file is often very large, and the log information of each module is mixed together. You need to find out the key information you need a little bit. Taking advantage of my work, I wrote a C program that analyzes logs. Of course, it is very simple. I just wrote a demon version. It is necessary to make this idea into an application that can dynamically specify parameters and dynamically analyze logs ...

#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;
}
Published 53 original articles · praised 16 · visits 2213

Guess you like

Origin blog.csdn.net/m0_37757533/article/details/105490598