![]() |
Data Gator
Hardware and software documentation for the Data Gator project.
|
Creates an interface for writing data to a log file on an SD card. More...
#include <SDLogger.hpp>
Public Member Functions | |
SDLogger () | |
Default constructor that performs no initialization. | |
SDLogger (std::string filename) | |
Constructor to set the file name. | |
SDLogger (std::string prefix, int month, int day, int year, std::string filetype) | |
SDLogger constructor which takes a prefix + date + filetype based filename. | |
bool | initialize_sd_card () |
Call before using SD card interface. Initializes connection to SD card. | |
void | set_filename (std::string) |
Set the filename to write to the easiest way possible. | |
void | set_filename (std::string prefix, int month, int day, int year, std::string filetype) |
Slightly more involved but potentially more useful filename creation. | |
void | write_header (std::vector< std::string > fields) |
Writes a header line to a file using append to prevent overwriting valuable data. | |
void | log_absolute_mqtt (std::string time, std::string mqtt_topic, std::string mqtt_message) |
Append a line to the target csv file containing an absolute time stamp, mqtt topic, and an mqtt message. | |
void | log_relative_mqtt (std::string time, int offset, std::string mqtt_topic, std::string mqtt_message) |
Append a line with a relative time stamp to the target file. | |
void | write_line (std::string line) |
Write a line to the file, will overwrite the contents of the file. | |
void | append_line (std::string line) |
Open file and append a line, add newline if needed. | |
void | read () |
Not implemented | |
File | open_file (const char *mode=FILE_WRITE) |
Opens the file attached to the logger object with the default mode. | |
bool | exists (std::string fn) |
File with name fn exists in SD card's filesystem. | |
bool | exists () |
File with name stored in this->filename exists in SD card's filesystem. | |
void | close_card () |
Close card connection. | |
Private Attributes | |
std::string | filename |
std::string | filetype = ".csv" |
std::string | separator = ";" |
SDCard | sd |
Creates an interface for writing data to a log file on an SD card.
A SDLogger is meant to be associated with one file object for the duration of its lifetime.
SDLogger constructor which takes a prefix + date + filetype based filename.
Constructs a SDLogger instance with file name by appending all the provided parameters separated by single characters (_
, -
, .
).
[in] | prefix | The first part of the filename, identifies purpose of file. |
[in] | month | The month the file corresponds to, integer on [1,12]. |
[in] | day | The day, an integer [1, 31]. |
[in] | year | The year, ex 2023 . |
[in] | filetype | The file/data type, ex csv |
void SDLogger::append_line | ( | std::string | line | ) |
Open file and append a line, add newline if needed.
Appends a line of text to the end of a file's contents. Note that a newline character is appended to the end of the input string to create a "line" and that the file is closed after the operation.
[in] | line | The text to append to the end of the file. |
void SDLogger::close_card | ( | ) |
Close card connection.
Closes the connection to the SD card. This is effectively shutting down the connection through the SPI interface.
bool SDLogger::initialize_sd_card | ( | ) |
Call before using SD card interface. Initializes connection to SD card.
true
if successfully initialized. void SDLogger::log_absolute_mqtt | ( | std::string | time, |
std::string | mqtt_topic, | ||
std::string | mqtt_message | ||
) |
Append a line to the target csv file containing an absolute time stamp, mqtt topic, and an mqtt message.
Logs an mqtt topic message to a file. This means that the logged data is timestamped, has a topic, and a message. The line is appended to the file using append_line(string)
from this class.
[in] | time | The timestamp as a string formatted <date_string>T<time_string> . |
[in] | mqtt_topic | The topic string as a <base>/<subtopic>/... formatted string. |
[in] | mqtt_message | A string, often JSON object string but not always. |
void SDLogger::log_relative_mqtt | ( | std::string | time, |
int | offset, | ||
std::string | mqtt_topic, | ||
std::string | mqtt_message | ||
) |
Append a line with a relative time stamp to the target file.
Logs an mqtt topic message to a file. This means that the logged data is timestamped, has a topic, and a message. The line is appended to the file using log_absolute_mqtt(string)
from this class. Essentially a wrapper which adds the offset
parameter to provide a relative time estimate in minutes from the previous known time.
[in] | time | The timestamp as a string formatted <date_string>T<time_string> . |
[in] | offset | The relative offset in minutes, probably calculated from the WDT module reset frequency. |
[in] | mqtt_topic | The topic string as a <base>/<subtopic>/... formatted string. |
[in] | mqtt_message | A string, often JSON object string but not always. |
File SDLogger::open_file | ( | const char * | mode = FILE_WRITE | ) |
Opens the file attached to the logger object with the default mode.
Opens a file in one of the two access modes, read only, or read/write.
[in] | mode | r , w , etc |
void SDLogger::set_filename | ( | std::string | prefix, |
int | month, | ||
int | day, | ||
int | year, | ||
std::string | filetype | ||
) |
Slightly more involved but potentially more useful filename creation.
Creates a filename by concatenating the parameters to this method, separated by _
, -
, .
. Unlike set_filename(string)
this method allows the user to change the date, prefix and filetype (file extension) associated with the file.
[in] | prefix | The first string in the filename, ex log |
[in] | month | The month this file was created, ex 09 or september |
[in] | day | The day this file was created, ex 12 |
[in] | year | The year this file was created, ex 2023 |
[in] | filetype | The filetype/extension of the file, ex .csv or .txt |
void SDLogger::set_filename | ( | std::string | fn | ) |
Set the filename to write to the easiest way possible.
Stores a file name in this object for repeated access. Use if planning to use this object for operating on one file only.
[in] | fn | The file name to open/close. |
void SDLogger::write_header | ( | std::vector< std::string > | fields | ) |
Writes a header line to a file using append to prevent overwriting valuable data.
Used to initialize a CSV file by writing the list of comma separated fields to the first line of the file. In this case the fields are provided as a vector and are written using write_line(string)
so the values overwrite all other data in the file.
[in] | fields | A vector list of strings which represent data columns/fields in the CSV file. |
void SDLogger::write_line | ( | std::string | line | ) |
Write a line to the file, will overwrite the contents of the file.
Write a line to a file, this replaces whatever is currently in the file so use with caution. File is closed at end of operation.
Note that a newline character is appended to the end of the input parameter.
[in] | line | The string data which is written as a "line". |