• 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 <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 using cuttlefish::MetricsExitCodes;
25 
main(int argc,char ** argv)26 int main(int argc, char** argv) {
27   google::ParseCommandLineFlags(&argc, &argv, true);
28   ::android::base::InitLogging(argv, android::base::StderrLogger);
29   auto config = cuttlefish::CuttlefishConfig::Get();
30   CHECK(config) << "Could not open cuttlefish config";
31   auto instance = config->ForDefaultInstance();
32   auto metrics_log_path = instance.PerInstanceLogPath("metrics.log");
33   if (instance.run_as_daemon()) {
34     android::base::SetLogger(
35         cuttlefish::LogToFiles({metrics_log_path, instance.launcher_log_path()}));
36   } else {
37     android::base::SetLogger(
38         cuttlefish::LogToStderrAndFiles(
39             {metrics_log_path, instance.launcher_log_path()}));
40   }
41   if (config->enable_metrics() != cuttlefish::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       cuttlefish::CuttlefishConfig::Answer::kYes == config->enable_metrics();
48   cuttlefish::MetricsHostReceiver host_receiver(is_metrics_enabled);
49   if (!host_receiver.Initialize(cuttlefish::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