0

I have a problem where I need to read lines from a log file, check if the line contains the word "fail" and if so output how many seconds passed from midnight to the time the error occured.

An example line from a logfile looks like this:

Aug 20 10:20:57 kernel: ata3: SATA link down

I have the first two parts of the problem workig correctly (reading lines from the logfile and checking if they contain the word "fail") but I'm a bit stumped on how to extract the time from the logfile and converting it into seconds passed since midnight.

What I have so far looks like this (the relevant part of the code):

do
{
    currLine = fgets(buff, sizeof(buff), inFile);

    if(strstr(currLine, "fail") != NULL)
    {
        /*convert the time into seconds since midnight and output it*/
    }
} while(!feof(inFile));

So far I have only been able to come up with one idea of how to implement it, but it doesn't work (this would be inside the if statement above, where I have placed the placeholder comment explaining what it needs to do):

int hr, min, sec, timePassed;           
fscanf(currLine, "%d:%d:%d", &hr, &min, &sec);
timePassed = hr * 3600 + min * 60 + sec;

Thanks very much in advance

rjt197197
  • 11
  • 2

2 Answers2

0

regarding this posted code:

do
{
    currLine = fgets(buff, sizeof(buff), inFile);

    if(strstr(currLine, "fail") != NULL)
    {
        /*convert the time into seconds since midnight and output it*/
    }
} while(!feof(inFile));

do not use 'feof()' as it is only 'true' after trying to read past the end of the file, so is rarely, if ever useful.

Do not want to be processing the log file line if the log file is empty,.

instead use this:

while( currLine = fgets(buff, sizeof(buff), inFile) )
{

    if(strstr(currLine, "fail") != NULL)
    {
        /*convert the time into seconds since midnight and output it*/
    }
}

as that will not try to process an empty line, (the log file is empty or have tried to read past end of file)

unless the pointer: currLine is used elsewhere, all references could be eliminated and in the call to strstr(), use: 'buff'

to extract the time from the line, first need to step over the first two fields. There are several possible ways to do that.

You could use the comment given by 'chux' to extract the time fields

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

Your example didn't work for two reasons. First, you're using fscanf instead of sscanf (file and string) and second: you didn't jump to the numbers. Your scanning starts from the beginning of the string which has no number, so it won't match. An easy hack to make this work, if the size of the month and day part are constant, would be something like:

sscanf(currLine+7, "%d:%d:%d", &hr, &min, &sec);

The "official" and safer way to do it is using strptime, if your implementation makes it available.

#include <time.h>

struct tm time;

/* ... */

strptime(currLine, "%b %d %H:%M:%S", &time);

Now you have the time struct and can extract the components you need.

timePassed = time.tm_hour * 3600 + time.tm_min * 60 + time.tm_sec;

Warning: As said in the comments, check your usage of while(!feof(inFile)).

sidyll
  • 57,726
  • 14
  • 108
  • 151