Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
logging_util.cpp File Reference

Utilities for logging data. More...

#include <Preferences.h>
#include <NTPClient.h>
#include <TimeStamp.hpp>
#include <SDLogger.hpp>
#include <SDReader.hpp>
Include dependency graph for logging_util.cpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

std::string cache_retrieve_timestamp ()
 
void cache_timestamp (std::string timestamp)
 
bool init_data_logger ()
 Initialize logging interfaces.
 
std::string get_log_filename (TimeStampParser *)
 
void log_to_sd_file (std::string filename, std::string time, std::string topic, std::string message)
 
void cache_log_offset (int log_offset)
 
int cache_retrieve_log_offset (void)
 
void log_to_sd_file (SDLogger *l, std::string time, std::string topic, std::string message)
 

Variables

Preferences gator_prefs
 Reference to non-volatile-storage on ESP32.
 
bool logging_available = false
 is some logging interface available?
 
bool absolute_timestamp_available = false
 is an exact/accurate timestamp available?
 
bool logged_relative_data = false
 relatively accurate timestamp used, last exact timestamp + # of minutes/resets elapsed

 
std::string time_stamp_to_cache = ""
 the formatted timestamp to write to non-volatile memory for next reset
 
SDLoggerlogger = NULL
 reads and writes to files
 
TimeStampBuildertsb = NULL
 builds timestamp strings
 
TimeStampParsertsp = NULL
 parses timestamp strings
 

Detailed Description

Utilities for logging data.

Summary

Define the helper functions/utilities which have been written for logging data on the Data Gators.

Emphasis is placed on making this as simple to use as possible. Unfortunately it is likely that the implementation is rather difficult to follow due to the number of edge cases covered. These include:

Key Functionality

Key functionality includes functions for:

  1. caching and retreiving data from NVS
    • timestamps
    • log offsets (calculated from # of resets)
  2. constructing file names for logging
  3. logging to SD card file
Author
Garrett Wells

Function Documentation

◆ cache_log_offset()

void cache_log_offset ( int  log_offset)

Save the reset count.

Parameters
[in]log_offsetThe offset in ticks.

◆ cache_retrieve_log_offset()

int cache_retrieve_log_offset ( void  )

Retrieves the reset count.

Returns
Number of resets/times the device as booted.

◆ cache_retrieve_timestamp()

std::string cache_retrieve_timestamp ( )

Retrieve time stamp from non-volatile memory so that it can be used for log file access.

Returns
The string value stored in non-volatile memory, should be expected to be of format produced by TimeStampBuilder

◆ cache_timestamp()

void cache_timestamp ( std::string  timestamp)

Cache/update the last known absolute timestamp from the NTPClient to non-volatile memory

Parameters
[in]timestampA timestamp string following the syntax of TimeStampBuilder

◆ get_log_filename()

std::string get_log_filename ( TimeStampParser tsp)

Generate a file name based on the current time stamp. Should only be called if TimeStampBuilder has been updated.

Parameters
[in]tspTimeStampParser which defines parser for reading the current time stamp from a string.
Returns
a constructed filename log_<month>-<day>-<year>

◆ init_data_logger()

bool init_data_logger ( )

Initialize logging interfaces.

Perform all initialization necessary before logging any data with log_message to the uSD card.

Returns
bool true if successfully initialized, false for:
  • no SD card detected
  • some other potential error

◆ log_to_sd_file() [1/2]

void log_to_sd_file ( SDLogger l,
std::string  time,
std::string  topic,
std::string  message 
)

Write an MQTT message to a file on the SD card. Requires a time stamp and filename.

Parameters
[in]lThe SDLogger instance with all filename and separator information pre-set
[in]timeThe time hr:min:sec+offset that will be written to the log file
[in]topicThe mqtt topic the data should have been logged to
[in]messageThe mqtt message containing the data we want to save

◆ log_to_sd_file() [2/2]

void log_to_sd_file ( std::string  filename,
std::string  time,
std::string  topic,
std::string  message 
)

Write an MQTT message to a file on the SD card. Requires a time stamp and filename.

Parameters
[in]filenameThe string file name to log the topic and message to.
[in]timestring time, formatted according to TimeStampBuilder.
[in]topicThe topic, according to MQTT format.
[in]messageThe message which contains data. Often JSON object.

Variable Documentation

◆ absolute_timestamp_available

bool absolute_timestamp_available = false

is an exact/accurate timestamp available?

Flag set during initialization, indicates that WiFi connection to NTP for update was obtained.

◆ gator_prefs

Preferences gator_prefs
extern

Reference to non-volatile-storage on ESP32.

Reference to non-volatile-storage on ESP32.

NVS memory access interface.

◆ logged_relative_data

bool logged_relative_data = false

relatively accurate timestamp used, last exact timestamp + # of minutes/resets elapsed

Flag set to indicate that no network connection was available for NTP update to get precise timestamp.

◆ logging_available

bool logging_available = false

is some logging interface available?

Flag showing that some logging interface is available. May be uSD card or MQTT.

◆ time_stamp_to_cache

std::string time_stamp_to_cache = ""

the formatted timestamp to write to non-volatile memory for next reset

The timestamp, last exact timestamp + offset to write to non-volatile storage. This timestamp will be retrieved on next reset and replaced if network connection to NTP available. If NTP not available, then the offset is incremented.