1 | #ifndef LOGGER_H |
---|
2 | #define LOGGER_H |
---|
3 | |
---|
4 | #include <iostream> |
---|
5 | #include <fstream> |
---|
6 | #include "dataset.h" |
---|
7 | |
---|
8 | namespace WiiC { |
---|
9 | /* |
---|
10 | * LOG_START: open a dataset |
---|
11 | * LOG_STOP: close the dataset |
---|
12 | */ |
---|
13 | enum LogStatus { |
---|
14 | WIIC_LOG_START = 0x0, |
---|
15 | WIIC_LOG_STOP = 0x1 |
---|
16 | }; |
---|
17 | } |
---|
18 | |
---|
19 | using namespace std; |
---|
20 | using namespace WiiC; |
---|
21 | |
---|
22 | class Logger { |
---|
23 | public: |
---|
24 | Logger() : deviceAddr(""), logFile(""), relativeTs(0.0), logEnabled(false), |
---|
25 | logType(WIIC_LOG_NONE), logStatus(WIIC_LOG_STOP) { } |
---|
26 | Logger(const Logger& logger) : deviceAddr(logger.deviceAddr), logFile(logger.logFile), |
---|
27 | startTs(logger.startTs), relativeTs(logger.relativeTs), logEnabled(logger.logEnabled), |
---|
28 | logType(logger.logType), logStatus(logger.logStatus) { |
---|
29 | if(out.is_open()) out.close(); |
---|
30 | if(logger.out.is_open()) out.open(logFile.c_str(),ios::app); |
---|
31 | } |
---|
32 | |
---|
33 | ~Logger() { |
---|
34 | if(out.is_open()) out.close(); |
---|
35 | } |
---|
36 | |
---|
37 | Logger& operator=(const Logger& logger) { |
---|
38 | deviceAddr = logger.deviceAddr; |
---|
39 | logFile = logger.logFile; |
---|
40 | logType = logger.logType; |
---|
41 | logStatus = logger.logStatus; |
---|
42 | logEnabled = logger.logEnabled; |
---|
43 | startTs = logger.startTs; |
---|
44 | relativeTs = logger.relativeTs; |
---|
45 | if(out.is_open()) out.close(); |
---|
46 | if(logger.out.is_open()) out.open(logFile.c_str(),ios::app); |
---|
47 | |
---|
48 | return *this; |
---|
49 | } |
---|
50 | |
---|
51 | void InitLog(); |
---|
52 | void LogAcc(float x, float y, float z); |
---|
53 | void LogGyro(float x, float y, float z); |
---|
54 | void SaveLogHeader(); |
---|
55 | |
---|
56 | void SetLogLevel(LogStatus status, int type =WIIC_LOG_NONE, const string& file =""); |
---|
57 | inline int GetLogType() const { return logType; } |
---|
58 | inline string GetLogFilename() const { return logFile; } |
---|
59 | inline void SetLogStatus(const LogStatus status) { logStatus = status; } |
---|
60 | inline bool isLogEnabled() { return logEnabled; } |
---|
61 | |
---|
62 | inline void ResetTimestamp() { gettimeofday(&startTs,0); } |
---|
63 | inline void SetDeviceAddress(const string& addr) { deviceAddr = addr;} |
---|
64 | |
---|
65 | protected: |
---|
66 | bool CheckLogFile(); |
---|
67 | |
---|
68 | private: |
---|
69 | string deviceAddr; |
---|
70 | |
---|
71 | // Log file info |
---|
72 | string logFile; |
---|
73 | ofstream out; |
---|
74 | |
---|
75 | // Timestamp info |
---|
76 | struct timeval startTs; |
---|
77 | double relativeTs; |
---|
78 | |
---|
79 | // Logging info |
---|
80 | bool logEnabled; |
---|
81 | int logType; |
---|
82 | LogStatus logStatus; |
---|
83 | }; |
---|
84 | |
---|
85 | #endif |
---|