Skip to content

Commit

Permalink
implement sd card sink
Browse files Browse the repository at this point in the history
  • Loading branch information
Exortions committed Dec 26, 2024
1 parent f5f2488 commit 57db3cf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
19 changes: 19 additions & 0 deletions include/lemlog/logger/sinks/sd-card.hpp
Original file line number Diff line number Diff line change
@@ -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;
};
}
55 changes: 55 additions & 0 deletions src/lemlog/logger/sinks/sd-card.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "lemlog/logger/sinks/sd-card.hpp"
#include "pros/apix.h"
#include <fstream>

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: <time> [LEVEL] (topic) message
std::string output = "";

if (this->logTimestamp) output += this->formatTimestamp((long long) pros::millis());

switch (level) {
case (Level::DEBUG): {
output += " [DEBUG]";
break;
}
case (Level::INFO): {
output += " [INFO]";
break;
}
case (Level::WARN): {
output += " [WARN]";
break;
}
case (Level::ERROR): {
output += " [ERROR]";
break;
}
}

output += " (" + topic + ") " + message + "\n";

std::ofstream file(this->filename);
file << output;
file.close();
}
}

0 comments on commit 57db3cf

Please sign in to comment.