Notices
Evo X Engine Management / Tuning Forums Discuss the major engine management systems.

EvoScan Log Parsing Tool - Source Code in C

Thread Tools
 
Search this Thread
 
Old Mar 28, 2012 | 05:46 PM
  #1  
smanders's Avatar
Thread Starter
Evolving Member
iTrader: (4)
 
Joined: Sep 2002
Posts: 273
Likes: 0
From: Raleigh, NC
EvoScan Log Parsing Tool - Source Code in C

Hello Evo'ers,

I wrote some C code that parses an EvoScan csv log file. I was getting knock when just driving around town and I wanted to always be logging, but I did not want to have to look through thousands of lines of log files to find the knock areas.

This code will look for a Column named "KnockSum" and output the line before the knock started and then all the remaining non-zero knock lines of the log. It will do it for multiple occurrences within a single log file.

./logparser -i inputfile [ -o outputfile ]

The "-o" is optional. It will print to the screen if no output file is specified.

Enjoy...

Code:
/*
 * Copyright smanders
 *
 * GPLv2 License
 *
 */

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

#define DELIM           ","
#define COLUMN_NAME     "KnockSum"
/* #define DEBUG */

int main(int argc, char *argv[])
{
    FILE *fin = NULL;
    FILE *fout = stdout;
    char  header_line[1024] = {0};
    char  line[1024] = {0};
    char  prev_line[1024] = {0};
    char  buf[1024] = {0};
    char *p = NULL;
    int   knock_sum = 0;
    int   count = 0;
    int   i = 0;
    int   header_printed = 0;
    int   c;
    
    while ((c = getopt(argc, argv, "i:o:h")) != -1) {
        switch (c) {
        case 'i':
            fin = fopen(optarg,"r");
            if (!fin) return -1;
            break;
        case 'o':
            fout = fopen(optarg,"w");
            if (!fout) return -1;
            break;
        case 'h':
        case '?':
        default:
             printf("%s -i file [-o file]\n", argv[0]);
             return -1;
             break;
        }
    }

#ifdef DEBUG
    printf("argc: %d\n", argc);
    for (i=0;i<argc;i++)
        printf("argv[%d]: %s\n", i, argv[i]);
#endif


    p = fgets(header_line,sizeof(header_line),fin);

    if (!p) return -1;

    strncpy(buf,header_line,sizeof(buf));

    for (p=strtok(buf, DELIM),count = 0; 
         p != NULL;
         p = strtok(NULL, DELIM), count++) {

        if (strncmp(p,COLUMN_NAME, sizeof(COLUMN_NAME)) == 0) {
#ifdef DEBUG
            printf("Found Count: %d\n", count);
#endif
            break;
        }
    }

    while (p = fgets(line,sizeof(line),fin)) {

        if (!p) return -1;
 
        strncpy(buf,line,sizeof(buf));

        for (p=strtok(buf, DELIM),i = 0; 
             p != NULL && i < (count-1);
             p = strtok(NULL, DELIM), i++);

        if (!p) {
#ifdef DEBUG
            printf("Not Found i: %d\n", i);
#endif
            break;
        }

        sscanf(p,"%d,",&knock_sum);

        if (knock_sum) {
            
            if (!header_printed) {
                fputs(header_line,fout);
                header_printed = 1;
            }
            if (strlen(prev_line)) {
                fputs(prev_line,fout);
                prev_line[0] = '\0';
            }
            fputs(line,fout);

        } else {
            strncpy(prev_line,line,sizeof(prev_line));
        }

    }

    if (fin)
        fclose(fin);

    if (fout)
        fclose(fout);

    return 0;
}

Last edited by smanders; Mar 28, 2012 at 08:40 PM.
Reply
Old Mar 28, 2012 | 07:07 PM
  #2  
richardjh's Avatar
Evolved Member
 
Joined: Oct 2010
Posts: 2,447
Likes: 14
From: Australia
Cool.

And can I say, I just love an auto forum where people post in C!

Rich
Reply
Old Mar 28, 2012 | 08:40 PM
  #3  
smanders's Avatar
Thread Starter
Evolving Member
iTrader: (4)
 
Joined: Sep 2002
Posts: 273
Likes: 0
From: Raleigh, NC
Originally Posted by richardjh
Cool.

And can I say, I just love an auto forum where people post in C!

Rich
I contemplated PHP/Bash/Perl but C is my core. Architecting Embedded Linux Software pays the bills Long live C.
Reply
Old Mar 29, 2012 | 08:37 AM
  #4  
Golden's Avatar
Evolved Member
iTrader: (1)
 
Joined: Nov 2009
Posts: 1,456
Likes: 0
From: Omaha, NE
I could do it in just a few lines of awk.
Reply
Old Mar 29, 2012 | 03:48 PM
  #5  
richardjh's Avatar
Evolved Member
 
Joined: Oct 2010
Posts: 2,447
Likes: 14
From: Australia
But I bet you can't generate core dumps in awk, can you...



Rich
Reply
Old Mar 30, 2012 | 10:54 AM
  #6  
smanders's Avatar
Thread Starter
Evolving Member
iTrader: (4)
 
Joined: Sep 2002
Posts: 273
Likes: 0
From: Raleigh, NC
Originally Posted by Golden
I could do it in just a few lines of awk.
Yes, awk is fun ;-)
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
mrb00st
E85 / Ethanol
189
Oct 11, 2024 04:59 PM
ak47po
The Loft / EvoM Car Talk Corner
0
Apr 11, 2016 08:31 PM
kiddsm95
ECU Flash
9
Feb 18, 2016 06:01 AM
crimson red
Evo X Engine Management / Tuning Forums
1
Aug 23, 2009 02:21 AM
MalibuJack
ECU Flash
44
Dec 3, 2007 07:12 AM




All times are GMT -7. The time now is 12:45 PM.