ulfjack | 236635a | 2017-06-26 09:25:52 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.runtime; |
| 15 | |
| 16 | import static com.google.common.base.Preconditions.checkNotNull; |
| 17 | |
| 18 | import com.google.common.base.Supplier; |
| 19 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 20 | import com.google.devtools.build.lib.runtime.commands.InfoItem; |
| 21 | import com.google.devtools.build.lib.util.AbruptExitException; |
| 22 | import com.google.devtools.build.lib.util.LoggingUtil; |
| 23 | import com.google.devtools.build.lib.util.io.OutErr; |
| 24 | import com.google.devtools.build.lib.vfs.Path; |
juliexxia | e91a450 | 2018-08-15 14:42:29 -0700 | [diff] [blame] | 25 | import com.google.devtools.common.options.OptionsParsingResult; |
ulfjack | 236635a | 2017-06-26 09:25:52 +0200 | [diff] [blame] | 26 | import java.io.IOException; |
| 27 | import java.io.OutputStream; |
| 28 | import java.util.logging.Level; |
| 29 | |
| 30 | /** This module logs complete stdout / stderr output of Bazel to a local file. */ |
| 31 | public class CommandLogModule extends BlazeModule { |
| 32 | private CommandEnvironment env; |
| 33 | private OutputStream logOutputStream; |
| 34 | |
| 35 | @Override |
juliexxia | e91a450 | 2018-08-15 14:42:29 -0700 | [diff] [blame] | 36 | public void serverInit(OptionsParsingResult startupOptions, ServerBuilder builder) { |
ulfjack | 236635a | 2017-06-26 09:25:52 +0200 | [diff] [blame] | 37 | builder.addInfoItems(new CommandLogInfoItem()); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void beforeCommand(CommandEnvironment env) { |
| 42 | this.env = env; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public OutErr getOutputListener() { |
| 47 | Path commandLog = getCommandLogPath(env.getOutputBase()); |
| 48 | // Unlink old command log from previous build, if present. |
| 49 | try { |
| 50 | commandLog.delete(); |
| 51 | } catch (IOException ioException) { |
| 52 | LoggingUtil.logToRemote(Level.WARNING, "Unable to delete command.log", ioException); |
| 53 | } |
| 54 | |
| 55 | try { |
| 56 | if (writeCommandLog(env.getRuntime()) && !"clean".equals(env.getCommandName())) { |
| 57 | logOutputStream = commandLog.getOutputStream(); |
| 58 | return OutErr.create(logOutputStream, logOutputStream); |
| 59 | } |
| 60 | } catch (IOException ioException) { |
| 61 | LoggingUtil.logToRemote(Level.WARNING, "Unable to delete or open command.log", ioException); |
| 62 | } |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | static boolean writeCommandLog(BlazeRuntime runtime) { |
juliexxia | e91a450 | 2018-08-15 14:42:29 -0700 | [diff] [blame] | 67 | OptionsParsingResult startupOptionsProvider = runtime.getStartupOptionsProvider(); |
ulfjack | 236635a | 2017-06-26 09:25:52 +0200 | [diff] [blame] | 68 | return startupOptionsProvider.getOptions(BlazeServerStartupOptions.class).writeCommandLog; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * For a given output_base directory, returns the command log file path. |
| 73 | */ |
| 74 | static Path getCommandLogPath(Path outputBase) { |
| 75 | return outputBase.getRelative("command.log"); |
| 76 | } |
| 77 | |
| 78 | @Override |
buchgr | 0cf475a | 2018-10-08 11:20:22 -0700 | [diff] [blame] | 79 | public void commandComplete() { |
ulfjack | 236635a | 2017-06-26 09:25:52 +0200 | [diff] [blame] | 80 | this.env = null; |
| 81 | if (logOutputStream != null) { |
| 82 | try { |
| 83 | logOutputStream.flush(); |
| 84 | logOutputStream.close(); |
| 85 | } catch (IOException e) { |
| 86 | throw new RuntimeException(e); |
| 87 | } finally { |
| 88 | logOutputStream = null; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Info item for the command log |
| 95 | */ |
| 96 | public static final class CommandLogInfoItem extends InfoItem { |
| 97 | public CommandLogInfoItem() { |
jingwen | 68c57f0 | 2018-11-21 16:17:17 -0800 | [diff] [blame] | 98 | super( |
| 99 | "command_log", |
| 100 | "Location of the log containing the output from the build commands.", |
ulfjack | 236635a | 2017-06-26 09:25:52 +0200 | [diff] [blame] | 101 | false); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public byte[] get(Supplier<BuildConfiguration> configurationSupplier, CommandEnvironment env) |
| 106 | throws AbruptExitException { |
| 107 | checkNotNull(env); |
| 108 | return print(getCommandLogPath(env.getRuntime().getWorkspace().getOutputBase())); |
| 109 | } |
| 110 | } |
| 111 | } |