Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
SDLogger Class Reference

Creates an interface for writing data to a log file on an SD card. More...

#include <SDLogger.hpp>

Collaboration diagram for SDLogger:

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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ SDLogger()

SDLogger::SDLogger ( std::string  prefix,
int  month,
int  day,
int  year,
std::string  filetype 
)

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 (_, -, .).

<prefix>_<month>-<day>-<year>.<filetype>
int reset_count
number of resets retrieved for NVS
Definition scheduler.hpp:31
Parameters
[in]prefixThe first part of the filename, identifies purpose of file.
[in]monthThe month the file corresponds to, integer on [1,12].
[in]dayThe day, an integer [1, 31].
[in]yearThe year, ex 2023.
[in]filetypeThe file/data type, ex csv

Member Function Documentation

◆ append_line()

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.

Parameters
[in]lineThe text to append to the end of the file.

◆ close_card()

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.

◆ initialize_sd_card()

bool SDLogger::initialize_sd_card ( )

Call before using SD card interface. Initializes connection to SD card.

Returns
true if successfully initialized.

◆ log_absolute_mqtt()

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.

Parameters
[in]timeThe timestamp as a string formatted <date_string>T<time_string>.
[in]mqtt_topicThe topic string as a <base>/<subtopic>/... formatted string.
[in]mqtt_messageA string, often JSON object string but not always.

◆ log_relative_mqtt()

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.

Parameters
[in]timeThe timestamp as a string formatted <date_string>T<time_string>.
[in]offsetThe relative offset in minutes, probably calculated from the WDT module reset frequency.
[in]mqtt_topicThe topic string as a <base>/<subtopic>/... formatted string.
[in]mqtt_messageA string, often JSON object string but not always.

◆ open_file()

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.

Parameters
[in]moder, w, etc
Returns
An open file object. If it fails, the error is printed and it should stop execution.

◆ set_filename() [1/2]

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.

<prefix>_<month>-<day>-<year>.<filetype>
Parameters
[in]prefixThe first string in the filename, ex log
[in]monthThe month this file was created, ex 09 or september
[in]dayThe day this file was created, ex 12
[in]yearThe year this file was created, ex 2023
[in]filetypeThe filetype/extension of the file, ex .csv or .txt

◆ set_filename() [2/2]

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.

Parameters
[in]fnThe file name to open/close.

◆ write_header()

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.

Parameters
[in]fieldsA vector list of strings which represent data columns/fields in the CSV file.

◆ write_line()

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.

Parameters
[in]lineThe string data which is written as a "line".

The documentation for this class was generated from the following files: