Package de.freaklamarsch.systarest
Class DataLogger<T>
java.lang.Object
de.freaklamarsch.systarest.DataLogger<T>
public class DataLogger<T>
extends java.lang.Object
A utility class for logging data entries represented as
T[] to
delimited text files. The default delimiter used for this is ;,
making it a logger for CSV files. The DataLogger has a defined
capacity, which is the number of elements that can be stored. Depending on
the saveLoggedData setting, adding new elements will trigger
writeLoggedDataToFile() and empty the dataBuffer or just
overwrite the oldest element stored.-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDataLogger.DataLoggerStatusInner class for representing the status of this @see DataLogger. -
Field Summary
Fields Modifier and Type Field Description private intcapacityprivate CircularBuffer<T[]>dataBufferprivate static intDEFAULT_CAPACITYprivate static java.lang.StringDEFAULT_DELIMITERprivate static java.lang.StringDEFAULT_FILENAMEprivate static java.time.format.DateTimeFormatterDEFAULT_FORMATTERprivate static java.lang.StringDEFAULT_PREFIXprivate static java.lang.StringDEFAULT_ROOT_PATHprivate java.lang.StringlogEntryDelimiterprivate java.lang.StringlogFilenameprivate java.lang.StringlogFilePrefixprivate java.lang.StringlogFileRootPathprivate booleansaveLoggedDataprivate CircularBuffer<java.lang.String>timestampBufferprivate java.time.format.DateTimeFormattertimestampFormatterprivate intwriterFileCount -
Constructor Summary
Constructors Constructor Description DataLogger()Constructor for a DataLogger that writes one file per 60 entries.DataLogger(java.lang.String prefix, java.lang.String filename, java.lang.String delimiter, int entriesPerFile, java.lang.String rootPath, java.time.format.DateTimeFormatter formatter)Constructor for a DataLogger that writes one file perentriesPerFileentries. -
Method Summary
Modifier and Type Method Description voidaddData(T[] data, long timestamp)add T[] data and its timestamp to dataBuffer, respectively timestampBufferprivate booleancheckAndFixBufferSync()private java.lang.String[][]convertBuffersToStringArray()intgetCapacity()java.lang.StringgetLogEntryDelimiter()java.lang.StringgetLogFilename()java.lang.StringgetLogFilePrefix()java.lang.StringgetLogFileRootPath()DataLogger.DataLoggerStatusgetStatus()intgetWriterFileCount()voidsaveLoggedData()Activates the saving of log files.voidsaveLoggedData(int entriesPerFile)Activates the saving of log files.voidsaveLoggedData(java.lang.String filePrefix)activate the saving of logged data and set thelogFilePrefixvoidsaveLoggedData(java.lang.String filePrefix, java.lang.String delimiter, int entriesPerFile)voidsetCapacity(int capacity)voidsetLogEntryDelimiter(java.lang.String delimiter)voidsetLogFilename(java.lang.String logFilename)voidsetLogFilePrefix(java.lang.String logFilePrefix)voidsetLogFileRootPath(java.lang.String logFileRootPath)voidsetWriterFileCount(int writerFileCount)private booleanstopLoggingAndWriteFileIfRunning()checks ifsaveLoggedDatais alreadytrue.voidstopSavingLoggedData()if data is currently saved to a file, this command will write the last file and stop the saving of files.private booleanwriteLogFile(java.lang.String[][] fileContent, java.lang.String fileName)private booleanwriteLoggedDataToFile()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
DEFAULT_CAPACITY
private static final int DEFAULT_CAPACITY- See Also:
- Constant Field Values
-
DEFAULT_DELIMITER
private static final java.lang.String DEFAULT_DELIMITER- See Also:
- Constant Field Values
-
DEFAULT_PREFIX
private static final java.lang.String DEFAULT_PREFIX- See Also:
- Constant Field Values
-
DEFAULT_FILENAME
private static final java.lang.String DEFAULT_FILENAME- See Also:
- Constant Field Values
-
DEFAULT_ROOT_PATH
private static final java.lang.String DEFAULT_ROOT_PATH -
DEFAULT_FORMATTER
private static final java.time.format.DateTimeFormatter DEFAULT_FORMATTER -
capacity
private int capacity -
dataBuffer
-
timestampBuffer
-
saveLoggedData
private boolean saveLoggedData -
logFilePrefix
private java.lang.String logFilePrefix -
logFilename
private java.lang.String logFilename -
logFileRootPath
private java.lang.String logFileRootPath -
logEntryDelimiter
private java.lang.String logEntryDelimiter -
timestampFormatter
private java.time.format.DateTimeFormatter timestampFormatter -
writerFileCount
private int writerFileCount
-
-
Constructor Details
-
DataLogger
public DataLogger()Constructor for a DataLogger that writes one file per 60 entries. -
DataLogger
public DataLogger(java.lang.String prefix, java.lang.String filename, java.lang.String delimiter, int entriesPerFile, java.lang.String rootPath, java.time.format.DateTimeFormatter formatter)Constructor for a DataLogger that writes one file perentriesPerFileentries.- Parameters:
prefix- the value to use forlogFilePrefixfilename- the value to use forlogFilenamedelimiter- the value to use forlogEntryDelimiterentriesPerFile- the value to use forcapacityrootPath- the value to use forlogFileRootPathrootPath- the formatter to use fortimestampFormatter
-
-
Method Details
-
setLogEntryDelimiter
public void setLogEntryDelimiter(java.lang.String delimiter)- Parameters:
delimiter- the delimiter for the logged entries to set. The delimiter is set inlogEntryDelimiter.
-
getStatus
- Returns:
- returns the status of the DataLogger. The status is composed of the
fields
capacity,saveLoggedData,logFilePrefix,logFileRootPath,writerFileCount
-
getCapacity
public int getCapacity()- Returns:
- the capacity
-
setCapacity
public void setCapacity(int capacity)- Parameters:
capacity- the capacity to set
-
getLogFilePrefix
public java.lang.String getLogFilePrefix()- Returns:
- the logFilePrefix
-
setLogFilePrefix
public void setLogFilePrefix(java.lang.String logFilePrefix)- Parameters:
logFilePrefix- the logFilePrefix to set
-
getLogFilename
public java.lang.String getLogFilename()- Returns:
- the logFilename
-
setLogFilename
public void setLogFilename(java.lang.String logFilename)- Parameters:
logFilename- the logFilename to set
-
getLogFileRootPath
public java.lang.String getLogFileRootPath()- Returns:
- the logFileRootPath
-
setLogFileRootPath
public void setLogFileRootPath(java.lang.String logFileRootPath)- Parameters:
logFileRootPath- the logFileRootPath to set
-
getWriterFileCount
public int getWriterFileCount()- Returns:
- the writerFileCount
-
setWriterFileCount
public void setWriterFileCount(int writerFileCount)- Parameters:
writerFileCount- the writerFileCount to set
-
getLogEntryDelimiter
public java.lang.String getLogEntryDelimiter()- Returns:
- the logEntryDelimiter
-
saveLoggedData
public void saveLoggedData()Activates the saving of log files. For eachcapacityentries a new log file will be written. -
stopLoggingAndWriteFileIfRunning
private boolean stopLoggingAndWriteFileIfRunning()checks ifsaveLoggedDatais alreadytrue. If it istrue,stopSavingLoggedData()is called. If it isfalse,writeLoggedDataToFile()is called.- Returns:
- returns the initial value of
saveLoggedData
-
saveLoggedData
public void saveLoggedData(int entriesPerFile)Activates the saving of log files. For eachentriesPerFileentries a new log file will be written. IfsaveLoggedDatais alreadytrue, the current logging will be stopped, causing all recorded entries to be written to a log file, before the new logging is started.- Parameters:
entriesPerFile- this is the number of entries contained in each written log file.capacitywill be set to this value
-
saveLoggedData
public void saveLoggedData(java.lang.String filePrefix)activate the saving of logged data and set thelogFilePrefix- Parameters:
filePrefix- the value to use forlogFilePrefix
-
saveLoggedData
public void saveLoggedData(java.lang.String filePrefix, java.lang.String delimiter, int entriesPerFile)- Parameters:
filePrefix- the value to use forlogFilePrefixdelimiter- the value to use forlogEntryDelimiterentriesPerFile- the value to use forcapacity
-
stopSavingLoggedData
public void stopSavingLoggedData()if data is currently saved to a file, this command will write the last file and stop the saving of files. This will clear theDataLogger. -
addData
add T[] data and its timestamp to dataBuffer, respectively timestampBuffer- Parameters:
data- the T[] that should be added to the dataBuffertimestamp- the timestamp for the added data. It will be added to timestampBuffer
-
writeLoggedDataToFile
private boolean writeLoggedDataToFile() -
convertBuffersToStringArray
private java.lang.String[][] convertBuffersToStringArray()- Returns:
-
checkAndFixBufferSync
private boolean checkAndFixBufferSync()- Returns:
- returns true, if buffers are ok, to work with. Returns false, if buffers are empty, or out of sync.
-
writeLogFile
private boolean writeLogFile(java.lang.String[][] fileContent, java.lang.String fileName)- Parameters:
fileContent-fileName-- Returns:
- true if writing the file was successful, false otherwise
-