EvoScan Log Parsing Tool - Source Code in C
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...
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.
Thread
Thread Starter
Forum
Replies
Last Post
ak47po
The Loft / EvoM Car Talk Corner
0
Apr 11, 2016 08:31 PM
crimson red
Evo X Engine Management / Tuning Forums
1
Aug 23, 2009 02:21 AM



I contemplated PHP/Bash/Perl but C is my core. Architecting Embedded Linux Software pays the bills
Long live C.
