Skyward boardcore
Loading...
Searching...
No Matches
Deserializer.h
Go to the documentation of this file.
1/* Copyright (c) 2015-2022 Skyward Experimental Rocketry
2 * Authors: Luca Erbetta, Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <sys/stat.h>
26#include <tscpp/stream.h>
27
28#include <cstdio>
29#include <fstream>
30#include <iostream>
31#include <limits>
32#include <ostream>
33#include <regex>
34#include <string>
35#include <vector>
36
37using std::ostream;
38using std::string;
39using std::vector;
40using tscpp::TypePoolStream;
41using tscpp::UnknownInputArchive;
42
43namespace Boardcore
44{
45
46typedef std::numeric_limits<float> flt;
47
48// linter off
49
55{
56public:
60 Deserializer(std::string fileName);
61
63
74 template <typename T>
75 void registerType();
76
82 bool deserialize();
83
87 void close();
88
89private:
98 template <typename T>
99 void printType(T& t, std::string path = "", std::string prefix = "");
100
101 bool closed = false;
102
103 std::map<std::string, std::ofstream*> fileStreams;
104 tscpp::TypePoolStream tps;
105
106 std::string logFilename;
107 std::string logFilenameWithoutExtension;
108 std::string logFolderPath;
109};
110
111Deserializer::Deserializer(std::string logFilename) : logFilename(logFilename)
112{
113 // Prepare the folder path
114 logFilenameWithoutExtension =
115 logFilename.substr(0, logFilename.find_last_of("."));
116 logFolderPath = logFilenameWithoutExtension + "/";
117}
118
120
121template <typename T>
123{
124 std::function<void(T & t)> callback =
125 std::bind(&Deserializer::printType<T>, this, std::placeholders::_1,
126 logFolderPath, logFilenameWithoutExtension);
127
128 tps.registerType<T>(callback);
129}
130
131template <typename T>
132void Deserializer::printType(T& t, std::string path, std::string prefix)
133{
134 static std::ofstream* stream;
135 std::string demangledTypeName = tscpp::demangle(typeid(T).name());
136
137 // Replace the :: with the _ in order to make the format string cross
138 // platform compatible
139 demangledTypeName =
140 std::regex_replace(demangledTypeName, std::regex("::"), "_");
141
142 try
143 {
144 stream = fileStreams.at(demangledTypeName);
145 }
146 // If not already initialize, open the file and write the header
147 catch (std::out_of_range e)
148 {
149 stream = new std::ofstream();
150 std::string filename = path + prefix + "_" + demangledTypeName + ".csv";
151 std::cout << "Creating file " + filename << std::endl;
152 stream->open(filename);
153
154 if (!stream->is_open())
155 {
156 printf("Error opening file %s.\n", filename.c_str());
157 perror("Error is:");
158 return;
159 }
160
161 // Print the header in the file
162 *stream << T::header();
163
164 // Set stream precision to maximum float precision
165 stream->precision(flt::max_digits10);
166
167 // Add the file to the vector such that it will be closed
168 fileStreams.emplace(demangledTypeName, stream);
169 }
170
171 // Print data into the file if it is open
172 if (stream->is_open())
173 t.print(std::ref(*stream));
174}
175
177{
178 if (closed)
179 return false;
180
181 // Create the folder
182 mkdir(logFolderPath.c_str(), 0777);
183
184 // Move the log file into the folder
185 if (rename(logFilename.c_str(), (logFolderPath + logFilename).c_str()))
186 {
187 std::cout << logFilename + " does not exists." << std::endl;
188 return false;
189 }
190
191 // Open the log file
192 std::ifstream file(logFolderPath + logFilename);
193
194 // Check if the file exists
195 if (!file)
196 {
197 std::cout << logFolderPath + logFilename + " does not exists."
198 << std::endl;
199 return false;
200 }
201
202 tscpp::UnknownInputArchive inputArchive(file, tps);
203 while (true)
204 {
205 try
206 {
207 inputArchive.unserialize();
208 }
209 catch (tscpp::TscppException& ex)
210 {
211 // Reached end of file
212 if (strcmp(ex.what(), "eof") == 0)
213 {
214 return true;
215 }
216 // Unknown type found
217 else if (strcmp(ex.what(), "unknown type") == 0)
218 {
219 std::string unknownTypeName = ex.name();
220 std::cout << "Unknown type found: " << unknownTypeName
221 << std::endl;
222 return false;
223 }
224 }
225 }
226
227 file.close();
228 return true;
229}
230
232{
233 if (!closed)
234 {
235 closed = true;
236 for (auto it = fileStreams.begin(); it != fileStreams.end(); it++)
237 {
238 it->second->close();
239 delete it->second;
240 }
241 }
242}
243
244} // namespace Boardcore
Class used to deserialize the binary logs created using fedetft's logger into csv files.
void close()
Closes all the openend files.
bool deserialize()
Deserializes the provided file.
Deserializer(std::string fileName)
Initializes the deserializer with the filename provided.
void registerType()
Register a type to be deserialized.
This file includes all the types the logdecoder script will decode.
std::numeric_limits< float > flt