[9780] | 1 | #include "logger.h" |
---|
| 2 | #include "wiicpp.h" |
---|
| 3 | #include "wiic_internal.h" |
---|
| 4 | |
---|
| 5 | void Logger::SetLogLevel(LogStatus status, int type, const string& file) |
---|
| 6 | { |
---|
| 7 | // Avoid repetitions |
---|
| 8 | if(status == logStatus && type == logType && file == logFile) |
---|
| 9 | return; |
---|
| 10 | |
---|
| 11 | if(status == WIIC_LOG_START) { |
---|
| 12 | ResetTimestamp(); |
---|
| 13 | |
---|
| 14 | if((file == "" && logFile != "") || (file == logFile)) { // Same file |
---|
| 15 | if(logEnabled) |
---|
| 16 | out << "END" << endl; |
---|
| 17 | |
---|
| 18 | // New training |
---|
| 19 | unsigned long trainingTs = (startTs.tv_sec % 86400) * 1000 + startTs.tv_usec / 1000; |
---|
| 20 | out << "START " << trainingTs << endl; |
---|
| 21 | } |
---|
| 22 | else if(file != "") { // Open a new log file |
---|
| 23 | if(out.is_open()) { // In case a previous log file is still open |
---|
| 24 | if(logEnabled) |
---|
| 25 | out << "END" << endl; |
---|
| 26 | out.close(); |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | logFile = file; |
---|
| 30 | |
---|
| 31 | // Check if the file already exists, and whether it is a WiiC logfile |
---|
| 32 | bool resume = CheckLogFile(); |
---|
| 33 | |
---|
| 34 | out.open(logFile.c_str(),ios::app); |
---|
| 35 | if(!out.is_open()) |
---|
| 36 | return; |
---|
| 37 | |
---|
| 38 | // Sometimes we want to modify a log file, not to create a new one |
---|
| 39 | if(!resume) |
---|
| 40 | SaveLogHeader(); |
---|
| 41 | |
---|
| 42 | // New training |
---|
| 43 | unsigned long trainingTs = (startTs.tv_sec % 86400) * 1000 + startTs.tv_usec / 1000; |
---|
| 44 | out << "START " << trainingTs << endl; |
---|
| 45 | } |
---|
| 46 | else // How can I log if you don't provide me with a file??? |
---|
| 47 | return; |
---|
| 48 | |
---|
| 49 | // Status init |
---|
| 50 | logType = type; |
---|
| 51 | logStatus = WIIC_LOG_START; |
---|
| 52 | logEnabled = true; |
---|
| 53 | } |
---|
| 54 | else if(status == WIIC_LOG_STOP) { |
---|
| 55 | out << "END" << endl; |
---|
| 56 | if(out.is_open()) |
---|
| 57 | out.close(); |
---|
| 58 | |
---|
| 59 | // Status reset |
---|
| 60 | logStatus = WIIC_LOG_STOP; |
---|
| 61 | logType = WIIC_LOG_NONE; |
---|
| 62 | logEnabled = false; |
---|
| 63 | logFile = ""; |
---|
| 64 | } |
---|
| 65 | else { |
---|
| 66 | return; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | bool Logger::CheckLogFile() |
---|
| 71 | { |
---|
| 72 | ifstream in(logFile.c_str()); |
---|
| 73 | if(!in.is_open()) |
---|
| 74 | return false; |
---|
| 75 | |
---|
| 76 | string wiicToken; |
---|
| 77 | int logVersion; |
---|
| 78 | in >> wiicToken >> logVersion; |
---|
| 79 | in.close(); |
---|
| 80 | |
---|
| 81 | if(wiicToken == "WiiC" && logVersion <= WIIC_LOG_VERSION) |
---|
| 82 | return true; |
---|
| 83 | |
---|
| 84 | return false; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Save the dataset header into a file. |
---|
| 89 | * |
---|
| 90 | * @param out Output file stream |
---|
| 91 | * @param addr Wii device MAC address |
---|
| 92 | */ |
---|
| 93 | void Logger::SaveLogHeader() |
---|
| 94 | { |
---|
| 95 | // Log Version |
---|
| 96 | out << "WiiC " << WIIC_LOG_VERSION << endl; |
---|
| 97 | |
---|
| 98 | // Date |
---|
| 99 | time_t tim=time(NULL); |
---|
| 100 | char *s=ctime(&tim); |
---|
| 101 | s[strlen(s)-1]=0; // remove \n |
---|
| 102 | out << s << endl; |
---|
| 103 | |
---|
| 104 | // Mac Address |
---|
| 105 | out << deviceAddr << endl; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void Logger::InitLog() |
---|
| 109 | { |
---|
| 110 | // Compute the relative timestamp |
---|
| 111 | struct timeval t; |
---|
| 112 | gettimeofday(&t,0); |
---|
| 113 | relativeTs = ((t.tv_sec*1000000.0 + t.tv_usec) - (startTs.tv_sec*1000000.0 + startTs.tv_usec))/1000.0; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void Logger::LogAcc(float x, float y, float z) |
---|
| 117 | { |
---|
| 118 | out << "ACC " << relativeTs << " " << x << " " << y << " " << z << endl; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | void Logger::LogGyro(float roll, float pitch, float yaw) |
---|
| 122 | { |
---|
| 123 | out << "GYRO " << relativeTs << " " << roll << " " << pitch << " " << yaw << endl; |
---|
| 124 | } |
---|