1 //
2 // Copyright (C) 2019 The Android Open Source Project
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 #include "host/commands/run_cvd/launch/log_tee_creator.h"
17
18 #include <vector>
19
20 #include "common/libs/utils/result.h"
21 #include "common/libs/utils/subprocess.h"
22 #include "host/libs/config/cuttlefish_config.h"
23
24 namespace cuttlefish {
25
26 namespace {
CreateLogTeeImpl(Command & cmd,const CuttlefishConfig::InstanceSpecific & instance,std::string process_name,const std::vector<Subprocess::StdIOChannel> & log_channels)27 Result<Command> CreateLogTeeImpl(
28 Command& cmd, const CuttlefishConfig::InstanceSpecific& instance,
29 std::string process_name,
30 const std::vector<Subprocess::StdIOChannel>& log_channels) {
31 auto name_with_ext = process_name + "_logs.fifo";
32 auto logs_path = instance.PerInstanceInternalPath(name_with_ext.c_str());
33 auto logs = CF_EXPECT(SharedFD::Fifo(logs_path, 0666));
34
35 for (const auto& channel : log_channels) {
36 cmd.RedirectStdIO(channel, logs);
37 }
38
39 return Command(HostBinaryPath("log_tee"))
40 .AddParameter("--process_name=", process_name)
41 .AddParameter("--log_fd_in=", logs);
42 }
43 } // namespace
44
LogTeeCreator(const CuttlefishConfig::InstanceSpecific & instance)45 LogTeeCreator::LogTeeCreator(const CuttlefishConfig::InstanceSpecific& instance)
46 : instance_(instance) {}
47
CreateFullLogTee(Command & cmd,std::string process_name)48 Result<Command> LogTeeCreator::CreateFullLogTee(Command& cmd,
49 std::string process_name) {
50 return CF_EXPECT(CreateLogTeeImpl(
51 cmd, instance_, std::move(process_name),
52 {Subprocess::StdIOChannel::kStdOut, Subprocess::StdIOChannel::kStdErr}));
53 }
54
CreateLogTee(Command & cmd,std::string process_name,Subprocess::StdIOChannel log_channel)55 Result<Command> LogTeeCreator::CreateLogTee(
56 Command& cmd, std::string process_name,
57 Subprocess::StdIOChannel log_channel) {
58 CF_EXPECT(log_channel != Subprocess::StdIOChannel::kStdIn,
59 "Invalid channel for log tee: stdin");
60 return CF_EXPECT(
61 CreateLogTeeImpl(cmd, instance_, std::move(process_name), {log_channel}));
62 }
63
64 } // namespace cuttlefish
65