blob: f460268c7d671374cf17a70866dec9d3aa12576c [file] [log] [blame]
Bertrand SIMONNET608e4282015-11-12 17:52:17 -08001/*
2 * Copyright (C) 2015 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
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080017#include <thread>
18
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080019#include <base/at_exit.h>
20#include <base/command_line.h>
21#include <base/files/file_path.h>
22#include <base/logging.h>
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080023#include <base/metrics/statistics_recorder.h>
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080024#include <base/strings/string_util.h>
25#include <base/time/time.h>
26#include <brillo/flag_helper.h>
27#include <brillo/syslog_logging.h>
28
29#include "constants.h"
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080030#include "uploader/bn_metricsd_impl.h"
31#include "uploader/crash_counters.h"
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080032#include "uploader/upload_service.h"
33
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080034int main(int argc, char** argv) {
35 DEFINE_bool(foreground, false, "Don't daemonize");
36
37 // Upload the metrics once and exit. (used for testing)
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080038 DEFINE_bool(uploader_test, false, "run the uploader once and exit");
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080039
40 // Upload Service flags.
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080041 DEFINE_int32(upload_interval_secs, 1800,
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080042 "Interval at which metrics_daemon sends the metrics. (needs "
43 "-uploader)");
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080044 DEFINE_string(server, metrics::kMetricsServer,
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080045 "Server to upload the metrics to. (needs -uploader)");
Bertrand SIMONNET9d3a4ae2015-11-25 13:49:12 -080046 DEFINE_string(private_directory, metrics::kMetricsdDirectory,
47 "Path to the private directory used by metricsd "
48 "(testing only)");
49 DEFINE_string(shared_directory, metrics::kSharedMetricsDirectory,
50 "Path to the shared metrics directory, used by "
51 "metrics_collector, metricsd and all metrics clients "
52 "(testing only)");
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080053
54 DEFINE_bool(logtostderr, false, "Log to standard error");
55 DEFINE_bool(logtosyslog, false, "Log to syslog");
56
57 brillo::FlagHelper::Init(argc, argv, "Brillo metrics daemon.");
58
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080059 int logging_location =
60 (FLAGS_foreground ? brillo::kLogToStderr : brillo::kLogToSyslog);
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080061 if (FLAGS_logtosyslog)
62 logging_location = brillo::kLogToSyslog;
63
64 if (FLAGS_logtostderr)
65 logging_location = brillo::kLogToStderr;
66
67 // Also log to stderr when not running as daemon.
68 brillo::InitLog(logging_location | brillo::kLogHeader);
69
70 if (FLAGS_logtostderr && FLAGS_logtosyslog) {
71 LOG(ERROR) << "only one of --logtosyslog and --logtostderr can be set";
72 return 1;
73 }
74
75 if (!FLAGS_foreground && daemon(0, 0) != 0) {
76 return errno;
77 }
78
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080079 std::shared_ptr<CrashCounters> counters(new CrashCounters);
80
81 UploadService upload_service(
Bertrand SIMONNET9d3a4ae2015-11-25 13:49:12 -080082 FLAGS_server, base::TimeDelta::FromSeconds(FLAGS_upload_interval_secs),
83 base::FilePath(FLAGS_private_directory),
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080084 base::FilePath(FLAGS_shared_directory), counters);
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080085
Bertrand SIMONNET6b8629a2015-11-18 13:46:33 -080086 base::StatisticsRecorder::Initialize();
87
88 // Create and start the binder thread.
89 BnMetricsdImpl binder_service(counters);
90 std::thread binder_thread(&BnMetricsdImpl::Run, &binder_service);
91
92 upload_service.Run();
Bertrand SIMONNET608e4282015-11-12 17:52:17 -080093}