00001
00002 #include "stats.h"
00003 #include <algorithm>
00004
00005
00006 Stats::Stats(const Configuration& c): configuration_(c) {
00007 }
00008
00009
00010 void
00011 Stats::add(const LogRecord& r) {
00012
00013
00014
00015
00016 set<unsigned int> levels(configuration_.levels());
00017 unsigned int max_level(configuration_.max_level());
00018
00019 switch (configuration_.format()) {
00020
00021 case Configuration::DATE: {
00022 const DatePattern& d(r.date());
00023
00024
00025 unsigned int m(min(d.size(),max_level));
00026 for (unsigned int l=1;(l<=m);++l)
00027 if (levels.count(l)) {
00028 DatePattern dp(d,l);
00029 ++dates_[dp];
00030 }
00031 }
00032 break;
00033
00034 case Configuration::PATH: {
00035 const Path& path(r.path());
00036 unsigned int m(min(path.size(),max_level));
00037 for (unsigned int l=1; l<=m; ++l)
00038 if (levels.count(l)) {
00039 Path pp(path,l);
00040 ++paths_[pp];
00041 }
00042 }
00043 break;
00044
00045 case Configuration::DOMAIN: {
00046 const Domain& domain(r.domain());
00047 unsigned int sz(domain.size());
00048 unsigned int m(min(sz,max_level));
00049 for (unsigned int l=1; l<=m; ++l) {
00050 if (levels.count(l)) {
00051 Domain dom(domain,l);
00052 ++(domains_[dom]);
00053 }
00054 }
00055 }
00056 break;
00057 }
00058 }
00059
00060
00061 void
00062 Stats::print_dates(ostream& os) const {
00063 for (DateCount::const_iterator i=dates_.begin(); i!=dates_.end(); ++i) {
00064 const DatePattern& d((*i).first);
00065 os << d << "\t" << (*i).second << "\n";
00066 }
00067 }
00068
00069
00070 void
00071 Stats::print_paths(ostream& os) const {
00072 for (PathCount::const_iterator i=paths_.begin(); i!=paths_.end(); ++i) {
00073 const Path& path((*i).first);
00074 os << path << "\t" << (*i).second << "\n";
00075 }
00076 }
00077
00078
00079 void
00080 Stats::print_domains(ostream& os) const {
00081 for (DomainCount::const_iterator i=domains_.begin(); i!=domains_.end(); ++i) {
00082 const Domain& domain((*i).first);
00083 os << domain << "\t" << (*i).second << "\n";
00084 }
00085 }
00086
00087
00088 ostream&
00089 operator<<(ostream& os,const Stats& stats) {
00090 switch (stats.configuration_.format()) {
00091 case Configuration::DATE:
00092 stats.print_dates(os);
00093 break;
00094 case Configuration::PATH:
00095 stats.print_paths(os);
00096 break;
00097 case Configuration::DOMAIN:
00098 stats.print_domains(os);
00099 break;
00100 }
00101 return os;
00102 }