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

httpstats.C File Reference

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

Go to the source code of this file.

Functions

const string usage ("httpstats configfile logfile")
const string default_config_text ("format path 1-4\nselect path/\nexclude path/robots.txt\n")
int main (int argc, char *argv[])


Function Documentation

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

const string default_config_text ( "format path 1-4\nselect path/\nexclude path/robots.txt\n" )
 

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

Definition at line 20 of file httpstats.C.

00020                             {
00021 // We put the lot in a try .. catch block, in case something throws
00022 // an exception, which should not happen but..
00023 try {
00024   // 1. check arguments: there must be 1 or 2 of them
00025   if (argc<2 || argc>3) {
00026     cerr << "Usage: " << usage << endl;
00027     return 1; 
00028     }
00029   
00030   string log_file_name(argv[1]);
00031 
00032   // 2. parse configuration file. We will use a class Configuration that
00033   // to contain the configuration information. 
00034 
00035   istream* config_file(0);
00036   string config_file_name((argc==3) ? argv[2] : "default");
00037 
00038   if (argc==3) {
00039     // we have a configuration file argument
00040 
00041     config_file = new ifstream(config_file_name.c_str());
00042 
00043     if (!*config_file) {
00044       cerr << "Cannot open configuration file \"" 
00045            << config_file_name << "\"" << endl;
00046       return 1;
00047       }
00048     }
00049   else 
00050     config_file = new istrstream(default_config_text.data(),
00051                                  default_config_text.size());
00052     
00053   
00054   // Parse configuration.
00055   Configuration config; 
00056   unsigned int err_lineno(config.parse(*config_file));
00057   if (err_lineno) {
00058     cerr << "Error on line " << err_lineno
00059          << " in configuration file \"" << config_file_name << "\"" << endl;
00060     return 1;
00061     }
00062   
00063   delete config_file; config_file = 0; // not needed anymore
00064 
00065   cerr << config << endl;
00066   // 3. Process the log file line by line and process each line using config.
00067   ifstream log_file(log_file_name.c_str());
00068   if (!log_file) {
00069     cerr << "Cannot open log file \"" << log_file_name << "\"" << endl;
00070     return 1;
00071     }
00072   
00073   Stats             stats(config);  // A stats object must know about config.
00074   const Expression* select(config.select()); // Records must satisfy this to get into stats.
00075   string            line; //  Buffer for lines from the log_file.
00076   LogRecord         logrecord; // A line will be parsed into this object.
00077   int               n_lines_parsed(0); // Number of lines succesfully parsed.
00078   int               n_lines_selected(0); // Number of lines parsed and selected.
00079   int               n_error_lines(0); // Number of lines with errors.
00080   
00081   // Processing log file..
00082 
00083   while (getline(log_file,line)) {
00084     if (logrecord.parse_line(line)) {
00085       if (select) {
00086         if (select->eval(logrecord)) {
00087           ++n_lines_selected;
00088           stats.add(logrecord);
00089           }
00090         }
00091       else {
00092         stats.add(logrecord);
00093         ++n_lines_selected;
00094         }
00095       ++n_lines_parsed;
00096       }
00097     else
00098       ++n_error_lines;
00099     }
00100   
00101   cerr << "// Generate statistics output." << endl;
00102   cout << stats;
00103   
00104   // Show something on cerr to keep the user amused.
00105   cerr << n_lines_parsed << " lines processed, " 
00106        << n_lines_selected << " lines selected, " 
00107        << n_error_lines << " lines had errors" << endl;
00108   }
00109 catch (exception& e) {
00110   cerr << e.what() << endl;
00111   }
00112 }


httpstats-stage03 [ 7 April, 2001]