Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
AutonomyLogging.h
Go to the documentation of this file.
1
18#include <quill/Backend.h>
19#include <quill/Frontend.h>
20#include <quill/LogMacros.h>
21#include <quill/Logger.h>
22
23#include "quill/backend/PatternFormatter.h"
24#include "quill/core/Attributes.h"
25#include "quill/core/Common.h"
26#include "quill/core/Filesystem.h"
27
28#include "quill/sinks/ConsoleSink.h"
29#include "quill/sinks/RotatingFileSink.h"
30
31#include <RoveComm/RoveComm.h>
32#include <RoveComm/RoveCommManifest.h>
33#include <atomic>
34#include <shared_mutex>
35
37
38#include "./AutonomyConstants.h"
40
41#ifndef AUTONOMY_LOGGING_H
42#define AUTONOMY_LOGGING_H
43
44
69namespace logging
70{
72 // Declare namespace external variables and objects.
74
75 extern quill::Logger* g_qFileLogger;
76 extern quill::Logger* g_qConsoleLogger;
77 extern quill::Logger* g_qSharedLogger;
78 extern quill::Logger* g_qRoveCommLogger;
79
80 extern quill::LogLevel g_eConsoleLogLevel;
81 extern quill::LogLevel g_eFileLogLevel;
82 extern quill::LogLevel g_eRoveCommLogLevel;
83
84 extern std::string g_szProgramStartTimeString;
85
87 // Declare namespace methods.
89
90 void InitializeLoggers(std::string szLoggingOutputPath, std::string szProgramTimeLogsDir = timeops::GetTimestamp());
91
93 // Declare namespace callbacks.
95
96 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> SetLoggingLevelsCallback =
97 [](const rovecomm::RoveCommPacket<uint8_t>& stPacket, const sockaddr_in& stdAddr)
98 {
99 // Not using this.
100 (void) stdAddr;
101
102 // Convert Minimum Permitted Console Level to Integer Value
103 const int nMinConsoleLevel = static_cast<int>(constants::CONSOLE_MIN_LEVEL);
104 const int nMinFileLevel = static_cast<int>(constants::FILE_MIN_LEVEL);
105 const int nMinRoveCommLevel = static_cast<int>(constants::ROVECOMM_MIN_LEVEL);
106
107 // Convert Requested Console Level to Integer Value
108 const int nRequestedConsoleLevel = stPacket.vData[0];
109 const int nRequestedFileLevel = stPacket.vData[1];
110 const int nRequestedRoveCommLevel = stPacket.vData[2];
111
112 // Determine if change is allowed
113 bool bConsoleLevelChangePermitted = nRequestedConsoleLevel >= nMinConsoleLevel;
114 bool bFileLevelChangePermitted = nRequestedFileLevel >= nMinFileLevel;
115 bool bRoveCommLevelChangePermitted = nRequestedRoveCommLevel >= nMinRoveCommLevel;
116
117 // Convert RoveComm Enumeration to Quill Enumeration and store to logging globals if permitted
118 logging::g_eConsoleLogLevel = bConsoleLevelChangePermitted ? static_cast<quill::LogLevel>(stPacket.vData[0]) : logging::g_eConsoleLogLevel;
119 logging::g_eFileLogLevel = bFileLevelChangePermitted ? static_cast<quill::LogLevel>(stPacket.vData[1]) : logging::g_eFileLogLevel;
120 logging::g_eRoveCommLogLevel = bRoveCommLevelChangePermitted ? static_cast<quill::LogLevel>(stPacket.vData[2]) : logging::g_eRoveCommLogLevel;
121
122 // Submit logger message.
123 LOG_INFO(logging::g_qSharedLogger, "Incoming SETLOGGINGLEVELS: [Console: {}, File: {}, RoveComm: {}]", stPacket.vData[0], stPacket.vData[1], stPacket.vData[2]);
124 };
125
127 // Define namespace file filters.
129
130
140 class LoggingFilter : public quill::Filter
141 {
142 private:
143 // Declare private member variables.
144 quill::LogLevel m_eMinLogLevel;
145
146 public:
147
156 LoggingFilter(const std::string szFilterBaseType, const quill::LogLevel eMinLogLevel) : quill::Filter(szFilterBaseType)
157 {
158 // Set member variables.
159 m_eMinLogLevel = eMinLogLevel;
160 };
161
162
181 QUILL_NODISCARD bool filter(const quill::MacroMetadata* qLogMetadata,
182 uint64_t unLogTimestamp,
183 std::string_view szThreadID,
184 std::string_view szThreadName,
185 std::string_view szLoggerName,
186 quill::LogLevel qLogLevel,
187 std::string_view szLogMessage,
188 std::string_view szLogStatement) noexcept override
189 {
190 // Not using these.
191 (void) qLogMetadata;
192 (void) unLogTimestamp;
193 (void) szThreadID;
194 (void) szThreadName;
195 (void) szLoggerName;
196 (void) szLogMessage;
197 (void) szLogStatement;
198
199 // Log only m_eMinLogLevel or higher to stdout.
200 return qLogLevel >= m_eMinLogLevel;
201 }
202 };
203
205 // Define namespace custom sinks
207
208
236 class MRDTConsoleSink : public quill::ConsoleSink
237 {
238 public:
239
263 MRDTConsoleSink(const quill::ConsoleSink::Colours& qColors, // Custom Colors Import
264 const quill::ConsoleSink::ColourMode& qColorMode,
265 const std::string& szFormatPattern, // Custom Format Pattern
266 const std::string& szTimeFormat, // Custom Time Format
267 quill::Timezone qTimestampTimezone = quill::Timezone::LocalTime, // Timezone
268 const std::string& szStream = "stdout" // Stream
269 ) :
270 quill::ConsoleSink(qColors, qColorMode, szStream), // Pass Parameters into quill::ConsoleSink
271 qFormatter(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone)) // Pass Parameters into qFormatter type
272 {}
273
274 void write_log(const quill::MacroMetadata* qLogMetadata,
275 uint64_t unLogTimestamp,
276 std::string_view szThreadID,
277 std::string_view szThreadName,
278 const std::string& szProcessID,
279 std::string_view szLoggerName,
280 quill::LogLevel qLogLevel,
281 std::string_view szLogLevelDescription,
282 std::string_view szLogLevelShortCode,
283 const std::vector<std::pair<std::string, std::string>>* vNamedArgs,
284 std::string_view szLogMessage,
285 std::string_view) override;
286
287 private:
288 quill::PatternFormatter qFormatter;
289 };
290
291
319 class MRDTRotatingFileSink : public quill::RotatingFileSink
320 {
321 public:
322
347 MRDTRotatingFileSink(const quill::fs::path& qFilename, // File Path
348 const quill::RotatingFileSinkConfig& qConfig, // Rotating File Sink Config
349 const std::string& szFormatPattern, // Custom Format Pattern
350 const std::string& szTimeFormat, // Custom Time Format
351 quill::Timezone qTimestampTimezone = quill::Timezone::LocalTime, // Timezone
352 quill::FileEventNotifier qFileEventNotifier = quill::FileEventNotifier{} // Event Notifier (Default: None)
353 ) :
354 quill::RotatingFileSink(qFilename, qConfig, qFileEventNotifier), // Pass Parameters into quill::RotatingFileSink
355 qFormatter(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone)) // Pass Parameters into qFormatter type
356 {}
357
358 void write_log(const quill::MacroMetadata* qLogMetadata,
359 uint64_t unLogTimestamp,
360 std::string_view szThreadID,
361 std::string_view szThreadName,
362 const std::string& szProcessID,
363 std::string_view szLoggerName,
364 quill::LogLevel qLogLevel,
365 std::string_view szLogLevelDescription,
366 std::string_view szLogLevelShortCode,
367 const std::vector<std::pair<std::string, std::string>>* vNamedArgs,
368 std::string_view szLogMessage,
369 std::string_view) override;
370
371 private:
372 quill::PatternFormatter qFormatter;
373 };
374
375
402 class MRDTRoveCommSink : public quill::Sink
403 {
404 private:
405 quill::PatternFormatter qFormatter;
406
407
417 std::vector<char> StringToVector(const std::string& szString)
418 {
419 std::vector<char> result;
420 int length = std::min(static_cast<int>(szString.length()), 255);
421 result.reserve(length);
422
423 for (int i = 0; i < length; ++i)
424 {
425 result.push_back(szString[i]);
426 }
427
428 return result;
429 }
430
431 public:
432
455 MRDTRoveCommSink(std::string const& szFormatPattern, std::string const& szTimeFormat, quill::Timezone qTimestampTimezone) :
456 qFormatter(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone))
457 {}
458
459
465 ~MRDTRoveCommSink() override = default;
466
467 void write_log(const quill::MacroMetadata* qLogMetadata,
468 uint64_t unLogTimestamp,
469 std::string_view szThreadID,
470 std::string_view szThreadName,
471 const std::string& szProcessID,
472 std::string_view szLoggerName,
473 quill::LogLevel qLogLevel,
474 std::string_view szLogLevelDescription,
475 std::string_view szLogLevelShortCode,
476 const std::vector<std::pair<std::string, std::string>>* vNamedArgs,
477 std::string_view szLogMessage,
478 std::string_view) override;
479
480
487 void flush_sink() noexcept override {}
488 };
489
490} // namespace logging
491#endif // AUTONOMY_LOGGING_H
Defines and implements functions related to operations on time and date within the timeops namespace.
This class serves as a container class for handling log filtering of loggers. This must be used if yo...
Definition AutonomyLogging.h:141
QUILL_NODISCARD bool filter(const quill::MacroMetadata *qLogMetadata, uint64_t unLogTimestamp, std::string_view szThreadID, std::string_view szThreadName, std::string_view szLoggerName, quill::LogLevel qLogLevel, std::string_view szLogMessage, std::string_view szLogStatement) noexcept override
This method should never be called by this codebase, it is called internally by the quill library....
Definition AutonomyLogging.h:181
LoggingFilter(const std::string szFilterBaseType, const quill::LogLevel eMinLogLevel)
Construct a new Console Filter object.
Definition AutonomyLogging.h:156
A custom console sink for logging messages with specific formatting and timestamping....
Definition AutonomyLogging.h:237
void write_log(const quill::MacroMetadata *qLogMetadata, uint64_t unLogTimestamp, std::string_view szThreadID, std::string_view szThreadName, const std::string &szProcessID, std::string_view szLoggerName, quill::LogLevel qLogLevel, std::string_view szLogLevelDescription, std::string_view szLogLevelShortCode, const std::vector< std::pair< std::string, std::string > > *vNamedArgs, std::string_view szLogMessage, std::string_view) override
Writes a log message to the MRDT console sink, formats the message using the provided formatter,...
Definition AutonomyLogging.cpp:214
MRDTConsoleSink(const quill::ConsoleSink::Colours &qColors, const quill::ConsoleSink::ColourMode &qColorMode, const std::string &szFormatPattern, const std::string &szTimeFormat, quill::Timezone qTimestampTimezone=quill::Timezone::LocalTime, const std::string &szStream="stdout")
Constructs a new MRDTConsoleSink object with specified formatting and console colors....
Definition AutonomyLogging.h:263
A custom rotating file sink that formats and logs messages to a file with automatic rotation based on...
Definition AutonomyLogging.h:320
void write_log(const quill::MacroMetadata *qLogMetadata, uint64_t unLogTimestamp, std::string_view szThreadID, std::string_view szThreadName, const std::string &szProcessID, std::string_view szLoggerName, quill::LogLevel qLogLevel, std::string_view szLogLevelDescription, std::string_view szLogLevelShortCode, const std::vector< std::pair< std::string, std::string > > *vNamedArgs, std::string_view szLogMessage, std::string_view) override
Writes a log message to the MRDT rotating file sink. The log message is first formatted using a custo...
Definition AutonomyLogging.cpp:296
MRDTRotatingFileSink(const quill::fs::path &qFilename, const quill::RotatingFileSinkConfig &qConfig, const std::string &szFormatPattern, const std::string &szTimeFormat, quill::Timezone qTimestampTimezone=quill::Timezone::LocalTime, quill::FileEventNotifier qFileEventNotifier=quill::FileEventNotifier{})
Constructs a new MRDTRotatingFileSink object with specified formatting, file rotation settings,...
Definition AutonomyLogging.h:347
A custom logger sink designed to send formatted log messages over the RoveComm protocol....
Definition AutonomyLogging.h:403
MRDTRoveCommSink(std::string const &szFormatPattern, std::string const &szTimeFormat, quill::Timezone qTimestampTimezone)
Constructs a new MRDTRoveCommSink object with the specified format pattern, time format,...
Definition AutonomyLogging.h:455
void write_log(const quill::MacroMetadata *qLogMetadata, uint64_t unLogTimestamp, std::string_view szThreadID, std::string_view szThreadName, const std::string &szProcessID, std::string_view szLoggerName, quill::LogLevel qLogLevel, std::string_view szLogLevelDescription, std::string_view szLogLevelShortCode, const std::vector< std::pair< std::string, std::string > > *vNamedArgs, std::string_view szLogMessage, std::string_view) override
Formats a log message and sends it as a RoveComm packet to the BaseStation. The log message is format...
Definition AutonomyLogging.cpp:385
~MRDTRoveCommSink() override=default
Destroy the MRDTRoveCommSink object.
std::vector< char > StringToVector(const std::string &szString)
A utility function to convert a string to a vector that is no longer than 255 characters long.
Definition AutonomyLogging.h:417
void flush_sink() noexcept override
This method should never be called by this codebase, it is called internally by the quill library.
Definition AutonomyLogging.h:487
::uint64_t uint64_t
Namespace containing all global type/structs that will be used project wide for logging.
Definition AutonomyLogging.cpp:33
void InitializeLoggers(std::string szLoggingOutputPath, std::string szProgramTimeLogsDir)
Logger Initializer - Sets Up all the logging handlers required for having the above loggers.
Definition AutonomyLogging.cpp:58
std::string GetTimestamp(const std::string &szFormat="%Y%m%d-%H%M%S")
Accessor for getting the current time in a specified format.
Definition TimeOperations.hpp:42