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 extern std::string g_szLoggingOutputPath;
86
88 // Declare namespace methods.
90
91 void InitializeLoggers(std::string szLoggingOutputPath, std::string szProgramTimeLogsDir = timeops::GetTimestamp());
92
94 // Declare namespace callbacks.
96
97 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> SetLoggingLevelsCallback =
98 [](const rovecomm::RoveCommPacket<uint8_t>& stPacket, const sockaddr_in& stdAddr)
99 {
100 // Not using this.
101 (void) stdAddr;
102
103 // Convert Minimum Permitted Console Level to Integer Value
104 const int nMinConsoleLevel = static_cast<int>(constants::CONSOLE_MIN_LEVEL);
105 const int nMinFileLevel = static_cast<int>(constants::FILE_MIN_LEVEL);
106 const int nMinRoveCommLevel = static_cast<int>(constants::ROVECOMM_MIN_LEVEL);
107
108 // Convert Requested Console Level to Integer Value
109 const int nRequestedConsoleLevel = stPacket.vData[0];
110 const int nRequestedFileLevel = stPacket.vData[1];
111 const int nRequestedRoveCommLevel = stPacket.vData[2];
112
113 // Determine if change is allowed
114 bool bConsoleLevelChangePermitted = nRequestedConsoleLevel >= nMinConsoleLevel;
115 bool bFileLevelChangePermitted = nRequestedFileLevel >= nMinFileLevel;
116 bool bRoveCommLevelChangePermitted = nRequestedRoveCommLevel >= nMinRoveCommLevel;
117
118 // Convert RoveComm Enumeration to Quill Enumeration and store to logging globals if permitted
119 logging::g_eConsoleLogLevel = bConsoleLevelChangePermitted ? static_cast<quill::LogLevel>(stPacket.vData[0]) : logging::g_eConsoleLogLevel;
120 logging::g_eFileLogLevel = bFileLevelChangePermitted ? static_cast<quill::LogLevel>(stPacket.vData[1]) : logging::g_eFileLogLevel;
121 logging::g_eRoveCommLogLevel = bRoveCommLevelChangePermitted ? static_cast<quill::LogLevel>(stPacket.vData[2]) : logging::g_eRoveCommLogLevel;
122
123 // Submit logger message.
124 LOG_INFO(logging::g_qSharedLogger, "Incoming SETLOGGINGLEVELS: [Console: {}, File: {}, RoveComm: {}]", stPacket.vData[0], stPacket.vData[1], stPacket.vData[2]);
125 };
126
128 // Define namespace file filters.
130
131
141 class LoggingFilter : public quill::Filter
142 {
143 private:
144 // Declare private member variables.
145 quill::LogLevel m_eMinLogLevel;
146
147 public:
148
157 LoggingFilter(const std::string szFilterBaseType, const quill::LogLevel eMinLogLevel) : quill::Filter(szFilterBaseType)
158 {
159 // Set member variables.
160 m_eMinLogLevel = eMinLogLevel;
161 };
162
163
182 QUILL_NODISCARD bool filter(const quill::MacroMetadata* qLogMetadata,
183 uint64_t unLogTimestamp,
184 std::string_view szThreadID,
185 std::string_view szThreadName,
186 std::string_view szLoggerName,
187 quill::LogLevel qLogLevel,
188 std::string_view szLogMessage,
189 std::string_view szLogStatement) noexcept override
190 {
191 // Not using these.
192 (void) qLogMetadata;
193 (void) unLogTimestamp;
194 (void) szThreadID;
195 (void) szThreadName;
196 (void) szLoggerName;
197 (void) szLogMessage;
198 (void) szLogStatement;
199
200 // Log only m_eMinLogLevel or higher to stdout.
201 return qLogLevel >= m_eMinLogLevel;
202 }
203 };
204
206 // Define namespace custom sinks
208
209
237 class MRDTConsoleSink : public quill::ConsoleSink
238 {
239 public:
240
265 MRDTConsoleSink(const quill::ConsoleSinkConfig::Colours& qColors, // Custom Colors Import
266 const quill::ConsoleSinkConfig::ColourMode& qColorMode,
267 const std::string& szFormatPattern, // Custom Format Pattern
268 const std::string& szTimeFormat, // Custom Time Format
269 quill::Timezone qTimestampTimezone = quill::Timezone::LocalTime, // Timezone
270 const std::string& szStream = "stdout" // Stream
271 ) :
272 quill::ConsoleSink(
273 [&]
274 {
275 // Configure ConsoleSinkConfig in a lambda to inline
276 quill::ConsoleSinkConfig qConsoleConfig;
277 qConsoleConfig.set_stream(szStream);
278 qConsoleConfig.set_colour_mode(qColorMode);
279 qConsoleConfig.set_colours(qColors);
280 qConsoleConfig.set_override_pattern_formatter_options(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone));
281 return qConsoleConfig;
282 }()),
283 qFormatter(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone)) // Pass Parameters into qFormatter type
284 {}
285
286 void write_log(const quill::MacroMetadata* qLogMetadata,
287 uint64_t unLogTimestamp,
288 std::string_view szThreadID,
289 std::string_view szThreadName,
290 const std::string& szProcessID,
291 std::string_view szLoggerName,
292 quill::LogLevel qLogLevel,
293 std::string_view szLogLevelDescription,
294 std::string_view szLogLevelShortCode,
295 const std::vector<std::pair<std::string, std::string>>* vNamedArgs,
296 std::string_view szLogMessage,
297 std::string_view) override;
298
299 private:
300 quill::PatternFormatter qFormatter;
301 };
302
303
331 class MRDTRotatingFileSink : public quill::RotatingFileSink
332 {
333 public:
334
359 MRDTRotatingFileSink(const quill::fs::path& qFilename, // File Path
360 const quill::RotatingFileSinkConfig& qConfig, // Rotating File Sink Config
361 const std::string& szFormatPattern, // Custom Format Pattern
362 const std::string& szTimeFormat, // Custom Time Format
363 quill::Timezone qTimestampTimezone = quill::Timezone::LocalTime, // Timezone
364 quill::FileEventNotifier qFileEventNotifier = quill::FileEventNotifier{} // Event Notifier (Default: None)
365 ) :
366 quill::RotatingFileSink(qFilename, qConfig, qFileEventNotifier), // Pass Parameters into quill::RotatingFileSink
367 qFormatter(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone)) // Pass Parameters into qFormatter type
368 {}
369
370 void write_log(const quill::MacroMetadata* qLogMetadata,
371 uint64_t unLogTimestamp,
372 std::string_view szThreadID,
373 std::string_view szThreadName,
374 const std::string& szProcessID,
375 std::string_view szLoggerName,
376 quill::LogLevel qLogLevel,
377 std::string_view szLogLevelDescription,
378 std::string_view szLogLevelShortCode,
379 const std::vector<std::pair<std::string, std::string>>* vNamedArgs,
380 std::string_view szLogMessage,
381 std::string_view) override;
382
383 private:
384 quill::PatternFormatter qFormatter;
385 };
386
387
414 class MRDTRoveCommSink : public quill::Sink
415 {
416 private:
417 quill::PatternFormatter qFormatter;
418
419
429 std::vector<char> StringToVector(const std::string& szString)
430 {
431 std::vector<char> result;
432 int length = std::min(static_cast<int>(szString.length()), 255);
433 result.reserve(length);
434
435 for (int i = 0; i < length; ++i)
436 {
437 result.push_back(szString[i]);
438 }
439
440 return result;
441 }
442
443 public:
444
467 MRDTRoveCommSink(std::string const& szFormatPattern, std::string const& szTimeFormat, quill::Timezone qTimestampTimezone) :
468 qFormatter(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone))
469 {}
470
471
477 ~MRDTRoveCommSink() override = default;
478
479 void write_log(const quill::MacroMetadata* qLogMetadata,
480 uint64_t unLogTimestamp,
481 std::string_view szThreadID,
482 std::string_view szThreadName,
483 const std::string& szProcessID,
484 std::string_view szLoggerName,
485 quill::LogLevel qLogLevel,
486 std::string_view szLogLevelDescription,
487 std::string_view szLogLevelShortCode,
488 const std::vector<std::pair<std::string, std::string>>* vNamedArgs,
489 std::string_view szLogMessage,
490 std::string_view) override;
491
492
499 void flush_sink() noexcept override {}
500 };
501
502} // namespace logging
503#endif // AUTONOMY_LOGGING_H
Declares constants for the autonomy software.
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:142
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:182
LoggingFilter(const std::string szFilterBaseType, const quill::LogLevel eMinLogLevel)
Construct a new Console Filter object.
Definition AutonomyLogging.h:157
A custom console sink for logging messages with specific formatting and timestamping....
Definition AutonomyLogging.h:238
MRDTConsoleSink(const quill::ConsoleSinkConfig::Colours &qColors, const quill::ConsoleSinkConfig::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:265
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:219
A custom rotating file sink that formats and logs messages to a file with automatic rotation based on...
Definition AutonomyLogging.h:332
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:301
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:359
A custom logger sink designed to send formatted log messages over the RoveComm protocol....
Definition AutonomyLogging.h:415
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:467
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:390
~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:429
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:499
::uint64_t uint64_t
Namespace containing all global type/structs that will be used project wide for logging.
Definition AutonomyLogging.cpp:34
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:60
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