Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2020 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 | //! Daemon program to collect system traces. |
| 18 | |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 19 | use anyhow::{bail, Result}; |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 20 | use std::env; |
| 21 | |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 22 | const HELP_MSG: &str = r#" |
| 23 | profcollectd background daemon. |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 24 | usage: profcollectd [command] |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 25 | nostart Start daemon but do not schedule profile collection. |
| 26 | "#; |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 27 | |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 28 | fn main() -> Result<()> { |
| 29 | libprofcollectd::init_logging(); |
| 30 | |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 31 | let args: Vec<String> = env::args().collect(); |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 32 | if args.len() > 2 { |
| 33 | bail!("This program only takes one or no argument{}", &HELP_MSG); |
| 34 | } |
| 35 | if args.len() == 1 { |
| 36 | libprofcollectd::init_service(true)?; |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | let action = &args[1]; |
| 40 | match action.as_str() { |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 41 | "nostart" => libprofcollectd::init_service(false)?, |
| 42 | "help" => println!("{}", &HELP_MSG), |
| 43 | arg => bail!("Unknown argument: {}\n{}", &arg, &HELP_MSG), |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 44 | } |
Yi Kong | e3aab14 | 2021-03-02 13:58:25 +0800 | [diff] [blame] | 45 | Ok(()) |
Yi Kong | e5576ae | 2020-08-05 02:00:05 +0800 | [diff] [blame] | 46 | } |