robert@464: /* robert@464: * logger.cpp robert@464: * robert@464: * Created on: Aug 6, 2014 robert@464: * Author: VIctor Zappi and Andrew McPherson robert@464: */ robert@464: robert@464: #include "logger.h" robert@464: robert@464: // main extern vars robert@464: extern int gShouldStop; robert@464: extern int gVerbose; robert@464: robert@464: // file nanme extern vars robert@464: extern char gId; robert@464: extern char gGroup; robert@464: robert@464: robert@464: // logged extern vars robert@464: extern int s0TouchNum; robert@464: extern float s0Touches_[MAX_TOUCHES]; robert@464: extern float s0Size_[MAX_TOUCHES]; robert@464: extern int s0LastIndex; robert@464: robert@464: extern int s1TouchNum; robert@464: extern float s1Touches_[MAX_TOUCHES]; robert@464: extern float s1Size_[MAX_TOUCHES]; robert@464: extern int s1LastIndex; robert@464: robert@464: extern int fsr; robert@464: robert@464: robert@464: robert@464: string logPath = "/boot/uboot/instrumentLog"; robert@464: string logFileIncipit = "/datalog"; robert@464: string logFileName = ""; robert@464: ofstream logFile; robert@464: timeval logTimeVal; robert@464: unsigned long long logTimeOrig; robert@464: int logCnt = 0; // counts how many lines so far robert@464: int logCntSwap = 50; // how many log lines before closing and re-opening the file robert@464: robert@464: robert@464: // create the log file, using incremental name convention robert@464: int initLogLoop() robert@464: { robert@464: if(gVerbose==1) robert@464: cout << "---------------->Init Log Thread" << endl; robert@464: robert@464: robert@464: // transform chars into strings via stringstream objs robert@464: stringstream id_ss, group_ss, freedom_ss; robert@464: id_ss << gId; robert@464: group_ss << gGroup; robert@464: robert@464: int logNum = -1; robert@464: int logMax = -1; robert@464: int pathLen = logPath.length() + logFileIncipit.length() + 4; // + 4 is: "_", id, group, "_" robert@464: glob_t globbuf; robert@464: robert@464: // check how many log files are already there, and choose name according to this robert@464: glob( (logPath + logFileIncipit + "*").c_str(), 0, NULL, &globbuf); robert@464: robert@464: // cycle through all and find the highest index robert@464: for(unsigned int i=0; i logMax) robert@464: logMax = logNum; robert@464: } robert@464: logNum = logMax + 1; // new index robert@464: robert@464: globfree(&globbuf); robert@464: robert@464: ostringstream numString; robert@464: numString << setw (4) << setfill ('0') << logNum; // set integer with 4 figures robert@464: robert@464: // here are the new names: PATH + DIR + INCIPIT + _ + id + group + freedom + _ + NUM (4figures) + _A.txt robert@464: logFileName = logPath + logFileIncipit; robert@464: logFileName += "_" + id_ss.str() + group_ss.str() + freedom_ss.str(); robert@464: logFileName += "_" + numString.str(); //static_cast( &(ostringstream() << logNum) )->str(); robert@464: logFileName += ".txt"; robert@464: robert@464: robert@464: // create new files robert@464: FILE *fp_a = fopen(logFileName.c_str(), "wb"); robert@464: if(!fp_a) robert@464: { robert@464: dbox_printf("Cannot create files...\n"); robert@464: return 2; robert@464: } robert@464: fclose(fp_a); robert@464: robert@464: // ready to append robert@464: logFile.open(logFileName.c_str(), ios::out | ios::app); robert@464: robert@464: dbox_printf("Logging on file %s\n", logFileName.c_str()); robert@464: robert@464: return 0; robert@464: } robert@464: robert@464: robert@464: void writeData(unsigned long long time) robert@464: { robert@464: robert@464: float fsr_ = ((float)(1799-fsr)/1799.0); robert@464: logFile << time << "\t" // timestamp robert@464: << s0TouchNum << "\t"; // sensor 0 touch count robert@464: for(int i=0; i= logCntSwap) robert@464: { robert@464: logFile.close(); // close file, dump stream robert@464: logCnt = 0; // ready for another whole round robert@464: robert@464: // open again, ready to append robert@464: logFile.open(logFileName.c_str(), ios::out | ios::app); robert@464: } robert@464: robert@464: writeData(time); robert@464: robert@464: logCnt++; robert@464: } robert@464: robert@464: robert@464: robert@464: robert@464: void *logLoop(void *) robert@464: { robert@464: set_realtime_priority(10); robert@464: robert@464: if(gVerbose==1) robert@464: dbox_printf("_________________Log Thread!\n"); robert@464: robert@464: // get time reference robert@464: gettimeofday(&logTimeVal, NULL); robert@464: logData(0); robert@464: robert@464: logTimeOrig = logTimeVal.tv_usec; robert@464: logTimeOrig *= 0.001; // from usec to msec robert@464: logTimeOrig += logTimeVal.tv_sec*1000; // from sec to msec robert@464: robert@464: usleep(5000); robert@464: robert@464: while(!gShouldStop) robert@464: { robert@464: gettimeofday(&logTimeVal, NULL); robert@464: unsigned long long currentTime = logTimeVal.tv_usec; robert@464: currentTime *= 0.001; // from usec to msec robert@464: currentTime += logTimeVal.tv_sec*1000; // from sec to msec robert@464: robert@464: logData(currentTime-logTimeOrig); robert@464: robert@464: usleep(5000); robert@464: } robert@464: robert@464: if(logFile!=NULL) robert@464: logFile.close(); robert@464: robert@464: dbox_printf("log thread ended\n"); robert@464: robert@464: return (void *)0; robert@464: }