From 57db3cf6970eecdfe8b19bd358782cf2c2a1c719 Mon Sep 17 00:00:00 2001 From: Exortions Date: Wed, 25 Dec 2024 16:34:59 -0800 Subject: [PATCH] implement sd card sink --- include/lemlog/logger/sinks/sd-card.hpp | 19 +++++++++ src/lemlog/logger/sinks/sd-card.cpp | 55 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 include/lemlog/logger/sinks/sd-card.hpp create mode 100644 src/lemlog/logger/sinks/sd-card.cpp diff --git a/include/lemlog/logger/sinks/sd-card.hpp b/include/lemlog/logger/sinks/sd-card.hpp new file mode 100644 index 0000000..b3f78f3 --- /dev/null +++ b/include/lemlog/logger/sinks/sd-card.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "lemlog/logger/logger.hpp" + +namespace logger { +/** + * @brief SD card class. Outputs all data to the SD card connected to the brain. + */ +class SDCard : public Sink { + public: + SDCard(std::string filename = ".log", bool logTimestamp = true); + void send(Level level, std::string topic, std::string message) override; + private: + std::string formatTimestamp(long long ms); + + std::string filename; + bool logTimestamp; +}; +} \ No newline at end of file diff --git a/src/lemlog/logger/sinks/sd-card.cpp b/src/lemlog/logger/sinks/sd-card.cpp new file mode 100644 index 0000000..782182a --- /dev/null +++ b/src/lemlog/logger/sinks/sd-card.cpp @@ -0,0 +1,55 @@ +#include "lemlog/logger/sinks/sd-card.hpp" +#include "pros/apix.h" +#include + +namespace logger { +SDCard::SDCard(std::string filename, bool logTimestamp) : filename("/usd/" + filename), logTimestamp(logTimestamp) {} + +std::string SDCard::formatTimestamp(long long ms) { + // use the % operator to get the remainder of the division + long long minutes = ms / 60000; + ms %= 60000; + long long seconds = ms / 1000; + ms %= 1000; + + std::string output = ""; + + if (minutes > 0) output += std::to_string(minutes) + "m "; + if (seconds > 0 || minutes > 0) output += std::to_string(seconds) + "s "; + output += std::to_string(ms) + "ms"; + + return output; +} + +void SDCard::send(Level level, std::string topic, std::string message) { + // output: