]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
Log improvements
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 7 May 2024 15:39:21 +0000 (17:39 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 13 May 2024 07:37:44 +0000 (09:37 +0200)
pom.xml
src/main/java/org/sonarsource/scanner/cli/Exit.java
src/main/java/org/sonarsource/scanner/cli/Main.java
src/main/java/org/sonarsource/scanner/cli/Stats.java
src/test/java/org/sonarsource/scanner/cli/MainTest.java
src/test/java/org/sonarsource/scanner/cli/StatsTest.java

diff --git a/pom.xml b/pom.xml
index 226c5110780b09c7cbebcc2f6325e542a3058116..451b4f588e055dd1f07febe03ba03f65ae18791e 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -1,4 +1,6 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
     <groupId>org.sonarsource.parent</groupId>
     <dependency>
       <groupId>org.sonarsource.scanner.lib</groupId>
       <artifactId>sonar-scanner-java-library</artifactId>
-      <version>3.0.0.141</version>
+      <version>3.0.0.146</version>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+      <version>2.0.13</version>
     </dependency>
     <dependency>
       <groupId>ch.qos.logback</groupId>
index 703d0812dc1de3740a5e64dcf7c15cb89c3a00ac..d265bed99e5a21e50c5d9a468f0a8b4cb7d36c7a 100644 (file)
@@ -23,6 +23,7 @@ class Exit {
   static final int SUCCESS = 0;
   static final int INTERNAL_ERROR = 1;
   static final int USER_ERROR = 2;
+  static final int SCANNER_ENGINE_ERROR = 3;
 
   void exit(int status) {
     System.exit(status);
index 7ec139d20b0045801ef0abfdc57f94eace1ca2b5..df60585b630475bd3ec5aae2d1faea3345b35e46 100644 (file)
@@ -43,7 +43,6 @@ import org.sonarsource.scanner.lib.ScannerProperties;
 public class Main {
   private static final Logger LOG = LoggerFactory.getLogger(Main.class);
 
-  private static final String SEPARATOR = "------------------------------------------------------------------------";
   private final Exit exit;
   private final Cli cli;
   private final Conf conf;
@@ -75,13 +74,18 @@ public class Main {
       init(p);
       try (var engine = scannerEngineBootstrapper.bootstrap()) {
         logServerType(engine);
-        engine.analyze((Map) p);
-        displayExecutionResult(stats, "SUCCESS");
-        status = Exit.SUCCESS;
+        var success = engine.analyze((Map) p);
+        if (success) {
+          displayExecutionResult(stats, "SUCCESS");
+          status = Exit.SUCCESS;
+        } else {
+          displayExecutionResult(stats, "FAILURE");
+          status = Exit.SCANNER_ENGINE_ERROR;
+        }
       }
     } catch (Throwable e) {
       displayExecutionResult(stats, "FAILURE");
-      showError("Error during SonarScanner CLI execution", e, cli.isDebugEnabled());
+      showError(e, cli.isDebugEnabled());
       status = isUserError(e) ? Exit.USER_ERROR : Exit.INTERNAL_ERROR;
     } finally {
       exit.exit(status);
@@ -123,14 +127,12 @@ public class Main {
   }
 
   private static void displayExecutionResult(Stats stats, String resultMsg) {
-    LOG.info(SEPARATOR);
     LOG.info("EXECUTION {}", resultMsg);
-    LOG.info(SEPARATOR);
     stats.stop();
-    LOG.info(SEPARATOR);
   }
 
-  private void showError(String message, Throwable e, boolean debug) {
+  private void showError(Throwable e, boolean debug) {
+    var message = "Error during SonarScanner CLI execution";
     if (debug || !isUserError(e)) {
       LOG.error(message, e);
     } else {
index 0d9913a6fa16d7ab5b642624da9eb26c944c0628..28cb88d8aecd9f08d64fb04ed5556c71e8ad83f0 100644 (file)
@@ -34,12 +34,6 @@ class Stats {
   Stats stop() {
     long stopTime = System.currentTimeMillis() - startTime;
     LOG.atInfo().addArgument(() -> formatTime(stopTime)).log("Total time: {}");
-
-    System.gc();
-    Runtime r = Runtime.getRuntime();
-    long mb = 1024L * 1024;
-    LOG.atInfo().addArgument((r.totalMemory() - r.freeMemory()) / mb + "M/" + r.totalMemory() / mb + "M").log("Final Memory: {}");
-
     return this;
   }
 
index 7b6ccfd8d137f566d57e9a366e5b35d43f952c0d..48b2ed35a422728217148fbe822a21c519aff8ae 100644 (file)
@@ -61,6 +61,7 @@ class MainTest {
   void setUp() {
     when(scannerEngineBootstrapperFactory.create(any(Properties.class), any(String.class))).thenReturn(bootstrapper);
     when(bootstrapper.bootstrap()).thenReturn(engine);
+    when(engine.analyze(any())).thenReturn(true);
     when(conf.properties()).thenReturn(properties);
   }
 
@@ -223,7 +224,6 @@ class MainTest {
 
     inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
     inOrder.verify(scannerEngineBootstrapperFactory, times(1)).create(p, "");
-    inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
   }
 
   @Test
index 489564040323adabf3e3bea12641e1d36fe2d29e..174acbc35d2996c0df3602983b8dfe17ab79ade4 100644 (file)
@@ -34,9 +34,8 @@ class StatsTest {
   void shouldPrintStats() {
     new Stats().start().stop();
 
-    assertThat(logTester.logs(Level.INFO)).hasSize(2);
+    assertThat(logTester.logs(Level.INFO)).hasSize(1);
     assertThat(logTester.logs(Level.INFO).get(0)).startsWith("Total time: ");
-    assertThat(logTester.logs(Level.INFO).get(1)).startsWith("Final Memory: ");
   }
 
   @Test