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
79 extern quill::LogLevel g_eConsoleLogLevel;
80 extern quill::LogLevel g_eFileLogLevel;
81
82 extern std::string g_szProgramStartTimeString;
83 extern std::string g_szLoggingOutputPath;
84
86 // Declare namespace methods.
88
89 void InitializeLoggers(std::string szLoggingOutputPath, std::string szProgramTimeLogsDir = timeops::GetTimestamp());
90
92 // Declare namespace callbacks.
94
95 const std::function<void(const rovecomm::RoveCommPacket<uint8_t>&, const sockaddr_in&)> SetLoggingLevelsCallback =
96 [](const rovecomm::RoveCommPacket<uint8_t>& stPacket, const sockaddr_in& stdAddr)
97 {
98 // Not using this.
99 (void) stdAddr;
100
101 // Convert Minimum Permitted Console Level to Integer Value
102 const int nMinConsoleLevel = static_cast<int>(constants::CONSOLE_MIN_LEVEL);
103 const int nMinFileLevel = static_cast<int>(constants::FILE_MIN_LEVEL);
104
105 // Convert Requested Console Level to Integer Value
106 const int nRequestedConsoleLevel = stPacket.vData[0];
107 const int nRequestedFileLevel = stPacket.vData[1];
108
109 // Determine if change is allowed
110 bool bConsoleLevelChangePermitted = nRequestedConsoleLevel >= nMinConsoleLevel;
111 bool bFileLevelChangePermitted = nRequestedFileLevel >= nMinFileLevel;
112
113 // Convert RoveComm Enumeration to Quill Enumeration and store to logging globals if permitted
114 logging::g_eConsoleLogLevel = bConsoleLevelChangePermitted ? static_cast<quill::LogLevel>(stPacket.vData[0]) : logging::g_eConsoleLogLevel;
115 logging::g_eFileLogLevel = bFileLevelChangePermitted ? static_cast<quill::LogLevel>(stPacket.vData[1]) : logging::g_eFileLogLevel;
116
117 // Submit logger message.
118 LOG_INFO(logging::g_qSharedLogger, "Incoming SETLOGGINGLEVELS: [Console: {}, File: {}]", stPacket.vData[0], stPacket.vData[1]);
119 };
120
122 // Define namespace file filters.
124
125
135 class LoggingFilter : public quill::Filter
136 {
137 private:
138 // Declare private member variables.
139 quill::LogLevel m_eMinLogLevel;
140
141 public:
142
151 LoggingFilter(const std::string szFilterBaseType, const quill::LogLevel eMinLogLevel) : quill::Filter(szFilterBaseType)
152 {
153 // Set member variables.
154 m_eMinLogLevel = eMinLogLevel;
155 };
156
157
176 QUILL_NODISCARD bool filter(const quill::MacroMetadata* qLogMetadata,
177 uint64_t unLogTimestamp,
178 std::string_view szThreadID,
179 std::string_view szThreadName,
180 std::string_view szLoggerName,
181 quill::LogLevel qLogLevel,
182 std::string_view szLogMessage,
183 std::string_view szLogStatement) noexcept override
184 {
185 // Not using these.
186 (void) qLogMetadata;
187 (void) unLogTimestamp;
188 (void) szThreadID;
189 (void) szThreadName;
190 (void) szLoggerName;
191 (void) szLogMessage;
192 (void) szLogStatement;
193
194 // Log only m_eMinLogLevel or higher to stdout.
195 return qLogLevel >= m_eMinLogLevel;
196 }
197 };
198
200 // Define namespace custom sinks
202
203
231 class MRDTConsoleSink : public quill::ConsoleSink
232 {
233 public:
234
259 MRDTConsoleSink(const quill::ConsoleSinkConfig::Colours& qColors, // Custom Colors Import
260 const quill::ConsoleSinkConfig::ColourMode& qColorMode,
261 const std::string& szFormatPattern, // Custom Format Pattern
262 const std::string& szTimeFormat, // Custom Time Format
263 quill::Timezone qTimestampTimezone = quill::Timezone::LocalTime, // Timezone
264 const std::string& szStream = "stdout" // Stream
265 ) :
266 quill::ConsoleSink(
267 [&]
268 {
269 // Configure ConsoleSinkConfig in a lambda to inline
270 quill::ConsoleSinkConfig qConsoleConfig;
271 qConsoleConfig.set_stream(szStream);
272 qConsoleConfig.set_colour_mode(qColorMode);
273 qConsoleConfig.set_colours(qColors);
274 qConsoleConfig.set_override_pattern_formatter_options(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone));
275 return qConsoleConfig;
276 }()),
277 qFormatter(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone)) // Pass Parameters into qFormatter type
278 {}
279
280 void write_log(const quill::MacroMetadata* qLogMetadata,
281 uint64_t unLogTimestamp,
282 std::string_view szThreadID,
283 std::string_view szThreadName,
284 const std::string& szProcessID,
285 std::string_view szLoggerName,
286 quill::LogLevel qLogLevel,
287 std::string_view szLogLevelDescription,
288 std::string_view szLogLevelShortCode,
289 const std::vector<std::pair<std::string, std::string>>* vNamedArgs,
290 std::string_view szLogMessage,
291 std::string_view) override;
292
293 private:
294 quill::PatternFormatter qFormatter;
295 };
296
297
325 class MRDTRotatingFileSink : public quill::RotatingFileSink
326 {
327 public:
328
353 MRDTRotatingFileSink(const quill::fs::path& qFilename, // File Path
354 const quill::RotatingFileSinkConfig& qConfig, // Rotating File Sink Config
355 const std::string& szFormatPattern, // Custom Format Pattern
356 const std::string& szTimeFormat, // Custom Time Format
357 quill::Timezone qTimestampTimezone = quill::Timezone::LocalTime, // Timezone
358 quill::FileEventNotifier qFileEventNotifier = quill::FileEventNotifier{} // Event Notifier (Default: None)
359 ) :
360 quill::RotatingFileSink(qFilename, qConfig, qFileEventNotifier), // Pass Parameters into quill::RotatingFileSink
361 qFormatter(quill::PatternFormatterOptions(szFormatPattern, szTimeFormat, qTimestampTimezone)) // Pass Parameters into qFormatter type
362 {}
363
364 void write_log(const quill::MacroMetadata* qLogMetadata,
365 uint64_t unLogTimestamp,
366 std::string_view szThreadID,
367 std::string_view szThreadName,
368 const std::string& szProcessID,
369 std::string_view szLoggerName,
370 quill::LogLevel qLogLevel,
371 std::string_view szLogLevelDescription,
372 std::string_view szLogLevelShortCode,
373 const std::vector<std::pair<std::string, std::string>>* vNamedArgs,
374 std::string_view szLogMessage,
375 std::string_view) override;
376
377 private:
378 quill::PatternFormatter qFormatter;
379 };
380} // namespace logging
381#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:136
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:176
LoggingFilter(const std::string szFilterBaseType, const quill::LogLevel eMinLogLevel)
Construct a new Console Filter object.
Definition AutonomyLogging.h:151
A custom console sink for logging messages with specific formatting and timestamping....
Definition AutonomyLogging.h:232
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:259
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:205
A custom rotating file sink that formats and logs messages to a file with automatic rotation based on...
Definition AutonomyLogging.h:326
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:287
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:353
::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:57
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