Skyward boardcore
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1/* Copyright (c) 2018 Skyward Experimental Rocketry
2 * Author: Federico Terraneo
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 <Singleton.h>
26#include <miosix.h>
27#include <stdint.h>
28
29#include <cstdio>
30#include <list>
31#include <queue>
32#include <string>
33#include <type_traits>
34
35#include "LoggerStats.h"
36
37namespace Boardcore
38{
39
43enum class LoggerResult
44{
45 Queued,
46 Dropped,
47 Ignored,
49};
50
54class Logger : public Singleton<Logger>
55{
56 friend class Singleton<Logger>;
57
58public:
72 bool start();
73
83 void stop();
84
88 static bool testSDCard();
89
91
92 std::string getCurrentFileName();
93
95
96 void resetStats();
97
98 bool isStarted() const;
99
117 template <typename T>
118 LoggerResult log(const T& t);
119
125 void logStats();
126
130 static constexpr unsigned int getMaxFilenameNumber()
131 {
132 return maxFilenameNumber;
133 }
134
135private:
136 Logger();
137
138 static std::string getFileName(int logNumber);
139
140 static void packThreadLauncher(void* argv);
141
142 static void writeThreadLauncher(void* argv);
143
147 void packThread();
148
152 void writeThread();
153
161 LoggerResult logImpl(const char* name, const void* data, unsigned int size);
162
163 static constexpr unsigned int maxFilenameNumber =
164 10000;
165#ifndef _ARCH_CORTEXM3_STM32F2
166 static constexpr unsigned int maxRecordSize = 512;
167 static constexpr unsigned int numRecords = 512;
168 static constexpr unsigned int numBuffers = 8;
169 static constexpr unsigned int bufferSize = 64 * 1024;
170#else
171 static constexpr unsigned int maxRecordSize = 512;
172 static constexpr unsigned int numRecords = 64;
173 static constexpr unsigned int numBuffers = 8;
174 static constexpr unsigned int bufferSize = 4 * 1024;
175#endif
176
183 class Record
184 {
185 public:
186 Record() : size(0) {}
187 char data[maxRecordSize] = {};
188 unsigned int size;
189 };
190
197 class Buffer
198 {
199 public:
200 Buffer() : size(0) {}
201 char data[bufferSize] = {};
202 unsigned int size;
203 };
204
205 int fileNumber = -1;
206
207 miosix::Queue<Record*, numRecords> fullRecordsQueue;
208 miosix::Queue<Record*, numRecords> emptyRecordsQueue;
209 std::queue<Buffer*, std::list<Buffer*>> fullBufferList;
210 std::queue<Buffer*, std::list<Buffer*>> emptyBufferList;
211 miosix::FastMutex mutex;
212 miosix::ConditionVariable cond;
213
214 miosix::Thread* packTh = nullptr;
215 miosix::Thread* writeTh = nullptr;
216
217 volatile bool started = false;
218
219 FILE* file = nullptr;
220 LoggerStats stats;
221};
222
223template <typename T>
225{
226 static_assert(
227 std::is_trivially_copyable<T>::value,
228 "The type T must be trivially copyable in order to be logged!");
229
230 return logImpl(typeid(t).name(), &t, sizeof(t));
231}
232
233} // namespace Boardcore
Buffered logger. Needs to be started before it can be used.
Definition Logger.h:55
LoggerResult log(const T &t)
Call this function to log a class.
Definition Logger.h:224
bool isStarted() const
Definition Logger.cpp:163
void logStats()
Log logger stats using the logger itself.
Definition Logger.cpp:165
std::string getCurrentFileName()
Definition Logger.cpp:140
static constexpr unsigned int getMaxFilenameNumber()
Returns the Max Filename number.
Definition Logger.h:130
int getCurrentLogNumber()
Definition Logger.cpp:138
LoggerStats getStats()
Definition Logger.cpp:142
static bool testSDCard()
Tests if the Logger can write to the SD card by opening a file.
Definition Logger.cpp:131
void stop()
Call this function to stop the logger.
Definition Logger.cpp:111
bool start()
Call this function to start the logger.
Definition Logger.cpp:46
This file includes all the types the logdecoder script will decode.
LoggerResult
Possible outcomes of Logger::log().
Definition Logger.h:44
@ Dropped
Buffers are currently full, data will not be written. Sorry.
@ Queued
Data has been accepted by the logger and will be written.
@ TooLarge
Data is too large to be logged. Increase maxRecordSize.
@ Ignored
Logger is currently stopped, data will not be written.
Statistics for the logger.
Definition LoggerStats.h:35