TwirreLink
 All Classes Functions Pages
TwirreLogger.h
1 /*
2  * Twirre: architecture for autonomous UAVs using interchangeable commodity components
3  *
4  * Copyright© 2017 Centre of expertise in Computer Vision & Data Science, NHL Stenden University of applied sciences
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7  *
8  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11  */
12 
13 #ifndef LOGGER_TWIRRELOGGER_H_
14 #define LOGGER_TWIRRELOGGER_H_
15 
16 #include "../Core/Actuator.h"
17 #include "../Core/Sensor.h"
18 
19 #include <fstream>
20 #include <string>
21 #include <memory>
22 #include <vector>
23 #include <chrono>
24 #include <thread>
25 #include <mutex>
26 #include <numeric>
27 #include <queue>
28 
29 namespace twirre
30 {
32  {
33  public:
34  TwirreLogger(const std::string & logpath, const std::string & binpath);
35  virtual ~TwirreLogger();
36 
37  void logActuators(std::map<std::string, Actuator*>& actuators);
38  void logSensors(std::map<std::string, Sensor*>& sensors);
39  void logSensorEvent(Sensor * sensor, std::map<std::string, Value *> sensorValues);
40  void manualSensorEvent(std::string sensorName, const std::vector<std::pair<std::string, std::string>> & values);
41  void logActuatorEvent(Actuator * actuator, std::map<std::string, Parameter *> actuatorParameters);
42 
43  //callback for twirrelink
44  void onDevicelistChanged(void);
45 
46  //get current log timestamp
47  uint64_t getTimestamp(void);
48 
49  //arrays with more than max elements will not have their data written to the binfile
50  void setMaxArraySize(size_t max);
51 
52  private:
53  std::shared_ptr<std::ofstream> _logfile;
54  std::shared_ptr<std::ofstream> _binfile;
55  std::chrono::time_point<std::chrono::steady_clock> _tp_start;
56 
57  size_t _binaryDataOffset = 0; //offset where the next binary blob should be written in the binfile
58  size_t _maxArraySize = std::numeric_limits<size_t>::max(); //arrays with size above this value are not written to the binfile
59 
60  template<class T>
61  std::string logDeviceValues(const std::map<std::string, T *> & values);
62 
63  void logString(const std::string & str);
64  void logBinArrayValue(Value * val);
65 
66  //async thread
67  std::vector<std::string> _logQueue;
68  std::vector<char> _binQueue;
69  std::size_t _binQueueSize = 0; //_binQueue is used as dynamic array, so vector::size() will not work. This means we have to keep track of array size ourselves.
70  std::mutex _logfileMutex;
71  std::mutex _binfileMutex;
72  std::condition_variable _logfileCV;
73  std::condition_variable _binfileCV;
74  std::thread * _logfileThread;
75  std::thread * _binfileThread;
76 
77  bool _runLogfileThread = false;
78  bool _runBinfileThread = false;
79 
80  void logfileThreadMain();
81  void binfileThreadMain();
82 
83  };
84 }
85 #endif /* LOGGER_TWIRRELOGGER_H_ */
Definition: TwirreLogger.h:31
Definition: Sensor.h:22
Definition: Value.h:140
Actuator is the base class for all 'actuator'-type devices.
Definition: Actuator.h:41