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.
  • 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

      private CircularBuffer<T[]> dataBuffer
    • timestampBuffer

      private CircularBuffer<java.lang.String> 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 per entriesPerFile entries.
      Parameters:
      prefix - the value to use for logFilePrefix
      filename - the value to use for logFilename
      delimiter - the value to use for logEntryDelimiter
      entriesPerFile - the value to use for capacity
      rootPath - the value to use for logFileRootPath
      rootPath - the formatter to use for timestampFormatter
  • Method Details

    • setLogEntryDelimiter

      public void setLogEntryDelimiter​(java.lang.String delimiter)
      Parameters:
      delimiter - the delimiter for the logged entries to set. The delimiter is set in logEntryDelimiter.
    • getStatus

      public DataLogger.DataLoggerStatus 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 each capacity entries a new log file will be written.
    • stopLoggingAndWriteFileIfRunning

      private boolean stopLoggingAndWriteFileIfRunning()
      checks if saveLoggedData is already true. If it is true, stopSavingLoggedData() is called. If it is false, writeLoggedDataToFile() is called.
      Returns:
      returns the initial value of saveLoggedData
    • saveLoggedData

      public void saveLoggedData​(int entriesPerFile)
      Activates the saving of log files. For each entriesPerFile entries a new log file will be written. If saveLoggedData is already true, 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 the logFilePrefix
      Parameters:
      filePrefix - the value to use for logFilePrefix
    • saveLoggedData

      public void saveLoggedData​(java.lang.String filePrefix, java.lang.String delimiter, int entriesPerFile)
      activate the saving of logged data and set the logFilePrefix, logEntryDelimiter, capacity.
      Parameters:
      filePrefix - the value to use for logFilePrefix
      delimiter - the value to use for logEntryDelimiter
      entriesPerFile - the value to use for capacity
    • 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 the DataLogger.
    • addData

      public void addData​(T[] data, long timestamp)
      add T[] data and its timestamp to dataBuffer, respectively timestampBuffer
      Parameters:
      data - the T[] that should be added to the dataBuffer
      timestamp - 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