aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar/scanner/util/ProgressReport.java
blob: 5673ad3cc60dd3418bd9025503a62d782711fc92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.util;

import java.util.function.Supplier;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProgressReport implements Runnable {

  private static final Logger LOG = LoggerFactory.getLogger(ProgressReport.class);
  private final long period;
  private long startTime;
  private Supplier<String> messageSupplier = () -> "";
  private final Thread thread;
  private String stopMessage = null;

  public ProgressReport(String threadName, long period) {
    this.period = period;
    thread = new Thread(this);
    thread.setName(threadName);
    thread.setDaemon(true);
  }

  @Override
  public void run() {
    while (!Thread.interrupted()) {
      try {
        Thread.sleep(period);
        log(messageSupplier.get());
      } catch (InterruptedException e) {
        break;
      }
    }
    if (stopMessage != null) {
      log(stopMessage);
    }
  }

  public void start(String startMessage) {
    log(startMessage);
    thread.start();
    startTime = currentTimeMillis();
  }

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

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

  public void stop(@Nullable String stopMessage) {
    this.stopMessage = stopMessage;
    thread.interrupt();
    try {
      thread.join();
    } catch (InterruptedException e) {
      // Ignore
    }
  }

  public void stopAndLogTotalTime(@Nullable String stopMessage) {
    long stopTime = currentTimeMillis();
    stopMessage += String.format(" (done) | time=%dms", timeDiff(stopTime, startTime));
    stop(stopMessage);
  }

  private static void log(String message) {
    synchronized (LOG) {
      LOG.info(message);
      LOG.notifyAll();
    }
  }

  private static long currentTimeMillis() {
    return System.currentTimeMillis();
  }

  private static long timeDiff(long endTime, long startTime) {
    return endTime - startTime;
  }
}