Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

httpstats.C File Reference

#include <string>
#include <fstream>
#include "configuration.h"
#include "logrecord.h"
#include "stats.h"

Go to the source code of this file.

Functions

const string usage ("httpstats configfile logfile")
int main (int argc, char *argv[])


Function Documentation

const string usage ( "httpstats configfile logfile" ) [static]
 

int main ( int argc,
char * argv[] )
 

Definition at line 15 of file httpstats.C.

00015                             {
00016 // We put the lot in a try .. catch block, in case something throws
00017 // an exception, which should not happen but..
00018 try {
00019   // 1. check arguments: there must be exactly 2 of them.
00020   if (argc!=3) {
00021     cerr << "Usage: " << usage << endl;
00022     return 1; 
00023     }
00024   
00025   string config_file_name(argv[1]);
00026   string log_file_name(argv[2]);
00027   
00028   // 2. parse confiuration file. We will use a class Configuration that
00029   // will contain the configuration information. 
00030   // But first, we attempt to open the file.
00031   ifstream config_file(config_file_name.c_str());
00032   if (!config_file) {
00033     cerr << "Cannot open configuration file \"" << config_file_name << "\"" << endl;
00034     return 1;
00035     }
00036   
00037   // Then we try to parse.
00038   Configuration config; 
00039   if (!config.parse(config_file)) {
00040     cerr << "Fatal error in configuration file \"" << config_file_name << "\"" << endl;
00041     return 1;
00042     }
00043   
00044   cerr << config << endl;
00045   // 3. Process the log file line by line and process each line using config.
00046   ifstream log_file(log_file_name.c_str());
00047   if (!log_file) {
00048     cerr << "Cannot open log file \"" << log_file_name << "\"" << endl;
00049     return 1;
00050     }
00051   
00052   Stats             stats(config);  // A stats object must know about config.
00053   const Expression* select(config.select()); // Records must satisfy this to get into stats.
00054   string            line; //  Buffer for lines from the log_file.
00055   LogRecord         logrecord; // A line will be parsed into this object.
00056   int               n_lines_parsed(0); // Number of lines succesfully parsed.
00057   int               n_lines_selected(0); // Number of lines parsed and selected.
00058   int               n_error_lines(0); // Number of lines with errors.
00059   
00060   // Processing log file..
00061 
00062   while (getline(log_file,line)) {
00063     if (logrecord.parse_line(line)) {
00064       if (select) {
00065         if (select->eval(logrecord)) {
00066           ++n_lines_selected;
00067           stats.add(logrecord);
00068           }
00069         }
00070       else {
00071         stats.add(logrecord);
00072         ++n_lines_selected;
00073         }
00074       ++n_lines_parsed;
00075       }
00076     else
00077       ++n_error_lines;
00078     }
00079   
00080   cerr << "// Generate statistics output." << endl;
00081   cout << stats;
00082   
00083   // Show something on cerr to keep the user amused.
00084   cerr << n_lines_parsed << " lines processed, " 
00085        << n_lines_selected << " lines selected, " 
00086        << n_error_lines << " lines had errors" << endl;
00087   }
00088 catch (exception& e) {
00089   cerr << e.what() << endl;
00090   }
00091 }


httpstats-stage02b [ 7 April, 2001]