Class FileAccumulator

java.lang.Object
com.scivicslab.actoriac.accumulator.FileAccumulator
All Implemented Interfaces:
com.scivicslab.pojoactor.core.accumulator.Accumulator, Closeable, AutoCloseable

public class FileAccumulator extends Object implements com.scivicslab.pojoactor.core.accumulator.Accumulator, Closeable
Accumulator that writes output to a text file.

This accumulator writes all output to a text file as it arrives. The output format is identical to what appears on the console, ensuring consistency across all output destinations.

Usage

try (FileAccumulator fileAcc = new FileAccumulator(Path.of("run.log"))) {
    fileAcc.add("node-1", "stdout", "command output");
    fileAcc.add("workflow", "cowsay", renderedCowsayArt);
}
Since:
2.12.0
Author:
devteam@scivicslab.com
  • Field Details

  • Constructor Details

    • FileAccumulator

      public FileAccumulator(Path filePath) throws IOException
      Constructs a FileAccumulator that writes to the specified file.
      Parameters:
      filePath - the path to the output file
      Throws:
      IOException - if the file cannot be opened for writing
    • FileAccumulator

      public FileAccumulator(String filePath) throws IOException
      Constructs a FileAccumulator that writes to the specified file.
      Parameters:
      filePath - the path to the output file as a string
      Throws:
      IOException - if the file cannot be opened for writing
  • Method Details

    • getFilePath

      public Path getFilePath()
      Returns the path to the output file.
      Returns:
      the file path
    • add

      public void add(String source, String type, String data)
      Specified by:
      add in interface com.scivicslab.pojoactor.core.accumulator.Accumulator
    • formatOutput

      private String formatOutput(String source, String data)
      Formats the output with a fixed-width source prefix on each line.

      Every line of output is prefixed with [source] where source is left-justified in a fixed-width field. This allows multi-line output (such as cowsay ASCII art) to remain properly aligned while still being identifiable by source.

      Parameters:
      source - the source identifier (e.g., "node-web-01", "cli")
      data - the output data (may contain multiple lines)
      Returns:
      the formatted output string with prefix on each line
    • formatPrefix

      private String formatPrefix(String source)
      Creates a prefix from the source name.
      Parameters:
      source - the source identifier
      Returns:
      formatted prefix like "[node-web-01] "
    • getSummary

      public String getSummary()
      Specified by:
      getSummary in interface com.scivicslab.pojoactor.core.accumulator.Accumulator
    • getCount

      public int getCount()
      Specified by:
      getCount in interface com.scivicslab.pojoactor.core.accumulator.Accumulator
    • clear

      public void clear()
      Specified by:
      clear in interface com.scivicslab.pojoactor.core.accumulator.Accumulator
    • close

      public void close()
      Closes the file writer.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable