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

httpstats.C

Go to the documentation of this file.
00001 // $Id: httpstats.C,v 1.3 2001/04/12 14:55:57 dvermeir Exp $
00002 
00003 #include <string>
00004 #include <fstream>
00005 
00006 // Httpstats main function.
00007 
00008 static const string usage("httpstats configfile logfile");
00009 
00010 #include "configuration.h"
00011 #include "logrecord.h"
00012 #include "stats.h"
00013 
00014 int 
00015 main(int argc,char* argv[]) {
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   // 3. Process the log file line by line and process each line using config.
00045   ifstream log_file(log_file_name.c_str());
00046   if (!log_file) {
00047     cerr << "Cannot open log file \"" << log_file_name << "\"" << endl;
00048     return 1;
00049     }
00050   
00051   Stats      stats(config);  // A stats object must know about config.
00052   string     line; //  Buffer for lines from the log_file.
00053   LogRecord  logrecord; // A line will be parsed into this object.
00054   int        n_lines_parsed(0); // Number of lines succesfully parsed.
00055   int        n_error_lines(0); // Number of lines with errors.
00056   
00057   while (getline(log_file,line)) {
00058     if (logrecord.parse_line(line)) {
00059       stats.add(logrecord);
00060       ++n_lines_parsed;
00061       }
00062     else
00063       ++n_error_lines;
00064     }
00065   
00066   cerr << "// Generate statistics output." << endl;
00067   cout << stats;
00068   
00069   // Show something on cerr.
00070   cerr << n_lines_parsed << " lines processed, " 
00071        << n_error_lines << " lines had errors" << endl;
00072   }
00073 catch (exception& e) {
00074   cerr << e.what() << endl;
00075   }
00076 }
00077 

httpstats-stage02a [ 7 April, 2001]