SdCardLogHandlerRK
Public Member Functions
SdCardPrintHandler Class Reference

Class for writing a data stream to SD card. More...

#include <SdCardLogHandlerRK.h>

Inheritance diagram for SdCardPrintHandler:
SdCardLogHandler

Public Member Functions

 SdCardPrintHandler (SdFat &sd, uint8_t csPin, uint8_t divisor)
 
SdCardPrintHandlerwithLogsDirName (const char *value)
 Sets the log directory name. Default: "logs". More...
 
SdCardPrintHandlerwithDesiredFileSize (size_t value)
 Desired file size in bytes. Default: 1000000 bytes (1 MB) More...
 
SdCardPrintHandlerwithMaxFilesToKeep (size_t value)
 The number of files to keep. Default: 10. More...
 
SdCardPrintHandlerwithCardCheckPeriod (unsigned long value)
 The number of milliseconds to between checks to see if the SD card is present. Default: 10000. More...
 
SdCardPrintHandlerwithSyncEveryEntry (size_t value)
 Set whether to sync the file system after every log entry. Default: true. More...
 
SdCardPrintHandlerwithNoSerialLogging ()
 The default is to log to Serial as well as SD card; to only log to SD card call this method. More...
 
SdCardPrintHandlerwithWriteToStream (Stream *value)
 Write to a different Stream, such as Serial1. Default: Serial. More...
 
virtual size_t write (uint8_t)
 Virtual override for the StreamLogHandler to write data to the log.
 
bool getLastBeginResult () const
 Checks the result of the last time sd.begin() was called. More...
 

Detailed Description

Class for writing a data stream to SD card.

You normally instantiate one of these as a global variable, passing in the SdFat object and the parameters you'd normally pass to SdFat::begin().

const int SD_CHIP_SELECT = A2;
SdFat sd;
SdCardLogHandler logHandler(sd, SD_CHIP_SELECT, SPI_FULL_SPEED);

You can pass additional options using the fluent-style methods beginning with "with" like withLogsDirName().

This class is a subclass of Print, so you can use all of the overloads of print, println, and printf that are supported by Print. Data is buffered until the
, then written to the card, for performance reasons and to avoid splitting a line between multiple files.

Member Function Documentation

◆ getLastBeginResult()

bool SdCardPrintHandler::getLastBeginResult ( ) const
inline

Checks the result of the last time sd.begin() was called.

This call determines whether the last call to sd.begin() succeeded or not. You might do this if you also need to call SdFat from your own code. This library needs to call sd.begin() internally to do it very early, and also after the SD card is ejected.

Returns
true if begin was successful

◆ withCardCheckPeriod()

SdCardPrintHandler& SdCardPrintHandler::withCardCheckPeriod ( unsigned long  value)
inline

The number of milliseconds to between checks to see if the SD card is present. Default: 10000.

When you remove the SD card, it's necessary to reinitialize the SdFat library and check for the card. Because of timeouts, this takes a while, so logging would become very slow if every attempt to log caused this delay. The card check period reduces the frequency of checking to see if a card has been inserted.

Note that as long as you leave the card in, you won't experience this.

Parameters
valueThe time in milliseconds (unsigned ling)

◆ withDesiredFileSize()

SdCardPrintHandler& SdCardPrintHandler::withDesiredFileSize ( size_t  value)
inline

Desired file size in bytes. Default: 1000000 bytes (1 MB)

Each log file will be approximately this size. It typically will be slightly larger than this, as the log is rotated when the size is exceeded. If changed, it will only be enforced for the current log file; old log files are not modified.

Parameters
valueThe maximum file size for a log file in bytes (size_t)

◆ withLogsDirName()

SdCardPrintHandler& SdCardPrintHandler::withLogsDirName ( const char *  value)
inline

Sets the log directory name. Default: "logs".

The logs must always be in a subdirectory, so make sure you set it to something, setting it to an empty string or NULL will disable logging.

Parameters
valueThe name to use instead of "logs" (const char *). This is not copied, as a constant string is normally passed in. If you calculate it, make sure you put it in a static or allocated buffer!

◆ withMaxFilesToKeep()

SdCardPrintHandler& SdCardPrintHandler::withMaxFilesToKeep ( size_t  value)
inline

The number of files to keep. Default: 10.

The maximum number of log files to keep is enforced at startup, when a SD card is inserted, and when the current log file is full.

Parameters
valueNumber of files to kee. Values are 1 <= num <= 999999 (size_t)

◆ withNoSerialLogging()

SdCardPrintHandler& SdCardPrintHandler::withNoSerialLogging ( )
inline

The default is to log to Serial as well as SD card; to only log to SD card call this method.

If you want to log to a different stream (like Serial1), use withWriteToStream() instead.

◆ withSyncEveryEntry()

SdCardPrintHandler& SdCardPrintHandler::withSyncEveryEntry ( size_t  value)
inline

Set whether to sync the file system after every log entry. Default: true.

Setting this to false dramatically improves the performance, but it also makes it much more likely that in the case of a reboot, the last log messages will be lost. The SdFat library normally only flushes the file in 512 byte increments so if you log infrequently, you could lose a number of log messages.

Parameters
valueThe value to set (size_t)

◆ withWriteToStream()

SdCardPrintHandler& SdCardPrintHandler::withWriteToStream ( Stream *  value)
inline

Write to a different Stream, such as Serial1. Default: Serial.

Parameters
valueThe stream to write log output to (such as &Serial1) or NULL to only write to the SD card.

Only one stream is supported. Setting it again replaces the last setting.


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