Написать программу анализирующую лог-файл сервера apache - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Может кто поможет: Написать программу анализирующую лог файл сервера apache, позволяющую отображать сообщения об ошибках за интервал времени.

Решение задачи: «Написать программу анализирующую лог-файл сервера apache»

textual
Листинг программы
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <string.h>
 
 
static const char* months[] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
    
const char** find_month(const char*);
time_t construct_time(int, int, int, int, const char*, int);
 
#define ENTRY_SIZE 512
#define DATE_FMT "%3s %2d %2d:%2d:%2d %4d"
 
int main(int argc, char* argv[])
{
    char entry[ENTRY_SIZE];
    time_t bounds[2], date;
    size_t i;
    int sec, min, hour, mday, year;
    char mon[4];
        
    if(argc != 3)
    {
        fputs("Usage: analize DATE DATE\n", stderr);
        exit(1);
    }
    
    for(i = 0; i < 2; ++i)
    {
        if(sscanf(argv[i + 1], DATE_FMT, 
                  mon, &mday, &hour, &min, &sec, &year) != 6)
        {
            fprintf(stderr, "Malformed date specification: %s\n", argv[i + 1]);
            exit(1);
        }
        
        bounds[i] = construct_time(sec, min, hour, mday, mon, year);
    }
    
    if(bounds[0] >= bounds[1])
    {
        fprintf(stderr, "Illegal date range: %s -- %s\n", argv[1], argv[2]);
        exit(1);
    }
    
    while(fgets(entry, ENTRY_SIZE, stdin) != NULL)
    {
        if(sscanf(entry, "[%*3s " DATE_FMT,
                  mon, &mday, &hour, &min, &sec, &year) != 6)
        {
            fprintf(stderr, "Malformed date for entry: %s", entry);
            exit(1);
        }
        
        date = construct_time(sec, min, hour, mday, mon, year);
 
        if(date >= bounds[0] && date <= bounds[1])
            fputs(entry, stdout);
    }
    
    if(ferror(stdin))
    {
        fputs("Error occured while reading stdin\n", stderr);
        exit(1);
    }
    
    exit(0);
}
 
const char** find_month(const char* mon)
{
    size_t i;
    
    for(i = 0; i < sizeof(months) / sizeof(*months); ++i)
        if(strcmp(months[i], mon) == 0)
            return months + i;
    return NULL;
}
 
time_t construct_time(int sec, int min, int hour, int mday, const char* mon, int year)
{
    struct tm date;
    
    const char** ptrmon = find_month(mon);
    assert(ptrmon != NULL);
    
    date.tm_sec = sec;
    date.tm_min = min;
    date.tm_hour = hour;
    date.tm_mday = mday;
    date.tm_mon = months - ptrmon;
    date.tm_year = year - 1900;
    date.tm_isdst = 0;
 
    return mktime(&date);
}

Оцени полезность:

8   голосов , оценка 3.375 из 5
Похожие ответы