Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // 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 Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 8 | #include <base/at_exit.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 9 | #include <base/command_line.h> |
| 10 | #include <base/file_path.h> |
| 11 | #include <base/logging.h> |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 12 | #include <base/string_number_conversions.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 13 | #include <chromeos/syslog_logging.h> |
| 14 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 15 | #include "shill/dbus_control.h" |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 16 | #include "shill/dhcp_provider.h" |
| 17 | #include "shill/shill_daemon.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 18 | |
| 19 | using std::string; |
| 20 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 21 | namespace switches { |
| 22 | |
| 23 | // Don't daemon()ize; run in foreground. |
| 24 | static const char kForeground[] = "foreground"; |
| 25 | // Directory to read confguration settings. |
| 26 | static const char kConfigDir[] = "config-dir"; |
| 27 | // Directory to read default configuration settings (Read Only). |
| 28 | static const char kDefaultConfigDir[] = "default-config-dir"; |
| 29 | // Flag that causes shill to show the help message and exit. |
| 30 | static const char kHelp[] = "help"; |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 31 | // LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR. |
| 32 | static const char kLogLevel[] = "log-level"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 33 | |
| 34 | // The help message shown if help flag is passed to the program. |
| 35 | static const char kHelpMessage[] = "\n" |
| 36 | "Available Switches: \n" |
| 37 | " --foreground\n" |
| 38 | " Don\'t daemon()ize; run in foreground.\n" |
| 39 | " --config_dir\n" |
| 40 | " Directory to read confguration settings.\n" |
| 41 | " --default_config_dir\n" |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 42 | " Directory to read default configuration settings (Read Only)." |
| 43 | " --log-level=N\n" |
| 44 | " LOG() level. 0 = INFO, 1 = WARNING, 2 = ERROR.\n" |
| 45 | " --v=N\n" |
| 46 | " Enables VLOG(N) and below.\n" |
| 47 | " --vmodule=\"*file_pattern*=1,certain_file.cc=2\".\n" |
| 48 | " Enable VLOG() at different levels in different files/modules.\n"; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 49 | } // namespace switches |
| 50 | |
| 51 | // Always logs to the syslog and logs to stderr if |
| 52 | // we are running in the foreground. |
| 53 | void SetupLogging(bool foreground) { |
| 54 | int log_flags = 0; |
| 55 | log_flags |= chromeos::kLogToSyslog; |
| 56 | if (foreground) { |
| 57 | log_flags |= chromeos::kLogToStderr; |
| 58 | } |
| 59 | chromeos::InitLog(log_flags); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | int main(int argc, char** argv) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 64 | base::AtExitManager exit_manager; |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 65 | CommandLine::Init(argc, argv); |
| 66 | CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 67 | |
| 68 | // If the help flag is set, force log in foreground. |
| 69 | SetupLogging(cl->HasSwitch(switches::kForeground) || |
| 70 | cl->HasSwitch(switches::kHelp)); |
| 71 | if (cl->HasSwitch(switches::kHelp)) { |
| 72 | LOG(INFO) << switches::kHelpMessage; |
| 73 | return 0; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 74 | } |
Chris Masone | 7ccc819 | 2011-05-24 14:54:49 -0700 | [diff] [blame] | 75 | if (cl->HasSwitch(switches::kLogLevel)) { |
| 76 | std::string log_level = cl->GetSwitchValueASCII(switches::kLogLevel); |
| 77 | int level = 0; |
| 78 | if (base::StringToInt(log_level, &level) && |
| 79 | level >= 0 && level < logging::LOG_NUM_SEVERITIES) { |
| 80 | logging::SetMinLogLevel(level); |
| 81 | } else { |
| 82 | LOG(WARNING) << "Bad log level: " << log_level; |
| 83 | } |
| 84 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 85 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 86 | FilePath config_dir(cl->GetSwitchValueASCII(switches::kConfigDir)); |
| 87 | FilePath default_config_dir( |
| 88 | !cl->HasSwitch(switches::kDefaultConfigDir) ? |
| 89 | shill::Config::kShillDefaultPrefsDir : |
| 90 | cl->GetSwitchValueASCII(switches::kDefaultConfigDir)); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 91 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 92 | shill::Config config; /* (config_dir, default_config_dir) */ |
| 93 | |
| 94 | // TODO(pstew): This should be chosen based on config |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 95 | shill::DBusControl *dbus_control = new shill::DBusControl(); |
| 96 | dbus_control->Init(); |
| 97 | shill::DHCPProvider::GetInstance()->Init(dbus_control->connection()); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 98 | |
Darin Petkov | d1b715b | 2011-06-02 21:21:22 -0700 | [diff] [blame] | 99 | shill::Daemon daemon(&config, dbus_control); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 100 | daemon.Run(); |
| 101 | |
| 102 | return 0; |
| 103 | } |