Skip to content

Commit

Permalink
chore: improve log readability
Browse files Browse the repository at this point in the history
  • Loading branch information
D-D-H committed Oct 21, 2023
1 parent 5f9810b commit 81b1927
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public void sendUserMessage(Level level, String message, Throwable throwable) {
append(sw.toString());
}

@Override
public void reset() {
this.total = this.done = 0;
}

@Override
public String log() {
return log.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand All @@ -46,38 +47,39 @@ public SerDesParser(Parser parser) {
this.parser = parser;
}

private Path storage(Path from) {
return Paths.get(from.toFile().getAbsoluteFile() + ".kryo");
}

@Override
public Snapshot parse(Path path, ProgressListener listener) {
// TODO: multi-threads support
Path storage = storage(path);
if (storage.toFile().exists()) {
Path serializedDataPath = resolveSerializedDataPath(path);
if (Files.exists(serializedDataPath)) {
try {
listener.beginTask("Deserializing thread dump snapshot", 100);
Snapshot snapshot = deserialize(storage);
listener.beginTask("Deserializing thread dump", 100);
Snapshot snapshot = deserialize(serializedDataPath);
listener.worked(100);
return snapshot;
} catch (Throwable t) {
log.error("Deserialize thread dump snapshot failed", t);
listener.sendUserMessage(ProgressListener.Level.WARNING, "Deserialize thread dump snapshot failed", t);
log.error("Failed to deserialize thread dump: {}", t.getMessage());
listener.sendUserMessage(ProgressListener.Level.WARNING, "Deserialize thread dump failed", t);
listener.reset();
}
}

listener.beginTask(null, 5);
Snapshot snapshot = parser.parse(path, listener);
try {
serialize(snapshot, storage);
listener.worked(5);
listener.beginTask("Serializing thread dump", 5);
serialize(snapshot, serializedDataPath);
} catch (Throwable t) {
log.error("Serialize snapshot failed", t);
log.warn("Failed to serialize thread dump: {}", t.getMessage());
} finally {
listener.worked(5);
}
return snapshot;
}

private Path resolveSerializedDataPath(Path source) {
return Paths.get(source.toFile().getAbsoluteFile() + ".kryo");
}

private void serialize(Snapshot snapshot, Path path) throws FileNotFoundException {
Kryo kryo = KRYO.get();
try (Output out = new Output(new FileOutputStream(path.toFile()))) {
Expand Down

0 comments on commit 81b1927

Please sign in to comment.