aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-main/src/main/java/org/sonar/application/process/StreamGobbler.java
blob: 4e63e0fa704f17f1abcb19bb89dcc2718d068679 (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
103
104
105
106
107
108
109
110
/*
 * 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.application.process;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.application.config.AppSettings;
import org.sonar.process.Props;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.sonar.process.ProcessProperties.Property.LOG_JSON_OUTPUT;

/**
 * Reads process output and writes to logs
 */
public class StreamGobbler extends Thread {
  public static final String LOGGER_STARTUP = "startup";
  public static final String LOGGER_GOBBLER = "gobbler";

  private static final String LOGGER_STARTUP_FORMAT = String.format("[%s]", LOGGER_STARTUP);

  private final AppSettings appSettings;

  private final InputStream is;
  private final Logger logger;
  /*
  This logger forwards startup logs (thanks to re-using fileappender) from subprocesses to sonar.log when running SQ not from wrapper.
   */
  private final Logger startupLogger;

  StreamGobbler(InputStream is, AppSettings appSettings, String processKey) {
    this(is, processKey, appSettings, LoggerFactory.getLogger(LOGGER_GOBBLER), LoggerFactory.getLogger(LOGGER_STARTUP));
  }

  StreamGobbler(InputStream is, String processKey, AppSettings appSettings, Logger logger, Logger startupLogger) {
    super(String.format("Gobbler[%s]", processKey));
    this.is = is;
    this.logger = logger;
    this.appSettings = appSettings;
    this.startupLogger = startupLogger;
  }

  @Override
  public void run() {
    try (BufferedReader br = new BufferedReader(new InputStreamReader(is, UTF_8))) {
      String line;
      while ((line = br.readLine()) != null) {
        if (line.contains(LOGGER_STARTUP)) {
          logStartupLog(line);
        } else {
          logger.info(line);
        }
      }
    } catch (Exception ignored) {
      // ignore
    }
  }

  private void logStartupLog(String line) {
    if (isJsonLoggingEnabled()) {
      JsonElement jsonElement = JsonParser.parseString(line);
      if (!jsonElement.getAsJsonObject().get("logger").getAsString().equals(LOGGER_STARTUP)) {
        // Log contains "startup" string but only in the message content. We skip.
        return;
      }
      startupLogger.warn(jsonElement.getAsJsonObject().get("message").getAsString());
    } else if (line.contains(LOGGER_STARTUP_FORMAT)) {
      startupLogger.warn(line.substring(line.indexOf(LOGGER_STARTUP_FORMAT) + LOGGER_STARTUP_FORMAT.length() + 1));
    }
  }

  private boolean isJsonLoggingEnabled() {
    Props props = appSettings.getProps();
    return props.valueAsBoolean(LOG_JSON_OUTPUT.getKey(), Boolean.parseBoolean(LOG_JSON_OUTPUT.getDefaultValue()));
  }

  static void waitUntilFinish(@Nullable StreamGobbler gobbler) {
    if (gobbler != null) {
      try {
        gobbler.join();
      } catch (InterruptedException ignored) {
        // consider as finished, restore the interrupted flag
        Thread.currentThread().interrupt();
      }
    }
  }
}