• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2020 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 <android-base/logging.h>
17 #include <android-base/strings.h>
18 #include <gflags/gflags.h>
19 
20 #include "common/libs/fs/shared_fd.h"
21 #include "common/libs/utils/tee_logging.h"
22 #include "host/libs/config/cuttlefish_config.h"
23 
24 DEFINE_string(process_name, "", "The process to credit log messages to");
25 DEFINE_int32(log_fd_in, -1, "The file descriptor to read logs from.");
26 
main(int argc,char ** argv)27 int main(int argc, char** argv) {
28   ::android::base::InitLogging(argv, android::base::StderrLogger);
29   google::ParseCommandLineFlags(&argc, &argv, /* remove_flags */ true);
30 
31   CHECK(FLAGS_log_fd_in >= 0) << "-log_fd_in is required";
32 
33   auto config = cuttlefish::CuttlefishConfig::Get();
34 
35   CHECK(config) << "Could not open cuttlefish config";
36 
37   auto instance = config->ForDefaultInstance();
38 
39   if (config->run_as_daemon()) {
40     android::base::SetLogger(
41         cuttlefish::LogToFiles({instance.launcher_log_path()}));
42   } else {
43     android::base::SetLogger(
44         cuttlefish::LogToStderrAndFiles({instance.launcher_log_path()}));
45   }
46 
47   auto log_fd = cuttlefish::SharedFD::Dup(FLAGS_log_fd_in);
48   CHECK(log_fd->IsOpen()) << "Failed to dup log_fd_in: " <<  log_fd->StrError();
49   close(FLAGS_log_fd_in);
50 
51   if (FLAGS_process_name.size() > 0) {
52     android::base::SetDefaultTag(FLAGS_process_name);
53   }
54 
55   char buf[1 << 16];
56   ssize_t chars_read = 0;
57 
58   LOG(DEBUG) << "Starting to read from process " << FLAGS_process_name;
59 
60   while ((chars_read = log_fd->Read(buf, sizeof(buf))) > 0) {
61     auto trimmed = android::base::Trim(std::string(buf, chars_read));
62     // Newlines inside `trimmed` are handled by the android logging code.
63     // These checks attempt to determine the log severity coming from crosvm.
64     // There is no guarantee of success all the time since log line boundaries
65     // could be out sync with the reads, but that's ok.
66     if (android::base::StartsWith(trimmed, "[INFO")) {
67       LOG(INFO) << trimmed;
68     } else if (android::base::StartsWith(trimmed, "[ERROR")) {
69       LOG(ERROR) << trimmed;
70     } else if (android::base::StartsWith(trimmed, "[WARNING")) {
71       LOG(WARNING) << trimmed;
72     } else if (android::base::StartsWith(trimmed, "[VERBOSE")) {
73       LOG(VERBOSE) << trimmed;
74     } else {
75       LOG(DEBUG) << trimmed;
76     }
77   }
78 
79   if (chars_read < 0) {
80     LOG(DEBUG) << "Failed to read from process " << FLAGS_process_name << ": "
81                << log_fd->StrError();
82   }
83 
84   LOG(DEBUG) << "Finished reading from process " << FLAGS_process_name;
85 }
86