Skip to content

Commit c8d12a5

Browse files
committed
ValidateTool: Add filename prefix to log output
When validating multiple files this makes it clear which file the messages are relevant to.
1 parent 3a52b88 commit c8d12a5

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/org/netpreserve/jwarc/tools/ValidateTool.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,43 @@ public class ValidateTool extends WarcTool {
2929

3030
private static class Logger {
3131
protected Optional<StringBuilder> sb;
32+
private static final ThreadLocal<String> currentFilename = new ThreadLocal<>();
33+
3234
public Logger() {
3335
this.sb = Optional.of(new StringBuilder());
3436
}
37+
38+
public void setCurrentFilename(String filename) {
39+
currentFilename.set(filename);
40+
}
41+
42+
public void clearCurrentFilename() {
43+
currentFilename.remove();
44+
}
45+
46+
private String getPrefix() {
47+
String filename = currentFilename.get();
48+
return filename != null ? filename + ": " : "";
49+
}
50+
3551
public void log(String form) {
36-
sb.ifPresent(s -> s.append(" ").append(form).append('\n'));
52+
sb.ifPresent(s -> s.append(" ").append(getPrefix()).append(form).append('\n'));
3753
}
3854
public void log(String form, Object... args) {
39-
sb.ifPresent(s -> s.append(" ").append(String.format(form, args)).append('\n'));
55+
sb.ifPresent(s -> s.append(" ").append(getPrefix()).append(String.format(form, args)).append('\n'));
4056
}
4157
public void error(String form, Object... args) {
4258
if (sb.isPresent()) {
4359
log("ERROR: " + form, args);
4460
} else {
45-
System.err.println("ERROR: " + String.format(form, args));
61+
System.err.println(getPrefix() + "ERROR: " + String.format(form, args));
4662
}
4763
}
4864
public void exception(String message, Exception e) {
4965
if (sb.isPresent()) {
5066
log("ERROR: %s: %s", message, e);
5167
} else {
52-
System.err.println("ERROR: " + message + ": " + e);
68+
System.err.println(getPrefix() + "ERROR: " + message + ": " + e);
5369
}
5470
}
5571
public String print() {
@@ -238,10 +254,10 @@ record = reader.next().orElse(null);
238254
long length = reader.position() - position;
239255

240256
if (verbose) {
241-
System.out.printf(" offset %d (length %d) %s %s\n%s", position, length, recordType, contentType,
257+
System.out.printf(logger.getPrefix() + " offset %d (length %d) %s %s\n%s", position, length, recordType, contentType,
242258
logger.print());
243259
} else if (!valid) {
244-
System.err.printf(" offset %d (length %d) %s %s failed\n", position, length, recordType, contentType);
260+
System.err.printf(logger.getPrefix() + " offset %d (length %d) %s %s failed\n", position, length, recordType, contentType);
245261
}
246262

247263
if (!valid) {
@@ -256,7 +272,8 @@ record = reader.next().orElse(null);
256272
return warcValidates;
257273
}
258274

259-
private boolean validate(Path warcFile) {
275+
boolean validate(Path warcFile) {
276+
logger.setCurrentFilename(warcFile.getFileName().toString());
260277
try (WarcReader reader = new WarcReader(warcFile)) {
261278
reader.calculateBlockDigest();
262279
if (verbose)
@@ -270,6 +287,8 @@ private boolean validate(Path warcFile) {
270287
System.err.println("Exception validating " + warcFile + ": " + e);
271288
e.printStackTrace();
272289
return false;
290+
} finally {
291+
logger.clearCurrentFilename();
273292
}
274293
}
275294

0 commit comments

Comments
 (0)