ISLogger.h
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright (c) 2014-2020 Inertial Sense, Inc. - http://inertialsense.com
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 IS_LOGGER_H
14 #define IS_LOGGER_H
15 
16 #define _FILE_OFFSET_BITS 64
17 
18 #include <stdio.h>
19 #include <string>
20 #include <vector>
21 #include <map>
22 
23 #include "DeviceLogSerial.h"
24 
25 #if !defined(PLATFORM_IS_EVB_2) || !PLATFORM_IS_EVB_2
26 #include "DeviceLogSorted.h"
27 #include "DeviceLogCSV.h"
28 #include "DeviceLogJSON.h"
29 #include "DeviceLogKML.h"
30 #endif
31 
32 #if PLATFORM_IS_EVB_2
33 #include "drivers/d_time.h"
34 #endif
35 
36 #include "ISConstants.h"
37 #include "ISDisplay.h"
38 #include "ISLogFile.h"
39 
40 using namespace std;
41 
42 // default logging path if none specified
43 #define DEFAULT_LOGS_DIRECTORY "IS_logs"
44 
45 class cLogStats;
46 
47 class cISLogger
48 {
49 public:
50  enum eLogType
51  {
52  LOGTYPE_DAT = 0,
56  LOGTYPE_JSON
57  };
58 
59  static const string g_emptyString;
60 
61  cISLogger();
62  virtual ~cISLogger();
63 
64  // Setup logger to read from file.
65  bool LoadFromDirectory(const string& directory, eLogType logType = LOGTYPE_DAT, vector<string> serials = {});
66 
67  // Setup logger for writing to file.
68  bool InitSave(eLogType logType = LOGTYPE_DAT, const string& directory = g_emptyString, int numDevices = 1, float maxDiskSpacePercent = 0.5f, uint32_t maxFileSize = 1024 * 1024 * 5, bool useSubFolderTimestamp = true);
69  bool InitSaveTimestamp(const string& timeStamp, const string& directory = g_emptyString, const string& subDirectory = g_emptyString, int numDevices = 1, eLogType logType = LOGTYPE_DAT, float maxDiskSpacePercent = 0.5f, uint32_t maxFileSize = 1024 * 1024 * 5, bool useSubFolderTimestamp = true);
70 
71  // update internal state, handle timeouts, etc.
72  void Update();
73  bool LogData(unsigned int device, p_data_hdr_t* dataHdr, const uint8_t* dataBuf);
74  p_data_t* ReadData(unsigned int device = 0);
75  p_data_t* ReadNextData(unsigned int& device);
76  void EnableLogging(bool enabled) { m_enabled = enabled; }
77  bool Enabled() { return m_enabled; }
78  void CloseAllFiles();
79  void OpenWithSystemApp();
80  string TimeStamp() { return m_timeStamp; }
81  string LogDirectory() { return m_directory; }
82  uint64_t LogSizeAll();
83  uint64_t LogSize(unsigned int device = 0);
84  float LogSizeAllMB();
85  float LogSizeMB(unsigned int device = 0);
86  float FileSizeMB(unsigned int device = 0);
87  uint32_t FileCount(unsigned int device = 0);
88  uint32_t GetDeviceCount() { return (uint32_t)m_devices.size(); }
89  bool SetDeviceInfo(const dev_info_t *info, unsigned int device = 0);
90  const dev_info_t* GetDeviceInfo(unsigned int device = 0);
91  bool CopyLog(cISLogger& log, const string& timestamp = g_emptyString, const string& outputDir = g_emptyString, eLogType logType = LOGTYPE_DAT, float maxDiskSpacePercent = 0.5f, uint32_t maxFileSize = 1024 * 1024 * 5, bool useSubFolderTimestamp = true);
92  const cLogStats& GetStats() { return *m_logStats; }
93  eLogType GetType() { return m_logType; }
94 
99  time_t GetTimeoutFlushSeconds() { return m_timeoutFlushSeconds; }
100 
105  void SetTimeoutFlushSeconds(time_t timeoutFlushSeconds) { m_timeoutFlushSeconds = timeoutFlushSeconds; }
106 
107  // check if a data header is corrupt
108  static bool LogHeaderIsCorrupt(const p_data_hdr_t* hdr);
109 
110  // create a timestamp
111  static string CreateCurrentTimestamp();
112 
113  // check if a data packet is corrupt, NULL data is OK
114  static bool LogDataIsCorrupt(const p_data_t* data);
115 
116  // read all log data into memory - if the log is over 1.5 GB this will fail on 32 bit processes
117  // the map contains device id (serial number) key and a vector containing log data for each data id, which will be an empty vector if no log data for that id
118  static bool ReadAllLogDataIntoMemory(const string& directory, map<uint32_t, vector<vector<uint8_t>>>& data);
119 
120  void SetKmlConfig(bool showPath = true, bool showSample = false, bool showTimeStamp = true, double updatePeriodSec = 1.0, bool altClampToGround = true)
121  {
122  m_showPath = showPath;
123  m_showSample = showSample;
124  m_showTimeStamp = showTimeStamp;
125  m_iconUpdatePeriodSec = updatePeriodSec;
126  m_altClampToGround = altClampToGround;
127 
128  for (unsigned int dev = 0; dev < GetDeviceCount(); dev++)
129  {
130  m_devices[dev]->SetKmlConfig(m_showPath, m_showSample, m_showTimeStamp, m_iconUpdatePeriodSec, m_altClampToGround);
131  }
132  }
133 
134  static eLogType ParseLogType(const string& logTypeString)
135  {
136  if (logTypeString == "csv")
137  {
138  return cISLogger::eLogType::LOGTYPE_CSV;
139  }
140  else if (logTypeString == "kml")
141  {
142  return cISLogger::eLogType::LOGTYPE_KML;
143  }
144  else if (logTypeString == "sdat")
145  {
146  return cISLogger::eLogType::LOGTYPE_SDAT;
147  }
148  else if (logTypeString == "json")
149  {
150  return cISLogger::eLogType::LOGTYPE_JSON;
151  }
152  return cISLogger::eLogType::LOGTYPE_DAT;
153  }
154 
155 private:
156 #if CPP11_IS_ENABLED
157  cISLogger(const cISLogger& copy) = delete;
158 #else
159  cISLogger(const cISLogger& copy); // Disable copy constructors
160 #endif
161 
162  bool InitSaveCommon(eLogType logType, const string& directory, const string& subDirectory, int numDevices, float maxDiskSpacePercent, uint32_t maxFileSize, bool useSubFolderTimestamp);
163  bool InitDevicesForWriting(int numDevices = 1);
164  void Cleanup();
165 
166  static time_t GetTime()
167  {
168 #if PLATFORM_IS_EVB_2
169  return static_cast<time_t>(time_msec() / 1000);
170 #else
171  return time(NULLPTR);
172 #endif
173  }
174 
176  bool m_enabled;
177  string m_directory;
178  string m_timeStamp;
179  vector<cDeviceLog*> m_devices;
180  uint64_t m_maxDiskSpace;
181  uint32_t m_maxFileSize;
184 
192 };
193 
195 {
196 public:
197  uint64_t count; // count for this data id
198  uint64_t errorCount; // error count for this data id
199  double averageTimeDelta; // average time delta for the data id
200  double totalTimeDelta; // sum of all time deltas
206  uint64_t timestampDropCount; // count of delta timestamps > 50% different from previous delta timestamp
207 
208  cLogStatDataId();
209  void LogTimestamp(double timestamp);
210  void Printf();
211 };
212 
214 {
215 public:
216  cLogStatDataId dataIdStats[DID_COUNT];
217  uint64_t count; // count of all data ids
218  uint64_t errorCount; // total error count
219 
220  cLogStats();
221  void Clear();
222  void LogError(const p_data_hdr_t* hdr);
223  void LogData(uint32_t dataId);
224  void LogDataAndTimestamp(uint32_t dataId, double timestamp);
225  void Printf();
226  void WriteToFile(const string& fileName);
227 };
228 
229 #endif // IS_LOGGER_H
void SetKmlConfig(bool showPath=true, bool showSample=false, bool showTimeStamp=true, double updatePeriodSec=1.0, bool altClampToGround=true)
Definition: ISLogger.h:120
vector< cDeviceLog * > m_devices
Definition: ISLogger.h:179
uint32_t time_msec(void)
Definition: d_time.c:95
double totalTimeDelta
Definition: ISLogger.h:200
f
uint32_t GetDeviceCount()
Definition: ISLogger.h:88
uint64_t timestampDropCount
Definition: ISLogger.h:206
#define DID_COUNT
Definition: data_sets.h:138
double lastTimestamp
Definition: ISLogger.h:201
bool m_showPath
Definition: ISLogger.h:187
time_t m_lastCommTime
Definition: ISLogger.h:190
string TimeStamp()
Definition: ISLogger.h:80
uint64_t count
Definition: ISLogger.h:197
bool m_altClampToGround
Definition: ISLogger.h:185
uint64_t errorCount
Definition: ISLogger.h:218
#define NULLPTR
Definition: ISConstants.h:426
eLogType GetType()
Definition: ISLogger.h:93
void EnableLogging(bool enabled)
Definition: ISLogger.h:76
cISLogFileBase * m_errorFile
Definition: ISLogger.h:183
static eLogType ParseLogType(const string &logTypeString)
Definition: ISLogger.h:134
string m_timeStamp
Definition: ISLogger.h:178
string LogDirectory()
Definition: ISLogger.h:81
uint32_t m_maxFileSize
Definition: ISLogger.h:181
double m_iconUpdatePeriodSec
Definition: ISLogger.h:189
bool m_showTimeStamp
Definition: ISLogger.h:188
USBInterfaceDescriptor data
time_t m_timeoutFlushSeconds
Definition: ISLogger.h:191
double lastTimestampDelta
Definition: ISLogger.h:202
uint64_t count
Definition: ISLogger.h:217
uint64_t m_maxDiskSpace
Definition: ISLogger.h:180
void SetTimeoutFlushSeconds(time_t timeoutFlushSeconds)
Definition: ISLogger.h:105
uint64_t timestampDeltaCount
Definition: ISLogger.h:205
cLogStats * m_logStats
Definition: ISLogger.h:182
GeneratorWrapper< T > map(Func &&function, GeneratorWrapper< U > &&generator)
Definition: catch.hpp:3853
double maxTimestampDelta
Definition: ISLogger.h:204
double minTimestampDelta
Definition: ISLogger.h:203
static const string g_emptyString
Definition: ISLogger.h:59
bool Enabled()
Definition: ISLogger.h:77
const cLogStats & GetStats()
Definition: ISLogger.h:92
double averageTimeDelta
Definition: ISLogger.h:199
string m_directory
Definition: ISLogger.h:177
static time_t GetTime()
Definition: ISLogger.h:166
bool m_enabled
Definition: ISLogger.h:176
uint64_t errorCount
Definition: ISLogger.h:198
eLogType m_logType
Definition: ISLogger.h:175
time_t GetTimeoutFlushSeconds()
Definition: ISLogger.h:99
bool m_showSample
Definition: ISLogger.h:186


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:04