DeviceLog.cpp
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 #include <ctime>
14 #include <string>
15 #include <sstream>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <iomanip>
19 #include <iostream>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 
24 #include "DeviceLog.h"
25 #include "ISFileManager.h"
26 #include "ISLogger.h"
27 #include "ISConstants.h"
28 #include "ISDataMappings.h"
29 #include "ISLogFileFactory.h"
30 
31 using namespace std;
32 
33 
35 {
36  m_pFile = NULL;
37  m_pHandle = 0;
38  m_fileSize = 0;
39  m_logSize = 0;
40  m_fileCount = 0;
41  memset(&m_devInfo, 0, sizeof(dev_info_t));
42  m_logStats = new cLogStats;
43  m_altClampToGround = true;
44  m_showTracks = true;
45  m_showPointTimestamps = true;
46  m_pointUpdatePeriodSec = 1.0f;
47 }
48 
50 {
51  // Close open files
52  CloseISLogFile(m_pFile);
53  CloseAllFiles();
54  delete m_logStats;
55 }
56 
57 void cDeviceLog::InitDeviceForWriting(int pHandle, std::string timestamp, std::string directory, uint64_t maxDiskSpace, uint32_t maxFileSize)
58 {
59  m_pHandle = pHandle;
60  m_timeStamp = timestamp;
61  m_directory = directory;
62  m_fileCount = 0;
63  m_maxDiskSpace = maxDiskSpace;
64  m_maxFileSize = maxFileSize;
65  m_logSize = 0;
66 }
67 
68 
70 {
71  m_fileSize = 0;
72  m_logSize = 0;
73  m_fileCount = 0;
74 }
75 
76 
78 {
79 #if 1
80  string str = m_directory + "/stats_SN" + to_string(m_devInfo.serialNumber) + ".txt";
81  m_logStats->WriteToFile(str);
82 #else // stringstream not working on embedded platform
83  ostringstream serialNumString;
84  serialNumString << m_devInfo.serialNumber;
85  m_logStats->WriteToFile(m_directory + "/stats_SN" + serialNumString.str() + ".txt");
86 #endif
87  m_logStats->Clear();
88  return true;
89 }
90 
92 {
93 
94 #if PLATFORM_IS_WINDOWS
95 
96  std::wstring stemp = std::wstring(m_fileName.begin(), m_fileName.end());
97  LPCWSTR filename = stemp.c_str();
98  ShellExecuteW(0, 0, filename, 0, 0, SW_SHOW);
99 
100 #endif
101 
102  return true;
103 }
104 
105 
106 bool cDeviceLog::SaveData(p_data_hdr_t *dataHdr, const uint8_t* dataBuf)
107 {
108  if (dataHdr != NULL)
109  {
110  double timestamp = cISDataMappings::GetTimestamp(dataHdr, dataBuf);
111  m_logStats->LogDataAndTimestamp(dataHdr->id, timestamp);
112  }
113  return true;
114 }
115 
116 bool cDeviceLog::SetupReadInfo(const string& directory, const string& serialNum, const string& timeStamp)
117 {
118  m_directory = directory;
119  m_fileCount = 0;
120  m_timeStamp = timeStamp;
121  m_fileNames.clear();
122  vector<ISFileManager::file_info_t> fileInfos;
123  SetSerialNumber((uint32_t)strtoul(serialNum.c_str(), NULL, 10));
124  ISFileManager::GetDirectorySpaceUsed(directory, string("[\\/\\\\]" IS_LOG_FILE_PREFIX) + serialNum + "_", fileInfos, false, false);
125  if (fileInfos.size() != 0)
126  {
127  m_fileName = fileInfos[0].name;
128  for (size_t i = 0; i < fileInfos.size(); i++)
129  {
130  m_fileNames.push_back(fileInfos[i].name);
131  }
132  }
133  return true;
134 }
135 
136 
138 {
139  // Close existing file
140  CloseISLogFile(m_pFile);
141 
142  // Ensure directory exists
143  if (m_directory.empty())
144  {
145  return false;
146  }
147 
148  // create directory
149  _MKDIR(m_directory.c_str());
150 
151 #if !PLATFORM_IS_EMBEDDED
152 
153  // clear out space if we need to
154  if (m_maxDiskSpace != 0)
155  {
156  vector<ISFileManager::file_info_t> files;
157  uint64_t spaceUsed = ISFileManager::GetDirectorySpaceUsed(m_directory.c_str(), files, true, false);
158  unsigned int index = 0;
159 
160  // clear out old files until we have space
161  while (spaceUsed > m_maxDiskSpace && index < files.size())
162  {
163  spaceUsed -= files[index].size;
164  ISFileManager::DeleteFile(files[index++].name);
165  }
166  }
167 
168 #endif
169 
170  // Open new file
171  m_fileCount++;
172  uint32_t serNum = m_devInfo.serialNumber;
173  if (!serNum)
174  {
175  serNum = m_pHandle;
176  }
177  string fileName = GetNewFileName(serNum, m_fileCount, NULL);
178  m_pFile = CreateISLogFile(fileName, "wb");
179  m_fileSize = 0;
180 
181  if (m_pFile && m_pFile->isOpened())
182  {
183 #if LOG_DEBUG_WRITE
184  printf("Opened save file: %s\n", filename.str().c_str());
185 #endif
186  return true;
187  }
188  else
189  {
190 #if LOG_DEBUG_WRITE
191  printf("FAILED to open save file: %s\n", filename.str().c_str());
192 #endif
193  return false;
194  }
195 }
196 
197 
199 {
200  // Close file if open
201  CloseISLogFile(m_pFile);
202 
203  if (m_fileCount == m_fileNames.size())
204  {
205  return false;
206  }
207 
208  m_fileName = m_fileNames[m_fileCount++];
209  m_pFile = CreateISLogFile(m_fileName, "rb");
210 
211  if (m_pFile)
212  {
213 
214 #if LOG_DEBUG_READ
215  printf("File opened: %s\n", m_fileName.c_str());
216 #endif
217  return true;
218  }
219  else
220  {
221 #if LOG_DEBUG_READ
222  printf("FAILED to open file: %s\n", m_fileName.c_str());
223 #endif
224  return false;
225  }
226 }
227 
228 string cDeviceLog::GetNewFileName(uint32_t serialNumber, uint32_t fileCount, const char* suffix)
229 {
230  // file name
231 #if 1
232  char filename[200];
233  SNPRINTF(filename, sizeof(filename), "%s/%s%d_%s_%04d%s%s",
234  m_directory.c_str(),
236  (int)serialNumber,
237  m_timeStamp.c_str(),
238  (int)(fileCount % 10000),
239  (suffix == NULL || *suffix == 0 ? "" : (string("_") + suffix).c_str()),
240  LogFileExtention().c_str());
241  return filename;
242 #else
243  // This code is not working on the embedded platform
244  ostringstream filename;
245  filename << m_directory << "/" << IS_LOG_FILE_PREFIX <<
246  serialNumber << "_" <<
247  m_timeStamp << "_" <<
248  setfill('0') << setw(4) << (fileCount % 10000) <<
249  (suffix == NULL || *suffix == 0 ? "" : string("_") + suffix) <<
250  LogFileExtention();
251  return filename.str();
252 #endif
253 }
254 
255 
257 {
258  if (info == NULL)
259  {
260  return;
261  }
262  m_devInfo = *info;
263  SetSerialNumber(info->serialNumber);
264 }
265 
266 
268 {
269  if (data != NULL)
270  {
271  double timestamp = cISDataMappings::GetTimestamp(&data->hdr, data->buf);
272  m_logStats->LogDataAndTimestamp(data->hdr.id, timestamp);
273  }
274 }
275 
276 
filename
void OnReadData(p_data_t *data)
Definition: DeviceLog.cpp:267
virtual bool CloseAllFiles()
Definition: DeviceLog.cpp:77
uint32_t id
Definition: ISComm.h:375
#define printf(...)
#define NULL
Definition: nm_bsp.h:52
bool SetupReadInfo(const string &directory, const string &deviceName, const string &timeStamp)
Definition: DeviceLog.cpp:116
cISLogFileBase * CreateISLogFile()
virtual bool SaveData(p_data_hdr_t *dataHdr, const uint8_t *dataBuf)
Definition: DeviceLog.cpp:106
void CloseISLogFile(cISLogFileBase *&logFile)
static double GetTimestamp(const p_data_hdr_t *hdr, const uint8_t *buf)
void SetDeviceInfo(const dev_info_t *info)
Definition: DeviceLog.cpp:256
bool OpenNextReadFile()
Definition: DeviceLog.cpp:198
virtual void InitDeviceForWriting(int pHandle, string timestamp, string directory, uint64_t maxDiskSpace, uint32_t maxFileSize)
Definition: DeviceLog.cpp:57
#define IS_LOG_FILE_PREFIX
Definition: DeviceLog.h:29
bool DeleteFile(const std::string &fullFilePath)
p_data_hdr_t hdr
Definition: ISComm.h:388
#define SNPRINTF
Definition: ISConstants.h:146
bool OpenNewSaveFile()
Definition: DeviceLog.cpp:137
USBInterfaceDescriptor data
virtual void InitDeviceForReading()
Definition: DeviceLog.cpp:69
virtual ~cDeviceLog()
Definition: DeviceLog.cpp:49
uint32_t serialNumber
Definition: data_sets.h:442
#define _MKDIR(dir)
Definition: ISConstants.h:213
uint64_t GetDirectorySpaceUsed(const std::string &directory, bool recursive)
uint8_t buf[MAX_DATASET_SIZE]
Definition: ISComm.h:391
string GetNewFileName(uint32_t serialNumber, uint32_t fileCount, const char *suffix)
Definition: DeviceLog.cpp:228
virtual bool OpenWithSystemApp()
Definition: DeviceLog.cpp:91


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