blob: ad7e031c16195644e00aa63ffa8a9072df82bc66 [file] [log] [blame]
Ningyuan Wange44553d2015-11-18 14:53:18 -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
17
18#include <base/command_line.h>
19#include <base/logging.h>
20#include <brillo/syslog_logging.h>
21#include "dhcp_client/daemon.h"
22
23using std::vector;
24
25namespace {
26
27namespace switches {
28
29// Don't daemon()ize; run in foreground.
30const char kForeground[] = "foreground";
Ningyuan Wang88c688a2015-12-09 17:59:01 -080031// Flag to show the help message.
Ningyuan Wange44553d2015-11-18 14:53:18 -080032const char kHelp[] = "help";
33// The help message shown if help flag is passed to the program.
34const char kHelpMessage[] = "\n help message \n";
35
36} // namespace switches
37
38} // namespace
39
40// Always logs to the syslog and logs to stderr if
41// we are running in the foreground.
42void SetupLogging(bool foreground,
43 const char* daemon_name) {
44 int log_flags = 0;
45 log_flags |= brillo::kLogToSyslog;
46 log_flags |= brillo::kLogHeader;
47 if (foreground) {
48 log_flags |= brillo::kLogToStderr;
49 }
50 brillo::InitLog(log_flags);
51}
52
53void OnStartup(const char* daemon_name, base::CommandLine* cl) {
54 LOG(INFO) << __func__;
55 SetupLogging(cl->HasSwitch(switches::kForeground), daemon_name);
56 return;
57}
58
59int main(int argc, char* argv[]) {
60 base::CommandLine::Init(argc, argv);
61 base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
62
63 if (cl->HasSwitch(switches::kHelp)) {
64 LOG(INFO) << switches::kHelpMessage;
65 return 0;
66 }
67
68 dhcp_client::Daemon daemon(base::Bind(&OnStartup, argv[0], cl));
69
70 daemon.Run();
71
72 return 0;
73}