SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1/*
2 * SatNOGS-COMMS MCU software
3 *
4 * Copyright (C) 2023, Libre Space Foundation <http://libre.space>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GNU General Public License v3.0 or later
20 */
21#pragma once
22
23#include <etl/circular_buffer.h>
24#include <etl/string.h>
25#include <initializer_list>
27#include <string>
28#include <zephyr/devicetree.h>
29#include <zephyr/kernel.h>
30#include <zephyr/logging/log.h>
31
32#include "time.hpp"
33namespace satnogs::comms
34{
35
67class logger
68{
69public:
70 static constexpr const char *mcu_log_dir = "/SD" CONFIG_STORAGE_LOG_MCU_PATH;
71
75 enum class target
76 {
77 RTT = 0,
78 UART = 1,
81 };
82
87 logger(logger const &) = delete;
88
93 void
94 operator=(logger const &) = delete;
95
101 static logger &
103 {
104 static logger instance;
105 return instance;
106 }
107
108 void
109 boot();
110
111 void
112 log(std::initializer_list<target> list, const lib::exception &e);
113
114 void
115 log(std::initializer_list<target> list, const std::exception &e);
116
117 void
118 log(std::initializer_list<target> list, const etl::exception &e);
119
120 void
121 log(std::initializer_list<target> list, const char *msg);
122
123 void
124 log(const lib::exception &e);
125
126 void
127 log(const std::exception &e);
128
129 void
130 log(const etl::exception &e);
131
132 void
133 log(const char *msg);
134
135 void
136 log(const etl::istring &msg);
137
138 etl::string<CONFIG_LOG_MAX_MSG_LEN>
139 get_latest_exception() const;
140
141 void
142 delete_storage_logs(struct tm &start, struct tm &end);
143
145 {
146 public:
148 uint64_t m_t_posix;
149 etl::string<CONFIG_LOG_MAX_MSG_LEN> m_log;
150
151 ring_buf_msg() : m_t_src(time::time_src::UPTIME), m_t_posix(0), m_log("") {}
152
153 ring_buf_msg(time::time_src t_src, uint64_t t_posix,
154 const etl::string<CONFIG_LOG_MAX_MSG_LEN> &log)
155 : m_t_src(t_src), m_t_posix(t_posix), m_log(log)
156 {
157 }
158 };
159
160 void
161 get_ring_buffer_log(ring_buf_msg &data, size_t index);
162
163 size_t
165
166private:
177 template <typename T>
178 void
179 log_unlocked(std::initializer_list<target> list, const T &m)
180 {
181 for (auto i : list) {
182 switch (i) {
183 case target::RTT:
184 rtt_push(m);
185 break;
186 case target::UART:
187 uart_push(m);
188 break;
190 ring_buffer_push(m);
191 break;
192 case target::STORAGE:
193 storage_push(m);
194 break;
195 default:
196 break;
197 }
198 }
199 }
200
201 void
202 rtt_push(const lib::exception &e);
203
204 void
205 rtt_push(const std::exception &e);
206
207 void
208 rtt_push(const etl::exception &e);
209
210 void
211 rtt_push(const char *msg);
212
213 void
214 ring_buffer_push(const lib::exception &e);
215
216 void
217 ring_buffer_push(const std::exception &e);
218
219 void
220 ring_buffer_push(const etl::exception &e);
221
222 void
223 ring_buffer_push(const char *msg);
224
225 void
226 uart_push(const lib::exception &e);
227
228 void
229 uart_push(const std::exception &e);
230
231 void
232 uart_push(const etl::exception &e);
233
234 void
235 uart_push(const char *msg);
236
237 void
238 storage_push(const lib::exception &e);
239
240 void
241 storage_push(const std::exception &e);
242
243 void
244 storage_push(const etl::exception &e);
245
246 void
247 storage_push(const char *msg);
248
249 int
250 backup_sram_push(const lib::exception &e);
251
252 int
253 backup_sram_push(const std::exception &e);
254
255 int
256 backup_sram_push(const etl::exception &e);
257
258 logger();
259
260 void
261 current_log_file(etl::istring &path);
262
269 etl::circular_buffer<ring_buf_msg, CONFIG_LOG_RING_BUFFER_MSGS> m_ring_buffer;
273 mutable struct k_mutex m_mtx;
274
275 etl::string<CONFIG_STORAGE_MAX_PATH_LEN> m_logs_path;
276};
277
278} // namespace satnogs::comms
Exception base class.
Definition exception.hpp:63
etl::string< CONFIG_LOG_MAX_MSG_LEN > m_log
Definition logger.hpp:149
ring_buf_msg(time::time_src t_src, uint64_t t_posix, const etl::string< CONFIG_LOG_MAX_MSG_LEN > &log)
Definition logger.hpp:153
etl::string< CONFIG_LOG_MAX_MSG_LEN > get_latest_exception() const
Retrieves the latest exception message stored in BACKUP_SRAM.
Definition logger.cpp:791
size_t get_ring_buffer_size()
Definition logger.cpp:774
target
Enumeration for targets to send logs to.
Definition logger.hpp:76
@ RING_BUFFER
In-memory ring buffer for storing recent log messages.
Definition logger.hpp:79
@ STORAGE
eMMC logging for persistent storage
Definition logger.hpp:80
@ UART
UART logging output.
Definition logger.hpp:78
@ RTT
Real-Time Transfer logging (Segger RTT).
Definition logger.hpp:77
void get_ring_buffer_log(ring_buf_msg &data, size_t index)
Definition logger.cpp:780
static constexpr const char * mcu_log_dir
Definition logger.hpp:70
static logger & get_instance()
Singleton access to the logger subsystem.
Definition logger.hpp:102
void delete_storage_logs(struct tm &start, struct tm &end)
Definition logger.cpp:682
logger(logger const &)=delete
Disabled copy constructor to enforce singleton.
void boot()
Logs system initialization details to multiple targets (RTT, UART, and RING_BUFFER).
Definition logger.cpp:93
void operator=(logger const &)=delete
Disabled assignment operator to enforce singleton.
void log(std::initializer_list< target > list, const lib::exception &e)
Logs a satnogs::comms::lib::exception to a specified set of targets.
Definition logger.cpp:184
Time and position information.
Definition time.hpp:60
time_src
Source of the reported time.
Definition time.hpp:74