-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |