Skip to content

Commit

Permalink
Improve logging for IO utils and add thread info in build logs
Browse files Browse the repository at this point in the history
* Add additional logging on IO utils methods to detect
potential issues with concurrent build steps.
* Print thread information for DEBUG or TRACE log messages
in build logs.
  • Loading branch information
galderz committed Mar 6, 2024
1 parent 22dcfe1 commit bbb4e2e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ public boolean isEnabled(final Level level) {
void doActualLog(final Log log, final Level level, final String message, final Throwable thrown) {
final MessageBuilder buffer = MessageUtils.buffer();
// style options are limited unless we crack into jansi ourselves
buffer.strong("[").project(name).strong("]").a(" ").a(message);
if (Level.DEBUG.compareTo(level) <= 0) {
buffer.strong("[").project(name).strong("]").a(" ").a("(").a(Thread.currentThread().getName()).a(")").a(" ")
.a(message);
} else {
buffer.strong("[").project(name).strong("]").a(" ").a(message);
}
if (thrown != null) {
switch (level) {
case FATAL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Objects;
import java.util.UUID;

import org.jboss.logging.Logger;

/**
*
* @author Alexey Loubyansky
Expand All @@ -31,6 +33,8 @@ public class IoUtils {

private static final Path TMP_DIR = Paths.get(PropertyUtils.getProperty("java.io.tmpdir"));

private static final Logger log = Logger.getLogger(IoUtils.class);

private static void failedToMkDir(final Path dir) {
throw new IllegalStateException("Failed to create directory " + dir);
}
Expand All @@ -57,6 +61,7 @@ public static Path mkdirs(Path dir) {
}

public static void recursiveDelete(Path root) {
log.debugf("Recursively delete directory %s", root);
if (root == null || !Files.exists(root)) {
return;
}
Expand All @@ -68,6 +73,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
try {
Files.delete(file);
} catch (IOException ex) {
log.debugf(ex, "Unable to delete file " + file);
}
return FileVisitResult.CONTINUE;
}
Expand All @@ -79,6 +85,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e)
try {
Files.delete(dir);
} catch (IOException ex) {
log.debugf(ex, "Unable to delete directory " + dir);
}
return FileVisitResult.CONTINUE;
} else {
Expand All @@ -88,6 +95,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e)
}
});
} catch (IOException e) {
log.debugf(e, "Error recursively deleting directory");
}
}

Expand All @@ -98,19 +106,23 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e)
* @throws IOException in case of a failure
*/
public static void createOrEmptyDir(Path dir) throws IOException {
log.debugf("Create or empty directory %s", dir);
Objects.requireNonNull(dir);
if (!Files.exists(dir)) {
log.debugf("Directory %s does not exist, create directories", dir);
Files.createDirectories(dir);
return;
}
if (!Files.isDirectory(dir)) {
throw new IllegalArgumentException(dir + " is not a directory");
}
log.debugf("Iterate over contents of %s to delete its contents", dir);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path p : stream) {
if (Files.isDirectory(p)) {
recursiveDelete(p);
} else {
log.debugf("Delete file %s", p);
Files.delete(p);
}
}
Expand Down

0 comments on commit bbb4e2e

Please sign in to comment.