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 <gflags/gflags.h>
17 
18 #include "common/libs/utils/tee_logging.h"
19 #include "host/commands/metrics/host_receiver.h"
20 #include "host/commands/metrics/metrics_configs.h"
21 #include "host/commands/metrics/metrics_defs.h"
22 #include "host/libs/config/cuttlefish_config.h"
23 
24 namespace cuttlefish {
25 namespace {
26 
MetricsMain(int argc,char ** argv)27 int MetricsMain(int argc, char** argv) {
28   google::ParseCommandLineFlags(&argc, &argv, true);
29   ::android::base::InitLogging(argv, android::base::StderrLogger);
30   auto config = CuttlefishConfig::Get();
31   CHECK(config) << "Could not open cuttlefish config";
32   auto instance = config->ForDefaultInstance();
33   auto metrics_log_path = instance.PerInstanceLogPath("metrics.log");
34   if (instance.run_as_daemon()) {
35     android::base::SetLogger(
36         LogToFiles({metrics_log_path, instance.launcher_log_path()}));
37   } else {
38     android::base::SetLogger(
39         LogToStderrAndFiles({metrics_log_path, instance.launcher_log_path()}));
40   }
41   if (config->enable_metrics() != CuttlefishConfig::Answer::kYes) {
42     LOG(ERROR) << "metrics not enabled, but metrics were launched.";
43     return MetricsExitCodes::kInvalidHostConfiguration;
44   }
45 
46   bool is_metrics_enabled =
47       CuttlefishConfig::Answer::kYes == config->enable_metrics();
48   MetricsHostReceiver host_receiver(is_metrics_enabled);
49   if (!host_receiver.Initialize(kCfMetricsQueueName)) {
50     LOG(ERROR) << "metrics host_receiver failed to init";
51     return MetricsExitCodes::kMetricsError;
52   }
53   LOG(INFO) << "Metrics started";
54   host_receiver.Join();
55   return MetricsExitCodes::kMetricsError;
56 }
57 
58 }  // namespace
59 }  // namespace cuttlefish
60 
main(int argc,char ** argv)61 int main(int argc, char** argv) { return cuttlefish::MetricsMain(argc, argv); }
62