Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autosave functionality to BasicLogger #13679

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions vassal-app/src/main/java/VASSAL/build/module/BasicLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
* BasicLogger deals with VLOG Vassal Log files (i.e. NOT the errorLog--see below):
Expand Down Expand Up @@ -105,6 +108,7 @@ public class BasicLogger implements Logger, Buildable, GameComponent, CommandEnc
private NamedHotKeyConfigurer endLogKeyConfig;

private boolean undoInProgress = false;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

public BasicLogger() {
super();
Expand All @@ -114,6 +118,22 @@ public BasicLogger() {
newLogAction.setEnabled(false);
logInput = new ArrayList<>();
logOutput = new ArrayList<>();
startAutosave();
}

private void startAutosave() {
scheduler.scheduleAtFixedRate(this::autosave, 5, 5, TimeUnit.SECONDS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Five seconds is probably too short.

There's nothing to debounce the autosave if autosaving takes longer than the autosave interval.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed that to start after a minute and save every 5 minutes.

}

private void autosave() {
if (isLogging() && !isReplaying()) {
File autosaveFile = new File(outputFile.getParent(), "autosave_" + outputFile.getName()); //$NON-NLS-1$
try {
write(autosaveFile);
} catch (IOException e) {
GameModule.getGameModule().warn("Failed to autosave log file: " + e.getMessage());
}
}
}

/** Presently no XML attributes or subcomponents to be built */
Expand Down Expand Up @@ -435,28 +455,28 @@ else if (result == 2) { // Turn Preference Off
* Write the logfile to a file. The filename will have been selected when the logfile was begun.
*/
public void write() throws IOException {
write(outputFile);
}

/**
* Write the logfile to a specified file. This is used by the autosave feature.
*/
private void write(File file) throws IOException {
if (!logOutput.isEmpty()) {
GameModule.getGameModule().warn(Resources.getString("BasicLogger.logging_autosaving")); //$NON-NLS-1$
final Command log = beginningState;
for (final Command c : logOutput) {
log.append(new LogCommand(c, logInput, stepAction));
}

// FIXME: Extremely inefficient! Make encode write to an OutputStream
final String logString = GameModule.getGameModule().encode(log);

try (ZipWriter zw = new ZipWriter(outputFile)) {
try (ZipWriter zw = new ZipWriter(file)) {
try (OutputStream out = new ObfuscatingOutputStream(new BufferedOutputStream(zw.write(GameState.SAVEFILE_ZIP_ENTRY)))) {
out.write(logString.getBytes(StandardCharsets.UTF_8));
}
metadata.save(zw);
}

GameModule.getGameModule().getGameState().setModified(false);
undoAction.setEnabled(false);
ModuleManagerUpdateHelper.sendGameUpdate(outputFile);
}

endLogAction.setEnabled(false);
uckelman marked this conversation as resolved.
Show resolved Hide resolved
}

private File getSaveFile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ BasicLogger.replay_commencing=New Session Beginning
BasicLogger.replay_completed=End of Logfile
BasicLogger.dont_prompt_again=Don't prompt again
BasicLogger.logging_begun=Logging begun
BasicLogger.logging_autosaving=Autosaving logfile
BasicLogger.logfile_written=Logfile written.
BasicLogger.undo_icon=Undo button icon
BasicLogger.unsaved_log=Unsaved log
Expand Down
Loading