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 Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate final AtomicIntegerprivate final Pathprivate final PrintWriter -
Constructor Summary
ConstructorsConstructorDescriptionFileAccumulator(String filePath) Constructs a FileAccumulator that writes to the specified file.FileAccumulator(Path filePath) Constructs a FileAccumulator that writes to the specified file. -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidclear()voidclose()Closes the file writer.private StringformatOutput(String source, String data) Formats the output with a fixed-width source prefix on each line.private StringformatPrefix(String source) Creates a prefix from the source name.intgetCount()Returns the path to the output file.
-
Field Details
-
writer
-
filePath
-
count
-
closed
-
-
Constructor Details
-
FileAccumulator
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
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
-
add
-
formatOutput
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
Creates a prefix from the source name.- Parameters:
source- the source identifier- Returns:
- formatted prefix like "[node-web-01] "
-
getSummary
- Specified by:
getSummaryin interfacecom.scivicslab.pojoactor.core.accumulator.Accumulator
-
getCount
- Specified by:
getCountin interfacecom.scivicslab.pojoactor.core.accumulator.Accumulator
-
clear
- Specified by:
clearin interfacecom.scivicslab.pojoactor.core.accumulator.Accumulator
-
close
Closes the file writer.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-