Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 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 <string.h> |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 18 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 23 | |
| 24 | #include "command.h" |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 25 | #include "utils.h" |
Yabin Cui | 6d2db33 | 2015-06-30 18:03:34 -0700 | [diff] [blame] | 26 | |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 27 | int main(int argc, char** argv) { |
Yabin Cui | f560a6f | 2016-12-14 17:43:26 -0800 | [diff] [blame] | 28 | android::base::InitLogging(argv, android::base::StderrLogger); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 29 | std::vector<std::string> args; |
Yabin Cui | 767dd17 | 2016-06-02 21:02:43 -0700 | [diff] [blame] | 30 | android::base::LogSeverity log_severity = android::base::INFO; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 31 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 32 | for (int i = 1; i < argc; ++i) { |
| 33 | if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) { |
| 34 | args.insert(args.begin(), "help"); |
| 35 | } else if (strcmp(argv[i], "--log") == 0) { |
| 36 | if (i + 1 < argc) { |
| 37 | ++i; |
Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 38 | if (!GetLogSeverity(argv[i], &log_severity)) { |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 39 | LOG(ERROR) << "Unknown log severity: " << argv[i]; |
Yabin Cui | 6d2db33 | 2015-06-30 18:03:34 -0700 | [diff] [blame] | 40 | return 1; |
| 41 | } |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 42 | } else { |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 43 | LOG(ERROR) << "Missing argument for --log option.\n"; |
| 44 | return 1; |
Yabin Cui | 9759e1b | 2015-04-28 15:54:13 -0700 | [diff] [blame] | 45 | } |
Yabin Cui | 6ef55f7 | 2016-07-26 18:42:38 -0700 | [diff] [blame] | 46 | } else if (strcmp(argv[i], "--version") == 0) { |
Yabin Cui | df6333c | 2017-05-03 16:34:02 -0700 | [diff] [blame] | 47 | LOG(INFO) << "Simpleperf version " << GetSimpleperfVersion(); |
Yabin Cui | 6ef55f7 | 2016-07-26 18:42:38 -0700 | [diff] [blame] | 48 | return 0; |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 49 | } else { |
| 50 | args.push_back(argv[i]); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 51 | } |
| 52 | } |
Yabin Cui | 6d2db33 | 2015-06-30 18:03:34 -0700 | [diff] [blame] | 53 | android::base::ScopedLogSeverity severity(log_severity); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 54 | |
Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 55 | if (args.empty()) { |
| 56 | args.push_back("help"); |
| 57 | } |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 58 | std::unique_ptr<Command> command = CreateCommandInstance(args[0]); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 59 | if (command == nullptr) { |
| 60 | LOG(ERROR) << "malformed command line: unknown command " << args[0]; |
| 61 | return 1; |
| 62 | } |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 63 | std::string command_name = args[0]; |
Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 64 | args.erase(args.begin()); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 65 | |
| 66 | LOG(DEBUG) << "command '" << command_name << "' starts running"; |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 67 | bool result = command->Run(args); |
Yabin Cui | 323e945 | 2015-04-20 18:07:17 -0700 | [diff] [blame] | 68 | LOG(DEBUG) << "command '" << command_name << "' " |
| 69 | << (result ? "finished successfully" : "failed"); |
Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 70 | return result ? 0 : 1; |
| 71 | } |