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 class
DataLogger.DataLoggerStatus
Inner class for representing the status of this @see DataLogger. -
Field Summary
Fields Modifier and Type Field Description private int
capacity
private CircularBuffer<T[]>
dataBuffer
private static int
DEFAULT_CAPACITY
private static java.lang.String
DEFAULT_DELIMITER
private static java.lang.String
DEFAULT_FILENAME
private static java.time.format.DateTimeFormatter
DEFAULT_FORMATTER
private static java.lang.String
DEFAULT_PREFIX
private static java.lang.String
DEFAULT_ROOT_PATH
private java.lang.String
logEntryDelimiter
private java.lang.String
logFilename
private java.lang.String
logFilePrefix
private java.lang.String
logFileRootPath
private boolean
saveLoggedData
private CircularBuffer<java.lang.String>
timestampBuffer
private java.time.format.DateTimeFormatter
timestampFormatter
private int
writerFileCount
-
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 perentriesPerFile
entries. -
Method Summary
Modifier and Type Method Description void
addData(T[] data, long timestamp)
add T[] data and its timestamp to dataBuffer, respectively timestampBufferprivate boolean
checkAndFixBufferSync()
private java.lang.String[][]
convertBuffersToStringArray()
int
getCapacity()
java.lang.String
getLogEntryDelimiter()
java.lang.String
getLogFilename()
java.lang.String
getLogFilePrefix()
java.lang.String
getLogFileRootPath()
DataLogger.DataLoggerStatus
getStatus()
int
getWriterFileCount()
void
saveLoggedData()
Activates the saving of log files.void
saveLoggedData(int entriesPerFile)
Activates the saving of log files.void
saveLoggedData(java.lang.String filePrefix)
activate the saving of logged data and set thelogFilePrefix
void
saveLoggedData(java.lang.String filePrefix, java.lang.String delimiter, int entriesPerFile)
void
setCapacity(int capacity)
void
setLogEntryDelimiter(java.lang.String delimiter)
void
setLogFilename(java.lang.String logFilename)
void
setLogFilePrefix(java.lang.String logFilePrefix)
void
setLogFileRootPath(java.lang.String logFileRootPath)
void
setWriterFileCount(int writerFileCount)
private boolean
stopLoggingAndWriteFileIfRunning()
checks ifsaveLoggedData
is alreadytrue
.void
stopSavingLoggedData()
if data is currently saved to a file, this command will write the last file and stop the saving of files.private boolean
writeLogFile(java.lang.String[][] fileContent, java.lang.String fileName)
private boolean
writeLoggedDataToFile()
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 perentriesPerFile
entries.- Parameters:
prefix
- the value to use forlogFilePrefix
filename
- the value to use forlogFilename
delimiter
- the value to use forlogEntryDelimiter
entriesPerFile
- the value to use forcapacity
rootPath
- the value to use forlogFileRootPath
rootPath
- 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 eachcapacity
entries a new log file will be written. -
stopLoggingAndWriteFileIfRunning
private boolean stopLoggingAndWriteFileIfRunning()checks ifsaveLoggedData
is 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 eachentriesPerFile
entries a new log file will be written. IfsaveLoggedData
is 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.capacity
will 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 forlogFilePrefix
delimiter
- the value to use forlogEntryDelimiter
entriesPerFile
- 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
-