// testStatsMap -- Illustration of using the statisticsMap() feature of // ErrorLogger mechanism. // // This example program is in two logical pieces: // // First we show the user code -- many physicist users could contribute // code of this nature, using errlog in the manner shown. // // Then we show the framework code that would support this. One // programmer sets up this framework to coordinate all code. The // framework controls things like which output destinations exist and // what sort of messages to pass to each destination. // // THIS FRAMEWORK IS A VERY MINIMAL EXAMPLE for illustration. // // ----------------- // Physicsists' Code // ----------------- #include "testStatsMap.h" #include "ErrorLogger/ErrorLog.h" #include #include #include ZM_USING_NAMESPACE(zmel) void DoPhysics::operator()( Event & event ) { int n = 1; // a bunch of messages: if (event.data%2 == 0) { errlog( ELincidental, "incidental" ) << "data " << n++ << endmsg; errlog( ELsuccess, "success" ) << "data " << n++ << endmsg; errlog( ELinfo, "info" ) << "data " << n++ << endmsg; errlog( ELwarning, "warning" ) << "data " << n++ << endmsg; errlog( ELwarning2, "warning2" ) << "data " << n++ << endmsg; errlog( ELerror, "error" ) << "data " << n++ << endmsg; errlog( ELerror2, "error2" ) << "data " << n++ << endmsg; errlog( ELsevere, "severe" ) << "data " << n++ << endmsg; errlog( ELsevere2, "severe2" ) << "data " << n++ << endmsg; } if (event.data%3 == 0) { errlog( ELincidental, "incid" ) << "data " << n++ << endmsg; errlog( ELsuccess, "succ" ) << "data " << n++ << endmsg; errlog( ELinfo, "inf" ) << "data " << n++ << endmsg; errlog( ELwarning, "warn" ) << "data " << n++ << endmsg; errlog( ELwarning2, "warn" ) << "data " << n++ << endmsg; errlog( ELerror, "err" ) << "data " << n++ << endmsg; errlog( ELerror2, "err" ) << "data " << n++ << endmsg; errlog( ELsevere, "sev" ) << "data " << n++ << endmsg; errlog( ELsevere2, "sev" ) << "data " << n++ << endmsg; } if (event.data%5 == 0) { errlog( ELwarning, "warn" ) << "data " << n++ << endmsg; errlog( ELwarning2, "warn" ) << "data " << n++ << endmsg; errlog( ELerror, "err" ) << "data " << n++ << endmsg; errlog( ELerror2, "err" ) << "data " << n++ << endmsg; errlog( ELsevere, "sev" ) << "data " << n++ << endmsg; errlog( ELsevere, "sev" ) << "data " << n++ << endmsg; } // What a useless routine! return; } // DoPhysics::operator()(event) // --------------- // Framework Code // --------------- #include "testStatsMap.h" #include "ErrorLogger/ELadministrator.h" #include "ErrorLogger/ELdestControl.h" #include "ErrorLogger/ELoutput.h" #include "ErrorLogger/ELstatistics.h" #include "ZMutility/iostream" USING( std::cout ) Module::Module( const std::string & name ) { errlog.setModule( name ); } int main() { ELadministrator * logger = ELadministrator::instance(); // instantiate a logger object ELoutput logfileD( "testStatsMap.errlog" ); // make an ELdestination ELdestControl logfile ( logger->attach(logfileD) ); // tell logger about it logfile.setThreshold ( ELerror ); // ignore messages below // ELerror ELoutput outputD( cout ); // another ELdestination ELdestControl output ( logger->attach(outputD) ); // tell logger about it output.setThreshold ( ELsevere2 ); // ignore messages below // ELwevere2 ELstatistics statsD( cout ); // a statistics dest ELdestControl stats ( logger->attach(statsD) ); // tell logger about it stats.setThreshold ( ELsuccess ); // ignore messages below // ELerror // The following is a list of the modules in this program: DoPhysics doPhysics( "PHYSICS CODE" ); // There is only one module in this illustration. for ( int n=1 ; n <= 150; ++n ) { Event event( n ); doPhysics( event ); } stats.summary(std::cout); // OK, here's the key statement: std::map m = stats.statisticsMap(); std::map::const_iterator i; std::map database; // A really unsophisticated database! for (i = m.begin(); i != m.end(); ++i) { database [i->first.id] += i->second.aggregateN; } std::cout << "\n\n database contains: \n\n"; std::map::const_iterator n; for ( n = database.begin(); n != database.end(); ++n ) { std::cout<< std::setw(22) << n->first << std::setw(13) << n->second << "\n"; } return 0; } // main()