blob: 0039e09090f8dbffa5fd000e48a4c8c6f6d57743 [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <time.h>
6#include <string>
Paul Stewart75897df2011-04-27 09:05:53 -07007
Chris Masone0e1d1042011-05-09 18:07:03 -07008#include <base/at_exit.h>
Chris Masoneee929b72011-05-10 10:02:18 -07009#include <base/command_line.h>
10#include <base/file_path.h>
11#include <base/logging.h>
Chris Masone7ccc8192011-05-24 14:54:49 -070012#include <base/string_number_conversions.h>
Chris Masoneee929b72011-05-10 10:02:18 -070013#include <chromeos/syslog_logging.h>
14
Paul Stewart75897df2011-04-27 09:05:53 -070015#include "shill/dbus_control.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070016#include "shill/dhcp_provider.h"
Darin Petkovf7897bc2011-06-08 17:13:36 -070017#include "shill/glib.h"
Darin Petkovd1b715b2011-06-02 21:21:22 -070018#include "shill/shill_daemon.h"
Paul Stewart75897df2011-04-27 09:05:53 -070019
20using std::string;
21
Chris Masoneee929b72011-05-10 10:02:18 -070022namespace switches {
23
24// Don't daemon()ize; run in foreground.
25static const char kForeground[] = "foreground";
26// Directory to read confguration settings.
27static const char kConfigDir[] = "config-dir";
28// Directory to read default configuration settings (Read Only).
29static const char kDefaultConfigDir[] = "default-config-dir";
30// Flag that causes shill to show the help message and exit.
31static const char kHelp[] = "help";
Chris Masone7ccc8192011-05-24 14:54:49 -070032// LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.
33static const char kLogLevel[] = "log-level";
Chris Masoneee929b72011-05-10 10:02:18 -070034
35// The help message shown if help flag is passed to the program.
36static const char kHelpMessage[] = "\n"
37 "Available Switches: \n"
38 " --foreground\n"
39 " Don\'t daemon()ize; run in foreground.\n"
40 " --config_dir\n"
41 " Directory to read confguration settings.\n"
42 " --default_config_dir\n"
Chris Masone7ccc8192011-05-24 14:54:49 -070043 " Directory to read default configuration settings (Read Only)."
44 " --log-level=N\n"
45 " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n"
46 " --v=N\n"
47 " Enables VLOG(N) and below.\n"
48 " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n"
49 " Enable VLOG() at different levels in different files/modules.\n";
Chris Masoneee929b72011-05-10 10:02:18 -070050} // namespace switches
51
52// Always logs to the syslog and logs to stderr if
53// we are running in the foreground.
54void SetupLogging(bool foreground) {
55 int log_flags = 0;
56 log_flags |= chromeos::kLogToSyslog;
57 if (foreground) {
58 log_flags |= chromeos::kLogToStderr;
59 }
60 chromeos::InitLog(log_flags);
61}
62
63
64int main(int argc, char** argv) {
Chris Masone0e1d1042011-05-09 18:07:03 -070065 base::AtExitManager exit_manager;
Chris Masoneee929b72011-05-10 10:02:18 -070066 CommandLine::Init(argc, argv);
67 CommandLine* cl = CommandLine::ForCurrentProcess();
68
69 // If the help flag is set, force log in foreground.
70 SetupLogging(cl->HasSwitch(switches::kForeground) ||
71 cl->HasSwitch(switches::kHelp));
72 if (cl->HasSwitch(switches::kHelp)) {
73 LOG(INFO) << switches::kHelpMessage;
74 return 0;
Paul Stewart75897df2011-04-27 09:05:53 -070075 }
Chris Masone7ccc8192011-05-24 14:54:49 -070076 if (cl->HasSwitch(switches::kLogLevel)) {
77 std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel);
78 int level = 0;
79 if (base::StringToInt(log_level, &level) &&
80 level >= 0 && level < logging::LOG_NUM_SEVERITIES) {
81 logging::SetMinLogLevel(level);
82 } else {
83 LOG(WARNING) << "Bad log level: " << log_level;
84 }
85 }
Paul Stewart75897df2011-04-27 09:05:53 -070086
Chris Masoneee929b72011-05-10 10:02:18 -070087 FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir));
88 FilePath default_config_dir(
89 !cl->HasSwitch(switches::kDefaultConfigDir) ?
90 shill::Config::kShillDefaultPrefsDir :
91 cl->GetSwitchValueASCII(switches::kDefaultConfigDir));
Paul Stewart75897df2011-04-27 09:05:53 -070092
Paul Stewart75897df2011-04-27 09:05:53 -070093 shill::Config config; /* (config_dir, default_config_dir) */
94
95 // TODO(pstew): This should be chosen based on config
Darin Petkovd1b715b2011-06-02 21:21:22 -070096 shill::DBusControl *dbus_control = new shill::DBusControl();
97 dbus_control->Init();
Darin Petkovf7897bc2011-06-08 17:13:36 -070098 shill::GLib glib;
99 shill::DHCPProvider::GetInstance()->Init(dbus_control->connection(), &glib);
Paul Stewart75897df2011-04-27 09:05:53 -0700100
Darin Petkovd1b715b2011-06-02 21:21:22 -0700101 shill::Daemon daemon(&config, dbus_control);
Paul Stewart75897df2011-04-27 09:05:53 -0700102 daemon.Run();
103
104 return 0;
105}