metricsd_main.cc revision 608e428006fa317badd51b941e05bdba42bd08bd
1/*
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
17#include <base/at_exit.h>
18#include <base/command_line.h>
19#include <base/files/file_path.h>
20#include <base/logging.h>
21#include <base/strings/string_util.h>
22#include <base/time/time.h>
23#include <brillo/flag_helper.h>
24#include <brillo/syslog_logging.h>
25
26#include "constants.h"
27#include "uploader/upload_service.h"
28
29
30int main(int argc, char** argv) {
31  DEFINE_bool(foreground, false, "Don't daemonize");
32
33  // Upload the metrics once and exit. (used for testing)
34  DEFINE_bool(uploader_test,
35              false,
36              "run the uploader once and exit");
37
38  // Upload Service flags.
39  DEFINE_int32(upload_interval_secs,
40               1800,
41               "Interval at which metrics_daemon sends the metrics. (needs "
42               "-uploader)");
43  DEFINE_string(server,
44                metrics::kMetricsServer,
45                "Server to upload the metrics to. (needs -uploader)");
46  DEFINE_string(metrics_directory,
47                metrics::kMetricsDirectory,
48                "Root of the configuration files (testing only)");
49
50  DEFINE_bool(logtostderr, false, "Log to standard error");
51  DEFINE_bool(logtosyslog, false, "Log to syslog");
52
53  brillo::FlagHelper::Init(argc, argv, "Brillo metrics daemon.");
54
55  int logging_location = (FLAGS_foreground ? brillo::kLogToStderr
56                          : brillo::kLogToSyslog);
57  if (FLAGS_logtosyslog)
58    logging_location = brillo::kLogToSyslog;
59
60  if (FLAGS_logtostderr)
61    logging_location = brillo::kLogToStderr;
62
63  // Also log to stderr when not running as daemon.
64  brillo::InitLog(logging_location | brillo::kLogHeader);
65
66  if (FLAGS_logtostderr && FLAGS_logtosyslog) {
67    LOG(ERROR) << "only one of --logtosyslog and --logtostderr can be set";
68    return 1;
69  }
70
71  if (!FLAGS_foreground && daemon(0, 0) != 0) {
72    return errno;
73  }
74
75  UploadService service(FLAGS_server,
76                        base::TimeDelta::FromSeconds(FLAGS_upload_interval_secs),
77                        base::FilePath(FLAGS_metrics_directory));
78
79  service.Run();
80}
81