00001 #include "EventLogger.h"
00002 #include "Events/EventRouter.h"
00003 #include "Motion/MMAccessor.h"
00004 #include "Motion/LedMC.h"
00005 #include "ValueEditControl.h"
00006 #include "StringInputControl.h"
00007 #include "NullControl.h"
00008 #include <sstream>
00009 #include "Events/LocomotionEvent.h"
00010 #include "Events/TextMsgEvent.h"
00011 #include "Events/VisionObjectEvent.h"
00012
00013 EventLogger::EventLogger() : ControlBase("Event Logger","Allows you to see/log all of the un-trapped events as they are generated"), logfilePath(), logfile(), verbosity(0) {
00014 for(unsigned int i=0; i<EventBase::numEGIDs; i++) {
00015 std::string tmp=EventBase::EventGeneratorNames[i];
00016 pushSlot(new NullControl(("[ ] "+tmp).c_str(),"Show/hide events from "+tmp));
00017 }
00018 pushSlot(NULL);
00019 pushSlot(new ValueEditControl<unsigned int>("Verbosity","Controls verbosity level: 0=name,type; 1=0+duration,timestamp; 2=1+magnitude; 3=2+subclass info","Please enter a new verbosity level: 0=name,type; 1=0+duration,timestamp; 2=1+magnitude; 3=2+subclass info",&verbosity));
00020 pushSlot(new ControlBase("[X] Console Output","If selected, outputs events to the console"));
00021 pushSlot(new StringInputControl("[ ] File Output","Please enter the filename to log to (in /ms/...)"));
00022 }
00023
00024 ControlBase* EventLogger::doSelect() {
00025 ControlBase* ans=this;
00026 for(unsigned int i=0; i<hilights.size(); i++) {
00027 unsigned int cur=hilights[i];
00028 if(cur<EventBase::numEGIDs) {
00029 if(options[cur]->getName()[1]!=' ') {
00030 erouter->removeListener(this,(EventBase::EventGeneratorID_t)(cur));
00031 setStatus(cur,' ');
00032 } else {
00033 erouter->addListener(this,(EventBase::EventGeneratorID_t)(cur));
00034 setStatus(cur,'X');
00035 }
00036 } else if(cur==EventBase::numEGIDs+1) {
00037 ans=options[cur];
00038 } else if(cur==EventBase::numEGIDs+2) {
00039 if(options[cur]->getName()[1]!=' ') {
00040 setStatus(cur,' ');
00041 } else {
00042 setStatus(cur,'X');
00043 }
00044 } else if(cur==EventBase::numEGIDs+3) {
00045 if(options[cur]->getName()[1]!=' ') {
00046 logfile.close();
00047 options[cur]->setName("[ ] File Output");
00048 } else {
00049 ans=options[cur];
00050 }
00051 }
00052 sndman->PlayFile(config->controller.select_snd);
00053 }
00054 if(ans==this)
00055 refresh();
00056 return ans;
00057 }
00058
00059 void EventLogger::refresh() {
00060 checkLogFile();
00061 ControlBase::refresh();
00062 }
00063
00064
00065 void EventLogger::processEvent(const EventBase& event) {
00066 std::ostringstream logdata;
00067 logdata << event.getName();
00068 switch(event.getTypeID()) {
00069 case EventBase::activateETID:
00070 logdata << "\tA"; break;
00071 case EventBase::statusETID:
00072 logdata << "\tS"; break;
00073 case EventBase::deactivateETID:
00074 logdata << "\tD"; break;
00075 case EventBase::numETIDs:
00076 logdata << "\tErr"; break;
00077 }
00078 if(verbosity>=1)
00079 logdata << '\t' << event.getDuration() << '\t' << event.getTimeStamp();
00080 if(verbosity>=2)
00081 logdata << '\t' << event.getMagnitude();
00082 if(verbosity>=3) {
00083 if(const LocomotionEvent * loco=dynamic_cast<const LocomotionEvent*>(&event))
00084 logdata << '\t' << loco->x << '\t' << loco->y << '\t' << loco->a;
00085 if(const TextMsgEvent * text=dynamic_cast<const TextMsgEvent*>(&event))
00086 logdata << '\t' << text->getToken() << '\t' << text->getText();
00087 if(const VisionObjectEvent * vis=dynamic_cast<const VisionObjectEvent*>(&event))
00088 logdata << '\t' << vis->getCenterX() << '\t' << vis->getCenterY() << '\t' << vis->getDistance();
00089 }
00090
00091 if(options[EventBase::numEGIDs+2]->getName()[1]=='X')
00092 sout->printf("EVENT: %s\n",logdata.str().c_str());
00093 checkLogFile();
00094 if(logfile)
00095 logfile << logdata.str() << endl;
00096 }
00097
00098 void EventLogger::setStatus(unsigned int i, char c) {
00099 std::string tmp=options[i]->getName();
00100 tmp[1]=c;
00101 options[i]->setName(tmp);
00102 }
00103
00104 void EventLogger::checkLogFile() {
00105 unsigned int cur=EventBase::numEGIDs+3;
00106 StringInputControl * strin=dynamic_cast<StringInputControl*>(options[cur]);
00107 ASSERTRET(strin!=NULL,"The StringInputControl is misplaced");
00108 if(strin->getLastInput()!=logfilePath) {
00109 logfile.close();
00110 logfilePath=strin->getLastInput();
00111 logfile.clear();
00112 if(logfilePath.size()!=0) {
00113 sout->printf("Opening `/ms/%s'\n",logfilePath.c_str());
00114 logfile.open(("/ms/"+logfilePath).c_str());
00115 if(!logfile.fail()) {
00116 setStatus(cur,'X');
00117 strin->setName(strin->getName()+": "+logfilePath);
00118 } else {
00119 serr->printf("Opening `/ms/%s' failed\n",logfilePath.c_str());
00120 }
00121 }
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134