|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.testing; |
| 18 | + |
| 19 | +import static com.google.common.base.MoreObjects.firstNonNull; |
| 20 | + |
| 21 | +import com.google.common.base.Strings; |
| 22 | + |
| 23 | +import java.io.BufferedReader; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.InputStreamReader; |
| 27 | +import java.util.logging.Level; |
| 28 | +import java.util.logging.Logger; |
| 29 | +import java.util.regex.Matcher; |
| 30 | +import java.util.regex.Pattern; |
| 31 | + |
| 32 | +/** |
| 33 | + * This class allows to read a process output stream, block until a provided string appears on the |
| 34 | + * stream and redirect pertinent error logs to a provided logger. |
| 35 | + */ |
| 36 | +class BlockingProcessStreamReader extends Thread { |
| 37 | + |
| 38 | + private static final int STREAM_READER_SLEEP_INTERVAL_IN_MS = 200; |
| 39 | + private static final int LOG_LENGTH_LIMIT = 50000; |
| 40 | + |
| 41 | + private final BufferedReader errorReader; |
| 42 | + private final Logger logger; |
| 43 | + private StringBuilder currentLog; |
| 44 | + private Level currentLogLevel; |
| 45 | + private boolean collectionMode; |
| 46 | + private volatile boolean terminated; |
| 47 | + private final String emulatorTag; |
| 48 | + private final Pattern logLinePattern; |
| 49 | + |
| 50 | + private BlockingProcessStreamReader(String emulator, InputStream stream, String blockUntil, |
| 51 | + Logger logger) throws IOException { |
| 52 | + super("blocking-process-stream-reader"); |
| 53 | + setDaemon(true); |
| 54 | + errorReader = new BufferedReader(new InputStreamReader(stream)); |
| 55 | + this.logger = logger; |
| 56 | + this.emulatorTag = "[" + emulator + "]"; |
| 57 | + this.logLinePattern = Pattern.compile("(\\[" + emulator + "\\]\\s)?(\\w+):.*"); |
| 58 | + if (!Strings.isNullOrEmpty(blockUntil)) { |
| 59 | + String line; |
| 60 | + do { |
| 61 | + line = errorReader.readLine(); |
| 62 | + } while (line != null && !line.contains(blockUntil)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + void terminate() throws IOException { |
| 67 | + terminated = true; |
| 68 | + errorReader.close(); |
| 69 | + interrupt(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void run() { |
| 74 | + String previousLine = ""; |
| 75 | + String nextLine = ""; |
| 76 | + while (!terminated) { |
| 77 | + try { |
| 78 | + if (errorReader.ready()) { |
| 79 | + previousLine = nextLine; |
| 80 | + nextLine = errorReader.readLine(); |
| 81 | + if (nextLine == null) { |
| 82 | + terminated = true; |
| 83 | + } else { |
| 84 | + processLogLine(previousLine, nextLine); |
| 85 | + } |
| 86 | + } else { |
| 87 | + sleep(STREAM_READER_SLEEP_INTERVAL_IN_MS); |
| 88 | + } |
| 89 | + } catch (IOException e) { |
| 90 | + e.printStackTrace(System.err); |
| 91 | + } catch (InterruptedException e) { |
| 92 | + previousLine = nextLine; |
| 93 | + nextLine = null; |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + processLogLine(previousLine, firstNonNull(nextLine, "")); |
| 98 | + writeLog(); |
| 99 | + } |
| 100 | + |
| 101 | + private void processLogLine(String previousLine, String nextLine) { |
| 102 | + // Each log is two lines with the following format: |
| 103 | + // [Emulator]? [Date] [Time] [LoggingClass] [method] |
| 104 | + // [Emulator]? [LEVEL]: error message |
| 105 | + // [Emulator]? more data |
| 106 | + // Exceptions and stack traces are included in error stream, separated by a newline |
| 107 | + Level nextLogLevel = getLevel(nextLine); |
| 108 | + if (nextLogLevel != null) { |
| 109 | + writeLog(); |
| 110 | + currentLog = new StringBuilder(); |
| 111 | + currentLogLevel = nextLogLevel; |
| 112 | + collectionMode = true; |
| 113 | + } else if (collectionMode) { |
| 114 | + if (currentLog.length() > LOG_LENGTH_LIMIT) { |
| 115 | + collectionMode = false; |
| 116 | + } else if (currentLog.length() == 0) { |
| 117 | + // strip level out of the line |
| 118 | + currentLog.append(emulatorTag); |
| 119 | + currentLog.append(previousLine.split(":", 2)[1]); |
| 120 | + currentLog.append(System.getProperty("line.separator")); |
| 121 | + } else { |
| 122 | + if (!previousLine.startsWith(emulatorTag)) { |
| 123 | + currentLog.append(emulatorTag); |
| 124 | + currentLog.append(' '); |
| 125 | + } |
| 126 | + currentLog.append(previousLine); |
| 127 | + currentLog.append(System.getProperty("line.separator")); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private void writeLog() { |
| 133 | + if (currentLogLevel != null && currentLog != null && currentLog.length() != 0) { |
| 134 | + logger.log(currentLogLevel, currentLog.toString().trim()); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private Level getLevel(String line) { |
| 139 | + try { |
| 140 | + Matcher matcher = logLinePattern.matcher(line); |
| 141 | + if (matcher.matches()) { |
| 142 | + return Level.parse(matcher.group(2)); |
| 143 | + } else { |
| 144 | + return null; |
| 145 | + } |
| 146 | + } catch (IllegalArgumentException e) { |
| 147 | + return null; // level wasn't supplied in this log line |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + static BlockingProcessStreamReader start(String emulator, InputStream stream, String blockUntil, |
| 152 | + Logger logger) throws IOException { |
| 153 | + BlockingProcessStreamReader thread = |
| 154 | + new BlockingProcessStreamReader(emulator, stream, blockUntil, logger); |
| 155 | + thread.start(); |
| 156 | + return thread; |
| 157 | + } |
| 158 | +} |
0 commit comments