Skip to content

Commit

Permalink
SONAR-24067 Improve logs when preprocessing files
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-garcia-sonarsource authored and sonartech committed Jan 6, 2025
1 parent 1566baf commit e18b8c2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class ProjectFilePreprocessor {
private final Map<DefaultInputModule, List<Path>> mainSourcesByModule = new HashMap<>();
private final Map<DefaultInputModule, List<Path>> testSourcesByModule = new HashMap<>();

private final ProgressReport progressReport = new ProgressReport("Report about progress of file preprocessing",
TimeUnit.SECONDS.toMillis(10));
private int totalFilesPreprocessed = 0;

public ProjectFilePreprocessor(AnalysisWarnings analysisWarnings, ScmConfiguration scmConfiguration, InputModuleHierarchy inputModuleHierarchy,
Expand All @@ -93,9 +95,8 @@ public ProjectFilePreprocessor(AnalysisWarnings analysisWarnings, ScmConfigurati
}

public void execute() {
ProgressReport progressReport = new ProgressReport("Report about progress of file preprocessing",
TimeUnit.SECONDS.toMillis(10));
progressReport.start("Preprocessing files...");
progressReport.message(() -> String.format("Preprocessed %s files", totalFilesPreprocessed));
ExclusionCounter exclusionCounter = new ExclusionCounter();

if (useScmExclusion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.scanner.util;

import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -28,7 +29,7 @@ public class ProgressReport implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(ProgressReport.class);
private final long period;
private long startTime;
private String message = "";
private Supplier<String> messageSupplier = () -> "";
private final Thread thread;
private String stopMessage = null;

Expand All @@ -44,7 +45,7 @@ public void run() {
while (!Thread.interrupted()) {
try {
Thread.sleep(period);
log(message);
log(messageSupplier.get());
} catch (InterruptedException e) {
break;
}
Expand All @@ -61,7 +62,11 @@ public void start(String startMessage) {
}

public void message(String message) {
this.message = message;
this.message(() -> message);
}

public void message(Supplier<String> messageSupplier) {
this.messageSupplier = messageSupplier;
}

public void stop(@Nullable String stopMessage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ProgressReportTest {
@Rule
public LogTester logTester = new LogTester();

private ProgressReport underTest = new ProgressReport(THREAD_NAME, 1);
private final ProgressReport underTest = new ProgressReport(THREAD_NAME, 1);

@Test
public void stop_thread_on_stop() {
Expand All @@ -66,15 +66,28 @@ public void do_log() {
underTest.message("Some message");
boolean logged = false;
while (!logged) {
logged = containsWithRetry("Some message");
logged = waitForMessageStartingWith("Some message");
}
underTest.stop("stop");
assertThat(containsWithRetry("stop")).isTrue();
assertThat(waitForMessageStartingWith("stop")).isTrue();
}

private boolean containsWithRetry(String message) {
@Test
public void do_log_message_supplier() {
logTester.setLevel(Level.DEBUG);
underTest.start("start");
underTest.message(() -> "Some message " + System.currentTimeMillis());
boolean logged = false;
while (!logged) {
logged = waitForMessageStartingWith("Some message ");
}
underTest.stop("stop");
assertThat(waitForMessageStartingWith("stop")).isTrue();
}

private boolean waitForMessageStartingWith(String message) {
try {
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> logTester.logs().contains(message));
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> logTester.logs().stream().anyMatch(s -> s.startsWith(message)));
} catch (ConditionTimeoutException e) {
return false;
}
Expand Down

0 comments on commit e18b8c2

Please sign in to comment.